Automatic dusk-to-dawn light
Lights switch on by themselves at dusk and off again in the morning.
Shop by category
Shop all componentsA small knock on a table or box shows up as a sharp spike in an accelerometer. The MPU6050 reads acceleration at the ESP32's I2C bus, the sketch watches for a spike above a threshold and treats two spikes within 0.7 seconds as a double tap. That toggles a relay. Fix the box to the underside of a desk or inside a lamp base so you can knock the surface to switch the light. The threshold is adjustable so the lamp does not react to footsteps.
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.
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.
tap_switch.inoVerify & upload in Electrolark#include <Wire.h>
#include <Adafruit_MPU6050.h>
Adafruit_MPU6050 mpu;
const int RELAY_PIN = 26;
const float TAP_LIMIT = 6.0; // jolt in m/s^2: tune with the Serial Monitor
bool on = false;
float prevMag = 9.8;
unsigned long firstTap = 0, lockUntil = 0;
int taps = 0;
void setup() {
Serial.begin(115200);
Wire.begin(21, 22);
mpu.begin();
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // relay off (most modules are active-low)
}
void loop() {
sensors_event_t a, g, t;
mpu.getEvent(&a, &g, &t);
float mag = sqrt(a.acceleration.x * a.acceleration.x + a.acceleration.y * a.acceleration.y + a.acceleration.z * a.acceleration.z);
float jolt = fabs(mag - prevMag);
prevMag = mag;
unsigned long now = millis();
if (jolt > TAP_LIMIT && now > lockUntil) {
Serial.printf("tap, jolt %.1f\n", jolt);
lockUntil = now + 120; // ignore the bounce of the same knock
if (taps == 0 || now - firstTap > 700) { taps = 1; firstTap = now; }
else taps++;
if (taps >= 2) {
on = !on;
digitalWrite(RELAY_PIN, on ? LOW : HIGH);
taps = 0;
lockUntil = now + 800;
}
}
delay(5);
}
Raise TAP_LIMIT or fix the box more firmly to a solid surface.
Lower TAP_LIMIT, check the I2C wiring, and knock on the same surface the box is fixed to.
Flip HIGH and LOW for your relay type.
Roughly. A sharp knock is brief; the double-tap rule helps.
Yes, metal carries knocks well.
Yes, change taps >= 2 to 3.
5 parts: ESP32-WROOM-32 38-Pin Development Board, GY-521 MPU6050 6-Axis Gyro and Accelerometer Module, 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 (tap_switch.ino).
About ₹699 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Detecting a sudden change in acceleration; Timing two events for a double tap; Switching a relay without bounce problems.
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. 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.