Wanted features:
- one button to enable power supply to a circuit (which has an MCU in it which controls power afterwards)
- keep alive signal which is active (high or low) as long as power should be on
- sense output so that the MCU can sense the button's state and turn itself off when told so
- supply: probably a single LiFePO4 battery, maybe a higher supply voltage is needed
- no specialized parts: passives, transistors and diodes are ok
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_BUILTINboolpower_button_pressed(){
return !digitalRead(SENSE_PIN);
}
voidsetup(){
// 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");
}
voidloop(){
staticbool power_wait_off = false;
staticconstuint16_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;
}
elseif(power_wait_off && (!power_button_pressed()))
{
Serial.println("Aborting power button timeout");
power_wait_off = false;
}
elseif(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.
I also tried to modify the circuit to make it latch by itself. It just needed a strong enough pullup resistor (I had 2k7 at hand) from Q2's base to Vbat_out and once the button was pressed, power stayed on by itself.
Next: Test with a Teensy that can shut itself off with this circuit, because that's waht I had in mind when I started this project.
There's only one modification compared to the schematic in the previous log: A cap between Q1's gate and Vbat_in. When the button is pressed, this cap will be charged and keep the mosfet open should the switch bounce.
One of the headers has the same pinout as standard TO-220 MOSFET (G-D-S) so Q1 can be replaced by an external larger transistor, even on a breadboard.
The PCB:
The switch is centered so the board won't lean over when pressing the switch while breadboarded.
A pack of these is ordered from both oshpark and aisler, I'll compare, solder and test them when they're here.
I'll start with this one and write about how I (partially) understand it. It's basically the third schematic from the previous log plus a pull-down resistor for Q2's gate:
Vbat_in goes from 3 to 8 V, allowing me to use one or two LiFePO4 cells in series plus some headroom.
Q1 is a P-channel MOSFET IRLML6401. The allowed V_GS is 8 V so this part is at its maximum rating. I have it in my parts bin mainly for its low gate threshold voltage, a feature which doesn't come into play here. The gate is pulled high by R1, so it's not conducting if not actively pulled down by Q2 or SW1
SW1 is used to enable power to the regulator and subsequently the MCU.
Q2 is an N-channel MOSFET IRLML6344. V_GS,th is 1.1 V (max) so it should work well with low-voltage logic input from the keep alive input. The gate is pulled low so it will not conduct if not pulled high actively.
D1 and D2 (both SB120) because I had those at hand) make sure that, while Q2 is pulling down Q1's gate, the switch state can still be determined by an MCU connected to the sense output.
As soon as SW1 is pressed and the MCU is supplied with power, the MCU has to pull up Q2's gate to keep power enabled. The application can use the sense signal to get input from the push button. If, for example, the button is pressed for more than 1 second, the MCU can run some cleanup code and let go of the keep alive signal.
So much for the theory as I understand it. If you spot any serious flaws in this (especially component choice, since the basic circuit seems to be one that has been proven to work) please leave a comment.
I might remove R4. If this is built as a breadboardable pcb, the user can then choose to either pull Q2's base up or down depending on the application's needs.
This one seems to be a bit minimalistic, but doesn't have anything I don't need. There's no "press and hold button to switch off again" for example. OTOH, it doesn't feature a sense output to be read by an MCU.
Next one is again from stack exchange:
Different approach without a shottky in the way when compared to the previous one
there is a sense output that didn't get the love it needs.
Unnamed R (below the push button) must be matched to battery voltage and whatever should be connected to sense
Q4 is used to use an active "switch off" pulse rather than a constant "keep alive" and it would need a current limiting resistor for the base.
Great project! I like the idea of using only standard components which makes the design more adaptable. For the people looking for the most compact (and expansive) solution, the MAX16054 may be a good option (this is just for inspiration, not as replacement for your project). Thx again!
Great project! I like the idea of using only standard components which makes the design more adaptable. For the people looking for the most compact (and expansive) solution, the MAX16054 may be a good option (this is just for inspiration, not as replacement for your project). Thx again!