In this tutorial, we’ll discuss the Raspberry Pi LED basics on the Raspberry Pi 5: how to connect an LED to a GPIO pin, which resistor to use, and how to blink one or several LEDs from Python and C++. You’ll learn how to blink an LED without blocking your program, how to switch multiple LEDs together, and how to read and control the Raspberry Pi 5’s own red and green status LED.
We’ll answer the question: how do you blink LEDs on the Raspberry Pi 5 with accurate timing, from gpiozero in Python and libgpiod in C++? You’ll see my measurements of how far a blink loop drifts over time and how far apart two “simultaneous” LEDs switch, with oscilloscope captures, plus the real error messages you’ll meet. Without further ado, let’s get right into it!
Note: All examples in this tutorial are tested with: Raspberry Pi 5 (8GB) with Raspberry Pi OS (64-bit) Trixie.
This tutorial assumes your Raspberry Pi is set up with Raspberry Pi OS and reachable over SSH. If it isn’t, follow the Raspberry Pi Setup Guide first.
Raspberry Pi LED Blink Example (Raspberry Pi 5)
To blink an LED on a Raspberry Pi, connect GPIO17 (pin 11) to a 330Ω resistor, the resistor to the LED’s long leg (anode), and the LED’s short leg (cathode) to GND. Then toggle GPIO17 from your program. Both examples below blink the LED at 1Hz (0.5s on, 0.5s off) and turn it off on Ctrl+C. Neither needs sudo.
Wiring: GPIO17 (pin 11) → 330Ω → LED → GND (pin 9).
Python Code example (Raspberry Pi LED blink, gpiozero)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# LAB Name: Raspberry Pi LED Blink (gpiozero, Python) # Author: Khaled Magdy # For More Info Visit: www.DeepBlueMbedded.com from gpiozero import LED from signal import pause led = LED(17) # BCM GPIO17 = physical pin 11 -> 330Ω -> LED -> GND led.blink(on_time=0.5, off_time=0.5) # runs in a background thread print("Blinking the LED on GPIO17. Press Ctrl+C to stop.") try: pause() # keep the script alive; the blink thread does the work except KeyboardInterrupt: led.off() print("\nStopped, LED off.") |
C++ Code example (Raspberry Pi LED blink, libgpiod v2)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
/* * LAB Name: Raspberry Pi LED Blink With libgpiod v2 (C++) * Author: Khaled Magdy * For More Info Visit: www.DeepBlueMbedded.com */ #include <gpiod.hpp> #include <atomic> #include <chrono> #include <csignal> #include <iostream> #include <thread> static std::atomic<bool> running{true}; static void on_sigint(int) { running = false; } int main() { const gpiod::line::offset led_pin = 17; // BCM GPIO17 = physical pin 11 const auto half_period = std::chrono::milliseconds(500); std::signal(SIGINT, on_sigint); auto request = gpiod::chip("/dev/gpiochip0") // 40-pin header (RP1) on the Raspberry Pi 5 .prepare_request() .set_consumer("blink_led") .add_line_settings(led_pin, gpiod::line_settings() .set_direction(gpiod::line::direction::OUTPUT) .set_output_value(gpiod::line::value::INACTIVE)) .do_request(); std::cout << "Blinking the LED on GPIO17. Press Ctrl+C to stop." << std::endl; auto next = std::chrono::steady_clock::now(); bool on = false; while (running) { on = !on; request.set_value(led_pin, on ? gpiod::line::value::ACTIVE : gpiod::line::value::INACTIVE); next += half_period; // absolute deadline: no drift std::this_thread::sleep_until(next); } request.set_value(led_pin, gpiod::line::value::INACTIVE); // leave the LED off std::cout << "\nStopped, LED off." << std::endl; return 0; } |
Run the Python one with python3 blink_led.py. Build the C++ one with g++ -std=c++17 blink_led.cpp -o blink_led $(pkg-config --cflags --libs libgpiodcxx) and run ./blink_led (install the library once with sudo apt install libgpiod-dev). The rest of this tutorial explains the LED circuit and the resistor value, the blink patterns, multiple LEDs, the timing measurements, and the Raspberry Pi 5 status LED.
Raspberry Pi LED Circuit Explained
A Raspberry Pi GPIO pin set as an output is a 3.3v push-pull driver: HIGH connects the pin to 3.3v, LOW connects it to 0v. An LED needs a series resistor to set its current, because an LED has almost no resistance once its forward voltage is reached. You can wire it two ways:

- Active-high (sourcing): GPIO → resistor → LED → GND. The LED is on when the pin is HIGH. This is the usual wiring and the one this tutorial uses.
- Active-low (sinking): 3.3v → resistor → LED → GPIO. The LED is on when the pin is LOW. Tell the library with LED(17, active_high=False) in gpiozero or set_active_low(true) in libgpiod, and your code keeps saying “on” for on.
Both work on the Raspberry Pi 5. The LED’s long leg is the anode (+) and the short leg, on the flat side of the rim, is the cathode (−). An LED wired backwards stays dark and isn’t damaged at 3.3v.
Raspberry Pi LED Resistor Calculation
The resistor sets the LED current: R = (VGPIO − VF) / ILED, where VF is the LED’s forward voltage from its datasheet. For a red LED with VF ≈ 2.0v and a 330Ω resistor: I = (3.3v − 2.0v) / 330Ω ≈ 3.9mA, which is bright enough for an indicator. Typical values:
| LED Colour (Typical VF) | 220Ω | 330Ω | 1kΩ |
|---|---|---|---|
| Red (≈2.0v) | ≈5.9mA | ≈3.9mA | ≈1.3mA |
| Yellow, Green (≈2.1v) | ≈5.5mA | ≈3.6mA | ≈1.2mA |
| Blue, White (≈3.0v) | ≈1.4mA | ≈0.9mA (dim) | ≈0.3mA (very dim) |
Use 330Ω for red, yellow, and green LEDs, and 100Ω to 150Ω for blue and white LEDs, whose ≈3v forward voltage leaves only ≈0.3v across the resistor on a 3.3v pin. Don’t size the resistor for 20mA as many 5v tutorials do: the Raspberry Pi 5’s RP1 GPIO pads have drive strength settings of 2, 4, 8, or 12mA, and Raspberry Pi specifies a 50mA total for all GPIO pins together, with no guaranteed per-pin maximum on RP1 boards (Raspberry Pi GPIO pads documentation). A few milliamps per LED keeps you far from both limits.
Note: A GPIO pin can’t drive an LED strip, a high-power LED, or a 12v lamp directly. Those need an external supply and a transistor or MOSFET driver; I’ll cover them in a separate future tutorial.
Raspberry Pi LED Wiring and Parts
All the examples use two LEDs: a yellow LED on GPIO17 and a green LED on GPIO16, each with a 330Ω resistor. The single-LED examples use only the yellow one.
| QTY. | Component Name | Amazon.com | AliExpress |
|---|---|---|---|
| 1 | Raspberry Pi 5 (4GB or 8GB) | Amazon | AliExpress |
| 1 | Raspberry Pi 27W USB-C Power Supply | Amazon | AliExpress |
| 2 | 5mm LED (Yellow, Green) | Amazon | AliExpress |
| 2 | 330Ω Resistor (kit) | Amazon | AliExpress |
| 1 | Breadboard | Amazon | AliExpress |
| 1 | Jumper Wires (Female-Male) | Amazon | AliExpress |

Each LED goes from its GPIO pin through a 330Ω resistor to its anode, and its cathode goes to the GND rail. The pin positions are in my Raspberry Pi 5 pinout guide.
GPIO17 Yellow LED through 330Ω (pin 11)
GPIO16 Green LED through 330Ω (pin 36)
GND LED cathodes (pin 9)
Raspberry Pi LED Blink in Python (gpiozero)
gpiozero is pre-installed on Raspberry Pi OS, so there is nothing to install. Its LED class gives you on(), off(), toggle(), is_lit, and blink(). On the Raspberry Pi 5 it uses the lgpio pin factory and the kernel’s GPIO character device, so it runs without sudo. The GPIO basics and the command-line tools are in my Raspberry Pi GPIO tutorial.
gpiozero LED blink() Explained
blink(on_time=1, off_time=1, n=None, background=True) (gpiozero docs) starts a background thread that turns the LED on for on_time seconds and off for off_time seconds, n times or forever. With background=True it returns at once, so your program keeps running. That is why the code example ends with pause(): without it, the script reaches its end, exits, and the LED stops blinking right away.
Raspberry Pi LED Blink Patterns Example (Python)
In the following example project, we’ll use blink() three ways on the yellow LED: 3 blinks that the program waits for ( background=False), a fast blink in the background while the main loop counts, and an SOS pattern in Morse code.
Wiring: yellow LED + 330Ω on GPIO17 (pin 11) to GND.
Here is The Application Code For This Example Project (blink_patterns.py):
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# LAB Name: Raspberry Pi LED Blink Patterns (gpiozero, Python) # Author: Khaled Magdy # For More Info Visit: www.DeepBlueMbedded.com from gpiozero import LED from time import sleep led = LED(17) # BCM GPIO17 = physical pin 11 # 1) Blink 3 times, then continue (background=False waits until the 3 blinks are done) print("3 slow blinks") led.blink(on_time=0.5, off_time=0.5, n=3, background=False) sleep(1) # 2) Blink in the background while the main program keeps working print("Fast blink in the background, main loop counts to 5") led.blink(on_time=0.1, off_time=0.1) for i in range(1, 6): print(" main loop:", i) sleep(0.5) led.off() # off() also stops the background blink sleep(1) # 3) SOS in Morse code: dot = 0.15 s, dash = 0.45 s, gaps between symbols and letters DOT, DASH = 0.15, 0.45 print("SOS") for symbol_time in [DOT] * 3 + [DASH] * 3 + [DOT] * 3: led.blink(on_time=symbol_time, off_time=0.15, n=1, background=False) led.off() print("Done") |
led.off() stops a background blink as well as turning the LED off. Each SOS symbol is one blink(n=1, background=False) call, so the next symbol starts only after the previous one ends. This is the output on my Raspberry Pi 5:
Raspberry Pi LED Blink in C++ (libgpiod)
In C++, libgpiod v2 ( gpiod.hpp) talks to the same GPIO character device. The headers aren’t on the Raspberry Pi OS image, so install them once:
|
1 |
sudo apt install build-essential cmake pkg-config libgpiod-dev |
The code example requests GPIO17 as an output with chip.prepare_request().add_line_settings(...).do_request(), then flips it every 500ms. The program does two things to keep the blink clean:
- Absolute deadlines: next += half_period; sleep_until(next); schedules each edge from the start time, so small wake-up delays never add up (the drift section below measures the difference).
- Ctrl+C handling: the SIGINT handler only clears a flag; the loop ends, writes the line LOW, and the request releases GPIO17 when it goes out of scope.
Build it with g++ and run it:
Raspberry Pi LED Blink Patterns Example (C++)
The same 3-blink and SOS patterns in C++, with a small blink() helper that times every edge with sleep_until:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
/* * LAB Name: Raspberry Pi LED Blink Patterns (n Blinks, SOS) With libgpiod v2 (C++) * Author: Khaled Magdy * For More Info Visit: www.DeepBlueMbedded.com */ #include <gpiod.hpp> #include <chrono> #include <iostream> #include <thread> #include <vector> using namespace std::chrono; using gpiod::line::value; // Blink n times: on_ms HIGH, off_ms LOW, timed with absolute deadlines static void blink(gpiod::line_request& req, gpiod::line::offset pin, int on_ms, int off_ms, int n) { auto next = steady_clock::now(); for (int i = 0; i < n; i++) { req.set_value(pin, value::ACTIVE); next += milliseconds(on_ms); std::this_thread::sleep_until(next); req.set_value(pin, value::INACTIVE); next += milliseconds(off_ms); std::this_thread::sleep_until(next); } } int main() { const gpiod::line::offset led_pin = 17; // BCM GPIO17 = physical pin 11 auto request = gpiod::chip("/dev/gpiochip0") .prepare_request() .set_consumer("blink_patterns") .add_line_settings(led_pin, gpiod::line_settings() .set_direction(gpiod::line::direction::OUTPUT) .set_output_value(value::INACTIVE)) .do_request(); std::cout << "3 slow blinks" << std::endl; blink(request, led_pin, 500, 500, 3); std::this_thread::sleep_for(seconds(1)); std::cout << "SOS" << std::endl; const int DOT = 150, DASH = 450, GAP = 150; const std::vector<int> sos = {DOT, DOT, DOT, DASH, DASH, DASH, DOT, DOT, DOT}; for (int t : sos) blink(request, led_pin, t, GAP, 1); request.set_value(led_pin, value::INACTIVE); std::cout << "Done" << std::endl; return 0; } |
For several programs, a CMakeLists.txt is easier than one g++ line each (it’s in the download):
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
cmake_minimum_required(VERSION 3.16) project(rpi_led_examples CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(PkgConfig REQUIRED) pkg_check_modules(GPIODCXX REQUIRED IMPORTED_TARGET libgpiodcxx) foreach(app blink_led blink_patterns multiple_leds) add_executable(${app} ${app}.cpp) target_link_libraries(${app} PRIVATE PkgConfig::GPIODCXX) endforeach() |
Control Multiple LEDs on Raspberry Pi
To control several LEDs as one group, use gpiozero’s LEDBoard in Python and one multi-line request in libgpiod. Both examples drive the yellow LED on GPIO17 and the green LED on GPIO16.
Wiring: yellow LED on GPIO17, green LED on GPIO16, both through 330Ω to GND.
gpiozero LEDBoard Example (Python)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# LAB Name: Raspberry Pi Multiple LEDs With LEDBoard (gpiozero, Python) # Author: Khaled Magdy # For More Info Visit: www.DeepBlueMbedded.com from gpiozero import LEDBoard from time import sleep leds = LEDBoard(17, 16) # yellow LED on GPIO17 (pin 11), green LED on GPIO16 (pin 36), 330Ω each print("Both on") leds.on() # every LED on sleep(1) print("Only the green LED (index 1)") leds.off() leds.on(1) # turn on selected LEDs by index sleep(1) print("Set both at once with a tuple") leds.value = (1, 0) # yellow on, green off sleep(1) print("Alternate (wig-wag). Press Ctrl+C to stop.") try: while True: leds.value = (1, 0) sleep(0.3) leds.value = (0, 1) sleep(0.3) except KeyboardInterrupt: leds.off() print("\nStopped, all LEDs off.") |
LEDBoard(17, 16) creates one LED per pin. leds.on() and leds.off() switch all of them, leds.on(1) switches selected LEDs by index, and leds.value = (1, 0) sets every LED from a tuple. LEDBoard also has blink() and toggle() for the whole group.
libgpiod Multi-Line Request Example (C++)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
/* * LAB Name: Raspberry Pi Multiple LEDs With One libgpiod v2 Multi-Line Request (C++) * Author: Khaled Magdy * For More Info Visit: www.DeepBlueMbedded.com */ #include <gpiod.hpp> #include <atomic> #include <chrono> #include <csignal> #include <iostream> #include <thread> static std::atomic<bool> running{true}; static void on_sigint(int) { running = false; } using gpiod::line::value; int main() { const gpiod::line::offsets leds = {17, 16}; // yellow LED GPIO17 (pin 11), green LED GPIO16 (pin 36) std::signal(SIGINT, on_sigint); // One request holds both lines as outputs, starting LOW auto request = gpiod::chip("/dev/gpiochip0") .prepare_request() .set_consumer("multiple_leds") .add_line_settings(leds, gpiod::line_settings() .set_direction(gpiod::line::direction::OUTPUT) .set_output_value(value::INACTIVE)) .do_request(); std::cout << "Both on" << std::endl; request.set_values({value::ACTIVE, value::ACTIVE}); // both lines in one call std::this_thread::sleep_for(std::chrono::seconds(1)); std::cout << "Only the green LED (GPIO16)" << std::endl; request.set_values({value::INACTIVE, value::INACTIVE}); request.set_value(16, value::ACTIVE); // one line of the request by its offset std::this_thread::sleep_for(std::chrono::seconds(1)); std::cout << "Alternate (wig-wag). Press Ctrl+C to stop." << std::endl; bool flip = false; while (running) { flip = !flip; request.set_values({flip ? value::ACTIVE : value::INACTIVE, flip ? value::INACTIVE : value::ACTIVE}); std::this_thread::sleep_for(std::chrono::milliseconds(300)); } request.set_values({value::INACTIVE, value::INACTIVE}); std::cout << "\nStopped, all LEDs off." << std::endl; return 0; } |
One request holds both lines. set_values({...}) writes both lines in one call, and set_value(16, ...) still writes a single line of the request by its offset. This is the real hardware testing result for both examples on my Raspberry Pi 5:
Raspberry Pi Multiple LED Switching Skew (Measured)
“Turn both LEDs on” is never perfectly simultaneous on Linux. I measured the delay between the two LED pins for four ways of switching them together, with my oscilloscope on both pins (CH1 on GPIO16, CH3 on GPIO17) and the programs toggling both lines every 500µs. Each number comes from 5 single-shot records at 2ns resolution, every rising and falling edge pair measured:
| Method | Calls per Switch | Median Skew | Range | Edge Pairs | First Pin |
|---|---|---|---|---|---|
gpiozero LEDBoard.on()/off() | 2 (one per LED) | 5.99µs | 5.89µs to 6.11µs | 245 | GPIO17 (first in the list) |
libgpiod C++, two set_value() | 2 | 871ns | 845ns to 941ns | 279 | GPIO17 (written first) |
libgpiod C++, one set_values() | 1 | 215ns | 204ns to 221ns | 278 | GPIO16 (lower line number) |
lgpio Python, group_write() | 1 | 214ns | 205ns to 221ns | 250 | GPIO16 (lower line number) |
These are my oscilloscope captures of one switch for each libgpiod and gpiozero case. CH1 (yellow trace) is GPIO16 with the green LED, CH3 (cyan trace) is GPIO17 with the yellow LED, and the scope triggers on the GPIO17 rising edge. The cursors X1 and X2 sit on the two rising edges, and ΔX in the readout box is the skew.
The measurements show three things. First, gpiozero’s LEDBoard.on() is a loop that calls on() for each LED ( boards.py in gpiozero 2.0.1), so every extra LED adds about 6µs. Second, two separate libgpiod writes are two system calls, about 0.87µs apart. Third, one set_values() call brings the skew down to about 0.21µs, but not to zero: the Raspberry Pi 5’s RP1 GPIO driver has no multi-line write ( pinctrl-rp1.c implements .set but not .set_multiple, kernel source), so the kernel writes the lines one after the other, lowest line number first, whatever order you list them in. lgpio’s group_write() uses the same kernel call and gives the same result in Python.
For LEDs, any of these is far faster than your eye. It matters when the pins drive something that must change together, like a parallel bus or a multiplexed display: use one set_values() (or lgpio.group_write() in Python) and expect a fraction of a microsecond between pins.
Raspberry Pi LED Blink Timing Drift (Measured)
A blink loop that sleeps for a fixed time after each write drifts: every sleep wakes up a little late, and the error adds up. To measure it without any instrument, I looped GPIO23 back to GPIO24 with a jumper, blinked GPIO23 at 5Hz (100ms on, 100ms off) for 100s with each method, and logged every rising edge on GPIO24 with libgpiod’s kernel timestamps (500 edges per method):
| Blink Method | Period Error (Mean) | Late After 100s | Drift per Hour at 5Hz |
|---|---|---|---|
gpiozero LED.blink() | +138µs | 69.1ms | ≈2.5s |
Python on(); sleep(); off(); sleep() | +138µs | 68.7ms | ≈2.5s |
C++ sleep_for() loop | +125µs | 62.3ms | ≈2.2s |
C++ sleep_until() (absolute deadlines) | 0.1µs | 0.05ms | none |
| Python absolute-deadline loop | 0.1µs | 0.03ms | none |

Every relative sleep on my Pi 5 came back about 60 to 70µs late, two sleeps per period. Most of that is Linux’s default timer slack of 50µs ( /proc/self/timerslack_ns reads 50000 for a normal process; PR_SET_TIMERSLACK), which lets the kernel group wake-ups, plus the time of the write itself. gpiozero’s blink() waits with Event.wait(on_time) after each write (gpiozero source), so it drifts exactly like a plain sleep loop. The error is per period, so at 1Hz the same 138µs per period adds up to about 0.5s per hour.
The fix is the same in both languages: compute each edge time from the start and sleep until it, so a late wake-up shortens the next sleep instead of pushing every later edge. The C++ examples above already do this. In Python:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# LAB Name: Raspberry Pi LED Blink Without Drift (absolute deadlines, gpiozero, Python) # Author: Khaled Magdy # For More Info Visit: www.DeepBlueMbedded.com from gpiozero import LED from time import monotonic, sleep led = LED(17) # BCM GPIO17 = physical pin 11 HALF_PERIOD = 0.5 # 0.5 s on, 0.5 s off print("Blinking the LED on GPIO17 on a fixed schedule. Press Ctrl+C to stop.") next_edge = monotonic() try: while True: led.toggle() next_edge += HALF_PERIOD # the next edge time, counted from the start sleep(max(0, next_edge - monotonic())) # sleep only what is left until that time except KeyboardInterrupt: led.off() print("\nStopped, LED off.") |
Each individual edge still jitters by a few tens of microseconds (up to +59µs in my C++ run), but the average period stays exact. For timing tighter than that, use hardware PWM, which runs without the CPU: see my Raspberry Pi PWM tutorial.
Raspberry Pi 5 Status LED (Red and Green Light Meaning)
The Raspberry Pi 5 has one bi-colour status LED next to the power button, with a red and a green die in one package (Raspberry Pi LED documentation). It means:
| Status LED | Meaning |
|---|---|
| Red, right after power-up | Power is applied; the firmware is starting |
| Steady green | Running normally |
| Green flashing off | microSD card activity (no flashes when booting from an M.2 SSD) |
| Red after shutdown | Low-power standby; press the power button or reconnect power to start again |
| Repeating green flash pattern | Boot error code (table below) |
The Raspberry Pi 4 and older have two separate LEDs instead: a red PWR LED for power and a green ACT LED for SD card activity, per the same documentation.
Raspberry Pi 5 status LED flash codes (long flashes, short flashes)
| Long | Short | Status |
|---|---|---|
| 0 | 3 | Generic failure to boot |
| 0 | 4 | start*.elf not found |
| 0 | 7 | Kernel image not found |
| 0 | 8 | SDRAM failure |
| 0 | 9 | Insufficient SDRAM |
| 0 | 10 | In HALT state |
| 1 | 2 | SD card overcurrent detected |
| 2 | 1 | Partition not FAT |
| 2 | 2 | Failed to read from partition |
| 2 | 3 | Extended partition not FAT |
| 2 | 4 | File signature/hash mismatch |
| 3 | 1 | SPI EEPROM error |
| 3 | 2 | SPI EEPROM is write protected |
| 3 | 3 | I2C error |
| 3 | 4 | Secure-boot configuration isn’t valid |
| 4 | 3 | RP1 not found |
| 4 | 4 | Unsupported board type |
| 4 | 5 | Fatal firmware error |
| 4 | 6 | Power failure type A |
| 4 | 7 | Power failure type B |
Raspberry Pi 5 ACT and PWR LEDs in /sys/class/leds
Linux sees the two dies as two LEDs in /sys/class/leds/: ACT is the green die (BCM2712 AON GPIO9, default trigger mmc0) and PWR is the red die (RP1 GPIO44, default trigger none), per the board’s device tree. Each has a trigger file (what drives it) and a brightness file (0 or 1 when the trigger is none):
|
1 2 3 4 |
cat /sys/class/leds/ACT/trigger # the trigger in [brackets] is active echo heartbeat | sudo tee /sys/class/leds/ACT/trigger # double-blink like a heartbeat echo cpu | sudo tee /sys/class/leds/ACT/trigger # follow CPU activity echo mmc0 | sudo tee /sys/class/leds/ACT/trigger # back to the default (microSD activity) |
Note: On the Raspberry Pi 5 the green ACT LED is inverted: it shows activity by turning off, so brightness 0 leaves it lit and brightness 1 turns it off. Guides written for older boards say “echo 0” to turn the ACT LED off; on the Pi 5 that keeps it on. The red PWR LED is not inverted: 1 is on, 0 is off.
So, to turn the whole status LED off until the next reboot:
|
1 2 3 |
echo none | sudo tee /sys/class/leds/ACT/trigger echo 1 | sudo tee /sys/class/leds/ACT/brightness # green off (inverted) echo 0 | sudo tee /sys/class/leds/PWR/brightness # red off |
And to restore the defaults: echo mmc0 | sudo tee /sys/class/leds/ACT/trigger. This is the real hardware result on my Raspberry Pi 5: the heartbeat trigger, the inverted green LED, the red LED on, and the red LED with a heartbeat:
The trigger files belong to root, so changing a trigger needs sudo. The brightness files belong to the input group on Raspberry Pi OS, and the default user is in that group, so once the trigger is none you can switch the LED from a script without sudo:
Change the Raspberry Pi 5 Status LED at Boot (config.txt)
The sudo tee changes last until the next reboot. To set a trigger at boot, add a line to /boot/firmware/config.txt (overlay README, base device tree parameters):
|
1 2 |
dtparam=act_led_trigger=heartbeat # or none, cpu, mmc0 (default) dtparam=pwr_led_trigger=none # or heartbeat, default-on ... |
With act_led_trigger=none the green LED stays steadily lit on the Pi 5 (no activity flashes), because of the inversion above.
Use an External LED as a Raspberry Pi Status LED
The gpio-led overlay turns any GPIO into a Linux LED with the same triggers, so an LED on your breadboard can show disk or CPU activity with no program running. I loaded it at runtime on GPIO21 on my Pi 5, and it appeared as /sys/class/leds/myled with the heartbeat trigger:
|
1 2 3 4 |
sudo dtoverlay gpio-led gpio=21 label=myled trigger=heartbeat # now sudo dtoverlay -r gpio-led # remove it # at boot, in /boot/firmware/config.txt: dtoverlay=gpio-led,gpio=21,label=myled,trigger=heartbeat |
While the overlay owns the pin, gpiozero and libgpiod can’t use it (see the troubleshooting section).
Raspberry Pi LED With RPi.GPIO (Raspberry Pi 5)
Older LED code with GPIO.setup(17, GPIO.OUT) and GPIO.output(17, GPIO.HIGH) still runs on the Raspberry Pi 5: Raspberry Pi OS replaces the original RPi.GPIO with rpi-lgpio, a compatible library built on lgpio, and I tested its output, input, and PWM functions in my Raspberry Pi GPIO tutorial. Run it without sudo, and don’t install python3-rpi.gpio: that package replaces rpi-lgpio with the original library, which doesn’t support the Pi 5. For new code, use gpiozero; my Raspberry Pi GPIO library comparison compares all of them.
Raspberry Pi LED Troubleshooting
The LED doesn’t light up
Check, in this order: the LED’s polarity (long leg toward the GPIO pin in active-high wiring), the pin (GPIO17 is physical pin 11; code uses BCM numbers, so LED(11) is the wrong pin), the resistor value (a 10kΩ by mistake makes the LED too dim to see), and that your script doesn’t end right after blink() (add pause()). gpioset -t0 GPIO17=1 lights the LED from the shell and rules out your code.
error requesting GPIO lines: Device or resource busy
Another program holds the line. Here a Python script running in the background owns GPIO17, so the C++ program can’t request it. gpioinfo GPIO17 shows who: consumer="lg" is lgpio (gpiozero). Stop the other program first; only one program can own a line at a time.
lgpio.error: ‘unexpected error’ (xGpioHandleRequest: Invalid argument)
The pin is owned by a kernel driver, here the PWM overlay on GPIO12 from my Raspberry Pi PWM tutorial ( gpioinfo shows consumer="kernel"). Use another pin, or remove the dtoverlay line from config.txt and reboot. The same happens with gpio-led, I2C, SPI, or UART overlays on their pins.
The LED stays on after my program exits
A Raspberry Pi GPIO keeps its last level after the program that set it exits: Linux doesn’t reset the pin when the line is released. Turn the LED off before exiting (all the examples here do it on Ctrl+C), or run gpioset -t0 GPIO17=0.
echo: Permission denied on /sys/class/leds
The trigger file needs root: use echo none | sudo tee /sys/class/leds/ACT/trigger. sudo echo none > ... doesn’t work, because the redirection runs in your own unprivileged shell.
Raspberry Pi LED FAQ
What resistor do I need for an LED on a Raspberry Pi?
330Ω for red, yellow, and green LEDs (about 3.6 to 3.9mA from a 3.3v pin), and 100Ω to 150Ω for blue or white LEDs. Anything from 220Ω to 1kΩ works for red, yellow, and green; a higher value is dimmer.
How many LEDs can a Raspberry Pi drive?
One LED per GPIO pin, up to the 28 header GPIOs, as long as the total stays under the 50mA that Raspberry Pi specifies for all GPIO pins together: 28 LEDs at about 1.7mA each, or about 12 LEDs at 4mA. For more LEDs or more current, use a driver chip, transistors, or addressable LEDs.
Why does my Raspberry Pi 5 show a red light?
Red means the Pi 5 is powered but not running: right after power-up before the firmware starts, or in standby after a shutdown. If it stays red and never turns green, check the power supply and the boot media.
Can I turn off the Raspberry Pi 5 LEDs?
Yes. Set the ACT trigger to none, write 1 to /sys/class/leds/ACT/brightness (the green LED is inverted on the Pi 5), and write 0 to /sys/class/leds/PWR/brightness, all with sudo tee, as shown in the status LED section.
Can a Raspberry Pi power an LED strip?
Not from a GPIO pin. A strip needs its own 5v or 12v supply, and a transistor, MOSFET, or driver board switched by the GPIO pin; addressable strips take a data signal instead. I’ll cover them in a separate tutorial.
Download Raspberry Pi LED Examples
The download contains every example from this tutorial in Python and C++ (blink, blink patterns, the no-drift blink, multiple LEDs, and the CMake file), plus the bench programs behind the skew and drift measurements and the status LED script, with a README that lists each command.
Download Raspberry Pi LED Examples (Python, C++)
Download all attachment files for this Article/Tutorial (project files, schematics, code, etc.) using the link below. Everything is tested and verified to work on real hardware, as you’ve seen in the demo videos & screenshots. Please consider supporting my work if it helped you.
Wrap Up
To conclude this tutorial, we’ve connected LEDs to the Raspberry Pi 5 with the right resistor and blinked them from Python (gpiozero) and C++ (libgpiod): single LEDs, blink patterns, and several LEDs as a group. The measurements give two simple rules: schedule blink edges with absolute deadlines so they don’t drift, and write a group of pins with one set_values() call when they must switch together. We’ve also read and controlled the Pi 5’s own status LED, which is inverted on the green side.
For the GPIO basics, see my Raspberry Pi GPIO tutorial; to read a push button, my Raspberry Pi button tutorial; and to dim LEDs with PWM, my Raspberry Pi PWM tutorial.

