This project is about creating a WI-FI speaker. I had an old broken computer speaker and an unused Raspberry Pi 1B, like most engineers... My basic idea was to simply put the pi into the speaker. Reuse old stuff without creating new waste. It turned out that the speaker amplifier doesn’t work anymore and I decided to create a simple audio amplifier. Finally, I wanted to use a Spotify connect service to play music.
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Things Used for the Project
To set-up the WI-FI speaker, i used following supplies
- Raspberry Pi at least model 1 B (~15€)
- Old computer speaker box
- 3.5mm audio connection from old headphones
- DC-DC converter (0.39€) https://www.amazon.de/gp/product/B06XYDNM77
- USB audio card (10€) https://www.amazon.de/gp/product/B06XYDNM77
- USB WI-FI dongle (9€) https://www.amazon.de/gp/product/B06XYDNM77
For the amplifier board I decided to use the LM386N-4. This IC is a simple amplifier with good results for audio applications.
- LM386N-4 (0.81€)
- Resistors: 5Ω, 2x 1kΩ and 200Ω
- Capacitors: 4700µF, 1000µF, 100µF and 100nF
- Circuit board
That sums up to approximately 36€. Because i already had most of the stuff, I just had to buy the DC-DC converter, the USB audio card and the LM386N.
Step 2: Create the Amplifier Circuit
The heart of the amplifier is the LM386N-4. The LM386N-Family is a popular amplifier IC that is used for lot of portable music devices such as CD-Player, Bluetooth-Boxes, etc. There are already a lot of tutorials describing this amplifier: https://www.instructables.com/howto/LM386/
The circuit for this project was mainly inspired by this YouTube tutorial: https://www.youtube.com/watch?v=4ObzEft2R_g and a good friend of mine who helped me a lot. I choose the LM386N-4 because it has more power than the other ones and I decided to drive the board with 12V.
The first step to create the board is to test the circuit on a breadboard. My first approach had a lot of interferences and noises. Finally, I came up with the following list of points that improved the sound quality dramatically.
- Avoid long and crossing wires. I realigned components and reducing cabel.
- The speaker-box of my project was a subwoofer, so the speaker was supposed to play low frequencies. I integrated a second speaker for high frequencies that completes the sound to a nice result.
- Use a USB audio card. The raspberry pi as a very bad audio quality, because the build in digital-analog converter was not designed for HIFI audio applications.
- Connect Pin 2 just to ground of the audio signal. The ground of the 12V and the ground of the USB audio board differs with some noise. The LM386N amplifies the difference of Pin 2 and Pin 3 and therefore the noise was also amplified. I decided not to connect Pin 2 with ground, but just with the USB-audio-ground and finally the noise disappeared.
Step 3: Integrate Speaker for High Frequences
The speaker box I wanted to hack was originally a subwoofer. Because oft that the speaker was very bad for high frequencies. To solve that I added a second speaker from a broken Bluetooth speaker box. Combining the two speakers together in parallel results in good sound for both high and low frequencies.
Step 4: Connect All Components
I decided to power the amplifier with 12 Volts. The box already had a power switch so i reused it. The Raspberry Pi itself needs 5 Volts and 700-1000mA and i connect a USB WI-FI stick and a USB sound card. The challenge now was to come down to 5v out of 12v. My first try was to use the L7805, that is a 5v Regulator. Here is a very good description of the Regulator: https://www.instructables.com/id/5v-Regulator/ . However the performance of linear regulators is very bad. Regulating from 12v down to 5v burns (12v – 5v) * 1000mA = 7 Watt in just one component. That would be a massive waste of energy.
Finally, I decided to use a DC-DC converter. On the DaoRier LM2596 LM2596S I adjusted the board to create 5v. The converter does a great job and I didn‘t recognize any heat creation on that board.
A status LED should indicate the status of the Raspberry Pi. The speaker box already had an LED, so I reused that one. The LED needs 1.7v and 20mA. So a resistor has to burn 3.3-1.7v at 20mA:
R = U / I = (3.3v - 1.7v) / 20mA = 80Ω
I connected the LED to the Raspberry Pi GPIOs. Ground to Pin 9 and the positive supply to Pin 11 (GPIO 17). This allows the Pi to indicate the status (Power, WI-FI, Playing) by different blinking modes.
Step 5: Setup the Raspberry Pi
The Raspbian Buster Lite OS is totally sufficient. I connected the Pi to a monitor and keyboard to configure it. The raspi-config command allows you easily to configure the WI-FI credentials.
A simple startup script should play a startup sound. A python script should check the internet connection. If the Pi has internet access the status LED should be on, otherwise the LED should blink. Therefore, I created a bash script in init.d
sudo nano /etc/init.d/troubadix.sh
With the following content
#!/bin/bash ### BEGIN INIT INFO # Provides: startsound # Required-Start: $local_fs $network $remote_fs # Required-Stop: $local_fs $network $remote_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: play start sound # Description: Play start sound ### END INIT INFO # Start internet access watchdog python /home/pi/access_status.py & # Play start sound mpg123 /home/pi/startup.mp3 &> /home/pi/mpg123.log
Make the script executable
sudo chmod +x /etc/init.d/troubadix.sh
To execute the script at startup I registered the script the following command
sudo update-rc.d troubadix.sh defaults
Put the attached python watchdog in the home directory /home/pi/access_status.py The python-script has to loops. The first loop checks the internet connection by pinging www.google.com every 2 seconds. The second loop lets the GPIO Pin 17 blinking, depending on the current internet status.
The installation of the Spotify connect service is very easy. Here is a repository that hosts an installation script: https://github.com/dtcooper/raspotify So finally the installation is just one single command.
curl -sL https://dtcooper.github.io/raspotify/install.sh | sh
Step 6: Conclusion
During the project I learned a lot. Using a 5v Regulator instead of the DC-DC converter in an early prototype was a bad idea. But that mistake made me think about what the Regulator really does. The improvements of the audio quality were also a huge learning process. There is a reason why professional audio amplification is like rocket science :-)