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.

Parts in this circuit
| Part | Type |
|---|---|
| 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.
| # | Colour | From (part and leg) | To (header pin) |
|---|---|---|---|
| 1 | Coloured | Blue LED (PWM) - anode + | Pin 12 (GPIO18) |
| 2 | Black | Blue 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
- PWM turns the pin on for a part of each cycle. 50 % duty means on half the time, so the LED looks half as bright.
- led.value goes from 0.0 (off) to 1.0 (full).
- pulse() fades in and out by itself in the background.
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.
| # | Item | Qty | Buy / compare |
|---|---|---|---|
| 1 | Raspberry Pi 4 Model B about 4000 to 9000 INR | 1 | Robu.in Amazon.in ThinkRobotics |
| 2 | Official 15 W USB-C power supply (5.1 V, 3 A) Do not use an old phone charger | 1 | Robu.in Amazon.in ThinkRobotics |
| 3 | microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS) And a card reader to write it | 1 | Robu.in Amazon.in ThinkRobotics |
| 4 | 5 mm LED, blue | 1 | Robu.in Amazon.in ThinkRobotics |
| 5 | 330 ohm resistor, 1/4 W One per LED | 1 | Robu.in Amazon.in ThinkRobotics |
| 6 | Dupont jumper wires, male-to-female, 20 cm (pack of 40) | 1 | Robu.in Amazon.in ThinkRobotics |
| 7 | Breadboard 830 points For resistors and shared GND / 5 V | 1 | Robu.in Amazon.in ThinkRobotics |
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.