ESP32-H2 Zigbee Door Sensor
This circuit is live in Circuit Canvas. Open it to explore every part and wire, or make your own copy to change values, swap parts and build your own version. Free, right in your browser.
A battery-powered Zigbee door sensor for Home Assistant. A magnetic contact switch (or just a plain switch) connects GPIO10 to GND when the door is closed.
You can power the board with up to 3.6V through the 3V3 pin.
Check out the full tutorial: https://www.build-electronic-circuits.com/esp32-h2-zigbee-door-sensor/
ESP32-H2 Zigbee Door Sensor Schematic
SW1 can be a reed switch that works with magnets, or any other mechanical switch. No pull-up is needed as we're using the ESP32-H2's built-in pull-up.
ESP32-H2 Zigbee Door Sensor Parts List
How to Build the ESP32-H2 Zigbee Door Sensor
Code
#ifndef ZIGBEE_MODE_ED
#error "Select Tools -> Zigbee mode: Zigbee ED (end device)"
#endif
#include "Zigbee.h"
#include <Preferences.h>
#define SENSOR_PIN 10 // Must be GPIO 7-14 to wake the ESP32-H2 from deep sleep
#define ENDPOINT 1 // Same endpoint as the example you paired with
#define CHECKIN_HOURS 6 // Wake up anyway every few hours so Home Assistant knows we're alive
ZigbeeContactSwitch zbContact = ZigbeeContactSwitch(ENDPOINT);
Preferences preferences; // Stores the "enrolled" flag in flash
bool readIsOpen() {
return digitalRead(SENSOR_PIN) == HIGH;
}
void sendState(bool isOpen) {
if (isOpen) {
zbContact.setOpen();
Serial.println("Sent: OPEN");
} else {
zbContact.setClosed();
Serial.println("Sent: CLOSED");
}
}
void goToSleep() {
// Wake up when the pin changes: if it's HIGH now, wake on LOW, and the other way around
bool isOpen = readIsOpen();
esp_sleep_enable_ext1_wakeup(1ULL << SENSOR_PIN,
isOpen ? ESP_EXT1_WAKEUP_ANY_LOW : ESP_EXT1_WAKEUP_ANY_HIGH);
// Also wake up on a timer, as a "still alive" check-in
esp_sleep_enable_timer_wakeup((uint64_t)CHECKIN_HOURS * 3600ULL * 1000000ULL);
Serial.println("Going to sleep");
Serial.flush();
esp_deep_sleep_start();
}
void setup() {
Serial.begin(115200);
pinMode(SENSOR_PIN, INPUT_PULLUP); // Built-in pull-up resistor
// Has this board been enrolled before? (Same flag as the example uses)
preferences.begin("Zigbee", false);
bool enrolled = preferences.getBool("ENROLLED");
preferences.end();
// Set up the Zigbee device
zbContact.setManufacturerAndModel("DIY", "ESP32H2_Door_Sensor");
zbContact.setPowerSource(ZB_POWER_SOURCE_BATTERY);
Zigbee.addEndpoint(&zbContact);
// Settings for a sleepy end device (from Espressif's sleepy sensor example)
esp_zb_cfg_t zigbeeConfig = ZIGBEE_DEFAULT_ED_CONFIG();
zigbeeConfig.nwk_cfg.zed_cfg.keep_alive = 10000;
Zigbee.setTimeout(10000);
if (!Zigbee.begin(&zigbeeConfig, false)) {
Serial.println("Zigbee failed to start");
goToSleep();
}
// Wait for the network, but give up after 10 seconds to save the battery
unsigned long start = millis();
while (!Zigbee.connected()) {
if (millis() - start > 10000) {
Serial.println("No network, sleeping");
goToSleep();
}
delay(100);
}
// Restore the enrollment from flash, or ask for a new one the first time
if (enrolled) {
zbContact.restoreIASZoneEnroll();
} else {
zbContact.requestIASZoneEnroll();
}
start = millis();
while (!zbContact.enrolled()) {
if (millis() - start > 10000) {
Serial.println("Not enrolled, sleeping");
goToSleep();
}
delay(100);
}
// Save the flag the first time, so we can restore it after every wake-up
if (!enrolled) {
preferences.begin("Zigbee", false);
preferences.putBool("ENROLLED", true);
preferences.end();
}
// Send the state, then send again if it changed while we were busy
bool sentState = readIsOpen();
sendState(sentState);
delay(1000); // Give the radio time to deliver the message
if (readIsOpen() != sentState) {
sendState(readIsOpen());
delay(1000);
}
goToSleep();
}
void loop() {
// Never reached - the board sleeps at the end of setup()
}
Make it your own
Every circuit here is a live project in Circuit Canvas, not just a picture. Copy this one and change a value, swap a part or move it to a stripboard: the schematic and the breadboard layout are linked, so a part added to one appears in the other.
Or start from a blank canvas and draw your own circuit, with thousands of ready-made parts.