Overview:
This the 4th tutorial in Controlling the LEDs series. In order to make this tutorial more familiar, the set-up of this project is similar to the third tutorial in this series about controlling multiple LEDs. But for this one, an array of LED shall be controlled using the sound module.
This fun activity is very simple to do and will only take a few minutes to set up!
Hardware:
You can buy all this Hardware at Createlabz.
Software:
Application Discussion
When using a recorder from a mobile phone or playing music, an audio spectrum is usually noticed. This audio spectrum is often visualized as square bars.
The bars vary from the emitted sound. This tutorial will do the same thing. However, instead of having visible square bars as an output, the LED will be utilized to mimic the sound it picks up from the input.
The key component in this project will be the sound module.
Sound Module
The microphone sound sensor is a sound level detector module with an electric condenser microphone and a built-in potentiometer to adjust its sensitivity. This project makes use of the available sound sensor in the Upgraded Arduino Kit. Thus, this tutorial will be using the KY038 Microphone Module.
How it works:
Pin No. |
Pin Name |
1 |
Analog Out |
2 |
GND |
3 |
+V |
4 |
Digital Out |
It can be noticed that the sound module has two outputs, pins 1 and 4. The Analog Out uses the microphone signal as the voltage value. Thus, this output is taken real time. Meanwhile, the Digital Out pin of the module is connected to the LM393 comparator. When the sound intensity exceeds a certain value, the output goes high and it will send a signal via digital out.
The blue block on the module is a potentiometer. This potentiometer can control the sensitivity of the module. In order to reduce its sensitivity, the potentiometer is turned counter-clockwise. However, turning the potentiometer clockwise will increase it.
Direction |
Function |
Clockwise |
Increase Sensitivity |
Counter-clockwise |
Reduce Sensitivity |
Set-up the hardware
Set-up the software
Open the Arduino IDE and upload the code on the Arduino.
The whole compilation of the Arduino code can be found in Github here.
The code below shows how to check the serial monitor for maximum and minimum threshold.
int ledblu1= 13;
int ledblu2= 12;
int ledgrn3= 11;
int ledgrn4= 10;
int ledred5= 9;
int ledred6= 8;
int soundMod= A0;
int soundOut = 0;
void setup(){
pinMode(ledblu1, OUTPUT);
pinMode(ledblu2, OUTPUT);
pinMode(ledgrn3, OUTPUT);
pinMode(ledgrn4, OUTPUT);
pinMode(ledred5, OUTPUT);
pinMode(ledred6, OUTPUT);
pinMode(soundMod, INPUT);
Serial.begin (9600);
}
void loop (){
soundOut =analogRead(soundMod);
Serial.println (soundOut); //This is done to know the min and max threshold for the sound sensor
}
Code Breakdown
- int var
– Creates a variable integer.
- pinMode(pin,mode)
– Configures the identified pin to behave as an input or an output.
- analogRead
– This can range from 0-1023 depending on the input used.
-
Serial.begin(9600)
– tells the Arduino to get ready to exchange messages with the Serial Monitor at a data rate of 9600 bits per second -
Serial.println
– prints data to serial port
Obtaining the minimum and maximum threshold
Once the code is uploaded, open the serial Monitor by clicking the button on the upper right.
The serial monitor will start printing the values taken from the sound module.
From this, take note of the lowest value when the room is silent and the highest value when the loudest sound is played.
Note: The sound module output may vary. Upon testing, I obtained 420 as the lowest value for a silent room and 525 for the loudest sound. After distributing the values per LED, I also adjusted the sensitivity of the sensor through its potentiometer.
Distributing max and min threshold to the LED
The code below is the portion for controlling the LED.
// when the sensor detects a signal, LED flashes
// Once min and max threshold have been obtained, divide the values per LED
//Minimum Threshold
if (soundOut >= 495) {
digitalWrite(ledblu1, HIGH);
}
else {
digitalWrite(ledblu1, LOW);
}
if (soundOut >= 500) {
digitalWrite(ledblu2, HIGH);
}
else {
digitalWrite(ledblu2, LOW);
}
if (soundOut >= 510) {
digitalWrite(ledgrn3, HIGH);
}
else {
digitalWrite(ledgrn3, LOW);
}
if (soundOut >= 515) {
digitalWrite(ledgrn4, HIGH);
}
else {
digitalWrite(ledgrn4, LOW);
}
if (soundOut >= 520) {
digitalWrite(ledred5, HIGH);
}
else {
digitalWrite(ledred5, LOW);
}
//Max Threshold
if (soundOut >= 525) {
digitalWrite(ledred6, HIGH);
}
else {
digitalWrite(ledred6, LOW);
}
This can still be adjusted, another option for adjusting is by turning the potentiometer found on the sound module.
Output
Conclusion
The music reactive LED is a fun and easy project to do. We used a set-up similar to part 3 of the controlling led series in hopes to give a sense of familiarity. This project also tackled the basic functionality of the sound sensor. The use of serial monitor to obtain minimum and maximum threshold was also observed.
Checkout other tutorials in the Controlling an LED series:
Adjusting LED Brightness using a Potentiometer
Adjusting LED brightness using a Photoresistor
Turning on/off multiple LEDs and buzzer
Reference
https://community.fxhome.com/discussion/49382/square-bars-on-audio-spectrum
http://arduinolearning.com/code/ky038-microphone-module-and-arduino-example.php
https://www.instructables.com/Arduino-Sound-Sensor-with-LED/
https://sensorkit.en.joy-it.net/index.php?title=KY-038_Microphone_sound_sensor_module