/* Run my SRD-05VDC-SL-C relay... A contactor that makes a click when it switches (sucks!). 5VDC across the coil (either polarity flips the switch, and the switch is only flipped while voltage is applied). Coil has 70 ohms, so it draws about 70 mA (at 5 V). Practically no voltage or current limit from Common to NC or NO pins. Coil has inductance and requires more than 10 mA from a digital pin, so you don't connect directly to Arduino! View of the bottom (pins pointing up at you)... NO NC 1 2 CO NC is connected to CO only when no voltage is applied across 1 and 2. NO is connected to CO only when voltage is applied across 1 and 2. If controlling AC, put hot on CO for more flexibility. I think NO is usually the one to use (rather than NC). You may optionally use a sound sensor. I used model KY-037 described below. Connect KY-037 to Arduino... DO -> for digital output (ignore this) + -> 5V G -> GND A0 -> A0 for analog output The analog value changes IN BOTH directions when sound is detected. Keeping the analog value around 500 seems to maximize the effect of sound on analog value. "Loosening" the potentiometer screw (turning CCW) makes it less sensitive. That is, the analog value goes UP. Note that the analog value barely changes when sound is detected. Mine is much more sensitive to slightly blowing across the microphone than to very loud noises. It is quite sensitive to clapping! (c) 2018 Bradley Knockel */ int const relayPin = 7; bool soundSensor = true; int const volumeThreshold = 2; // at least 1 unsigned long const onTimeMax = 1; // max minutes to be on before auto turn off int const sensorPin = 0; // set analog pin to be A0 int const waitMin = 100; // min ms to wait after first clap for second clap int const waitMax = 1000; // max ms to wait after first clap for second clap // waitMax also corresponds to the dead time after changing state int val1; int val2; void setup() { pinMode(relayPin,OUTPUT); Serial.begin(9600); if (soundSensor) { delay(waitMax+1); val2 = analogRead(sensorPin); } } unsigned long const minute = 60000; // in ms bool on = false; unsigned long now; // current time unsigned long time1 = 0; // value of millis() at first clap unsigned long timeOn; // value of millis() upon turning on void loop() { if (soundSensor) { // clap twice to turn on or turn off val1 = val2; val2 = analogRead(sensorPin); //Serial.println(val2); now = millis(); if ( abs(val1-val2) > volumeThreshold ) { if (now - time1 >= waitMin && now - time1 <= waitMax) { on = !on; if (on) { digitalWrite(relayPin,HIGH); timeOn = now; } else { digitalWrite(relayPin,LOW); } delay(waitMax+1); val2 = analogRead(sensorPin); } else if (now - time1 > waitMax) { time1 = now; } else { // maybe put some code here to prevent continuous sound // from triggering the code } } if (on && now - timeOn > onTimeMax*minute) { // auto turn off on = false; digitalWrite(relayPin,LOW); delay(waitMax+1); val2 = analogRead(sensorPin); } // after about a month and a half, unsigned long runs out of bits if (now > 4000000000) {soundSensor = false;} } else { // turn on and off forever digitalWrite(relayPin,HIGH); // on delay(minute/6); digitalWrite(relayPin,LOW); // off delay(minute/6); } }