The sound sensor – large.
The detector with a potentiometer, changing the state when exceeding a certain level.
In this Step we amend the previous circuit and its sketch to include a sound sensor, which is ‘read’ to turn the lamp ‘on’ when a loud sound is heard. The loud sound is my voice. Although I say “Lamp On” loudly, it is the first “Lamp” that actually turns the lamp ‘on’. The second word On was not really required. I could have said “On” by itself, and this would also have turned the lamp ‘on’. A single loud sound is all that was/is required.
Here we ‘read’ the analog output of a sound sensor, and use it in addition to the relay already connected to the UNO R3. A digital sensor could also be used, but its sensitivity would also need to be calibrated, as does the analog sensor we use output from here.
The analog output (AO) is used in combination with the appropriate sketch to turn the lamp ‘on’ when a loud sound is heard. The sensor, and its associated sketch, containing an ‘if’ statement, evaluates the sensor’s analog input line, and when the sketch finds a value greater than the value it is looking for, it sends a signal to the relay’s input line to turn the lamp ‘on’.
// Program by R. J. Kreindler, Dec 2017
// to turn lamp on with loud sound
// Identify analog sensor pin
#define ANALOGPIN A1
// Identify relay switch pin
#define RELAYSWITCHPIN 3
// Set the time for lamp ‘on’
int delay1 = 5000;
void setup() {
// Set relay input pin for output
pinMode(RELAYSWITCHPIN, OUTPUT);
// Set sound sensor analog pin for input
pinMode(ANALOGPIN, INPUT);
} // End setup
void loop() {
int soundVol = analogRead(ANALOGPIN);
if(soundVol > 40) {
// Write a LOW to the relay switch pin
// that is, turn the relay on
digitalWrite(RELAYSWITCHPIN, LOW);
delay(delay1);
} // End if
else {
// Write a HIGH to the relay switch pin
// that is, turn the relay off
digitalWrite(RELAYSWITCHPIN, HIGH);
} // End else
// Turn relay off at the end of loop
digitalWrite(RELAYSWITCHPIN, HIGH);
} // End loop
Please note that the sound sensor has a sensitivity potentiometer; the blue component in the attached pictures. A closeup of that component is provided in the pictures. Thus, the value I use to determine a ‘loud’ sound, 40, is likely different from the value you will find and use. The value you will make a comparison against will depend on your sensitivity adjustment.
You may also chose to use the DO, digital output, rather than the AO, analog output.
In this example, I used a sound sensor. Alternatively you may want to use a passive infrared sensor (PIR), or a light sensor, water sensor, etc. You can “read” the output of whatever sensor you select to activate the relay and its attached device. The attached device, obviously, does not have to be a fan or lamp, e.g., it could be an alarm, radio, coffee maker, toaster, motor, television, etc.