0

$USD

$USD
PROJECTS Arduino

Arduino Projects 6: Sound Activated LED

DFRobot Jun 21 2017 1915

In this session we will make a sound activated LED. You will be able to make an LED flash by clapping your hands. To make it work, we need a sound sensor. We can use a sound sensor to make various interactive and sound control devices besides this experiment.


Parts Needed:

DFRduino Uno  (similar as Arduino UNO R3) x1
I/O Sensor Expansion Shield V7.1  x1
Analog Sound Sensor  x1 

Digital Piranha  LED-R  x1 

Connections

Connect the analog sound sensor to analog pin 0
Connect the Digital Piranha LED-R to Digital pin 13
Use the diagram below for reference.

Be sure that your power, ground and signal connections are correct or you risk damaging your components.

When you have connected the components and checked your connections, connect the arduino microcontroller to your PC with the USB cable so that you can upload a program.


Hardware Analysis
(Analog Input – Digital Output)

In the previous sessions we have talked about analog and digital signals. The analog sound sensor is, as the name suggests, an analog sensor.
Analog signals have many values, ranging between 0 - 1023

Digital signals have only two values: 0 and 1

More detailed information can be found in Session 2.


Code Input

// Item Four—Sound Control Light
int soundPin = 0; // Analog sound sensor is to be attached to analog
int ledPin = 13; // Digital Piranha LED-R is to be attached to digital
pinMode(ledPin, OUTPUT);
// Serial.begin(9600); // You can uncomment this for monitoring
}

void setup() {

void loop(){
int soundState = analogRead(soundPin); // Read sound sensor’s value

// Serial.println(soundState); // serial port print the sound sensor’s value
// if the sound sensor’s value is greater than 10, the light will be on for 10 seconds.
//Otherwise, the light will be turned off

if (soundState > 10) {
digitalWrite(ledPin, HIGH);
delay(10000);
}else{
digitalWrite(ledPin, LOW);
}
}


Use this sample code to implement the behavior we want.
You can copy and paste it in to Arduino IDE, but if you want to develop you skills we recommend typing it out.

When you have finished, click “Verify” to check the code for syntax errors. If the code verifies successfully, you can upload it to your DFRduino UNO microcontroller (such as Arduino UNO).

Once the code has uploaded, try clapping your hands. The LED should flash on.
When there is no sound, the LED should remain off.


Code Review

In the setup() function, the LED is declared as an OUTPUT.
Why isn’t the sound sensor declared as an INPUT?
The reason is because all analog pins are in INPUT mode by default, so we don’t need to set them!
analogRead(pin)

The sound sensor is the input unit, and it should read from analog pin 0. The function for this is:

This function is used to read the analog pin. Your microcontroller’s analog pins are attached to an A/D converter (analog to digital
converter - a special chip on the microcontroller board), so voltage between 0 and 5V will be mapped to a number between 0 and
1023. Each number collected represents a voltage. For example, 512 = 2.5V
Finally, an if statement checks if the result exceeds a certain value.
if (soundState > 10) {
...
}else{
... }


You can open the serial monitor and test out a threshold value that best represents the sound volume you need. Then you can adjust the value in the code accordingly.
There is also another arduino starter kit for you to choose.