In this tutorial, you will learn how to turn on and off the light with a sound sensor (KY-038) and an Arduino.
Parts Required
- Arduino
- Sound sensor (KY-038)
- Jumper wires
- LED
Circuit
The sensor output used in this tutorial is the digital output (DO).
Code
int soundSensor=2;
int LED=4;
boolean LEDStatus=false;
void setup() {
pinMode(soundSensor,INPUT);
pinMode(LED,OUTPUT);
}
void loop() {
int SensorData=digitalRead(soundSensor);
if(SensorData==1){
if(LEDStatus==false){
LEDStatus=true;
digitalWrite(LED,HIGH);
}
else{
LEDStatus=false;
digitalWrite(LED,LOW);
}
}
}