Close

Great Success!

A project log for Soft power button with sense output

One of my designs needs a soft power button with sense output. I'm posting my design here to be reviewed and improved.

christophChristoph 11/18/2017 at 21:062 Comments

It works!

The initial goal was to have a simple push button to

My breadboard is populated with:

The basic routing is battery -> power button -> reg -> teensy, and the teensy keeps an eye on the power button's sense output and controls the keep_alive signal (active high):

And here's the sketch:

#define SENSE_PIN 34
#define KEEP_ALIVE_PIN 33
#define LED_PIN LED_BUILTIN

bool power_button_pressed()
{
  return !digitalRead(SENSE_PIN);
}

void setup() {
  // put your setup code here, to run once:
  pinMode(SENSE_PIN, INPUT_PULLUP);
  pinMode(KEEP_ALIVE_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, 1);
  digitalWrite(KEEP_ALIVE_PIN, 1);
  while(power_button_pressed());
  Serial.println("Starting up");
}

void loop() {
  static bool power_wait_off = false;
  static const uint16_t power_off_time = 2000;
  static elapsedMillis power_off_timer;

  if((!power_wait_off) && power_button_pressed())
  {
    Serial.println("Starting power button timeout");
    power_wait_off = true;
    power_off_timer = 0;
  }
  else if(power_wait_off && (!power_button_pressed()))
  {
    Serial.println("Aborting power button timeout");
    power_wait_off = false;
  }
  else if(power_wait_off && (power_off_timer > power_off_time))
  {
    digitalWrite(LED_PIN, 0);
    Serial.print("powering down, let go of that button.");
    while(power_button_pressed());
    delay(500);
    digitalWrite(KEEP_ALIVE_PIN, 0);
    while(1); // loop until power is gone
  }
}

 The sketch not only implements the "sense and keep_alive thing", but also takes care of two caveats:

Discussions

Taiwo wrote 01/11/2020 at 09:32 point

Cool project. Thanks for sharing. 

I would like to know if this circuitry would allow the battery to charge if I make Vout greater that Vin.

  Are you sure? yes | no

Christoph wrote 01/12/2020 at 00:21 point

As we discussed in chat, I think that basically current can flow from VBat_out to VBat_in, but not really for the purpose of charging since the body diode's voltage drop will mess with the charger's voltage measurement. My breadboard setup does have a charger too, but it's part of the "battery" and operating on the VBat_in side.

  Are you sure? yes | no