Radar hallway sensor on an ESP32-C3
An RCWL-0516 radar holds a hallway "occupied" for ten seconds after the last movement and lights the lamp only when it is dark, with the honest limits of a Doppler sensor. The whole build, an ESP32-C3 and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $14.95. On Mokxi they're free.
What you'll learn
- Reading a digital motion sensor
- An off delay that keeps a light on after movement stops
- Combining two conditions: occupied and dark
- Hysteresis so a reading near the limit does not flicker
- Serving device state as JSON from an ESP32
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: ESP32-C3-DevKitM-1
- Parts: 6
How it works
The RCWL-0516 radar sets its output pin HIGH while it sees something moving. The sketch reads it on pin 18. A light sensor module on pin 0 reports how dark the room is, and a yellow LED on pin 19 stands in for the hallway light.
Radar only sees movement, so someone standing still would look like nobody is there. To fix that, the sketch calls the hallway occupied as soon as there is movement, and keeps it occupied until OFF_DELAY_MS (ten seconds) pass with no movement.
The lamp turns on only when the hallway is occupied and the light reading is dark. It turns off when occupancy clears. The sketch also joins simulated WiFi and serves its state as JSON at /state.
Parts list
6 parts, plus the jumper wires. They retail for about $14.95; on Mokxi they're free.
- 1 × ESP32-C3-DevKitM-1In Handheld Console Kit, Desk Buddy Kit, ESP32-C3 Starter Kit.
- 1 × Full-size breadboard
- 1 × Photoresistor sensor moduleIn Desk Buddy Kit, Escape Box Kit, First Inventions Kit.
- 1 × Microwave motion sensorIn Halloween Prop Kit.
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, yellowIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Photoresistor sensor module pin GND | ESP32-C3-DevKitM-1 pin GND |
| Photoresistor sensor module pin VCC | ESP32-C3-DevKitM-1 pin 3V3 |
| Photoresistor sensor module pin AO | ESP32-C3-DevKitM-1 pin 0 |
| Microwave motion sensor pin VIN | ESP32-C3-DevKitM-1 pin 5V |
| Microwave motion sensor pin GND | ESP32-C3-DevKitM-1 pin GND |
| LED, yellow pin C | ESP32-C3-DevKitM-1 pin GND |
| Microwave motion sensor pin OUT | ESP32-C3-DevKitM-1 pin 18 |
| Resistor, 220 Ω pin 1 | ESP32-C3-DevKitM-1 pin 19 |
| LED, yellow pin A | Resistor, 220 Ω pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Home Assistant radar sensor: a hallway light that waits for the dark.
//
// RCWL-0516 on GPIO 18 microwave Doppler radar, OUT high while it sees movement
// light module on GPIO 0 the analog output of an LDR board, darker reads higher
// LED on GPIO 19 the hallway light, standing in for a relay or a smart bulb
//
// Honest first: the RCWL-0516 is not an mmWave presence sensor. It is a 3.2 GHz
// Doppler board that sees MOVEMENT, like a PIR that also sees through plastic
// and thin walls. Sit perfectly still and it lets go. The 24 GHz mmWave boards
// people use for true presence, such as the LD2410, also sense the tiny motion
// of breathing and talk over UART; Mokxi does not model them. What this sketch
// shows is the trick everybody uses with a motion sensor anyway: an off delay,
// so a short pause does not turn the lights out (ESPHome calls it delayed_off).
//
// The light logic runs on the device, so the hallway still works when Home
// Assistant is down. Mokxi has no MQTT broker: the sketch prints what it would
// publish, and serves its state as JSON at /state for the Web tab.
#include <WebServer.h>
#include <WiFi.h>
const int RADAR_PIN = 18;
const int LIGHT_SENSOR_PIN = 0;
const int LAMP_PIN = 19;
const unsigned long OFF_DELAY_MS = 10000; // keep "occupied" this long after the last movement
const int DARK_ABOVE = 3000; // raw reading, 0 to 4095: higher is darker
WebServer server(80);
bool moving = false;
bool occupied = false;
bool lampOn = false;
bool lastDark = false;
unsigned long lastMovement = 0;
void publish(const char *topic, const char *payload) {
Serial.print("publish home/hallway/");
Serial.print(topic);
Serial.print(' ');
Serial.println(payload);
}
bool isDark() {
// A little hysteresis, so dusk does not flicker the light on and off.
int raw = analogRead(LIGHT_SENSOR_PIN);
if (lastDark) lastDark = raw > DARK_ABOVE - 150;
else lastDark = raw > DARK_ABOVE;
return lastDark;
}
void setLamp(bool on) {
if (on == lampOn) return;
lampOn = on;
digitalWrite(LAMP_PIN, on ? HIGH : LOW);
publish("light", on ? "ON" : "OFF");
}
void sendState() {
String json = "{\"motion\":";
json += moving ? "true" : "false";
json += ",\"occupancy\":";
json += occupied ? "true" : "false";
json += ",\"light\":";
json += lampOn ? "true" : "false";
json += ",\"light_level\":";
json += String(analogRead(LIGHT_SENSOR_PIN));
json += "}";
server.send(200, "application/json", json);
}
void setup() {
Serial.begin(115200);
pinMode(RADAR_PIN, INPUT);
pinMode(LAMP_PIN, OUTPUT);
digitalWrite(LAMP_PIN, LOW);
Serial.println("Radar hallway sensor: simulated WiFi, no MQTT broker in Mokxi.");
WiFi.begin("Mokxi-Lab", "breadboard");
while (WiFi.status() != WL_CONNECTED) {
delay(250);
}
Serial.print("online at http://");
Serial.print(WiFi.localIP());
Serial.println("/state");
server.on("/state", sendState);
server.on("/", sendState);
server.begin();
publish("occupancy", "OFF");
}
void loop() {
server.handleClient();
unsigned long now = millis();
bool seen = digitalRead(RADAR_PIN) == HIGH;
if (seen != moving) {
moving = seen;
publish("motion", moving ? "ON" : "OFF");
}
if (moving) lastMovement = now;
// Occupied the moment anything moves, clear only after a quiet OFF_DELAY_MS.
if (moving && !occupied) {
occupied = true;
publish("occupancy", "ON");
} else if (!moving && occupied && now - lastMovement >= OFF_DELAY_MS) {
occupied = false;
publish("occupancy", "OFF");
setLamp(false);
}
// While someone is about, the light comes on as soon as it is dark enough.
if (occupied && !lampOn && isDark()) setLamp(true);
delay(10);
}
Code walkthrough
The key functions of the sketch, and what each one does.
isDark sketch.ino
Reads the light sensor and compares it to DARK_ABOVE. Higher readings mean darker. Once the room counts as dark, the reading must fall 150 below the limit to count as bright again. That gap stops the lamp flickering at dusk.
bool isDark() {
// A little hysteresis, so dusk does not flicker the light on and off.
int raw = analogRead(LIGHT_SENSOR_PIN);
if (lastDark) lastDark = raw > DARK_ABOVE - 150;
else lastDark = raw > DARK_ABOVE;
return lastDark;
}loop sketch.ino
Reads the radar and remembers the time of the last movement. Movement makes the hallway occupied right away. It clears only after OFF_DELAY_MS of quiet, and the lamp goes off with it. While occupied and dark, the lamp comes on.
void loop() {
server.handleClient();
unsigned long now = millis();
bool seen = digitalRead(RADAR_PIN) == HIGH;
if (seen != moving) {
moving = seen;
publish("motion", moving ? "ON" : "OFF");
}
if (moving) lastMovement = now;
// Occupied the moment anything moves, clear only after a quiet OFF_DELAY_MS.
if (moving && !occupied) {
occupied = true;
publish("occupancy", "ON");
} else if (!moving && occupied && now - lastMovement >= OFF_DELAY_MS) {
occupied = false;
publish("occupancy", "OFF");
setLamp(false);
}
// While someone is about, the light comes on as soon as it is dark enough.
if (occupied && !lampOn && isDark()) setLamp(true);
delay(10);
}sendState sketch.ino
Builds a small JSON text with four values: motion, occupancy, light and the raw light level. It sends that to anyone who opens /state or the root page of the board's address.
void sendState() {
String json = "{\"motion\":";
json += moving ? "true" : "false";
json += ",\"occupancy\":";
json += occupied ? "true" : "false";
json += ",\"light\":";
json += lampOn ? "true" : "false";
json += ",\"light_level\":";
json += String(analogRead(LIGHT_SENSOR_PIN));
json += "}";
server.send(200, "application/json", json);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The lamp never turns on even with movement This project
The room also has to be dark. The lamp needs a light reading aboveDARK_ABOVE, so lower the light level on the sensor. - The lamp goes out while someone is sitting still This project
This radar sees movement, not presence. IncreaseOFF_DELAY_MSto keep the hallway occupied longer. - The sketch hangs after start This project
It waits insetupuntil WiFi connects. Check the Serial Monitor for the online address. - The digital output never changes, or is always on, no matter the light. Photoresistor sensor module
Turn the trimmer on the module until DO flips at the light level you want. The digital output is set by that threshold, and which direction means bright depends on the board. - Analog readings go down when it gets brighter, and the code expects the opposite. Photoresistor sensor module
The LDR sits in a divider, so the direction depends on how the module is built. Print the raw value in the light and dark, then flip it in code with 1023 minus the reading if needed. - It triggers by itself or on a person behind a wall, and the output stays high. The microwave goes through thin walls and plastic, and it holds OUT for a few seconds. Microwave motion sensor
Aim it away from fans, windows, and moving things. Wait for the output to drop. The hold time and range can be set with solder pads and resistors on the board. - The board is powered wrong and acts strange. It needs 4 to 28 V on VIN, and OUT is 3.3 V, so it is safe for 3.3 V and 5 V pins. Microwave motion sensor
Feed VIN with 5 V. Keep the supply clean, since noise causes false triggers. Wait about a minute after power on before trusting the output. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.