ESP32-C6 deep_sleep and external wakeup + interrupt

unlucio
Posts: 1
Joined: Sun Feb 18, 2024 2:12 pm

ESP32-C6 deep_sleep and external wakeup + interrupt

Postby unlucio » Sun Feb 18, 2024 2:54 pm

Hello,
I've a lot of experience with programming and computers, but I'm pretty new to microcontrollers and making electronics.

For a PIR project (substituting my current alarm's PIR's controller with new ones as the current controllers aren't supported any more), I've a little prototype with an ESP32-C6-WROOM-1 dev board connected on a breadboard in the following way:
- 3v pin to +
- GRD pin to -
- pin 4 pulled down with resistor to -
- a floating wire connected to pin 4 for testing

This is the behavior I need:
- ESP32 is turned on
- ESP32 goes to sleep
- pin 4 goes to high (testing wire touching +)
- ESP32 wakes up (cause: external signal)
- pirInterrupt() is called
- "Motion detected!" is printed
- ESP32 goes to sleep

To achieve this I wrote the following code:
  1. #include <esp_sleep.h>
  2.  
  3. #define PIR_PIN 4 // GPIO pin connected to the PIR sensor
  4. #define RTC_PIN GPIO_NUM_4 // RTC_IO pin corresponding to the GPIO pin connected to the PIR sensor
  5.  
  6. RTC_DATA_ATTR volatile bool pirDetected = false;
  7.  
  8. void RTC_IRAM_ATTR pirInterrupt() {
  9.   pirDetected = true;
  10. }
  11.  
  12. void printWakeupReason() {
  13.   esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause();
  14.  
  15.   switch(wakeup_reason) {
  16.     case 1  : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
  17.     case 2  : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
  18.     case 3  : Serial.println("Wakeup caused by timer"); break;
  19.     case 4  : Serial.println("Wakeup caused by touchpad"); break;
  20.     case 5  : Serial.println("Wakeup caused by ULP program"); break;
  21.     default : Serial.println("Wakeup was not caused by deep sleep"); break;
  22.   }
  23. }
  24.  
  25. void setup() {
  26.   Serial.begin(115200);
  27.   printWakeupReason();
  28.  
  29.   pinMode(PIR_PIN, INPUT_PULLDOWN);
  30.   attachInterrupt(digitalPinToInterrupt(PIR_PIN), pirInterrupt, RISING);
  31.   esp_sleep_enable_ext1_wakeup(RTC_PIN, ESP_EXT1_WAKEUP_ANY_HIGH);
  32.   Serial.println("Setup completed.");
  33.  
  34.   if (pirDetected) {
  35.     pirDetected = false;
  36.     Serial.println("Motion detected!");
  37.   }
  38.  
  39.   // Enter deep sleep
  40.   Serial.println("Entering deep sleep mode...");
  41.   esp_deep_sleep_start();
  42. }
  43.  
  44. void loop() {
  45.   // Empty loop
  46. }
When I touch + with the testing wire I get this:

Code: Select all

ESP-ROM:esp32c6-20220919
Build:Sep 19 2022
rst:0x5 (SLEEP_WAKEUP),boot:0xc (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:2
load:0x4086c410,len:0xc24
load:0x4086e610,len:0x2708
load:0x40875728,len:0x594
entry 0x4086c410
Wakeup caused by timer
Setup completed.
Entering deep sleep mode...
As you can see
- the wakeup is caused by "Timer"
- pirInterrupt() is not called
- "Motion detected!" is not printed

While debugging I noticed that if I quickly touch the + rail multiple times pirInterrupt() is called and "Motion detected!" is printed, I have one only possible explanation for this: Wakeup "consumes" the signal so the interrupt needs a new signal to work.

My questions are:
1) Why is the wake-up caused by "Timer"? I was expecting one of the 2 "Wakeup caused by external signal using [...]", what am I doing wrong? (I will need to do different things depending on the wakeup cause)
2) What's the best way to solve the signal issue? Since I cannot control the signal's producer, I thought I could put a small capacitor in to "buffer" the signal and perhaps allow it to be "consumed" twice, but I'm afraid the cap might also add a "debounce" effect and possibly mask series of signals or cause other bugs further into the project.
Attachments
IMG_7293.jpeg
IMG_7293.jpeg (515.29 KiB) Viewed 1286 times
IMG_7294.jpeg
IMG_7294.jpeg (2.19 MiB) Viewed 1286 times

some esp dev guy
Posts: 2
Joined: Wed Aug 21, 2024 12:08 pm

Re: ESP32-C6 deep_sleep and external wakeup + interrupt

Postby some esp dev guy » Wed Aug 21, 2024 12:19 pm

the esp_sleep_enable_ext1_wakeup function takes pin mask not the pin no as argument so value 4 corresponds to pin no 2 (2^2). Triggering the event by multiple touching could be caused by accidentally touching the neighbouring pins.

Who is online

Users browsing this forum: No registered users and 59 guests