We have exciting news to share soon! Sign up: bit.ly/2aOaL6c
Cart 0

Analog, Digital, and PWM

Ethan Saadia

If you have used the GPIO Python library before, you have seen that to turn on a GPIO pin you would use GPIO.output(pin, True), and to turn it off you would type GPIO.output(pin, False). The key here is the True and False. Just like a light switch, a GPIO pin can only be completely on, or completely off. This is called digital because the Raspberry Pi sends a binary signal of 1 or 0 to the GPIO pins, for on and off respectively, just like computers operate only in binary.

But, what if you want something in between? Something not fully on, but not off, either. Like, if you wanted to turn on a light three-quarters of the way on such as a dimmer, or make a buzzer buzz at a different frequency, or read a temperature from a sensor. This is called analog. Real life examples are volume knobs, speakers, and TV backlights. However, the Raspberry Pi can only output either digital signals (True or False) by itself. This is why we need something called Pulse Width Modulation. 

Pulse Width Modulation (PWM) acts as an analog signal by outputting a pin on and off very quickly. Using PWM, you can dim an LED with the same effect of applying a smaller amount of electricity, but using digital pulses. Here is a link to an example of an LED fading using a PWM signal. The more pulses a second, the brighter the LED will seem to be. 

PWM is easy to try. The GPIO Zero library supports it. For a start, try this:

import time

from gpiozero import PWMLED

led = PWMLED(18)

while True:

        for i in range(10):

                led.value(i / 10)


Older Post

Leave a comment

Please note, comments must be approved before they are published