I am trying to detect a power toggle event for a smart lamp I am building. Essentially, I want the lamp to behave differently when turned on after a long off period compared to when toggled on/off/on. I want to use a simple RC circuit to achieve this.
I am using an esp32-wroom-32d. R=80k, C=10u
To test, I can set IO13 output HIGH to charge the capacitor and then set IO13 to analog input mode to read the discharge of the capacitor. The readings look as follows (blue=expected, red=actual readings):
This looks as expected, and shows that I will have a couple seconds to make an analog reading at boot to detect a power toggle. If I read 0V then I know the power has been off for a long period, and if I read >0V then I know the power has toggled in the last few seconds. See sample code below.
from machine import Pin, ADC
import time
D7 = 13
LED = 2 # D9
adc_pin = Pin(D7, Pin.IN, Pin.OPEN_DRAIN)
adc_pin.value(1) # set high Z open drain
led = Pin(LED, Pin.OUT)
def boot():
rc_V = read_rc()
# R = 83k, C = 10u, tau = 0.8sec
if rc_V > 0.5: # was on very recently
led.value(1)
else:
led.value(0)
# read RC, then set the pin high to begin recharging the RC for the next power on/off event
def read_rc():
adc_pin = Pin(D7, Pin.IN, Pin.OPEN_DRAIN)
adc_pin.value(1) # set high Z open drain
adc = ADC(adc_pin)
adc.atten(ADC.ATTN_11DB) # attenuation for esp32
rc_V = 3.3*adc.read()/4096
print(rc_V)
adc = Pin(D7, Pin.OUT)
adc.value(1) # reset adc pin high so it can begin recharching the RC circuit
return rc_V
If I call read_rc()
a few seconds after booting the voltage measured is around 3.3V, as expected. At this point, if I toggle the power to the board off/on very quickly, I expect to see a >0V reading, however it reads 0V. Why is this? Why does the RC circuit drain so quickly when the board is off? How can I get this RC circuit to drain at the expected rate?
I'm curious to the underlying mechanism behind this behavior in the mcu (what state is the GPIO during power off?), so please do discuss.
Ultimately, I need a simple solution to allow the mcu to "remember" recent power toggles.
Thanks