Two years ago I produced a video showing the then Sonic Pi 2.7 driving the leds on a sense-hat using hacked lightshowPi software using the then current version. I didn’t write up the software which was quite complex, but merely posted a video of the system operating on youtube. Recently I was asked on twitter if I could share the code, as some students in California wanted to try it out. I managed to located the SD card containing the software, and fired it up on a Raspberry Pi2 using the then prevalent Sonic Pi and operating system and got it working. I then decided to see if I could update it to run on the latest OS on a Pi3 and with a later version of Sonic PI. I have now managed to do this and am publishing the code so that others can try it out.
The system also requires an external USB audio card, to give an audio input that can be used by LightshowPi. I used a sabrent usb module which I obtained on Amazon (UK) which is also available in the states here Three other pieces of hardware are required (apart from a sensehat!). A two way stereo audio splitter lead (3.5mm jack to 2 3.5mm sockets), a stereo 3.5mm jack to jack lead, and a short usb extender lead as the Sabrent audio card is a little fat to plug into the Pi directly.
PLug the sensehat into a Pi3 running Raspbian Stretch, set the default audio to the built in bcm2835 ALSA card using Prefereces=> Audio Device Settings on the Pi main menu. Click Select Controls if you can’t see the Playback slider, and make sure it is active and at maximum, before clicking OK and start Sonic Pi running. Use a “punchy” piece like my sparkling tom-toms for best effect. The audio output from this is fed to a two way splitter plugged into the audio out 3.5mm socket on the Pi. One of these outputs goes to an external audio amp so you can hear the sound, the other is fed to the microphone input on the Sabrent USB audio card. This supplies an audio feed to the modified lightshowpi code. You can see the wiring below. The dual splitter plugged into the 3.5mm audio out socket, with a red lead going to an audio amplifier and speakers (out of the photo) and the white lead fed back into the microphone input to the Sabrent usb audio card which is plugged into the Pi via a short extender lead, as it is rather fat to plug in directly. Other leads are to the hdmi monitor, the power supply and to a usb keyboard and usb mouse (all out of the picture).
Install the current version of lightshowpi using the instructions at http://lightshowpi.org/download-and-install/
Download my modified code for lightshowpi from here and copy the file to the top level of the lightshowpi folder. Right-click the file lsp_sensehat.tar.gz (in the gui) and select extract here, and two folders will be created, py_sensehat and sensehat_config These contain the modified files set up to utilise the sense hat rather than individual leds connected to the gpio pins. Essentially this is based on an earlier version of lightshowpi. The reason for installing the latest version is to install the various background files and utilities used by the lightshowpi files. You can see the contents of the lightshowpi folder with the two additional folders from the lsp_sensehat.tar.gz file below.
Before starting to use the system, you should check that the microphone input sensitivity is setup. to do this select Audio Device Settings from Preferences on the Raspberry Pi main menu. Select the USB Audio Device (Alsa mixer) from the Sound Card list at the top, click Select Controls and select Speaker and Microphone from the list (NOT Microphone Capture). Click Close, and adjust the Microphone slider to maximum, and make sure it is enabled in the box below the slider. You can disable the Speaker output in the left hand box below the two Speaker sliders as we are not using audio output via the USB card. Then close the Audio Device Settings window. NB make sure that bcm2835 ALSA is still marked as default, as shown in the FIRST Audio Device Settings picture above. Below you can see the settings for the USB Audio card.
To start lsp running do the following from a terminal:
cd ~ cd lightshowpi/py_sensehat sudo python synchronized_lights.py
You should see the message Running in audio-in mode, use Ctrl+C to stop on the screen. All being well, if you start Sonic Pi playing then after a pause of a few seonds as audio samples are built up by lightshowpi the sensehat leds should start flashing in response to the music You need quite loud music for it to work.
How does it all work?
I’m not going to go into great detail, as it would take too long, but I can give one or two pointers. Lightshowpi is a system which can take an audio input from an audio input or from a music file eg .wav, .mp3 and analyse it using fast fourier transforms to produce a series of outputs for different ranges in the audio spectrum. These can then be channeled to control LEDs connected to the gpio pins on the raspberry pi. What I have done is to hack the code that channels the outputs to the gpio pins, and instead send them to a python program testled.py within the py_sensehat folder which contains a series of function which can control LEDs on the sense hat. In the program I use columns of LEDs coloured with a rainbow, which can be switched on or off. using turn_on_light(num) and turn_off_light(num). These two commands are used in the hacked hardware_controller.py program in the py_sensehat folder, replacing calls to wiringpi which are commented out. The configuration settings for using the sensehat are contained in the sensehat_config folder. In particular the overrides.cfg file in that folder is worth looking at. As supplied the columns in the sensehat are triggered by different frequency ranges starting from low at one side of the sensehat up to high at the other. In the last line of the overrides.cfg file there is a commented out line which allows a different channel mapping which gives a symmetrical output which you may find preferable. To try it out uncomment the line and restart the synchronized_lights program. Notice also the details of the audio card which has the internal name ‘Device’ which you can see if you type aplay -l in a terminal.
The only update I had to do to my hacked files to work with the latest lightshowpi was to replace a reference to import wiringpi2 as wiringpi with import wiringpi as wiringpi as this module appears to have changed its names between versions, and is installed by the latest lightshowpi.
If I had more time, further work on integrating the sensehat to lightshowpi could be done, so that it would fit into the latest and any subsequent version, but I leave that to any other interested party to take on.
Finally, if you like this project you may also be interested in a project to flash the lights on the PiHat Christmas Tree which I have recently described here