You can make an LED blink with a Raspberry Pi by connecting it to a GPIO pin through a current-limiting resistor, then switching that pin on and off in a short Python program. The example below uses the GPIO Zero library, which is included in many Raspberry Pi OS installations and provides a straightforward way to control a basic LED.
What you need
For a first test, gather a Raspberry Pi, a compatible power supply, a breadboard, jumper wires, one ordinary LED, and a resistor. A resistor around 330 ohms is a common choice for this simple circuit; 220 ohms is also often used, depending on the LED and circuit. Use a resistor rather than connecting an LED directly to a GPIO pin: the resistor limits current and helps protect both the LED and the Pi.
- A Raspberry Pi with Raspberry Pi OS and access to its GPIO pins.
- One standard low-current LED, such as a red 5 mm LED.
- A 220–330 ohm resistor.
- A breadboard and two jumper wires.
- A way to run Python, such as the desktop’s terminal or an editor.
This setup is for a single indicator LED, not a high-power LED strip or lamp. Those require a suitable driver or transistor circuit and a separate power arrangement.
Wire the LED safely
Turn the Pi off and disconnect its power before changing the wiring. GPIO pin numbers can be confusing because a pin’s physical position on the header is not always the same as its GPIO number. This example uses GPIO17, which is physical header pin 11 on standard 40-pin Raspberry Pi headers. Check the pinout for your exact board before connecting anything.
- Connect a jumper from physical pin 11 (GPIO17) to one end of the resistor.
- Connect the resistor’s other end to the LED’s anode, the positive side. The longer LED leg is usually the anode, though the flat edge on the LED’s body generally marks the cathode.
- Connect the LED’s cathode to a ground pin, such as physical pin 6 (GND).
- Check that the LED and resistor are in series, and that neither lead is accidentally connected to a neighboring pin.
The resistor can go on either side of the LED in a series circuit. GPIO pins use 3.3 V logic; do not treat them as 5 V tolerant inputs or outputs. The Pi’s GPIO pins are not designed to power motors, strips, or other substantial loads directly.
Run a Python blink program
Reconnect power and open a terminal on the Pi. GPIO Zero is commonly available by default in Raspberry Pi OS. Save this code as blink.py:
from gpiozero import LED
from time import sleep
led = LED(17)
while True:
led.on()
sleep(1)
led.off()
sleep(1)
Run it with python3 blink.py. The LED should stay on for one second, then off for one second, repeating until you stop the program with Ctrl+C. The number passed to LED() is the GPIO number, not the physical header position: GPIO17 corresponds to physical pin 11 in this wiring example.
The sleep(1) calls pause the program for one second. Change both values to 0.5 for a quicker, roughly two-flashes-per-second cycle, or to 2 for slower blinking. A blink cycle includes both the on-time and the off-time, so one second on plus one second off is a two-second cycle.
How the circuit and code work
A GPIO output can be set high or low. In this circuit, setting GPIO17 high allows current to flow from the pin, through the resistor and LED, to ground. The LED lights. Setting the pin low stops that current path, so the LED goes dark. The resistor reduces the current; the LED’s orientation matters because it is a diode and normally conducts in only one direction.
GPIO Zero’s LED object handles the pin control. Its on() and off() methods make the intention easy to read, while sleep() sets the duration between changes. The infinite while True loop repeats the pattern. If you want the program to finish automatically after a fixed number of flashes, replace the infinite loop with a counted for loop.
For example, led.blink(on_time=1, off_time=1) is a GPIO Zero convenience method for repeating the same timing. The program will continue running while the blinking is managed; use Ctrl+C to stop it. The explicit loop is useful when learning because it makes each on and off interval visible in the code.
Adjusting the blink and using other GPIO pins
Use a different GPIO pin by changing the number in LED(17) and moving the wire to that pin’s physical header position. Choose a GPIO pin that is available for general input/output on your particular board, and verify its position using a current pinout diagram. Ground connections are shared, so the LED’s ground wire can use any ground pin.
- Different timing: Set the on and off delays independently, such as
sleep(0.2)while lit andsleep(0.8)while off. - Two LEDs: Give each LED its own GPIO pin and its own series resistor. Avoid connecting multiple LEDs to one pin without checking the electrical requirements.
- Stop cleanly: Press Ctrl+C in the terminal. If the program exits unexpectedly, GPIO Zero normally cleans up pin resources when the program ends.
Software timing through a regular Python loop is suitable for a visible status light, but it is not a precision timing method. The operating system can interrupt a program, so the intervals may vary slightly. For an ordinary blinking indicator this is rarely important; use a dedicated controller or timing hardware when precise timing is essential.
Common problems and safety checks
- The LED never lights: Check its direction, the GPIO number, the ground connection, and whether the resistor and LED form a complete series path. Confirm that the jumper is on physical pin 11 for GPIO17.
- The script reports a missing module: Check that the program is running with Python 3 and that GPIO Zero is installed for the operating system. On Raspberry Pi OS, the package can generally be installed using the system package manager; follow the current Raspberry Pi documentation for the command appropriate to your release.
- The wrong LED blinks: The code uses GPIO numbering, while diagrams may show physical pin numbers. Match the code’s GPIO number to the board pinout.
- The LED stays on or the program seems stuck: A repeating loop is expected. Press Ctrl+C in the terminal to stop it. Check that the code includes both an on and off action.
Keep wiring changes to a powered-down board, avoid shorting adjacent pins, and never connect an LED directly between a GPIO pin and ground without a resistor. If you are unsure about a component’s current rating or a nonstandard LED, consult its datasheet rather than assuming it is safe for a GPIO pin.
Frequently Asked Questions
Can I blink an LED without a resistor?
No. A resistor in series limits current and protects the LED and Pi’s GPIO circuitry. A typical starting range for a basic LED circuit is 220–330 ohms, but component specifications and the circuit supply matter.
Is GPIO17 the same as physical pin 17?
No. GPIO17 is the GPIO name; on a standard 40-pin header it is physical pin 11. Always use a pinout diagram and make sure the code and wiring refer to the same numbering system.
Why does my LED have a longer and shorter leg?
The longer leg is usually the anode, or positive side, and the shorter leg is usually the cathode. The cathode is also commonly indicated by a flat edge on the LED casing. These are useful clues, but check the component information if the legs have been trimmed.
Can I make the LED blink faster?
Yes. Reduce the sleep durations in the loop, for example from 1 second to 0.2 seconds. The Python loop is adequate for visible blinking, but operating-system scheduling means it is not guaranteed to keep exact timing.
Can a GPIO pin control an LED strip?
Not directly in most cases. Strips may draw far more current than a GPIO pin can supply and may require a different voltage. Use an appropriate driver circuit and power supply, and follow the strip and Pi documentation.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Delete Picture Kik? How to Remove Photos
- How to Stop WordPress Comment Spam: A Layered Guide
- What Happens to X Replies, DMs, and Followers After Deletion?
- Data Breaches That Shook the World
- Disable Google Assistant: Turn It Off on Android & Home
- Resin vs FDM Printers for Miniatures: Detail, Durability, Cost