In this illustration we will going to wire the microphone module, relay and the Arduino board. These project is compatible on most common microcontroller, the main objective of this project to make a sound activated switch or a clap switch. The light will turn on if sensor sense a sound then triggering the relay which is connected to the microcontroller.
Required Component
- Arduino Boards (UNO, MEGA, NANO, PRO, LEO, DUE)
- ESP8266 with Arduino IDE (Optional)
- Microphone Sensor
- Relay Module
- Soldering Board
- Jumper Wires / DuPont Wires
- External Device
Wiring Guide
The Sketch Code / Single Clap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/* 14CORE TEST CODE FOR SOUND DETECTION */ int MySound = 4; // Sound Sensor Connected to Pin 4 int MyRelay = 5; // Relay Module in Connected to Pin 5 boolean SensorState = false; void setup() { pinMode(MyRelay, OUTPUT); pinMode(MySound, INPUT); } void loop() { if (digitalRead(MySound) == LOW) { delay(100); SensorState = !SensorState; digitalWrite(MyRelay, SensorState); } } |
The Sketch Code / Dual Clap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
/* 14CORE / Dual Clap Sound Activated Switch Demonstration Code */ int InputSound = 0; int SoundWaveSensor = 12; int RelaySwitch = 6; long DetectSpan = 0; long SoundDetection = 0; boolean lightState = false; void setup() { Serial.begin(9600); pinMode(SoundWaveSensor, INPUT); pinMode(RelaySwitch, OUTPUT); Serial.print("Initializing Sound Sensor") } void loop() { int sensorState = digitalRead(SoundWaveSensor); if (sensorState == 0) { if (InputSound == 0) { SoundDetection = DetectSpan = millis(); InputSound++; } else if (InputSound > 0 && millis()-DetectSpan >= 50) { DetectSpan = millis(); InputSound++; } } if (millis()-SoundDetection >= 400) { if (InputSound == 2) { if (!lightState) { lightState = true; digitalWrite(RelaySwitch, HIGH); Serial.print("Switch is Activated") } else if (lightState) { lightState = false; digitalWrite(RelaySwitch, LOW); Serial.print("Switch is Deactivated") } } InputSound = 0; } } |
Make your own sound activated switch on Arduino
Hi, thank you for this!
I am wondering how to adapt this to be like a momentary switch. For example, when the sound stops, the relay is turned off. Thank you!