Keep an LED lit while playing a sound. When the user presses a button, an audio clip corresponding to that button will play via a speaker. A Python script will run continuously to grab button presses and play audio.
- Raspberry Pi
- solderless breadboard
- 1x LED
- 1x 330 ohm resistor
- 1x tactile button
- 2x male-to-male jumper leads
- 3x male-to-female jumper leads
- Python 3, GPIO Zero, and PyGame (all preinstalled in Raspbian)
See the included play_led_bb.png
file for the hardware layout. Connect the short leg of the LED and one set of legs of the button to ground. The other set of button legs goes to GPIO pin 27 on the Pi. The long leg of the LED connects to one end of the resistor and the other end of the resistor connects to GPIO pin 17.
Make sure play_led.py
is executable (chmod a+x play_led.py
) and run it with ./play_led.py
or python3 play_led.py
. Press the button to play the sound and light up the LED. Once the sound is finished playing, the LED will turn off. Press Ctrl+C
to quit the program.
The GPIO pins for the LED and button as well as the sound file can be specified via the command line:
./play_led.py <LED> <BUTTON> <SOUND FILE>
For example, if you've connected the LED to pin 4 and the button to pin 27 and you want to play a file called vinyl_backspin.ogg
, you would run the following:
./play_led.py 4 27 vinyl_backspin.ogg
All parameters must be specified for now, though a future update will allow setting them individually.
- Pressing a button causes audio to play
- Pressing the same button again while audio is playing does nothing
- Pressing a different button while audio is playing stops the first clip and plays a new one
- Turn on an LED above the button when the audio clip is playing to provide feedback to the user
- Use a switch or button to control power to the device; flipping the switch or pressing the button signals to the device to start shutting down gracefully
- Start script as soon as the device is booted and keep running as long as the device is on
- Set command line options with flags instead of positional parameters
This project's source code, play_led.py
, and documentation, README.md
(this file), are licensed according to the terms of the GNU General Public License, version 3 (GPLv3).
The included sound files, drum_roll.ogg
and vinyl_backspin.ogg
, have been released into the public domain under the CC0 1.0 Universal Public Domain Dedication (CC0 1.0). The original files can be found at freesound.org:
These versions of the files were copied from the Sonic Pi distribution samples folder and converted from FLAC to OGG.
The breadboard diagram, play_led_bb.png
, was created with Fritzing and is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License (CC BY-SA 4.0).