Automatic dusk-to-dawn light
Lights switch on by themselves at dusk and off again in the morning.
Shop by category
Shop all componentsA piezo knock sensor produces a small voltage spike when it is tapped. The ESP32 reads it on an analog pin and counts spikes. Three knocks within two seconds toggle the relay, while single bumps are ignored. Because any loud vibration can trigger it, this is a fun hidden switch for a lamp or a toy, not a lock for anything valuable.
Anything connected to 230 V mains must be installed by a qualified electrician in a closed, earthed enclosure. Test with a low-voltage lamp first.
Tick anything you already have and the total updates.
Piezo knock sensor
Follow the colours: red is power, dark grey is ground, blue is a signal.
USB phone charger. Anything connected to 230 V mains must be installed by a qualified electrician in a closed, earthed enclosure. Test with a low-voltage lamp first.
secret_knock.inoVerify & upload in Electrolarkconst int KNOCK_PIN = 34, RELAY_PIN = 26;
const bool ACTIVE_LOW = true;
const int THRESHOLD = 600; // 0-4095; raise it if it triggers too easily
const unsigned long WINDOW_MS = 2000, DEBOUNCE_MS = 120;
int knocks = 0;
unsigned long firstKnock = 0, lastKnock = 0;
bool on = false;
void setRelay(bool v) { on = v; digitalWrite(RELAY_PIN, (v == ACTIVE_LOW) ? LOW : HIGH); }
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
setRelay(false);
}
void loop() {
int v = analogRead(KNOCK_PIN);
unsigned long now = millis();
if (v > THRESHOLD && now - lastKnock > DEBOUNCE_MS) {
lastKnock = now;
if (knocks == 0 || now - firstKnock > WINDOW_MS) { knocks = 0; firstKnock = now; }
knocks++;
Serial.printf("knock %d (value %d)\n", knocks, v);
if (knocks >= 3) { setRelay(!on); knocks = 0; }
}
}
Raise THRESHOLD or move the sensor away from vibration like speakers or fans.
Lower THRESHOLD, and press the sensor firmly against the surface.
Flip ACTIVE_LOW.
No. Anyone can imitate a simple knock. Use it for fun, not security.
Yes, store the gaps between knocks and compare them with a pattern.
It is an input-only ADC pin that works well with analog sensors.
5 parts: ESP32-WROOM-32 38-Pin Development Board, Elecrow Crowtail-Knock Sensor Module – Piezoelectric Vibration Detection, 5V Relay Module with Optocoupler, Solderless Breadboard and Dupont Jumper Wires (40 pcs). On this page you can tick the parts you already have and add only the rest to your cart.
It is a intermediate project and takes about 1.5 hours. The build on this page has 6 steps.
No. This page has the complete, tested code (secret_knock.ino).
About ₹664 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Reading a piezo knock sensor on an ADC pin; Detecting spikes with a threshold and debounce; Counting events inside a time window.
Anything connected to 230 V mains must be installed by a qualified electrician in a closed, earthed enclosure. Test with a low-voltage lamp first.
Ideas that reuse this build: ESP32 clap switch relay. They are listed under Take it further on this page.
Lights switch on by themselves at dusk and off again in the morning.
A precise thermostat that keeps tropical fish at a steady 25–26 °C.
Starts the pump when the tank runs low and stops it when it is full.
Tap a hidden pad to switch a lamp on or off, even through a wooden desk.
Learning project. Check every circuit and the datasheet of each part before you power it up.