Blogs

Clap-Controlled Light Switch Using a KY-037 Sound Sensor + Raspberry Pi Pico (Real AC Power)

Build a fast, satisfying “clap switch” that toggles a real desk lamp using the KY-037 sound sensor’s digital output, a Raspberry Pi Pico, and a safe enclosed AC relay box. No Wi-Fi. No cloud. Just sound → power.

Quick Summary

The KY-037 has a digital output (DO) that goes HIGH when sound crosses a threshold you set with the onboard potentiometer. The Pico reads that DO pin and toggles a GPIO output to control an enclosed AC relay that switches your lamp on/off.

Table of Contents

Parts List

From ShillehTek

External (you can link these however you prefer)

Tip: If you want this to feel “product-like,” the enclosed relay box is the move. It keeps mains wiring out of your hands.

Step 1: Wiring

We only need the KY-037’s digital output (DO) and a single GPIO output from the Pico to control the relay. Wiring below assumes a Pico/Pico W/Pico 2W.

KY-037 → Pico

  • KY-037 VCC → Pico 3V3
  • KY-037 GND → Pico GND
  • KY-037 DO → Pico GP15

Pico → AC Relay Control

  • Pico GP16 → Relay Control Input (the low-voltage control side)
  • If your relay requires a ground reference on its control input: Pico GND → Relay Control GND

Plug the lamp into the relay’s switched outlet, then plug the relay into the wall outlet. You should hear an audible click when it switches.

Optional: Use the Pico’s onboard LED as a quick visual indicator while testing.

Step 2: Main Code (Clap Once = ON, Clap Again = OFF)

This script adds two important things so the project feels solid: (1) cooldown (prevents rapid retriggers) and (2) re-arm logic (requires the signal to go quiet before allowing the next clap).

from machine import Pin
import time

SOUND_PIN = 15      # KY-037 DO -> GP15
RELAY_PIN = 16      # IoT Relay control -> GP16
COOLDOWN_MS = 1200  # buffer time between triggers (tune 800-1500)

sound = Pin(SOUND_PIN, Pin.IN)
relay = Pin(RELAY_PIN, Pin.OUT)
led = Pin("LED", Pin.OUT)

relay_state = 0
relay.value(relay_state)

last_trigger = time.ticks_ms()

# Optional: require the signal to go LOW before allowing another trigger
armed = True

while True:
    v = sound.value()

    # re-arm once it goes quiet again
    if v == 0:
        armed = True

    now = time.ticks_ms()

    if v == 1 and armed and time.ticks_diff(now, last_trigger) > COOLDOWN_MS:
        relay_state = 0 if relay_state else 1
        relay.value(relay_state)

        led.toggle()
        print("RELAY:", "ON" if relay_state else "OFF")

        last_trigger = now
        armed = False  # wait for silence before next trigger

    time.sleep(0.01)

What this code is doing (in plain English)

  • Reads the KY-037 DO pin continuously.
  • If DO goes HIGH and we’re “armed” and the cooldown has passed → toggle the relay output.
  • Then it “disarms” until DO returns LOW (silence), preventing rapid double-triggers.

Step 3: Tune the KY-037 Threshold Sensitivity

The KY-037 has a small potentiometer (tiny screw knob). Turning it changes the sound threshold for the digital output. The goal is to set it right on the edge so a clap/knock triggers it, but normal room noise doesn’t.

How to tune it fast

  1. Run the test script below.
  2. While it’s running, slowly turn the potentiometer.
  3. You want the sensor to sit on the “border”: quiet = LOW, clap = HIGH.

Quick test script (use this while turning the potentiometer)

from machine import Pin
import time

sound = Pin(15, Pin.IN)      # KY-037 DO -> GP15
led = Pin("LED", Pin.OUT)

while True:
    v = sound.value()
    print("DO:", v)
    led.value(v)             # LED ON when DO is HIGH
    time.sleep(0.05)

Tuning tip: If DO is always 1, your threshold is too sensitive (turn the pot the other way). If DO is always 0, it’s not sensitive enough. Dial it until it flips cleanly when you clap.

Optional tuning: If you get double triggers, increase COOLDOWN_MS to ~1400–1500. If it feels too slow, lower it to ~800–1000.

Conclusion + Next Ideas

The KY-037 is one of those “cheap parts” that becomes seriously useful once you combine it with simple logic. In this project, you turned sound into a reliable toggle switch for real AC power — fast, satisfying, and practical.

What else you can build with the KY-037

  • Knock-to-toggle lights (closet, garage, desk)
  • Sound-activated alerts (door knock trigger)
  • Simple automation triggers (start/stop a device when sound crosses a threshold)
  • Noise level “activity detector” for fun projects

Get the KY-037 + basics from ShillehTek

If you want to build along, grab the KY-037 sound sensor, a breadboard, and jumper wires from ShillehTek:

Want a more advanced version (phone alerts, better filtering, multi-clap patterns, etc.)? ShillehTek consulting is available for real projects — hardware + software + deployment.