Controlling an LED (4/5): Music reactive LEDs using Sound Module
Music Reactive LED Using a Sound Module – CreateLabz

Music Reactive LED Using a Sound Module

01 Overview

This is the 4th tutorial in the 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 LEDs 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!

Project Use Case

This project recreates the audio spectrum bars commonly seen when playing music on a phone or a media player. Instead of visible square bars, a row of six LEDs lights up according to the sound intensity picked up by the microphone. It is a simple, beginner-friendly way to turn sound into light.

02 Hardware and Software Components

Gather everything below before you start. This is your checklist β€” names, models, and versions only.

Hardware Components

Component Description
Arduino Uno Γ— 1 The main controller board that reads the sound module and drives the LEDs.
LED (5 mm) Γ— 6 The output display that mimics the audio spectrum bars. Color options: red, yellow, green, blue, white.
220 ohm resistor Γ— 5 Resistors used to limit the current for five of the LEDs.
330 ohm resistor Γ— 1 Resistor used to limit the current for one of the LEDs.
Sound Module (KY038) Γ— 1 Microphone sound sensor with a built-in potentiometer for adjusting its sensitivity.
Breadboard Γ— 1 Holds the components in place for easy wiring and troubleshooting.

You can buy all this hardware at Createlabz.

Software Tools

Software Version / Details
Arduino IDE The official Arduino development environment used to write and upload the code. Download the latest version from arduino.cc.

Project Files

All required files are available in the project repository. Download the repository before proceeding to the Software Setup section.

Repository: github.com/createlabz/createlabz-public β€” arduino-upgradedStarterKit/SoundModuleLED

File Description
SoundModuleLED (folder) The complete Arduino code for the music reactive LED project β€” threshold checking and LED control.

03 Application Discussion

Here is what each component does and why it is part of this project.

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 according to the emitted sound. This tutorial will do the same thing. However, instead of having visible square bars as an output, the LEDs will be utilized to mimic the sound it picks up from the input. The key component in this project is the sound module.

Audio spectrum visualized as square bars

Sound Module (KY038)

Sound detector module photo

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 sound sensor available in the Upgraded Arduino Kit. Thus, this tutorial will be using the KY038 Microphone Module.

KY038 Microphone Module diagram

How It Works

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, so this output is taken in 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.

Pin No. Pin Name
1 Analog Out
2 GND
3 +V
4 Digital Out

Sensitivity (Potentiometer)

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

LED Array Γ— 6

Six LEDs (blue, green, and red) act as the output of the project. Each LED is assigned a threshold value, so as the detected sound gets louder, more LEDs light up β€” mimicking an audio spectrum.

04 Hardware Setup

Wire the components to the board using the tables below.

Wiring Diagram

Sound sensor and LED array wiring diagram

Sound Module to Arduino

Pin / Signal Connects To
Analog Out (pin 1) A0
GND (pin 2) GND
+V (pin 3) 5V
Digital Out (pin 4) Not used in this project

LED Array to Arduino

LED Board Pin Description
LED 1 (Blue) 13 Positive leg to pin; other leg to GND through a resistor
LED 2 (Blue) 12 Positive leg to pin; other leg to GND through a resistor
LED 3 (Green) 11 Positive leg to pin; other leg to GND through a resistor
LED 4 (Green) 10 Positive leg to pin; other leg to GND through a resistor
LED 5 (Red) 9 Positive leg to pin; other leg to GND through a resistor
LED 6 (Red) 8 Positive leg to pin; other leg to GND through a resistor
Note: Five LEDs use the 220 ohm resistor and one LED uses the 330 ohm resistor. Each resistor is connected in series between the LED and GND.

Assembly Instructions

  1. Place all the components on the breadboard.
  2. Connect the sound module β€” Analog Out to A0, GND to GND, and +V to 5V.
  3. Connect the positive leg of each LED to its assigned digital pin (13, 12, 11, 10, 9, 8).
  4. Connect the other leg of each LED to GND through a resistor (220 ohm for the first five LEDs and 330 ohm for the sixth).
  5. Adjust the potentiometer on the sound module as needed while testing.
Note: Keep the wiring tidy on the breadboard so each connection is easy to check if an LED does not light up.

05 Software Setup

Follow these steps in order. Do not skip any step.

Project Repository:github.com/createlabz/createlabz-public β€” arduino-upgradedStarterKit/SoundModuleLED
Download this first before proceeding.

Step 1 β€” Install the Arduino IDE

  1. Download and install the latest version of the Arduino IDE from arduino.cc.
  2. Accept the default settings during installation.

Step 2 β€” Board Settings

Use exactly these settings in your IDE. Wrong settings will cause upload failures.

Setting Value
Board Arduino Uno
Port Select the COM port shown under Tools > Port

Step 3 β€” Upload the Code

  1. Download the project repository from the Project Repository note above.
  2. Open the SoundModuleLED sketch (.ino) in the Arduino IDE.
  3. Select the correct board and port.
  4. Click Upload.

06 Code

Copy each file below into the correct location as described in the Software Setup section. Read the Code Breakdown section to understand what each part does.

soundModuleLED.ino β€” Threshold Check

The code below shows how to check the Serial Monitor for the maximum and minimum threshold.

Arduino / C++
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
}

soundModuleLED.ino β€” LED Control

The code below is the portion for controlling the LEDs.

Arduino / C++
// 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);
}

07 Code Breakdown

Here is what each part of the code does. Read this after uploading.

Key Functions

Function Purpose
int variable Creates a variable integer.
pinMode(pin, mode) Configures the identified pin to behave as an input or an output.
analogRead(pin) Reads an analog input. This can range from 0 to 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 the serial port.

General Program Workflow

  1. The program reads the sound level from the sound module using analogRead().
  2. The value is printed to the Serial Monitor so the minimum and maximum thresholds can be observed.
  3. Each LED turns on when the sound level reaches its assigned threshold.
  4. Each LED turns off when the sound level drops below its assigned threshold.
  5. The loop repeats, producing the audio-spectrum bar effect.

08 Testing and Calibration

After uploading, verify each of the following to confirm the system is working correctly.

Obtaining the Minimum and Maximum Threshold

Once the code is uploaded, open the Serial Monitor by clicking the button on the upper right of the Arduino IDE.

Opening the Serial Monitor

The Serial Monitor will start printing the values taken from the sound module.

Serial Monitor output values

From this, take note of the lowest value when the room is silent and the highest value when the loudest sound is played.

Common Issue: The sound module output may vary. Upon testing, the lowest value for a silent room was 420 and the highest value for the loudest sound was 525. After distributing the values per LED, adjust the sensitivity of the sensor through its potentiometer.

Distributing Thresholds per LED

Once the minimum and maximum thresholds are obtained, divide the values per LED. In the sample code, the thresholds used are 495, 500, 510, 515, 520, and 525.

This can still be adjusted. Another option for adjusting is by turning the potentiometer found on the sound module.

09 System Demonstration

The image and video below show the working system. Use these to verify your output matches what is expected.

System Setup

Complete system setup with the sound module and LED array

Video Demonstration

10 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 the LEDs series in hopes of giving a sense of familiarity. This project also tackled the basic functionality of the sound sensor, and the use of the Serial Monitor to obtain the minimum and maximum thresholds was also observed.

Other Tutorials in This Series

Possible Improvements and Future Enhancements

  • Split the sound into frequency bands (for example, using an FFT library) to display a true audio spectrum.
  • Use an LED strip or RGB LEDs to create smoother color transitions.
  • Add a potentiometer input so the sensitivity can be adjusted without editing the code.
  • Combine the digital output of the sound module with the analog reading for more precise triggering.

11 References

The following references were used while building this project.

  • Square Bars on Audio Spectrum
    https://community.fxhome.com/discussion/49382/square-bars-on-audio-spectrum
  • KY038 Microphone Module and Arduino Example
    http://arduinolearning.com/code/ky038-microphone-module-and-arduino-example.php
  • Arduino Sound Sensor with LED
    https://www.instructables.com/Arduino-Sound-Sensor-with-LED/
  • KY-038 Microphone Sound Sensor Module
    https://sensorkit.en.joy-it.net/index.php?title=KY-038_Microphone_sound_sensor_module

12 Project Authors

  • Nolf Ivan Labios
    Quality Checked by:
  • Elijah Joseph Orias
  • Lukie-Mar Lonzaga
  • Miles Darren Bagnol
Music Reactive LED Using a Sound Module – CreateLabz
Arduino, Arduino kit, Arduino starter kit, Arduino uno, Color led, Led, Multiple leds, Sound sensor

Leave a comment

All comments are moderated before being published