I am trying to interface magnetic switch door sensor with XIAO ESP32C6.
My goal:
It is to detect whenever the door closes or open and use it as a wakeup method.
Else, all the time the XIAO sleeps. That is, when the door opens, the xiao wakes up, then reports the sensor value to MQTT server and then change the wakeup method and sleep again. Then when the door closes, wake up, report, change wakeup method and then sleep.
Hardware connection:
Connected Door sensor on D0 Pin (RTC Pin) using XIAO ESP32C6. There is a 10k resistor between one pin of the door sensor and ground.
Firmware:
#include "esp_sleep.h"
#include "esp_log.h"
// Pin where the magnetic sensor is connected
#define DOOR_SENSOR_PIN GPIO_NUM_0
#define BUTTON_PIN_BITMASK (1ULL << DOOR_SENSOR_PIN) // GPIO 0 bitmask for ext1
RTC_DATA_ATTR int bootCount = 0;
RTC_DATA_ATTR bool prevDoorState = false; // Stored in RTC memory
void setup() {
// Initialize Serial communication for debugging
Serial.begin(115200);
delay(1000);
++bootCount;
Serial.println("Boot number: " + String(bootCount));
// Initialize the door sensor pin as input
pinMode(DOOR_SENSOR_PIN, INPUT);
// Read the current state of the door
bool doorState = digitalRead(DOOR_SENSOR_PIN);
// Check if the door state has changed since the last wakeup
if (doorState == LOW) {
Serial.println("Door is closed");
} else {
Serial.println("Door is open");
}
esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK, ESP_EXT1_WAKEUP_ANY_HIGH);
// Setup the ESP32 to wake up on state change of the door sensor
//esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK, ESP_EXT1_WAKEUP_ALL_LOW);
// Go to deep sleep
Serial.println("Going to sleep now...");
esp_deep_sleep_start();
}
void loop() {
// This will never be called since the ESP32 will be in deep sleep mode
}
Issue:
When using the ESP_EXT1_WAKEUP_ANY_HIGH wake-up method, the XIAO board correctly stays in sleep mode while the door is closed and wakes up as soon as it opens.
However, when I switch to the ESP_EXT1_WAKEUP_ALL_LOW method, the board doesn’t enter sleep mode at all and continuously wakes up, regardless of whether the door is open or closed. I also tried using ESP_EXT1_WAKEUP_ANY_LOW, but the results were the same—the XIAO does not sleep in either state.
Could you please advise on what might be causing this issue and provide guidance on how to resolve it?
This example may help you, it uses the internal pullup on gpio 3
#include "driver/rtc_io.h" // Need to enable GPIO PULLUP
#define WAKEUP_GPIO GPIO_NUM_3 // Only RTC IO are allowed - ESP32 Pin example
#define BUTTON_PIN_BITMASK (1ULL << WAKEUP_GPIO) // GPIO 0 bitmask for ext1
RTC_DATA_ATTR int bootCount = 0;
RTC_DATA_ATTR bool pinState = false;
void print_wakeup_reason(){
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
}
}
void setup(){
Serial.begin(115200);
pinMode(WAKEUP_GPIO, INPUT_PULLUP);
delay(1000); //Take some time to open up the Serial Monitor
//Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
//Print the wakeup reason for ESP32
print_wakeup_reason();
pinState = digitalRead(WAKEUP_GPIO);
Serial.println("Pin state: " + String(pinState));
if(esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_EXT1){
Serial.println("yes wegot here ");
if(pinState == 0){
// pin is low, want next wake up when pin goes high
Serial.println("Pin is low ");
rtc_gpio_pulldown_dis(WAKEUP_GPIO);
rtc_gpio_pullup_en(WAKEUP_GPIO); // Configure pin to be pullup
esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH); // Wake up when pin goes low
Serial.println("Going to sleep now pin Low");
Serial.flush();
esp_deep_sleep_start();
}
else{
Serial.println("Pin is high ");
// pin is high, want next wake up when pin goes low
rtc_gpio_pulldown_dis(WAKEUP_GPIO);
rtc_gpio_pullup_en(WAKEUP_GPIO); // Configure pin to be pullup
esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_LOW); // Wake up when pin goes low
Serial.println("Going to sleep now pin High");
Serial.flush();
esp_deep_sleep_start();
}
}
rtc_gpio_pulldown_dis(WAKEUP_GPIO);
rtc_gpio_pullup_en(WAKEUP_GPIO); // Configure pin to be pullup
esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_LOW); // Wake up when pin goes low
//Go to sleep now
Serial.println("Going to sleep now");
Serial.flush();
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
void loop(){
//This is not going to be called
}