Arduino Uno Clap-Activated Relay Switch with KY-038 Sound Sensor
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to function as a clap switch using an Arduino Uno R3 and a KY 038 sound sensor module. When a clap is detected by the sound sensor, the Arduino toggles the state of a relay module, effectively turning it on or off.
Component List
Arduino Uno R3
- Description: A microcontroller board based on the ATmega328P.
- Pins: D8, D9, D10, D11, D12, D13, GND, AREF, SDA, SCL, D0/RX, D1/Tx, D2, D3, D4, D5, 6, D7, A5/SCL, A4/SDA, A3, A2, A1, A0, Vin, 5V, 3.3V, RESET, IOREF, NONE, USB Jack, Power Jack
KY 038
- Description: A sound sensor module that detects sound levels.
- Pins: +, G, A0, D0
Wiring Details
Arduino Uno R3
- D2: Connected to KY 038 D0
- GND: Connected to KY 038 G
- 5V: Connected to KY 038 +
KY 038
- D0: Connected to Arduino Uno R3 D2
- G: Connected to Arduino Uno R3 GND
- +: Connected to Arduino Uno R3 5V
Code Documentation
Arduino Uno R3 Code
// Pin definitions
const int soundSensorPin = 2; // Pin connected to the sound sensor output
const int relayPin = 3; // Pin connected to the relay module
bool relayState = false; // Tracks the relay state (ON/OFF)
unsigned long lastClapTime = 0;
const unsigned long debounceDelay = 300; // Debounce delay in milliseconds
void setup() {
pinMode(soundSensorPin, INPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Initialize relay to OFF
Serial.begin(9600);
Serial.println("Clap Switch is ready!");
}
void loop() {
// Read the sound sensor state
int sensorValue = digitalRead(soundSensorPin);
// If a clap (sound detected) is registered
if (sensorValue == HIGH) {
unsigned long currentTime = millis();
// Check debounce delay to avoid false triggering
if (currentTime - lastClapTime > debounceDelay) {
relayState = !relayState; // Toggle relay state
digitalWrite(relayPin, relayState); // Update relay state
Serial.print("Relay State: ");
Serial.println(relayState ? "ON" : "OFF");
lastClapTime = currentTime; // Update last clap time
}
}
}
This code initializes the sound sensor and relay module, and continuously checks for sound detection. When a sound (clap) is detected, it toggles the state of the relay, turning it on or off. The debounce delay ensures that the relay is not falsely triggered by noise or multiple claps in quick succession.