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

Alternately flashing LEDs on the Raspberry Pi microcontroller

How to make two LEDs flash alternately on the Raspberry Pi?

You can connect the electronic components on the breadboard without a T-Cobbler. But then you need a jumper wire of type female - male for the connection to the J6 header of the Raspberry Pi, i.e. female for the Raspberry Pi and male for the breadboard.

Wiring diagram alternately flashing LED on Raspberry Pi


The program code (alternately blinking LED on the Raspberry Pi)

# From here the code can be copied directly into the Python software.
# Black, colored = code and gray = explanations
#We start with importing the program modules
import RPI.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM) #We set the mode to BCM
GPIO.setup(23,GPIO.OUT) #Pin 23 is an output.
GPIO.setup(24,GPIO.OUT) #Pin  24 is an output.
# The loop starts here
try:
while True: #While loop so that the program runs continuously
GPIO.output(23,GPIO.HIGH) # Turns on the LED at pin 23.
GPIO.output(24,GPIO.LOW) # Turns off the LED at pin 24.
time.sleep(1) # Wait 1 second
GPIO.output(23,GPIO.LOW) # Turns off the LED on pin 23.
GPIO.output(24,GPIO.HIGH) # Turns on the LED on pin 24.
time.sleep(1)
# Here at the end the program jumps to the start of the loop part. So..
# ...switch on the LED at pin 23.
# ... etc... etc... etc..
except KeyboardInterrupt: # With CTRL+C we interrupt the program
print ("Finished")# Write "Finished" into the shell window
GPIO.cleanup() # Exit the program

In example no. 01 Blinking LED we had implemented the infinite loop with while True and indentation of the following code lines. If you aborted this infinite loop with the key combination Ctrl-C, you will have noticed that firstly you get an error message and secondly the LED may still be on. To prevent these two unwanted effects, some lines of code have been added to this example. Before the while True loop the line try: is inserted. The colon leads to the indentation of the whole loop. If an error occurs in this loop while the program is running, there is no error message, but the program is continued at except KeyboardInterrupt:. With the print command, the end of the program is output in the Python shell (lower window at Thonny), with GPIO.cleanup(), the GPIOs (i.e. our LEDs) are switched off and released for other purposes.

Example: Traffic light cycle on Raspberry Pi

Here is a proposed solution for a traffic light cycle using the module gpiozero:

traffic light cycle on raspberry pi circuit diagram

traffic light cycle on raspberry pi circuit diagram replica

Excerpt Raspberry Pi code

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
- 5%
GPIO Multifunction Expansion Board for Raspberry Pi 2 and 3B+ GPIO Multifunction Expansion Board for...
Content 1 Piece
€5.48 * €5.77 *
Item no: F23108797
Add
- 5%
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.57 * €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
- 5%
GPIO Expansion Module for Raspberry Pi 3 - U-Type Red GPIO Expansion Module for Raspberry Pi 3 -...
Content 1 Piece
€4.43 * €4.66 *
Item no: F23108643
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
- 5%
GPIO Expansion Module for Raspberry Pi 3 - U-Type Blue GPIO Expansion Module for Raspberry Pi 3 -...
Content 1 Piece
€4.01 * €4.22 *
Item no: F23108407
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
- 5%
GPIO Adapter with 40 Pins for Raspberry Pi - T-Type GPIO Adapter with 40 Pins for Raspberry Pi -...
Content 1 Piece
€2.27 * €2.39 *
Item no: F23106530
Add