Close

Sleep Well!

A project log for Terminal-BASIC programmable calculator

A poket-to-laptop size microcomputer/programmable calculator, based on Terminal-BASIC

ptravptrav 11/12/2020 at 05:300 Comments

Now it was time to move to the power supply and the power management proper.

It has been discovered that none of the cheaper ESP32 boards have sleep power below 150 uA (see the excellent video with Andrew "Guy-With-The-Swiss-Accent" Spiess). The two boards I've tested personally - ESP Dev Module 1 and LoLin - both have the deep sleep power at about 3 mA (3.7 V connected to Raw).


Neither board has the battery discharge protection. The Lolin is capable of charging a LiPo cell, but the current for two 18650 seems insufficient.  Neither board has the battery protection in case the voltage drops below 3.0V, so a separate battery main switch is needed. If you forgot to turn it off... well, the cells will be dead.

So came the proposed schematics.


The logic is as following:

  1.   The system is initially OFF. The relay U1 is open, U2 (Arduino) is de-energized. The only power consumer is the U3 (CD4096UBC) chip, drawing about 8 uA via 0362a controller. At this state, the system may be connected to a charger through the dedicated USB port of the 0362a.
  2. User presses SW4 (or SW5). U3 closes the relay U1, sending power to Arduino U2, which performs its boot sequence. One of the first actions of Arduino is to declare port A1 as a digital output and set it HIGH.
  3. The high signal from A1 causes U3 via diode D2 to lock the relay U1 in "on" position. If the user releases the power button SW4, the system remains energized. Arduino can check the battery voltage by the port A3, configured for an analog input.
  4. If the user presses the SW4 again, the high signal via diode D2 reaches Arduino port A2, configured as a digital input. At this point, Arduino passes the signal to ESP32, which decides if the shutdown is needed (it may decide to ignore the user, for example if a program is running).
  5. Upon completing the neccesary shutdown sequence, ESP32 commands Arduino to shut-down. Arduino sets A1 to LOW, causing the relay U1 to disconnect. The only remaining power consumer is U3, and the rest of the system is totally off at the source.

If the Arduno program gets locked for some reason, the user can press reset button SW3 (or the optional external SW1). During the reset, the pin A2 momentarily goes low, which causes U3 to disconnect, - and the entire system is off.

If the system is powered via Arduino or ESP32 USB, the power board is protected by diode D4. Pressing the power switch has no effect - the system stays on. The system can be independently charged through 0362a. If the user pulls the USB plug off while the system is on, the power supply should pick up gracefully (not tested yet).

If the system is off, and DS3231 alarm goes off, the effect is the same as if the user presses the power button, only the signal is coming via diode D7 instead of D1. Note that the RT clock has its own small lithium cell.

If the system is on, and DS3231 alarm goes off, the system remains energized.

The power up and shutdown code is as following:

#include <Arduino.h>

class SelfShutdown{
  public:
    void init( uint8_t PortKey, uint8_t PortHold);
    inline bool isPowerPressed(){ return digitalRead(_portKey) == HIGH;};
    void shutdown();
  private:
    uint8_t _portKey = 3;
    uint8_t _portHold = 4;
};

//
// Power button is at PortKey and goes high if pressed
// Solid state relay is attached to PortHold, if held high, battery power is connected
// 
void SelfShutdown::init( uint8_t PortKey, uint8_t PortHold){
    uint8_t _portKey = PortKey;
    uint8_t _portHold = PortHold;
    pinMode( _portHold, OUTPUT);
    digitalWrite( _portHold, HIGH); // Lock yourself in power-on
    pinMode( _portKey, INPUT);
    delay(30);
    while( digitalRead(_portKey) == HIGH)
        delay(30); // suppress power button jitter
}

void SelfShutdown::shutdown(){
    digitalWrite(_portHold, LOW);
    delay(500);
    digitalWrite(_portHold, HIGH); // if at this point we are still alive, must be USB power... 
}

Discussions