This is the second video from the raspberry pi Pico tutorial series on RoboCircuits YouTube channel. Let’s take a quick recap
In the previous video, we learned the following stuff
1. What is Raspberry Pi Pico
2. Setting up our computer to program Pico using MicroPython
3. Made blink code to blink the inbuilt led on the board
In this video, we are going to learn about the PWM pins on the raspberry pi Pico. PWM means Pulse Width Modulation, is a method of reducing the average power delivered by an electrical signal, by effectively chopping it up into discrete parts. The average value of voltage (and current) fed to the load is controlled by turning the switch between supply and load on and off at a fast rate. The longer the switch is on compared to the off periods, the higher the total power supplied to the load.
RP2040 microcontroller, which is used to make the raspberry pi Pico, contains 8 PWM blocks and each PWM block provides two PWM signals. That means each slice can drive up to two PWM signals. Hence, there is a total of 16 PWM signal output available on Raspberry Pi Pico. All GPIO pins can be configured to get any PWM signal output.
Now we know what is PWM and does Pico supports it. Now let’s see what we can do with it.
1. PWM is used to reduce power delivery so that means we can use it to dim LED lights.
2. PWM has modulation in its name. You may have heard of Frequency Modulation and Amplitude Modulation in which a message signal is superimposed on a carrier signal. AM and FM are a type of analog modulation while PWM is a digital modulation so that means it can deliver data also. we can control Servo Motors and Electronic Speed Controllers with this wave.
Now we have enough information about PWM let's see how to implement it with our Pico Board.
Let’s connect Pico with a data cable to our computer.
In the previous video, you have created a folder named Pico and First Program. Let’s create another project named PWM in the Pico folder. Now, right-click and click open with VS Code. If you are confused about what we are doing you should watch part one again and if you are still confused ask me in the comments or message me on my Instagram handle @robocircuits.
When VS code will open you will see connecting to COM port in the terminal window … which means Pico is connected.Let us create a file named flash.py now press ctrl+shift+p to open command search and search for pico and select ‘configure project’.now we are ready to write our code.we will start by importing Pins and PWM from the machine library.Then we will import sleep from the time library.then we will define our pwm pin by using pwm = PWM(Pin(15))
Here capital PWM is a function to define pin and small pwm is a variable.
PWM has two variables frequency and duty cycle. We will set freq as 1000 using the pwm.freq function.
Now we will create an infinite loop using while. This loop will run until it returns true. So if we never return true it will run infinitely.
Then we will run a for loop from 0 to 65025. The variable in this for loop is duty. Here duty will change its value from 0 to 65025.in this for loop, we will set the PWM duty-cycle by using this function. then to give a small delay we will use sleep function. Sleep functions inputs value in seconds so to give a delay of 1 ms we will write 0.0001 here.
Then we will run for loop in reverse to using -1 increment. With the same task inside.So our code is done.let's try to run. In the bottom bar, you will find the run command just click on that after saving. I have connected a LED to pin no 15 and GND of Pico temporarily. The LED starts to Glow and Fade. That means our code is running fine.Let's take this to next level and try running a servo motor with it. for this we will need to mount the pico to the breadboard for that we will need male header pins. Currently, I have a set of pins from Arduino nano. But these are not enough for pico. For this time I am soldering these then I will order more to complete them. Let’s solder them.now pico is mounted on the breadboard. I have this 9g servo motor. Let’s connect it to pico. Servo has three wires GND VCC and IN. VCC will connect to Vsys and GND to GND.
Now, let us run the same code again.Servo rotates but it does weird stuff. There is some problem here which can be freq problem or duty cycle problem. I wrote a new code for running Servo Motor which includes minimum duty cycle and max duty cycle which is 1000 and 9000 respectively.and most of the rest code is almost similar you can understand it easily. Now let’s try running this code. And it worked pretty well.Now we can rotate our servo easily to our desired value. But when the pico will restart the code will not work.
this code runs temporarily on the pico so to run it every time we power pico we need to permanently flash it on pico. To do that we need to make a new file named main.py and copy the whole code to that file.now save it and click on the upload button.now code will run every time even after plugging out pico.So, that is it for this video in the next video we will try to add some sensors to the pico and write codes for them. If you don’t like this video hit thumbs down it will not hurt my feelings but if you like it then you know what to hit. If you are new to this channel and want to watch more videos hit subscribe and don’t forget to press the bell icon.Thank You for watching have a nice day.






Comments