Fast delivery
Discount conditions for companies
Up to 10% discount for pupils, students and educational institutions
Europe wide shipping for 4,90€

Flashing LED on Raspberry Pi microcontroller

How to make a LED blink on the Raspberry Pi?

Unlike the Arduino, you don't have access to the built-in LEDs on the Raspberry Pi. They only show power supply and access to the µSD card.

To make a LED blink, you need an external LED with a suitable series resistor and jumper cables. The most sensible way is to build the circuit on a breadboard with a T-Cobbler. This kind of arrangement also facilitates later experiments with several LEDs, pushbuttons and sensors. The programming is done in the programming language Python.

The circuit diagram (blinking LED on Raspberry Pi)

Wiring diagram for entry level Raspberry Pi flashing LED

What is an LED?

LED stands for Light Emitting Diode. The last word indicates the electronic properties. A diode is a semiconductor that lets the current through only in one direction, and blocks it in the other direction. The two "legs" are called anode and cathode. If you look closely, you can see that the legs are of different lengths. The positive voltage is applied to the longer leg, the anode; the shorter leg, the cathode, is connected to ground via a resistor. The resistor is necessary because of another property of diodes. First of all, a minimum voltage is required for any current to flow at all: This is called forward voltage. At higher voltages the diode becomes fully conductive. Therefore you need a series resistor to limit the current, because the pins of the micro controllers and also of the Raspberry Pi should be charged with a maximum of 15 to 20 mA. We use series resistors in the range of 100 to 330 Ω (Ohm).

The programming language Python can be extended by program modules, similar to C/C++ with libraries. For our blinking LED we need two of these modules: first "time" to set the blinking duration. And secondly a module to access the GPIOs (General Purpose Input/Output).

How do I control an LED with a Raspberry Pi?

Importing the modules is done at the beginning of the Python program; you have several options to do this. Examples:

The following line imports the module time:

import time

When calling the method sleep , the module name time must be prefixed with a dot:

time.sleep(1)

In contrast to delay(), where the time interval is specified as argument in milliseconds, time.sleep() specifies the values in seconds, possibly with decimal point.

import RPi.GPIO as GPIO

This simplifies the calls to the methods in the program, e.g.

GPIO.setmode(GPIO.BCM)

Third example for another module for GPIO access:

from gpiozero import LED

From the module gpiozero only the class LED is imported. With this import method, the module name is no longer prepended when the method/function is called.

Example programs to control a LED with a Raspberry Pi microcontroller

First example with the module RPi.GPIO:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)# pin name for Broadcomm processor
GPIO.setup(26,GPIO.OUT)# GPIO26 (=phys. pin 37) becomes output
while True:# endless loop, abort with Ctrl-C
GPIO.output(26,1)# GPIO 26 is switched HIGH
time.sleep(1)
GPIO.output(26,0)# GPIO 26 is switched LOW
time.sleep(1)

Second example with the module gpiozero. Only LED and sleep are imported.

from gpiozero import LED
from time import sleep
redLED = LED(26)# redLED is instantiated at GPIO 26
while True:
redLED.on()# redLED is switched on
sleep(1)
redLED.off() # redLED is switched off
sleep(1)

Challenge: Extend circuit and program for a traffic light cycle, e.g. red(3s), red/yellow(1s), green(3s), yellow(1s), restart with red.

Suggested solution see no. 02 The alternating blinker

Topics: #raspberry
Please enter the string in the text field below.

The fields marked with * are required.

Matching articles
100 pieces - LEDs with 5mm diameter (5 colors) 100 pieces - LEDs with 5mm diameter (5 colors)
Content 1 Piece
€1.97 *
Item no: F23107316
Add
GPIO Adapter with 40 Pins for Raspberry Pi - T-Type GPIO Adapter with 40 Pins for Raspberry Pi -...
Content 1 Piece
€2.39 *
Item no: F23106530
Add
- 15%
GPIO expansion board V3.0 for Raspberry Pi GPIO expansion board V3.0 for Raspberry Pi
Content 1 Piece
€1.95 * €2.29 *
Item no: F23108401
Add
GPIO Expansion Module for Raspberry Pi 3 - U-Type Blue GPIO Expansion Module for Raspberry Pi 3 -...
Content 1 Piece
€4.22 *
Item no: F23108407
Add
- 15%
GPIO reference board for Raspberry Pi GPIO reference board for Raspberry Pi
Content 1 Piece
€1.31 * €1.54 *
Item no: F23108615
Add
- 2%
GPIO Multifunction Expansion Board for Raspberry Pi 2 and 3B+ GPIO Multifunction Expansion Board for...
Content 1 Piece
€5.77 * €5.90 *
Item no: F23108797
Add
GPIO adapter with 40 pins for Raspberry Pi IDC socket to breadboard GPIO adapter with 40 pins for Raspberry Pi IDC...
Content 1 Piece
€2.71 *
Item no: F23108742
Add
- 26%
GPIO expansion board for Raspberry Pi 2, Pi 3, Model B GPIO expansion board for Raspberry Pi 2, Pi 3,...
Content 1 Piece
€7.31 * €9.90 *
Item no: F23108695
Add
GPIO Expansion Module for Raspberry Pi 3 - U-Type Red GPIO Expansion Module for Raspberry Pi 3 -...
Content 1 Piece
€4.66 *
Item no: F23108643
Add