Alternating flasher on the Raspberry Pi

FunduinoPublished on

Zwei LEDs abwechselnd blinken lassen – mit Python und gpiozero

Der Wechselblinker ist das klassische zweite Projekt – hier auf dem Raspberry Pi statt auf dem Arduino. Zwei LEDs blinken abwechselnd: Sobald die eine leuchtet, ist die andere aus.

Du schließt die LEDs an zwei GPIO-Pins an und schreibst ein kurzes Python-Programm in Thonny.

Two LEDs with series resistors connected to GPIO 23 and GPIO 24 on a Raspberry Pi

You need this

830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm
LED 5 mm - 100 Pieces, 5 Colors, 2 V LED 5 mm - 100 Pieces, 5 Colors, 2 V 1,97 € *Item no. F23107316About the product — LED 5 mm - 100 Pieces, 5 Colors, 2 V
Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W 1,72 € *Item no. F23107216About the product — Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W

All 4 parts together 8,33 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

Connect the LEDs

Two LEDs should blink alternately.

  • Connect the first LED through a 220-ohm resistor to GPIO 23 (Pin 16).
  • Connect the second LED through a 220-ohm resistor to GPIO 24 (Pin 18).
  • Connect the short legs of both LEDs to GND (Pin 6) via the breadboard's negative rail..

The Raspberry Pi needs a power supply with a USB-C connector. A standard USB cable is not enough.

Two LEDs with series resistors connected to GPIO 23 and GPIO 24 on a Raspberry Pi

The program

Write the program code on the Raspberry Pi in Thonny (Menu → Programming → Thonny), save it as wechselblinker.py, and start it with the green arrow. The gpiozero library is already installed on Raspberry Pi OS.

The program creates an object for each LED and alternately turns one on and the other off in an endless loop, with a one-second pause in between.

alternating-blinker.py
# Import the library for the LEDs
from gpiozero import LED
from time import sleep

LED1 = LED(23)   # the first LED is connected to GPIO 23 (Pin 16)
LED2 = LED(24)   # the second LED is connected to GPIO 24 (Pin 18)

while True:      # the main program runs forever
    LED1.on()    # turn on the first LED
    LED2.off()   # turn off the second LED
    sleep(1)     # wait 1 second
    LED1.off()   # turn off the first LED
    LED2.on()    # turn on the second LED
    sleep(1)     # wait 1 second
# the program then jumps back to the beginning of the loop
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.