Fareed Mohmed← Project library

Engineering · Raspberry Pi · Project 7 of 47 · Outputs · Beginner

Dimmer and breathing LED (PWM)

PWM switches a pin on and off very fast to fake brightness. First step the LED up and down, then make it breathe.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Dimmer and breathing LED (PWM): the board, its power supply and the parts wired to the 40-pin header

Parts in this circuit

PartType
Blue LED (PWM)LED

Wiring, pin by pin

Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.

#ColourFrom (part and leg)To (header pin)
1ColouredBlue LED (PWM) - anode +Pin 12 (GPIO18)
2BlackBlue LED (PWM) - cathode -Pin 6 (GND)

Python code (gpiozero)

from gpiozero import PWMLED
from time import sleep

led = PWMLED(18)

for step in range(0, 11):            # 0, 1, ... 10
    led.value = step / 10            # 0.0 to 1.0
    print("Brightness", step * 10, "%")
    sleep(0.3)

for step in range(10, -1, -1):       # back down
    led.value = step / 10
    print("Brightness", step * 10, "%")
    sleep(0.3)

print("Now let it breathe")
led.pulse(fade_in_time=1.5, fade_out_time=1.5)   # runs in the background
sleep(60)

The same program with RPi.GPIO

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 1000)        # 1 kHz PWM on GPIO18
pwm.start(0)                    # 0 % duty cycle

try:
    while True:
        for duty in range(0, 101, 5):
            pwm.ChangeDutyCycle(duty)
            print("Duty", duty, "%")
            time.sleep(0.1)
        for duty in range(100, -1, -5):
            pwm.ChangeDutyCycle(duty)
            print("Duty", duty, "%")
            time.sleep(0.1)
finally:
    pwm.stop()
    GPIO.cleanup()

How the code works

You will learn

  • PWM: duty cycle and brightness
  • for loops and ranges
  • The same idea runs motor speed and servo angle

Try this

  • Make the steps finer: range(0, 101, 5).
  • Make a slow 3 second breathing pattern.
  • Use a button to choose between 25 %, 50 % and 100 % brightness.

On a real Pi

  • Our eyes see average brightness. At 100 Hz or more you do not see any flicker.
  • GPIO12, 13, 18 and 19 have hardware PWM. Other pins use software PWM, which is less steady.

Parts to buy

The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
45 mm LED, blue1Robu.in Amazon.in ThinkRobotics
5330 ohm resistor, 1/4 W
One per LED
1Robu.in Amazon.in ThinkRobotics
6Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
7Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 6. Distance meter with an LED barAll projects8. Servo motor sweep →

This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.