It works!
The initial goal was to have a simple push button to
- power up a teensy
- let the application that's running on the teensy know if the button is pressed at runtime
- let the application itself turn off power when necessary
My breadboard is populated with:
- a #LiFePO4wered/18650
- this power button
- a pololu 5V step up/down regulator
- and a Teensy 3.6 with VUSB cut, so it's powered via VIN from the pololu regulator.
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:
- Once the board is powered up, the button is still pressed. Before waiting for the long power-off button press, setup() waits until the button is released again
- When the long button press has been detected, it waits until the button is released again before de-asserting keep_alive. Otherwise, the circuit would power up again as the button is still pressed, and enter an endless on-off loop.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.
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
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