With the month of October almost here, I have kicked these Halloween projects into high-gear. I previously said that update number four would be all about lighting, but since I just finished the audio portion of Project Trick or Trivia, I thought this would be a good time to tackle the audio portion of Foginator2000. Since Trick or Trivia will be playing triggered audio events based on the buttons, I did not want to muddy the dynamic sound-stage with too much audio. So Foginator 2000 will only be playing ambient audio. I may go back and add in a single triggered event with some sort of greeting that would play either when the fog triggers, or right after it finishes. In this update, I am going to show you how I managed to get the audio portion of this project up and running.
If you follow my Trick or Trivia project, you will find that much of this installment is the same. This is because the audio needs for both projects are quite similar, and I did not feel the need to reinvent the wheel for this update. With that said, I had originally planned on running the background / ambient audio from within the same python script that the main program was in, but I slowly realized that this was not needed. After several hours of experimentation with the audio on project Trick or Trivia, I decided that the best route to take for always-on, ambient audio was to create a separate python script that would play the ambient audio loop when the Raspberry PiRaspberry Pi booted up.
The Hardware
Below you will see a list of the hardware used to build out the audio portion of this project. In addition to these components, you will need the following tools: a soldering iron, solder, flush cutters, wire strippers, 3-10 feet of 2-pair cable, and a 6-inch or longer 3.5mm to 3.5mm audio extension cable.
Newark.com
Newark Part No. | Notes | Qty | Manufacturer / Description |
38Y646738Y6467 | RPi | 1 | RASPBERRY PI 2, MODEL B, |
38Y647038Y6470 | SD Card | 1 | RASPBERRY PI 8GB NOOBS MICRO SD CARD |
44W493244W4932 | PSU | 1 | POWER SUPPLY 5V, 1A |
06W104906W1049 | USB Cable | 1 | USB A PLUG TO MICRO USB B PLUG |
53W628553W6285 | WiFi Dongle | 1 | ADAFRUIT USB WIFI MODULE |
40P118440P1184 | Speaker | 1 | VISATON SPEAKER, 20 kHz, 8OHM, 4W |
49Y171249Y1712 | 7-Inch Touch Screen | 1 | Raspberry Pi 7" Touch Screen Display |
MCM Electronics
MCM Part No. | Notes | Qty | Manufacturer / Description |
Audio Amp | 1 | Audio Amplifier Kit 2 X 5W RMS |
Building the Velman 2x5W Amplifier
One of the major things that I have learned from being in the Haunted Attraction industry is that lighting and sound are two of the biggest “make it or break it” features of a successful prop. When I was putting together the kit for this project, I knew I wanted audio to be a big part of the project. The Raspberry Pi makes it quite easy to add audio to a project, but unfortunately, unless your project makes use of earbuds, you will need to add an amplifier to the project to drive more powerful speakers.
For this project, I chose the Velleman 2x5W Amplifier kit from MCM Electronics. This kit is designed for even the most novice maker to be able to assemble, and it’s quite powerful for its small size. I also chose to use a single small three-inch, eight-Ohm speaker from Visatoneight-Ohm speaker from Visaton. This speakers is a little undersized for this project and this amp, but it works just fine as long as you do not max out the amp’s volume control.
The kit is very straight forward, does not include any confusing, hard-to-identify parts, nor does it utilize any SMD parts that would make it hard to solder. The toughest part to solder in the whole kit is the power indicator LED, as you need to bend it at a very specific point if you want to follow the build instructions 100% word-for-word. I built this entire board in less than 10 minutes.
I sort of went off script and soldered up several of the amp’s components at once. If you follow the directions, you will solder each type of component step by step. This was way to slow for me, and I have hand soldered so many SMD boards in the past few months, that I can solder a through-hole board like this with my eyes closed.
I finished up the board with a second round of soldering. This time I soldered the IC, and other large / heavy components. When soldering terminal blocks, ICs, and other components that are hard to keep in place, or that have several leads, I like to solder one of the leads on an end of the component first. This lets me lock the component in place, then I can use my fingers to re-align the part while re-heating that single solder joint.
It’s hard to see in this photo, but I set the potentiometer all the way to the left, then placed the knob on it with the indicator dot down in the bottom left corner. This will place the dot almost perfectly opposite this position when the volume is turned to max.
The one thing I always say about soldering is that flux is your friend. Velleman must know this as well because they coated the entire bottom of the PCB in a very sticky resin-based flux. I still used my flux pen on a per-joint basis as I like flux on the component leads I am soldering as well.
Wiring up the speaker is pretty straight forward as Visaton was kind enough to mark the leads with + and - symbols to identify its leads. For those wondering, the + lead is almost always the larger of the two leads. Rumor has it, that this was adopted as common practice first in the automotive industry back in the 1970s. You will note that I used some spare two-conductor, shielded microphone wire. You can use any two-conductor wire you have, just pay attention to the polarity. The speaker will work even if it’s reversed, but the best sound quality comes from a properly wired speaker.
Connect the other end of the speaker wire to the amp while paying attention to the polarity. You can also connect the power cable to the screw terminals to the left at this point. The amp requires a 6-14v 1A DC power source. You can power this with an old 9v or 12v wall-adapter, or even a 9-volt battery, but the battery will struggle to output enough current to keep the amp at full capacty.
The Ambient Audio Code
To start off let's quickly cover the background / ambient audio working and how I set it up to begin when the Raspberry Pi Boots. Below is the Python script that I wrote to play the mp3 file I selected as the ambient source. I have broken out each section, and commented on what it does. You can download this code used in this tutorial at the Github repository for this project. The audio files are available for download from here. If you do not want to modify the code, create a folder in the Desktop directory called “audio” and move all three of the mp3 files into it.
To get started we need to import the pygame library. I know a lot of you would have liked to see me use OMXplayer, but there were some things I could not get to work as they should, and I just chose to use something I was familiar with instead.
import pygame
Next we need to define the path to the ambient.mp3 file, and give it a name.
audio_path = '/home/pi/Desktop/audio/ambient.mp3'
Now we need to set a variable to True
var = True
Now we need to write a while-loop to play our mp3 file, and set it to only play if var is equal to True.
while var ==True:
Now we need to initialize PyGame.
pygame.mixer.init()
Then we need to load the MP3 file we want to play.
pygame.mixer.music.load(audio_path)
Now we need to set the pygame player’s volume. The range is between 0.0 and 1.0 so a setting of 0.5 would be half way.
pygame.mixer.music.set_volume(1.0)
Finally we need to tell pygame to play the MP3 file, and set it to loop five times.
pygame.mixer.music.play(5)
The full code is pasted below. Alternatively you can download this code used in this tutorial at the Github repository for this project. The audio files are available for download from here. If you do not want to modify the code, create a folder in the Desktop directory called “audio” and move all three of the mp3 files into it.
import pygame audio_path = '/home/pi/Desktop/audio/ambient.mp3' var = True while var ==True: pygame.mixer.init() pygame.mixer.music.load(audio_path) pygame.mixer.music.set_volume(1.0) pygame.mixer.music.play(5)
Navigate to the project files folder adn then open a new file called ambient.py using the Nano text editor by entering the following command
sudo nano ambient.py
Then copy and paste the code above into the file. Save and exit, and then use the following command to test the pi.
sudo python ambient.py
You should hear the ambient.mp3 file begin to play if you have the amplifier / speaker combo we just built hooked up via a 3.5mm to 3.5mm audio cable from the amp to the Raspberry Pi. To get this python script to run on boot, we need to add it to the Raspberry Pi’s crontab. Enter the following command in the terminal to create a new crontab entry.
sudo crontab -e
Now paste the following line at the bottom of the crontab.
@reboot sudo python /home/pi/Desktop/Foginator2000/ambient.py
then save and exit out of the file. Reboot the Raspberry Pi using the command below. When the Pi reboots, you should hear the ambient.py file playing after you login.
sudo reboot
If the audio is quite low despite the amplifier’s volume being maxed out, you will need to turn the Raspberry Pi’s volume up. This is as simple as entering the small command found below, into the terminal.
amixer cset numid=1 -- 400
The range of amixer’s volume is -10200 and +400 in centi-dB units. Since we are using an external amplifier, we can set the Raspberry Pi’s volume to its max setting at +400, and adjust the volume on the amp accordingly. Once you have the volume set, you should be able to reboot the Pi, and the ambient audio will begin playing when you log in. I did not shoot a video of this for this installment, but if you check out the video below from my Trick or Trivia project, you will get the idea of whats going on with the ambient audio.
Well that is going to wrap up this weeks installment of the Foginator2000 project. Check back in a few days for the next update where I cover how to get Individually Addressable RGB LEDsIndividually Addressable RGB LEDs working with the Raspberry Pi, and how they will be incorporated into this project! If you have not checked it out yet, head over to my other Halloween 2015 Raspberry Pi project, Trick or Trivia, that makes use of the new 7-inch Touchscreen LCD from Raspberry Pi.7-inch Touchscreen LCD from Raspberry Pi.
- Project Introduction
- Fog Controller Hardware and Test
- Environment Sensing Coding & Testing
- Ambient Audio Hardware and Coding
- Lighting Coding and Testing
- October 16th - Final Assembly and Testing
- October 23th - Project Wrap-up