How to Control RGB LEDs With Sound Input Using an Arduino
by Celine_ in Circuits > Arduino
935 Views, 3 Favorites, 0 Comments
How to Control RGB LEDs With Sound Input Using an Arduino
Controlling the brightness of RGB LEDs based on sound input is a fascinating application of electronics that can result in stunning light displays that respond to audio input. In this tutorial, we will guide you through the process of building a circuit that takes audio input and translates it into dynamic light patterns. By the end of this tutorial, you will have an exciting and interactive project that responds to the beat of your favorite music or even the volume of your voice! Whether you are a hobbyist, a student, or a professional, this tutorial will provide you with the necessary knowledge to create a fascinating project that combines electronics and sound. So, let's get started!
Supplies
Arduino Nano ≈ $10 / unit
https://www.amazon.com/Deegoo-ATmega328P-Microcontroller-Board-Arduino/dp/B07R9VWD39/ref=sr_1_5?crid=1YLV2AHFD2D4I&keywords=arduino+nano&qid=1682722206&sprefix=arduino+nano%2Caps%2C350&sr=8-5
Breadboard ≈ $2 / unit
https://www.amazon.com/Breadborad-Solderless-Breadboards-Distribution-Connecting/dp/B082VYXDF1/ref=sr_1_1_sspa?crid=3N1TBD3MQQ5GV&keywords=breadboards&qid=1682722356&sprefix=breadboards%2Caps%2C124&sr=8-1-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUExUVk5Sk5ISUxJV0tWJmVuY3J5cHRlZElkPUEwOTc1MjAwMjhSSVlBRDRHMlU1VCZlbmNyeXB0ZWRBZElkPUEwOTM3NjM1MUxSWVZFMzU0QUdKWiZ3aWRnZXROYW1lPXNwX2F0ZiZhY3Rpb249Y2xpY2tSZWRpcmVjdCZkb05vdExvZ0NsaWNrPXRydWU=
DAOKI Sound Sensor ≈ $1 / unit
ZW5jcnlwdGVkUXVhbGlmaWVyPUEyWDFMNktDTVVaRURDJmVuY3J5cHRlZElkPUExMDM1ODE3MjVMTU4yR0ZBTjdFMiZlbmNyeXB0ZWRBZElkPUEwODE1MDA2MlhWTU5KTEtCRDhESSZ3aWRnZXROYW1lPXNwX2F0ZiZhY3Rpb249Y2xpY2tSZWRpcmVjdCZkb05vdExvZ0NsaWNrPXRydWU
RGB LEDs WS2812B ≈ $5 / ft
https://www.amazon.com/WESIRI-Programmable-Individual-Addressable-Non-Waterproof/dp/B07CBS3CN7/ref=sr_1_7?crid=2EZX4HPFRX7GF&keywords=WS2812B&qid=1682722477&sprefix=ws2812b%2Caps%2C153&sr=8-7&th=1
Jumper Wires ≈ $0.08 / unit
https://www.amazon.com/Elegoo-EL-CP-004-Multicolored-Breadboard-arduino/dp/B01EV70C78/ref=sr_1_1_sspa?crid=EPVDAPAWR544&keywords=jumper%2Bwires%2Bvariety&qid=1682722597&sprefix=jumper%2Bwires%2Bvariety%2Caps%2C126&sr=8-1-spons&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUFBRVBFSUxJOU5RQVUmZW5jcnlwdGVkSWQ9QTA2OTI2MjMxNU5KT0FISzA3RTVaJmVuY3J5cHRlZEFkSWQ9QTA5NDUzMjExRUtQVk9KOTU5MVg5JndpZGdldE5hbWU9c3BfYXRmJmFjdGlvbj1jbGlja1JlZGlyZWN0JmRvTm90TG9nQ2xpY2s9dHJ1ZQ&th=1
5v Power ≈ $7 / unit
https://www.amazon.com/Miady-5000mAh-Portable-Charger-Android/dp/B08T8TDS8S/ref=sr_1_2?crid=25B3DEY5UKM29&keywords=portable+charger+5v&qid=1682723024&s=wireless&sprefix=mini+portable+charger+5v%2Cmobile%2C119&sr=1-2
Wiring
Follow this schematic. Note that you should use a 5v battery, not a 9v. Additionally, the sound sensor in the image is represented by a TMP.
Programming
Copy this code into your Arduino IDE:
//Include libraries here
#include <Adafruit_NeoPixel.h>
//Define values here
#define NUM_LEDS 24 // number of LEDs
#define LED_PIN 7 // LED data pin
#define SOUND_PIN A1 // sound sensor output pin
#define NUM_READINGS 10 // number of readings to average
//LED variable setup
int readingsTotal = 0; // sum of past readings
int readingsIndex = 0; // index of the oldest reading in the sum
int readings[NUM_READINGS]; // array to store past readings
int currentBrightness = 0; // current LED brightness
int newBrightness = 0; // new LED brightness
int fadeAmount = 10; // amount to fade the brightness with each loop iteration
// initialize the NeoPixel strip
Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
//Setup for LEDs
strip.begin();
strip.show(); // Initialize all pixels to 'off'
pinMode(SOUND_PIN, INPUT);
for (int i = 0; i < NUM_READINGS; i++) {
readings[i] = 0; // initialize all readings to zero
}
} // <-- closing bracket for setup()
void loop() {
controlLEDs();
}
void controlLEDs() {
int soundValue = analogRead(SOUND_PIN); // read the sound sensor value
readingsTotal = readingsTotal - readings[readingsIndex] + soundValue; // update the sum of past readings
readings[readingsIndex] = soundValue; // store the new reading in the array
readingsIndex = (readingsIndex + 1) % NUM_READINGS; // update the index to wrap around the array
int average = readingsTotal / NUM_READINGS; // calculate the moving average
newBrightness = map(average, 0, 1023, 0, 255); // map the average value to the LED brightness range
if (newBrightness > currentBrightness) {
currentBrightness = min(currentBrightness + fadeAmount, newBrightness); // smoothly increase the brightness
}
else if (newBrightness < currentBrightness) {
currentBrightness = max(currentBrightness - fadeAmount, newBrightness); // smoothly decrease the brightness
}
for (int i = 0; i < NUM_LEDS; i++) {
if (i < 8) {
// LEDs 0-8 are affected by the sound sensor
strip.setPixelColor(i, strip.Color(0, 0, currentBrightness)); // set the color and brightness of each LED
} else {
// LEDs 9-24 are not affected by the sound sensor and have a fixed brightness of 50
strip.setPixelColor(i, strip.Color(0, 0, 50)); // set the color and brightness of each LED
}
}
strip.show(); // update the LED strip with the new colors and brightness
delay(10); // add a small delay to smooth out the brightness changes
fadeAmount = 20; // set the fade amount to 20 for faster changes
}
Adjust to Fit Your Needs
You can modify this section of code to fit your project!
NUM_LEDS.........................................................Change this number to match the number of LEDs you're using!
LED_PIN...............................................................Change this to match the pins that are available to you!
SOUND_PIN .........................................................Change this to match the pins that are available to you!
NUM_READINGS ................................................Change this to fit the number of readings to average!
Upload Your Code!
Plug the Arduino into your laptop, and make sure it recognizes the port. If you get an error that reads:
Upload error: failed uploading: uploading error: exit status 1
You may have multiple tabs or Arduino IDEs open, close them. Then compile and upload your sketch!
Done!
You're done!
In this tutorial, we have shown you how to create a sound-responsive RGB LED system using an Arduino board, a microphone module, and RGB LED strips. By following the steps outlined in this tutorial, you can create your own dynamic light show that responds to sound input, providing a fun and interactive experience for you and your audience.
Happy building!