Reading Material - PWM Control
Reading Material - PWM Control
The PWM, i.e Pulse Width Modulation in Raspberry Pi Pico. We will be gradually changing the
luminance of an LED through programming. Since the pulsing light looks like breathing, we give
it a magical name – breathing LED. We’ll accomplish this effect with pulse width modulation
(PWM).
Pulse Width Modulation or PWM is a common technique used to vary the width of the pulses in
a pulse train. PWM has many applications such as controlling servos and speed controllers,
limiting the effective power of motors and LEDs.
Pulse width modulation, or PWM, is a technique for getting analog results with digital means.
Digital control is used to create a square wave, a signal switched between on and off. This on-off
pattern can simulate voltages between full-on (5 Volts) and off (0 Volts) by changing the portion
of the time the signal spends on versus the time that the signal spends off.
The duration of “on time” is called the pulse width. To get varying analog values, you change or
modulate, that width. If you repeat this on-off pattern fast enough with some device, an LED,
for example, it would be like this: the signal is a steady voltage between 0 and 5V controlling the
brightness of the LED.
You will find that the smaller the PWM value is, the smaller the value will be after being
converted into voltage. Then the LED becomes dimmer accordingly. Therefore, we can control
the brightness of the LED by controlling the PWM value.
The RP2040 microcontroller at the core of the Raspberry Pi Pico has 8 Slices of PWM, and each
Slice is independent of the others. This simply means that we can set the frequency of a Slice
without affecting the others.
The RP2040 PWM block has 8 identical PWM slices, each with two output channels (A/B),
where the B pin can also be used as an input for frequency and duty cycle measurement. That
means each slice can drive two PWM output signals, or measure the frequency or duty cycle of
an input signal. This gives a total of up to 16 controllable PWM outputs. All 30 GPIO pins can be
driven by the PWM block.
An interesting thing at this moment to recall about PWM on the RP2040 is the possibility of
having Duty Cycle values at 0% and at 100% without spurious peaks as happens on most other
microcontrollers. Besides, it might be possible to have the two outputs of the same Slice with
inverted phase signals.
In the above pin diagram, pins 21/GP16 and 22/GP17 belong to the same slice – GP16 is output
A and GP17 is output B.
from machine import Pin, PWM #importing Pin Class and PWM Class in order to use it
import utime#Importing time library
led = PWM(Pin(25))
led_value=0
while True:
led_value += 1000
led.duty_u16(int(led_value)) # Set the duty cycle, between 0-65535
utime.sleep_ms(100)
Another method:
from machine import Pin, PWM #importing Pin Class and PWM Class in order to use it
import utime#Importing time library
led = PWM(Pin(25))
led.duty_u16(0)
while True:
for intensity in range(0,65535):
led.duty_u16(intensity)
Task :
1) Use PWM Pins to control traffic lights and buzzer on the Pico Pi board.
2)
3)https://drive.google.com/file/d/1i-JfUk0h5v6v-u8oCMhEJCmBtW_C0_uz/view?usp=sharing