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
The light switch controller fitted over a white UK wall switch, with the servo arm resting on the rocker

OLED screentime, weather, mode

SG90 servopresses the rocker

RadarLD2410C, 24 GHz

USB-C power

Touch padTTP223, behind the front wall

Version 3, the finished build, on my bedroom 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.

A simple plan drawing of the controller: display, RTC, a button, the ESP32, the servo next to the switch and a motion sensor underneath
2022. The first plan I drew.
Version 3 of the controller on the wall, with the servo arm resting on the rocker
2023. Version 3, the finished one.

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.

How the parts connect The radar and two touch pads feed the ESP32-C6. It drives the servo and the display, and connects to three services over Wi-Fi. Wi-Fi MQTT brokerMosquitto, on the home network SinricProAlexa and Google Home OpenWeatherMapSunrise, sunset, weather LD2410C radarUART · GPIO 0 / 20presence · GPIO 14 Action touch padTTP223 · GPIO 3 Menu touch padTTP223 · GPIO 1 ESP32-C6reads the inputsdecides on the light SG90 servoPWM · GPIO 2presses the switch OLED displayI²C · GPIO 21 / 22status and menus
  1. Radar→ESP32-C6

    Readings over UART on GPIO 0 / 20, and a presence signal on GPIO 14.

  2. Touch pads→ESP32-C6

    Action on GPIO 3, menu on GPIO 1.

  3. ESP32-C6→Servo

    A PWM signal on GPIO 2 sets the arm position.

  4. ESP32-C6→OLED

    I²C on GPIO 21 / 22.

  5. ESP32-C6→Network

    Wi-Fi to the MQTT broker, SinricPro and OpenWeatherMap.

Solid lines are wires and dashed lines are network links. Power isn't drawn. The full pin list is further down.

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.)

How the modes connect Auto is in the middle. A tap goes to Manual, and Manual goes back to Auto when its timer runs out. A double tap goes to Locked and another double tap comes back. Holding the pad goes to Bedtime and holding it again comes back. Alarm is picked from the app, and a tap in Alarm turns the light off and goes to Manual. tap timer runs out double tap double tap again hold hold again from the app tap: light off Alarmservo nudges the switch Manualuntil the timer runs out Autothe radar decideswhere it starts up Lockedradar can't switch it Bedtimenothing automatic
  1. Auto→Manual

    A tap. It goes back to Auto when the timer runs out.

  2. Auto↔Locked

    A double tap each way.

  3. Auto↔Bedtime

    Hold the pad for about a second each way.

  4. App→Alarm

    Picked from the app. A tap turns the light off and goes to Manual.

Solid lines are touch pad gestures and the Manual timer. The dashed one is a command from the app, which can also pick any of the other modes directly.
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.

  1. Where it starts up
  2. Auto
  3. 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.

  1. Tap the pad
  2. Manual
  3. 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.

  1. Picked in the app
  2. Alarm
  3. 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.

  1. Hold the pad
  2. Bedtime
  3. 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.

  1. Double tap
  2. Locked
  3. 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.

PartModelPinSignalWhat it does
ServoSG90GPIO 2PWMPresses the switch
Presence outLD2410CGPIO 14Digital inHardware presence signal
Radar RXLD2410CGPIO 20UART, 256kReadings and settings
Radar TXLD2410CGPIO 0UART, 256kReadings and settings
Action padTTP223GPIO 3Digital inTap to toggle
Menu padTTP223GPIO 1Digital inMenu and calibration
Display SDASSD1306 128×64GPIO 21I²C, 400kData
Display SCLSSD1306 128×64GPIO 22I²C, 400kClock

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:

40%Duration

How long I slept compared with the target I set.

35%Consistency

How much my bedtime moves around. The aim is under 15 minutes.

15%Timing

How close bedtime was to the time I picked.

10%Cycles

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.

θ¯=atan2(1N∑i=1Nsinθi,1N∑i=1Ncosθi)

Duration score

Full marks at the target length, falling off smoothly either side. The 1.2 sets how quickly it drops.

Sdur=100·e−(actual−target)22·1.22

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