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.
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
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.
This article takes a closer look at each of these devices, comparing them across key areas like Python support, ease of use, Python programming tools, pin design, and hardware projects.
Deciding between Scratch and Python for your child's coding journey? This article compares both languages based on learning background, goals, methods, and difficulty.