In this lesson, we’ll learn how to use a microphone to make our LED react to sound! The louder the sound, the brighter the LED will glow. Get ready to create a light show that dances to music!
Objective: Learn how to use a microphone to detect sound and control the brightness of an LED.
Materials:
- Arduino Uno board
- Breadboard
- LED (any color)
- 220-ohm resistor
- Electret microphone
- Jumper wires
Steps:
- Review: Recap previous lessons and components, including sensors and analog input.
- Meet the Microphone: Introduce the electret microphone and explain how it converts sound waves to electrical signals.
- Building the Circuit: Connect the microphone, LED, resistor, and Arduino board to the breadboard. The microphone should be connected to an analog input pin on the Arduino.
- Programming the Sound Response: Copy and paste the code below
Step 1: Connecting the Microphone
- Find the three legs of the electret microphone: One is positive (+), one is negative (-), and one is for the signal (often marked with a dot or a different color).
- Connect the positive leg of the microphone to a 5V power rail on the breadboard:
- Connect the negative leg of the microphone to a ground rail on the breadboard:
- Connect the signal leg of the microphone to an analog input pin on the Arduino: We’ll use pin A0 in this lesson.
Step 2: Connecting the LED and Resistor (Review)
- Connect the LED and resistor as you did in Lesson 1: Make sure the LED’s longer leg is connected to a power rail and the resistor’s other leg is connected to the same row as the LED’s shorter leg.
Step 3: Connecting the Arduino (Review)
- Connect the LED’s longer leg to digital pin 13 on the Arduino: This is the output pin for the LED.
Step 4: Programming the Arduino
- Open the Arduino IDE.
- Copy and paste the following code:
const int ledPin = 13; // LED connected to pin 13
const int micPin = A0; // Microphone connected to analog pin A0
void setup() {
pinMode(ledPin, OUTPUT); // Set LED pin as output
}
void loop() {
int micValue = analogRead(micPin); // Read microphone value
// Map microphone value to LED brightness (0-255)
int brightness = map(micValue, 0, 1023, 0, 255);
analogWrite(ledPin, brightness); // Control LED brightness
}
content_copyUse code with caution.Arduino
- Upload the code to the Arduino board.
Step 5: Test the Sound Reactive LED
- Make a sound near the microphone: You should see the LED glow brighter as the sound gets louder.
- Try different sounds: Experiment with different noises, music, or even your voice to see how the LED reacts.
Congratulations! You’ve created a sound reactive LED circuit!
Let’s Experiment:
- Adjust the brightness: Change the numbers in the map(micValue, 0, 1023, 0, 255) line to adjust the LED’s brightness range.
- Add a delay: Try adding a delay in the loop() function to make the LED react more slowly to sound changes.
Keep experimenting with sound and have fun with Arduino!