-
1Set up Raspberry Pi for use (virtual access recommended)
If your SD card does not come pre-loaded with Raspbian or Raspberry Pi OS, put it on your SD card using Raspberry Pi Imager
Insert SD card into Raspberry Pi SD card slot, and connect mouse, keyboard, and monitor. Then plug in the power cable and follow the prompts on the screen.
Once you are on the desktop, connect to your wifi and make note of the IP address which can be found by mousing over the wifi icon. Also go into the configuration setting and enable VNC connection. Now, you can access the PI through the software VNC Viewer on the computer of your choice, as long as both the computer and the Pi are on the same wifi network.
For more detail and step by step instructions see the Youtube video linked here:
-
2Set up the wiring for the lights
- Get your lights. They should come with the lights and a separate JST connector. We won’t be using the JST connector that comes with the lights. The wires are braided and are not ideal for a breadboard.
- Connect three MM jumper wires directly into the 3-pin connector on one side of the lights. The middle wire is data, and the wire on the two sides are power and ground (figure out which is which by looking at the nearest LED, or referring to the separate JST connector, where red is power)
- Add your batteries to the battery pack. Keep the battery pack switched off for now. (The red wire is power, black wire is ground.)
- Wire up the breadboard according to the diagram below. This is from the official Adafruit wiring guide, and you can see the “Raspberry Pi Wiring with Level Shifting Chip” section for more details.
For a step by step walkthrough, see the video here starting at 6:15:
-
3Install required software libraries and project code
- Clone the github repo with all the code for this project at https://github.com/keeratS/WECEtechmusiclights.
git clone https://github.com/keeratS/WECEtechmusiclights.git
Execute the above command in a terminal while in the intended destination folder (where you want the code for this project to be located).
- Verify that you have python 3 installed by running the following commands in a terminal. You should see that you have some version 3.6-3.8.
python3 exit()
- Again in a terminal, make sure that pip is updated (works for python 3.6 and 3.7, be sure to replace “python3.6” with the correct python version)
curl -O https://bootstrap.pypa.io/get-pip.py sudo python3.6 get-pip.py
- We will follow these instructions for the rest of set up. The essential parts of the process have been highlighted below, but if something doesn’t work as expected, we recommend checking the link for additional context and recommendations.
- Run the following commands to install the Adafruit_Blinka library that provides the CircuitPython support in python
sudo pip3 install rpi_ws281x adafruit-circuitpython-neopixel sudo python3 -m pip install --force-reinstall adafruit-blinka
- If you have completed the hardware setup steps above, you can now test your lights. Make sure your pi is up and running, and turn on the battery pack.
- Navigate (cd) to the lights_test folder in the WECEtechmusiclights directory. You should see a file called neopixel_rpi_simpletest.py, which is an adapted version of Adafruit's test code. See the page linked in step 4 for the original code.
- Run the test program using the following command. Use sudo to run all project code, as root access is required to access the pi peripherals.
sudo python3 neopixel_rpi_simpletest.py
sudo python3 neopixel_rpi_simpletest.py
- Your lights should start lighting up! Exit the program using Ctrl-C.
- Feel free to explore the other lights programs in the same folder, the weather lights in the weather_lights folder, or code your own animations!
- Clone the github repo with all the code for this project at https://github.com/keeratS/WECEtechmusiclights.
-
4Configure speech recognition
This project uses python's SpeechRecognition library with the CMU Sphynx speech recognition engine for audio command integration.
Dependency Setup
- Run the following commands in a terminal
sudo apt-get install portaudio19-dev sudo apt-get install python3-pyaudio sudo apt-get install python3 python3-all-dev python3-pip build-essential swig git libpulse-dev libasound2-dev sudo pip3 install pocketsphinx sudo pip3 install SeechRecognition
Microphone Setup
- Plug in your usb mouse (before starting up your pi). Make sure your mic shows up when you type lsusb into the command prompt. One easy way to do this is to check if the list of devices is different if your mic is plugged in.
- Run the following command and make a note of what card is associated with your usb microphone.
aplay -l
- Type this into the command prompt to get access to change your RPi config files
sudo nano /usr/share/alsa/alsa.conf
- Scroll until you see these lines
- defaults.ctl.card 0
- Defaults.pcm.card 0
- Change the lines so instead of 0, it says the card number you noted in step a
- Save the file and return to the command line using [CTRL-X], [Y], [ENTER]
- Note: If ever while running the program as a whole you find that the lights are not working after working with audio, restart your pi and repeat from step 2. Sometimes the microphone can change cards depending on what all is plugged in.
Testing
- We can write a quick test script to verify everything is working as expected. Add the following code to a new python file called, for example, speech_recognition_test.py. This code is taken from this tutorial, which you can reference for more details.
import speech_recognition as sr # obtain audio from the microphone r = sr.Recognizer() with sr.Microphone() as source: print("Please wait. Calibrating microphone...") # listen for 5 seconds and create the ambient noise energy level r.adjust_for_ambient_noise(source, duration=5) print("Say something!") audio = r.listen(source) # recognize speech using Sphinx try: print("Sphinx thinks you said '" + r.recognize_sphinx(audio) + "'") except sr.UnknownValueError: print("Sphinx could not understand audio") except sr.RequestError as e: print("Sphinx error; {0}".format(e))
- Run the file. There will be a bunch of text that looks like errors that start with ALSA, you can ignore those.
- Say something, it might take some time, and you might have to get closer to the mic, but it should respond with words. Don’t worry if it's not completely accurate.
- You'll notice that the recognition is not very accurate. Our project uses a custom dictionary of words to increase the speed and accuracy of the speech recognition. To test this, find the line in the above script that has r.recognize_sphinx(audio) and add a parameter called langauge with the path to the 'en-WECE' folder. For example,
r.recognize_sphinx(audio, language=”/home/pi/Documents/wece_lights/WECEtechmusiclights/en-WECE”)
- Run this script like before, saying something that is a combination of words found in en-WECE/pronounciation-dictionary.dict. You should notice a higher accuracy.
- Run the following commands in a terminal
-
5Music lights set up
A script called pitch_react.py is contained in the music_lights directory of the github. This script creates a pitch-based reaction to music. However, there are a few things that need to be set up and tested on your system before it will work.
Testing Music Lights
- Install aubio. From the terminal, run
sudo pip3 install aubio
- From the WECEtechmusiclights directory, run the pitch_react.py program with
sudo python3 music_lights/pitch_react.py
Play some simple music some distance from the mic. The lights should seem to change based on the changing pitch of the music. On your first attempt, the mic most likely won’t be able to pick much up. See 'Adjust Gain Settings' below.
Adjust Gain Settings
The pitch detection won’t work if the input is too quiet. This will allow the program to get input without being right up against the mic.
- Type the below command in the terminal. A program should show up in terminal. If you get the error ‘cannot open mixer: Invalid argument’, see the identically named section below.
- If the usb microphone is not the device on display, press F6 to change devices.
- Use arrow keys to select the mic section and use the up and down arrows to adjust the gain. You will most likely need to increase it.
- Press escape to return to the command line.
‘cannot open mixer: Invalid argument’
- Navigate to your home directory
cd ~
- Create or open the .asoutndrc file
nano .asoundrc
- If the document is empty (i.e. you just created it) copy the following into the document and change the card number (0) to match the card of your usb device.
pcm.!default { type hw card 0 } ctl.!default { type hw card 0 }
- If you already see some text in the document, find the two references to card number and change them to the card of your usb device.
- Install aubio. From the terminal, run
-
6Run the full program!
From the WECEtechmusiclights directory, run the pitch_react.py program with
sudo python3 main.py
Say commands corresponding to any of the available functionality. See the file intent_parser.py to see how we interpret commands.
Say "Stop Lights" at any point to stop the currently running command.
-
7Set up the main program to run "at boot" (ie as soon as the Raspberry Pi turns on)
Crontab is a scheduling feature that lets you run programs at specified times. Follow the set up below to have the main program run every time your Raspberry Pi turns on:
- In the command prompt, type
sudo crontab -e
This creates the file that you can edit to change the behavior of crontab. Since it is a file it must be changed using a text editor, the below instructions will assume you are using vim.
- If it asks for which text editor to use, select vim. Once the file is open, press "i" to enter insert mode and add "@reboot python3 /file/path/to/script/main.py", replacing the file path with the relevant file path for your setup. Press esc key to exit insert mode, and type ":wq" to save and close the file.
Restart the Raspberry Pi to verify that it is working. To stop the script from running on boot, remove this line from the crontab file.
- In the command prompt, type
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.