Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Uno Clap-Activated Relay Switch with KY-038 Sound Sensor

View and Edit Project In Cirkit DesignerImage of Arduino Uno Clap-Activated Relay Switch with KY-038 Sound Sensor

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

KY 038

Wiring Details

Arduino Uno R3

KY 038

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.