Audio Reactive LEDs

Published on: July 3, 2020

Welcome to the 16th Raspberry Pi tutorial! This is the 2020 Update of the tutorial I did in December 2018 titled Audio Reactive LED Strip with Raspberry Pi

There has been a lot of interest in that project and over the years, I've dealt with a lot of support requests. There's been a lot of requests to make the install easier. The original tutorial probably has a couple dozen steps you need to follow not counting the hardware work. So I decided to tackle this and make things easier. 

Be sure to get the code and more technical specs of this project on GitHub: https://github.com/naztronaut/dancyPi-audio-reactive-led

Here's a quick demo of what the end product looks like:

The above demo has sound. If it sounds a little weird it's because I chopped it up to fit it into 15 seconds. Watch the video below for the full demo!

Hardware Setup

Let's talk hardware first. Here is what I used for my project (same list as the last project):

  1. Raspberry Pi 3 (it should work even better on the Pi 4 -  you can use a Pi Zero/W but you may see performance issues)
  2. WS2812b LED - can be a strip or in any basic series. You should know the number of LEDs in the series. These lights typically have 3 connections: Power, Ground, and Data
  3. Microphone - I use a basic USB mic in the video
  4. Jumper cables - to connect the Pi to the lights
  5. Power - this can be supplied to the Pi and the Pi supplies power to the lights, or you can use two different power sources for the two components

Here's a quick fritz diagram of what the connection looks like:

By default, the project uses GPIO Pin 18 as the data pin with 5v and GND pins being any of the ones provided by the Pi. Find a quick the wiring below (Note: I'm using a Pi0 as the hardware even though I'm using a Pi 3 for the project - just reusing some graphics from another project!):

Fritz Diagram Raspberry Pi connecting to WS2812b LED Strip
Click image to see full resolution

Before you turn on the Pi, I recommend plugging in your USB Mic and then turn on the Pi. I've noticed that sometimes, the Pi won't recognize a new USB device if it's plugged in after it's turned on. 

Once the hardware is done, we can move onto the Software.

Software Setup

If you don't already have Git set up, install Git then clone the project repo and enter the isntall directory with these commands:

    sudo apt install git -y
sudo git clone https://github.com/naztronaut/dancyPi-audio-reactive-led.git
cd dancyPi-audio-reactive-led/python/install

Once you're in the install/ directory, you'll see a few files. The one we're intersted in is install.py. Running this script should install ALL the dependencies that you'll need for this project. These dependencies include numpy, scipy, pyqtgraph, pyaudio, and the rpi_ws281x library along with a few others. This script will also copy asound.conf and modify alsa.conf with the proper configuration settings. 

So let's run the script with this command:

    sudo python3 install.py

The setup can take anywhere from a few seconds to a few minutes, depending on how many of the dependencies you may already have installed. This script is pretty simple in that it just does a bunch of ssh commands in order for you so that you can run it, walk away, and it'll be done by the time you come back.

You can ignore the setup.py script for now, it's still a work-in-progress and it doesn't quite work the way I want it to at the moment. 

Config.py

Let's take a look at config.py next:

    DEVICE = 'pi'
    LED_PIN = 18
    USE_GUI = False
    DISPLAY_FPS = False
    N_PIXELS = 144
    MIC_RATE = 48000
    FPS = 50

Getting the MIC_RATE right is important. If you run into random unexplainable errors with your mic, check the rate! Although the N_PIXELS variable is not as important, it's still good to get that number correct, because it'll determine where the center is and start the 'dancing' there. 

If your change the number of pixels here, it would be great to change the value of LED_COUNT in off.py as well if you want to use that script to turn the lights off after use. 

Running the Software

If everything installed as expected without errors, you should be able to run the project with this simple command:

    sudo python3 visualization.py scroll

Substitute the scroll argument above with any of the following:

  • spectrum
  • scroll
  • energy

If you have DISPLAY_FPS as True, you should see an FPS value every second or so. If you run into errors, ask in the comments section below or on the GitHub issues forum here: https://github.com/naztronaut/dancyPi-audio-reactive-led/issues

Patreon

I'd like to give a shoutout to my first Patron on Patreon, Jeremy D! I do these projects to learn and teaching is a good way to learn. But if you do want to support me, head on over to Patreon and become a Patron: https://www.patreon.com/nazmus

If you've implemented this project, I'd like to see it! So please share it with me through any of my regular channels. 

Future TODOs

There are a few more things that I want to do in the future. Someone suggested that I make an image of my Pi OS so that it's easy to set up. I've investigated and I'm still working on this, but hopefully the install.py script removes a lot of complexity. 

Another item on my to-do list is to implement this using the ESP8266 module. I have a few lying around so I might as well. I will try to find some time this summer to do so. 

Remember to checkout the Resources section below for associated downloadable content, JSFiddle links, and other resources. Watch the video and follow along!

Resources:



Comments: