Close

Pulse counting (part 2)

A project log for Battery powered LED pulse counter

A battery powered WiFi light pulse counter for household energy monitoring.

theoTheo 03/01/2024 at 16:470 Comments

After testing if the ESP32-C3 could wake from a rising edge GPIO, it does not seem to work. The datasheet was a bit ambiguous, but after writing the RTC_CNTL_GPIO_WAKEUP_REG for edge trigger the MCU would not wake when a signal was applied to the GPIO pin.


I will instead use the level triggered interrupt and upon waking from sleep invert the level chosen for triggering the interrupt. This means the CPU will wake for both the rising edge and falling edge of the pulse but will still spend most of the time asleep.

void RTC_DATA_ATTR wake_stub_sleep_counter(void)
{
    wakeup_cause = esp_wake_stub_get_wakeup_cause();
    if(wakeup_cause & 0x04)
    {
        // Wakeup cause GPIO
        ++wake_count;
        intr_type = intr_type == GPIO_INTR_HIGH_LEVEL ? GPIO_INTR_LOW_LEVEL : GPIO_INTR_HIGH_LEVEL;
        gpio_ll_deepsleep_wakeup_enable(&GPIO, GPIO_NUM_4, intr_type);
        // Go back to sleep
        esp_wake_stub_sleep(&wake_stub_sleep_counter);
    }
    else
    {
        esp_default_wake_deep_sleep();
        // Return from the wake stub function to continue
        // booting the firmware.
        return;
    }

} 


I measured the time taken for the ESP32-C3 to wake and return to sleep as 12.4ms, using a 1 ohm resistor inline with the power supply, showing the current draw in this mode to be around 8.4mA. Assuming an average of 1 pulse counted per second, we can estimate the battery  life running from a 800mA 1.2V AAA not counting the wakeups to connect to WiFi.

0.8*1.2=0.96Wh
3.3*8.4e-3=0.0277W
0.97/0.277=34.63H (if on continuously)
34.6/(12.4e-3*2)=1395Hours (if one pulse per second on average)

Discussions