As an Amazon Associate we earn from qualifying purchases.
Blinking an LED is the first project most people try on a Raspberry Pi. It is the hardware version of “Hello, World”: you switch one pin on and off, and a light shows that your code and wiring work. The timing comes from a simple delay in Python, so you do not need a separate timer chip.
This guide covers four examples: blinking once, blinking forever, blinking faster, and a three-LED chaser. Each one uses the RPi.GPIO library and the same basic circuit.
What you need
- A Raspberry Pi with a 40-pin GPIO header, running Raspberry Pi OS with Python installed
- One LED for the first three examples, and three LEDs for the chaser
- One current-limiting resistor per LED (values of a few hundred ohms, such as 220 or 330 ohms, are common)
- A breadboard and jumper wires
Wiring the LED
Connect the GPIO pin to a resistor, the resistor to the LED’s longer leg (the anode), and the LED’s shorter leg (the cathode) to a ground pin. The resistor is not optional. Without it, the LED can draw too much current and damage itself or the Pi.
The diagram below shows the circuit layout. In the code that follows, the LED signal pin is physical pin 40 on the header. Pin 39, right beside it, is a ground pin, which makes the wiring easy to follow.
How the code works
All four programs follow the same pattern:
- Import the libraries.
RPi.GPIOcontrols the pins andtimeprovides the delay. - Choose the pin numbering.
gpio.setmode(gpio.BOARD)means the numbers refer to the physical position of the pin on the header, not the chip’s GPIO names. Physical pin 40 is GPIO21 in the other (BCM) scheme. - Set the pin as an output.
gpio.setup(40, gpio.OUT)tells the Pi it will drive this pin. - Write a value.
gpio.output(40, 1)turns the pin on (about 3.3 V) andgpio.output(40, 0)turns it off. - Wait.
time.sleep()pauses the program for the number of seconds you give it. This delay is your timer.
Blinking once
This is the simplest version. The LED turns on, stays lit for one second, then turns off and the program ends. It is a good first test to confirm that the LED, resistor and pin are wired correctly.
import RPi.GPIO as gpio # library that controls the GPIO pins
import time # library that provides delays
gpio.setmode(gpio.BOARD) # use physical pin numbers
gpio.setwarnings(False) # disable warnings
gpio.setup(40,gpio.OUT) # set pin 40 as an output
gpio.output(40,1) # turn the LED on
time.sleep(1) # wait one second
gpio.output(40,0) # turn the LED off
If nothing lights up, check the LED direction first. LEDs only work one way round, and swapping the legs is the most common mistake.
Blinking infinitely
To make the LED blink continuously, put the on and off steps inside a while True loop. Each pass turns the LED on for one second and off for one second. Press Ctrl+C in the terminal to stop the program.
import RPi.GPIO as gpio # library that controls the GPIO pins
import time # library that provides delays
gpio.setmode(gpio.BOARD) # use physical pin numbers
gpio.setwarnings(False) # disable warnings
gpio.setup(40,gpio.OUT) # set pin 40 as an output
while True: # start an infinite loop
gpio.output(40,1) # LED on
time.sleep(1) # wait one second
gpio.output(40,0) # LED off
time.sleep(1) # wait one second
Blinking faster
Changing the blink speed only means changing the delay. Here both delays are 0.1 seconds, so the LED completes about five on-off cycles every second.
import RPi.GPIO as gpio # library that controls the GPIO pins
import time # library that provides delays
gpio.setmode(gpio.BOARD) # use physical pin numbers
gpio.setwarnings(False) # disable warnings
gpio.setup(40,gpio.OUT) # set pin 40 as an output
while True: # start an infinite loop
gpio.output(40,1) # LED on
time.sleep(.1) # wait 0.1 seconds
gpio.output(40,0) # LED off
time.sleep(.1) # wait 0.1 seconds
You can use different on and off times too. A short on time with a long off time gives a quick flash, like a status light. Keep in mind that time.sleep() is not perfectly precise on a general-purpose operating system. That is fine for a visible blink, but if you need exact timing you should use hardware PWM or a dedicated timer.
LED chaser using Raspberry Pi
A chaser lights several LEDs in sequence so the light appears to move. This version uses three LEDs on physical pins 40, 38 and 36, each with its own resistor and a shared ground. Only one LED is on at a time, and the active LED changes every second.
import RPi.GPIO as gpio # library that controls the GPIO pins
import time # library that provides delays
gpio.setmode(gpio.BOARD) # use physical pin numbers
gpio.setwarnings(False) # disable warnings
gpio.setup(40,gpio.OUT) # LED 1 on pin 40
gpio.setup(38,gpio.OUT) # LED 2 on pin 38
gpio.setup(36,gpio.OUT) # LED 3 on pin 36
while True: # start an infinite loop
gpio.output(40,1) # LED 1 on
gpio.output(38,0)
gpio.output(36,0)
time.sleep(1) # wait one second
gpio.output(40,0)
gpio.output(38,1) # LED 2 on
gpio.output(36,0)
time.sleep(1) # wait one second
gpio.output(40,0)
gpio.output(38,0)
gpio.output(36,1) # LED 3 on
time.sleep(1) # wait one second
Once this works, try shortening the delays for a faster chase, or add more LEDs. If the repeated lines start to feel clumsy, store the pin numbers in a list and loop over it.
Tips for reliable blinking
- Always use a resistor. Each LED needs its own.
- Clean up when you finish. Calling
gpio.cleanup()at the end of a program resets the pins you used. In the looping examples you can place it after catchingKeyboardInterrupt. - Do not mix up numbering modes. If you use
BOARD, count physical pins. If you useBCM, use the GPIO names. - Do not power high-current loads directly from a pin. A GPIO pin can drive a small LED, but motors, relays and lamps need a transistor or driver.
Other boards and next steps
The idea of switching a pin and waiting is the same on every platform. If you prefer microcontrollers, see our guides on blinking an LED on an Arduino, how to control an LED with Arduino, or try the same exercise on an 8051 microcontroller. Once blinking feels easy, fading an LED on an Arduino introduces PWM, and RGB LED lights let you mix colours.
FAQ
Do I need a resistor to blink an LED on a Raspberry Pi?
Yes. The resistor limits the current through the LED. Without it, the LED can be damaged, and the extra current can stress the GPIO pin too.
Why does my LED not blink?
Check that the LED is the right way round, that the resistor is in the circuit, and that the ground wire is connected. Then confirm the pin number in your code matches the pin you wired, and that your numbering mode (BOARD or BCM) is correct.
How do I change the blink speed?
Change the value passed to time.sleep(). A smaller number gives a faster blink and a larger number gives a slower one. You can set the on and off delays separately.
What is the difference between BOARD and BCM modes?
BOARD uses the physical position of each pin on the 40-pin header. BCM uses the GPIO numbers assigned by the Broadcom chip. Pick one and use it consistently throughout your program.