Built from 2022 to 2023, over three versions
The light switch
A box that sits over the light switch in my bedroom and presses it with a servo. The switch underneath is untouched, so I never had to go near the mains wiring.
- Built
- 2022–23, three versions
- Microcontroller
- ESP32-C6
- Presence sensing
- 24 GHz mmWave radar
- Power
- USB-C
OLED screentime, weather, mode
SG90 servopresses the rocker
RadarLD2410C, 24 GHz
USB-C power
Touch padTTP223, behind the front wall
Why it presses the switch
Normally you'd make a light smart by swapping the switch, which means working on mains wiring. I wasn't going to do that in my bedroom at 13, so the controller sits over the rocker and a little servo presses it instead. Take the box off and it's just a normal switch again.
It runs off USB-C. Inside there's an ESP32-C6, a radar module for presence, two touch pads, a small OLED screen and the servo. It talks to the blinds controller and the Zaylo Remote app over MQTT, and to Alexa and Google Home through SinricPro.


Most of that plan survived. The RTC didn't, because the switch gets the time over Wi-Fi instead. The single button turned into two touch pads, and the motion sensor turned into a radar so it can tell when someone is sitting still.
How the parts connect
The radar and the touch pads are the inputs. The ESP32-C6 decides what the light should be doing, then moves the servo and updates the screen. Everything that needs the network goes over Wi-Fi.
- Radar→ESP32-C6
Readings over UART on GPIO 0 / 20, and a presence signal on GPIO 14.
- Touch pads→ESP32-C6
Action on GPIO 3, menu on GPIO 1.
- ESP32-C6→Servo
A PWM signal on GPIO 2 sets the arm position.
- ESP32-C6→OLED
I²C on GPIO 21 / 22.
- ESP32-C6→Network
Wi-Fi to the MQTT broker, SinricPro and OpenWeatherMap.
The five modes
The switch is always in one of five modes, and each one decides what's allowed to change the light. Auto is the normal one. The other four are different ways of taking over from the radar.
You get between them with the action pad. A tap switches the light, a double tap locks or unlocks it, and holding it for about a second starts or ends Bedtime. The app can jump straight to any mode. (Holding the pad for 15 seconds is a factory reset, so that one's hard to do by accident.)
- Auto→Manual
A tap. It goes back to Auto when the timer runs out.
- Auto↔Locked
A double tap each way.
- Auto↔Bedtime
Hold the pad for about a second each way.
- App→Alarm
Picked from the app. A tap turns the light off and goes to Manual.
01AutoThe radar decides
This is where it spends most of its time, and where the other modes come back to. When the radar sees someone it turns the light on. Once it hasn't seen anyone for the motion timeout, it turns it off again. The timeout can be 30 seconds, or 1, 5 or 10 minutes, picked from the menu on the screen.
Between sunrise and sunset it won't turn the light on by itself, but it'll still turn it off when I leave.
- Where it starts up
- Auto
- Tap, double tap or hold
02ManualI decide, for a while
A tap on the pad, or a command from the app, switches the light and puts it into Manual, so the radar stops overruling me. Every tap after that restarts the timer, and when the timer runs out it goes back to Auto by itself.
The timer can be anything from 10 seconds to 8 hours. It's saved, so a power cut halfway through doesn't cancel it.
- Tap the pad
- Manual
- Timer runs out: back to Auto
03AlarmWake-up light
At the alarm time I set in the app, the light comes on to wake me up, whatever mode it's in. If it was in Bedtime, that ends Bedtime, and with it the night's sleep recording. If it was Locked, it switches to Manual so the light is allowed on.
Alarm mode itself is the more insistent version, picked from the app. The servo gives the rocker a tiny nudge every two seconds until I tap the pad, which turns the light off and drops it into Manual. A command from the app or a voice command stops it too.
- Picked in the app
- Alarm
- Tap: light off, into Manual
04BedtimeNothing automatic while I sleep
Holding the pad for about a second starts Bedtime, and holding it again ends it. It's also what the sleep tracking runs on: the night is recorded from when Bedtime starts to when it ends.
It leaves the light exactly as it was. The radar can't switch it when I move around in bed, single and double taps are ignored, and if the light was on, nothing turns it off until Bedtime ends. If it was off, the app can still turn it on without ending the night's recording.
- Hold the pad
- Bedtime
- Hold again, or the alarm
05LockedThe radar's switched off
Locked stops the radar switching the light at all, but leaves everything else working. A tap still turns the light on or off without leaving Locked, and so do the app and voice commands. It's for when I want the light to stay how I've set it, however much I move around.
A double tap locks it, and another double tap unlocks it back to Auto. Holding the pad still starts Bedtime, the same as from any other mode.
- Double tap
- Locked
- Double tap again: back to Auto
Safe mode isn't one of the five. It only kicks in after repeated crashes (more on that in when something goes wrong).
Detecting presence
I used radar because an ordinary motion sensor stops seeing you once you sit still, which is exactly when I want the light to stay on. The LD2410C reports movement in nine distance gates, and the firmware filters those readings before it decides whether anyone is in the room.
There are two filters on the readings. A fast one reacts to big movements, so the light comes on as soon as I walk in. A slow one picks up the small movements you still make when you're sitting still, so the light stays on while I'm working at my desk. Turning the light on also needs a stronger signal than keeping it on. Without that gap it would flicker whenever a reading sat right on the edge.
At start-up, and again whenever the room is empty, it measures the background so it has something to compare against. Detection also pauses while the servo moves and for a moment after, because otherwise it would see its own arm pressing the switch and decide someone had walked in.
Pins and connections
These are the connections in my build. If you're copying them, check them against the firmware for your board first.
| Part | Model | Pin | Signal | What it does |
|---|---|---|---|---|
| Servo | SG90 | GPIO 2 | PWM | Presses the switch |
| Presence out | LD2410C | GPIO 14 | Digital in | Hardware presence signal |
| Radar RX | LD2410C | GPIO 20 | UART, 256k | Readings and settings |
| Radar TX | LD2410C | GPIO 0 | UART, 256k | Readings and settings |
| Action pad | TTP223 | GPIO 3 | Digital in | Tap to toggle |
| Menu pad | TTP223 | GPIO 1 | Digital in | Menu and calibration |
| Display SDA | SSD1306 128×64 | GPIO 21 | I²C, 400k | Data |
| Display SCL | SSD1306 128×64 | GPIO 22 | I²C, 400k | Clock |
Sleep tracking
This started as a programming experiment. The switch already knows roughly when I go to bed and when I get up, so I wrote a score based on those times. It all runs on the switch itself. All it knows is when the light went off and when it came back on, so it isn't measuring sleep in any medical sense.
This is how the score is weighted:
How long I slept compared with the target I set.
How much my bedtime moves around. The aim is under 15 minutes.
How close bedtime was to the time I picked.
Whether I woke near the end of a 90-minute cycle.
The interesting part is the averaging. You can't just average clock times, because 23:30 and 00:30 average to midday. So each bedtime becomes an angle on a circle and the firmware averages those instead.
Average bedtime
Each time is an angle θ round a 24-hour circle. Averaging the sines and cosines gives the right answer either side of midnight.
Duration score
Full marks at the target length, falling off smoothly either side. The 1.2 sets how quickly it drops.
On top of the score it keeps a running total of sleep I've missed over the last seven days, warns me when my weekend bedtime drifts more than an hour from weekdays, sorts the pattern into Regular, Night Owl, Early Bird or Irregular, and suggests a bedtime from my wake-up time and target length.
Talking to the blinds and the app
Wi-Fi is the main connection, but the switch doesn't need it to work. The radar, touch pads and modes all run on the device, so if the network goes down it carries on as a normal presence-controlled light.
The app
Zaylo Remote is where I change modes, set schedules, look at the sleep scores and tune the radar. Local control goes over the home network.
Bluetooth setup
The Wi-Fi details go in over Bluetooth the first time. If it loses Wi-Fi later, Bluetooth recovery turns on so I can fix it without taking it off the wall.
MQTT
It publishes the light state, motion levels, sleep scores and temperature to a broker on my home network, and listens for commands.
Alexa, Google Home and weather
Voice commands come in through SinricPro. Sunrise, sunset and temperature come from OpenWeatherMap. Both need an internet connection.
Closing the blinds when the room is empty
Because the switch already knows whether the room is occupied, it shares that over MQTT. The blinds controller listens for it and closes the blind once the room has been empty for a set time, as long as both of them can reach the broker.
Wi-Fi 6 power saving
The ESP32-C6 supports Target Wake Time, which lets it agree wake-up windows with the router and sleep the radio in between. It's a toggle in the app. I haven't measured how much it saves yet, and it depends a lot on the router anyway.
When something goes wrong
The switch is on my wall all the time, so it has to recover from power cuts and bad updates without me opening it up.
- 15s
Start-up delay
The servo stays off for the first 15 seconds after power-on, so the supply can settle before the motor starts drawing current.
- 2
Copies of the settings
Settings are saved in two slots, each with a checksum. If the power cuts out halfway through a save, it loads the other copy on the next start.
- 3
Crashes before safe mode
After three unexpected restarts in a row it stops the motor, Wi-Fi and radar and shows a warning. It tries to start normally again after 10 minutes.
- OTA
Updates that roll back
A new firmware update only becomes permanent once the switch has started up and connected properly. If it crashes, it rolls back to the last version that worked.
If I come back to it
The switch is finished, but if I do go back to it, the first thing I'd add is a link to Atlas, my balancing robot, over MQTT. Atlas has to balance properly first, though.
Until then, the easiest way to see what the switch does is the app demo, which has pretend versions of the switch and the blinds to play with.
Try the Zaylo Remote demo