Build it step by step
0 of 7 doneWire the reed
Pin D1 with a 100 kΩ pull-up.
Test the state
Print high and low.
Send one packet
ESP-NOW broadcast.
Sleep and wake
Use the GPIO wake source.
Add the receiver
A second ESP32 prints the state.
Measure sleep current
In microamps.
Work out battery life
Capacity over current.
How do you build a one-year battery door sensor?
A door sensor that wakes only when the reed switch changes and sends one packet, lasting over a year on two AAs. You need ESP32-C3 SuperMini, Door reed switch MC-38, Battery holder 2 × AA, AA alkaline battery × 2 and Resistor 100 kΩ. It takes 7 steps and about 180 minutes.
- Level
- Advanced
- Time
- About 180 min
- Parts
- 5
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Battery holder plus | XIAO C3 3V3 pin | 3 V supply (see the notes) |
| Battery holder minus | XIAO C3 GND | Ground |
| Reed switch | XIAO C3 D1 (GPIO3) and GND | Door contact |
| 100 kΩ resistor | XIAO C3 D1 to 3V3 | Pull-up (draws almost nothing) |
Code
The chip spends nearly all its life in deep sleep and wakes only when the reed switch changes: closed to open or open to closed. Each wake reads the new state, sends one packet to the broadcast address with ESP-NOW, and sets the next wake for the opposite level, so no polling is needed. Measure the sleep current with a multimeter on its microamp range in series with the battery; the on-board parts of a dev board often take more than the chip itself, so for a year of life you may need to remove the board's power LED and consider a bare module. Estimate battery life: 2500 mAh divided by the sleep current, then subtract about 0.1 mAh per event. Powering the 3V3 pin directly skips the board's regulator, which wastes current, but the chip browns out near 2.5 V, so lithium AAs last longer than alkaline. The sketch needs the arduino-esp32 package 2.0.x for the receive callback shown. Arduino IDE with the ESP32 boards package 2.0.x, board "XIAO_ESP32C3". This sends no secrets, but anyone nearby can send false door states, so it is a convenience sensor, not an alarm.
Runs on a virtual ESP32-C3 in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Door sensor: sleep, wake when the reed changes, send one ESP-NOW packet, sleep again. Board: XIAO ESP32C3.
// Receiver sketch is in the comment below.
#include <WiFi.h>
#include <esp_now.h>
#include <esp_sleep.h>
const gpio_num_t REED = GPIO_NUM_3; // D1 on the XIAO ESP32C3
uint8_t BROADCAST[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
void setup() {
pinMode(REED, INPUT); // external 100k pull-up is on the pin
bool open = digitalRead(REED) == HIGH; // magnet away from the switch = door open
WiFi.mode(WIFI_STA);
if (esp_now_init() == ESP_OK) {
esp_now_peer_info_t peer = {};
memcpy(peer.peer_addr, BROADCAST, 6);
peer.channel = 0; peer.encrypt = false;
esp_now_add_peer(&peer);
uint8_t msg = open ? 1 : 0;
esp_now_send(BROADCAST, &msg, 1);
delay(30); // let the packet go out
}
esp_deep_sleep_enable_gpio_wakeup(1ULL << REED, open ? ESP_GPIO_WAKEUP_GPIO_LOW : ESP_GPIO_WAKEUP_GPIO_HIGH); // wake on the opposite change
esp_deep_sleep_start();
}
void loop() {}
/* Receiver (any ESP32 board, package 2.0.x):
#include <WiFi.h>
#include <esp_now.h>
void onData(const uint8_t* mac, const uint8_t* data, int len) { Serial.println(data[0] ? "Door OPEN" : "Door CLOSED"); }
void setup() { Serial.begin(115200); WiFi.mode(WIFI_STA); esp_now_init(); esp_now_register_recv_cb(onData); }
void loop() {}
*/What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual ESP32-C3 DevKitM-1 with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
[deep sleep for 60 min 0 s]
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Open the door | Receiver prints OPEN |
| Close the door | Receiver prints CLOSED |
| Between events | Sleep current in microamps |
| Battery estimate | Calculated from the measurement |
If it does not work
No wake
Check the pull-up and the wake level logic.
No packet
Check the receiver is on and in range.
High sleep current
Remove the board LED and extra parts.
Reboots
Battery too low for the chip.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the one-year battery door sensor?
ESP32-C3 SuperMini, Door reed switch MC-38, Battery holder 2 × AA, AA alkaline battery × 2 and Resistor 100 kΩ.
How long does the one-year battery door sensor take to build?
About 180 minutes for a advanced maker, in 7 steps.
How do I know the one-year battery door sensor works?
Open the door: Receiver prints OPEN. Close the door: Receiver prints CLOSED. Between events: Sleep current in microamps.
No wake: what should I check?
Check the pull-up and the wake level logic.
No packet: what should I check?
Check the receiver is on and in range.
High sleep current: what should I check?
Remove the board LED and extra parts.
Reboots: what should I check?
Battery too low for the chip.
