Most microcontrollers aren't able to natively output an analog signal, instead, they use a little trick called PWM. In this video, we will be taking a look at how to use PWM in MicroPython on the Raspberry Pi PIco, and what it can be used for.

Transcript

We've just covered analog inputs, so we should now be covering analog outputs, right? Well, about that. Long story short, going from analog to digital is not too hard, but it turns out that going from digital to analog is a lot harder, to the point where most microcontrollers can't natively output a true analog signal, meaning they can't output a constant voltage between 0 and 3.3 volts.

However, we can use a little bit of trickery to create a replica or an imitation of an analog signal with something called Pulse Width Modulation, or PWM. The pins of our Pico can only output 3.3 or 0 volts, no in-between, but we can switch a pin on and off and on and off and off very fast, as in thousands of times a second fast, to create an average voltage somewhere between 0 and 3.3 volts. If we had a pin on for one millisecond, then off for another millisecond, then on for another and off for another, and we keep repeating that pattern over and over, we create an average voltage. And because we have that pin on for 50% of the time in this situation, we create an average voltage of 1.65 volts, which is 50% of 3.3 volts.

The percentage of time we keep the pin on in this on-and-off cycle is called the duty cycle, a cycle of 100% would be on all the time and would give us 3.3 volts, a duty cycle of 10% would be on 10% of the time and would give us 0.33 volts, a duty cycle of 20% would give us 20% of 3.3, which is 0.66 volts, and on and on. You can calculate any voltage between 0 and 3.3 for any percent duty cycle. And this is how we can create our fake analog output. We can use a PWM signal and set its duty cycle to output the average desired voltage that we want.

Now it's not a perfect replacement, but it's usually a good enough imitation and a lot of other digital devices won't notice the difference between PWM and a true analog signal. Let's copy and paste the demo code and go through an example of this. The code can be found on our course page on our website, link in the description for our YouTube audience.

So this is just the bare bones code to get our Pico to set a PWM signal on pin 16, which we have an LED and resistor connected to. To use PWM, we'll need to import it from machine and then initialize the pin that we wish to set up as our output. And this syntax is very similar to setting up a pin to use ADC. One thing different, though, is that any of the Pico's 26 GPIO pins can output a PWM signal, which is actually quite a lot for a microcontroller.

Now in this line, we're setting the frequency of our PWM signal. This is not duty cycle. It is how many times a second the pin is turned on and off, not what percentage of the time the pin is on. Setting the frequency like this is entirely optional, and you can remove this line and it will still happily work. It will just default to whatever the default PWM frequency is. There are just some cases where you will need to manually set this frequency, so we've included it. But I'd like to stress again, this is not the duty cycle. It's more so how many times a second the duty cycle is pulsed on the pin. Play around with it if you want. It can go all the way down to eight, but usually you should leave it at about 1,000.

Then here we set the duty cycle on the pin with "duty_U16". But we have the same issue as analog inputs. We can't just directly put in the PWM duty cycle that we want. We can't just put in 0 to 100. It has to be a number between 0 and 65,535, which linearly represents 0 to 100% duty cycle. That's why I've got this variable called max here. It's the maximum value that corresponds to 100% duty cycle or 3.3 volts. And the way I have it set up makes it easy to set a duty cycle. In this example, I'm setting up a 50% duty cycle with 0.5. If I run this code, the LED should lighten the video, we can see that the PWM is being used to control the brightness of an LED. By setting the PWM to different duty cycles, we can vary the average voltage on the pin of the Pico. For example, at 50% duty cycle, the LED is at half brightness, and at 20% duty cycle, the LED is dimmer, but not off. This demonstrates the ability to control the LED's brightness with more finesse than just turning it on or off.

Additionally, the video showcases the potential of PWM in controlling RGB LEDs, car headlights, and motors. PWM can be used to control the brightness of individual colour channels in RGB LEDs, adjust the brightness of car headlights, and precisely control the speed of motors in applications such as RC cars, drones, and fan speed control based on temperature readings.

Furthermore, the video highlights the versatility of PWM in controlling various motor-based actuators, including linear actuators, stepper motors, and servos. These actuators can be controlled with a PWM signal from the Pico, enabling precise and accurate control over their movements.

In summary, the video emphasizes three key takeaways: PWM can imitate an analog output by rapidly switching a digital pin on and off, the term duty cycle is used to describe the percentage of time the signal is on, and any of the Pico's 26 GPIO pins can be set up to use PWM, which uses a value between 0 and 65,535 to represent 0 to 100% duty cycle.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.