Project Video
Project Details
This video shows you how to turn an AC light ON with clap or your voice and Turn it off with another clap
or shout. This code is Advanced version where Analog output A0 of the module is used.
Please see other version where DO pin is used to control light.
This video has two Arduino code (sketch)
1-Baisc Code using Digital pin
2-Advanced Code using Analog pin
Project Code
/*
* This is the Arduino code Control AC Bulb with hand Clap or Voice (Advanced: Analog)
This video shows you how to turn an AC light ON with clap or your voice and Turn it off with another clap
or shout. This code is Advanced version where Analog output A0 of the module is used.
Please see other version where DO pin is used to control light.
*
// written by Ahmad S. for Robojax.com on
Feb 17, 2018 at 13:28 in Ajax, Ontario, Canada
* watch video for details https://youtu.be/QH1Lw9BwTJI
* Code is available at http://robojax.com/learn/arduino
*/
int relayPin = 10;// Connected to relay (LED)
int soundValue = 530;// The sound level to trigger ON/OFF
///////////// do NOT change the lines bellow
int val = 0; // light value from pin 2
int lightON = 0;//light status
int heard = 0;//sound heard status
int dVal =0;// digital value for "val"
void setup() {
// SOUND Sensor Advanced Code by Robojax.com 20180217
Serial.begin(9600);
pinMode(relayPin, OUTPUT);// set Pin 10 as output
}
void loop() {
// SOUND Sensor Advanced Code by Robojax.com 20180217
val = analogRead(A0);// read A0 pin value
if(val > soundValue && lightON == LOW){
heard = 1-heard;
dVal =HIGH;
delay(100);
}else{
dVal =LOW;
}
// SOUND Sensor Advanced Code by Robojax.com 20180217
lightON = dVal;
if(heard == HIGH){
Serial.print(val);// print the A0 value
Serial.println(" Light ON");
digitalWrite(relayPin, LOW); // Turn the light ON
}else{
Serial.print(val);// print the A0 value
Serial.println(" Light OFF");
digitalWrite(relayPin, HIGH);// Turn the light OFF
}
// SOUND Sensor Advanced Code by Robojax.com 20180217
delay(100);
}