Can a Pin hold it's value during deepsleep?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
FaithRaven
Posts: 8
Joined: Thu Dec 26, 2019 6:32 pm
Location: UK

Can a Pin hold it's value during deepsleep?

Post by FaithRaven » Fri Dec 27, 2019 10:09 pm

Basically all I'm trying to do is keep a LED on while the board is sleeping.

I've came across https://github.com/micropython/micropython/pull/4561 but I have no idea what to make of it.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Can a Pin hold it's value during deepsleep?

Post by pythoncoder » Sat Dec 28, 2019 10:48 am

As I understand that GitHub thread, if you specify hold, the pin will maintain its value during deepsleep. However its drive capability in this mode is likely to be minimal, so you'd need an external mosfet and series resistor to drive an LED from such a pin.

Note I haven't tested this - the above is based on reading that thread with a general knowledge of electronics.
Peter Hinch
Index to my micropython libraries.

paulg
Posts: 29
Joined: Fri Oct 23, 2015 1:06 pm

Re: Can a Pin hold it's value during deepsleep?

Post by paulg » Sat Dec 28, 2019 12:35 pm

The TinyPICO board uses a PNP transistor to control power to an onboard Dotstar RGB LED. The schematic is on Github here: https://github.com/tinypico/tinypico-ha ... ematic.pdf

Seon put the code to do this into a helper library. The source code is on Github here: https://github.com/tinypico/tinypico-mi ... inypico.py

Hope this helps.

FaithRaven
Posts: 8
Joined: Thu Dec 26, 2019 6:32 pm
Location: UK

Re: Can a Pin hold it's value during deepsleep?

Post by FaithRaven » Sat Dec 28, 2019 1:49 pm

Thanks, but I'm using an Adafruit Feather: htps://cdn-learn.adafruit.com/assets/assets/000/041/630/original/feather_schem.png?1494449413

Does it have anything similar?

FaithRaven
Posts: 8
Joined: Thu Dec 26, 2019 6:32 pm
Location: UK

Re: Can a Pin hold it's value during deepsleep?

Post by FaithRaven » Mon Dec 30, 2019 3:43 pm

I've figured it out. Pin.PIN_HOLD does the job, but it's usage is a bit confusing at first.

There are three things to keep in mind:
- if an GPIO value has been held previously, you can read it's current value only by creating the Pin with pull=machine.Pin.PULL_HOLD
- in order to be able to update the GPIO value you need to re-create the Pin with pull=None explicitly
- the newly assigned GPIO value can be held by re-creating the Pin with pull=machine.Pin.PULL_HOLD

Here's a working example:

Code: Select all

def set_led_value(pin_addr, desired_value):
    led = machine.Pin(pin_addr, machine.Pin.OUT, pull=machine.Pin.PULL_HOLD)

    current_value = led.value()

    if current_value != desired_value:
        led = machine.Pin(pin_addr, machine.Pin.OUT, pull=None)
        led.value(desired_value)
        led = machine.Pin(pin_addr, machine.Pin.OUT, pull=machine.Pin.PULL_HOLD)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Can a Pin hold it's value during deepsleep?

Post by pythoncoder » Tue Dec 31, 2019 7:01 am

That is useful information. In deepsleep what is the drive capability of the pin? Do you have any information or measurements?
Peter Hinch
Index to my micropython libraries.

FaithRaven
Posts: 8
Joined: Thu Dec 26, 2019 6:32 pm
Location: UK

Re: Can a Pin hold it's value during deepsleep?

Post by FaithRaven » Fri Jan 03, 2020 12:47 pm

3.3V and 1.13 mA. Does that make sense? I'm still getting to grips with my multimeter and my electronics knowledge.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Can a Pin hold it's value during deepsleep?

Post by pythoncoder » Sat Jan 04, 2020 8:26 am

It sounds plausible and backs up my suggestion of using a MOSFET or other device if you want to drive an LED - you'll probably want more current for reasonable brightness.
Peter Hinch
Index to my micropython libraries.

FaithRaven
Posts: 8
Joined: Thu Dec 26, 2019 6:32 pm
Location: UK

Re: Can a Pin hold it's value during deepsleep?

Post by FaithRaven » Sat Jan 04, 2020 12:10 pm

Hmm, the 5mm Blue diffused led is quite bright tbh. I've set it with a small resistor, 470 Ohm.

User avatar
tve
Posts: 216
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: Can a Pin hold it's value during deepsleep?

Post by tve » Sun Jan 05, 2020 1:15 am

All I/O pins capable of output have a hold function (there are some input-only pins).
The hold function is useful during reset, reprogramming, and sleep.
Only the pins in the RTC power domain can hold during deep sleep because the cpu power domain, which includes the "digital I/O" pins, is turned off. Those latter pins continue the hold when power comes back on.
The hold function uses the normal output transistors, so the current capability is the same as during normal operation.
The esp32 pins can source and sink plenty of current to power a signaling LED, no need for an external FET (unless you attach a bright LED to every pin...).
Pro tip: it's worth looking into the datasheet, technical reference manual, and ESP-IDF docs... ;)

Post Reply