Playing beep music on a Raspberry Pi with a very quick and cheap project. Assemble a few components in ten minutes and be making sounds and playing music with the Raspberry Pi.
VIDEO
The video here demonstrates the project which this article describes how to build.
The article breaks the project down into several stages:
Hardware
Software
Install
Project Costing
Hardware
This is a very simple project, not much more complicated than lighting an LED with the Raspberry Pi.
Piezo Driver Circuit
The circuit is the piezo sounder, driven by a transistor in order to keep the 3V3 of the GPIO separate from the 5V supplying the piezo sounder. There is a 10K resistor on the base of the transistor as the piezo sounder requires very little current to work.
The piezo sounder is connected between the collector of the transistor and 5V. And a 1K resistor is placed in parallel to the piezo sounder, this allows the potential across the piezo sounder to equalize when the transistor is switched off.
When the transistor is switched on the potential across the piezo sounder is raised to 5V.
Each time the transistor is switched on then off, a single click is produced by the piezo sounder. By continually switching the transistor off and on, a sound is produced at the same frequency as the transistor is being cycled.
Software
The software has a list of the frequencies of each musical note, for each octave, represented as cycle periods (1/Frequency). To play a note the software switches the transistor on, waits for 1/2 the required cycle period. Then turns the transistor off, and waits again for 1/2 the required cycle period.
To use the software, two command line arguments are used. The first is always the GPIO number which the piezo device is connected to. The second is a list of note data to play. The note data is sets of three values, the octave number 0-9, followed by the note name [C, C#, D, D#, E, F, F#, G, G#, A, A#, B] a '-' will play a silent period, and the period of time to play the note in seconds.
e.g.
sudo ./RPiBeep 14 "4 D 0.5 4 - 0.1 4 D 0.2 4 E 0.5 4 D 0.5 4 G 0.5 4 F 1.0 4 - 0.5 4 D 0.5 4 - 0.1 4 D 0.2 4 E 0.5 4 D 0.5 4 G 0.5 4 F 1.0 4 - 0.5 4 D 0.5 4 - 0.1 4 D 0.2 5 D 0.5 4 B 0.5 4 G 0.5 4 F 0.5 4 E 0.5 4 - 0.5 5 C 0.5 5 - 0.1 5 C 0.2 4 B 0.5 4 G 0.5 4 A 0.5 4 G 1.0"
Install
Copy the driver software to the Raspberry Pi from a remote computer, or download the driver directly onto the Raspberry Pi from a web browser:
scp RPiBeep.tar.gz pi@[IP_ADDRESS]:/home/pi/
Login to the Raspberry Pi, either locally or remotely: ssh pi@[IP_ADDRESS]
Unpack the driver: tar -xf RPiBeep.tar
Use the driver: sudo ./RPiBeep SCALE
Tested Raspberry Pi (Original)
Raspberry Pi 2
Raspberry Pi 3
DownloadV1.00 2016-07-29 - Raspberry Pi Piezo driver.