Light and sound on the Raspberry Pi
FunduinoPublished on
LED und Piezo-Lautsprecher gleichzeitig schalten – mit Python und gpiozero
You need this
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
2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
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
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
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
The program
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