Light and sound on the Raspberry Pi

FunduinoPublished on

LED und Piezo-Lautsprecher gleichzeitig schalten – mit Python und gpiozero

Eine LED und ein Piezo-Lautsprecher schalten gemeinsam ein und aus: Es leuchtet und piepst im Sekundentakt.

Du schließt beide Bauteile an zwei GPIO-Pins des Raspberry Pi an und steuerst sie mit einem kurzen Python-Programm.

LED and piezo speaker on GPIO 23 and GPIO 24 of a Raspberry Pi

You need this

5 V buzzer - electromagnetic sounder, 2400 Hz 5 V buzzer - electromagnetic sounder, 2400 Hz 0,90 € *Item no. F23108736About the product — 5 V buzzer - electromagnetic sounder, 2400 Hz
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 5 parts together 9,23 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

Connect an LED and piezo speaker

An LED and a piezo speaker are to be switched on and off simultaneously.

  • The LED is connected via a 220-ohm resistor to GPIO 23 (Pin 16).
  • The piezo speaker is connected to GPIO 24 (Pin 18).
  • The LED's short leg and the speaker's negative terminal connect via the negative rail to GND (Pin 6).

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

LED and piezo speaker on GPIO 23 and GPIO 24 of a Raspberry Pi

The program

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

The LED and speaker are switched on together in an infinite loop, switched off together after one second, and everything starts again after another second. A shorter or longer pause in sleep() makes it beep and light up faster or slower.

The gpiozero Buzzer class only switches the pin on and off. This works with an active buzzer, which generates its own tone. A passive piezo speaker only makes a click when supplied with DC voltage; the article does not specify whether the linked speaker is active or passive.

light_and_sound.py
# Import libraries for LED and piezo buzzer
from gpiozero import LED, Buzzer
from time import sleep

Licht = LED(23)      # the LED is connected to GPIO 23 (pin 16)
Pieps = Buzzer(24)   # the piezo buzzer is connected to GPIO 24 (pin 18)

while True:          # the main program runs indefinitely
    Licht.on()       # turn the LED on
    Pieps.on()       # turn the piezo buzzer on
    sleep(1)         # wait 1 second (it beeps and lights up)
    Licht.off()      # turn the LED off
    Pieps.off()      # turn the piezo buzzer off
    sleep(1)         # wait 1 second (no noise, no light)
# Reducing or increasing the pause (sleep)
# makes it beep and light up faster or slower.
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.