Power circuit for the movement alarm project: turn on by hardware, off by software

A key part of the bag movement alarm project that I have started is that you can hit a button to turn the board on, but hitting the button again does not turn it off.  This is to prevent the thief from being able to turn the alarm off by simply hitting the on/off switch.  I also wanted to have it so that the device would completely power off when not in use, not in a sleep mode or similar waiting for the arm button to be pressed, so the battery doesn’t run out too quickly.

Essentially, I want to have it turned on by hardware, but off by software.

In order to implement this I decided to make a power circuit where the power from the battery would flow to the board by one of two ways:

1) a button

2) a transistor, which is controlled by digital pin on the microcontroller

The basic schematic is below: (the 7 is just because I used that pin when testing the circuit)

power circuit whiteboard schematic

The transistor that I used for experimenting with this circuit is an N-channel power MOSFET transistor, that I got from Adafruit here: https://www.adafruit.com/products/355?&main_page=product_info&products_id=355

This transistor is way over kill for this project, but it was what I had on hand.  I imagine I can replace it with a ZVN2110A, which I can get from Mouser for less than a dollar.  I anticipate relatively low current needs for this project, so that transistor should be able to handle it.

The big problem was getting the MOSFET to stay off when there should be no power going to the Uno board.  I set up the circuit with the button that worked fine (board was off until I pressed it, and then it went off when I released the button).  Then I added the MOSFET, with the source tied to ground, the gate tied to ground through a 1k resistor, and the drain tied to the output of the button which was in turn connected to the ground on the Uno board.  I then had a jumper from the gate loose to test with (green jumper in the pic below):

power circuit on breadboard floating gate

When I put the green jumper from the gate to the ground, the Uno stayed off, as expected.  When I put it to the +5v rail, it turned on, as expected.  But when I touched it to any pin on the Uno board, it turned the board on, which was unexpected.  You can see the issue below.

power circuit on breadboard with transistor gate open when it shouldnt

I expected the pins on the board would normally be a 0V when the board had not power, but clearly either not or something else is going on.  When I touched the green jumper to the drain the board also turned on, so there may be something about the drain being connected to the ground on the Uno board, and then each pin being connected to the ground through a pull-down resistor.  There must be something about connecting the gate and the drain results in the transistor turning on. Either way I needed to solve this as if the circuit couldn’t be normally off while connected to the pin, the power circuit would not work for my project.

The solution was to put some resistance between the MOSFET gate and the Uno board pin.  I had put in a 1k resistor to tie the gate of the MOSFET to ground (which was expected, and designed to keep that part of the transistor circuit normally off), but was not expecting to need to put in resistors between the digital pin on the microcontroller and the MOSFET gate.  After some experimentation, I found that a resistor of between 1.7k and 2.2k did the trick.  So I put in two 2k resistors when I constructed the circuit on the breadboard.

power circuit on breadboard

In the sketch, I have the digital pin that is tied to the MOSFET gate (in my case, pin 7) pulled to HIGH in the setup, so the MOSFET gate opens nearly as soon as the button is pressed.  When the button is depressed, the MOSFET remains on and power continues to flow to the board.  Further presses of the button have no impact.  The circuit stays on until I decide to put the pin LOW, turning off the circuit completely and de-powering the board.  In the test circuit, this occurred 5 seconds after the board turns on, but in the actual project I will program it to do so only when the correct 4-digit code has been punched in.

Finally the code I used for the test of the power circuit is below.

int OnOffPin = 7;

void setup() {
// initialize digital OnOffPin as an output.
pinMode(OnOffPin, OUTPUT);

// initialize digital pin 13 to show when the OnOffPin is high
pinMode(13, OUTPUT);

void loop() {
digitalWrite(OnOffPin, HIGH); // turn the OnOffPin on by making the voltage HIGH
digitalWrite(13, HIGH);
delay(5000); // wait for five seconds

digitalWrite(OnOffPin, LOW); // turn the OnOffPin off by making the voltage LOW
digitalWrite(13, LOW);
delay(5000); // wait for five seconds, but this should never come back on

I also put a short summary of this post on the Hackaday.io project page.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s