A question about transistors and an Arduino Uno. Probably a daft one!

Hi all, I’m very new to Arduinos and electronics in general, and I’m currently learning the basics whilst having fun with motors and sensors etc. I really can’t overemphasise how little I know, but I’m keen to learn!

During one of my usual absent minded daydreams a few days ago, my mind wandered onto the subject of battery life, and if I could make an Arduino turn itself off when a task is complete. I came up with an idea and tried it out, and it didn’t work. I just can’t figure out why…

I thought about using an NPN transistor, with the base connected to one of the digital outputs on the Arduino Uno. I thought I could have a 9v battery wired to the collector, and the Vin pin of the Arduino wired up to the emitter. In my head it seemed simple, I would use a push button supply 9v to the Arduino for a few seconds to switch it on, which would then use the coding to set an output pin to high. This would switch the transistor and send the 9V through to the Vin on the Arduino. When the Arduino had completed its tasks it would just set the output to low, switching the transistor off and cutting power to Vin.

In practice all that happened was that the Arduino was only on for as long as I held the push button. If I let go, the Arduino turned off. Obviously my idea didn’t work, but it’s left me wondering WHY it didn’t work. It’s probably something really stupid, but I don’t want to just leave it at “oh well, it didn’t work”. I want to understand why.

Any one care to explain it to a noob please? :slight_smile:

In order for current to flow through a BJT's collector, the base-emitter junction must be forward biased. For an NPN, that means the base must be about 0.6V higher than the emitter voltage. Without going into details too much, that is impossible in the circuit you have described.

More importantly, this is a terrible, horrible, no good very bad day way to extend battery life. The ATmega328P microcontroller used on the Uno board has many options that you can use to shut off peripherals or sleep the CPU to save huge amounts of power without needing to disconnect the voltage source. In the deepest sleep mode (with all peripherals turned off) you can get down to 100 nA, though that's a hard sleep mode to use properly. A more realistic amount is single-digit uAs. Compared to the normal current draw of around 20 mA, that's over 2,000 times less power.

However, an Arduino Uno board has some extra chips on it (primarily the USB->Serial converter and 5V and 3.3V regulators) that will draw over 30 mA of current even if you unplug the main chip. You can't shut those off, they just sit there burning power forever and ever as long as the board's plugged in. Most reasonably-sized batteries will get sucked flat within a few days at most.

Solution: learn to make a "bare bones" Arduino circuit or buy a Pro Mini (and desolder the LEDs). When you learn how to use the power reduction features properly, you can get far more battery life than by just physically shutting off the power.

There's an article in the Useful Links sticky that was written by Nick Gammon, one of the moderators here. He investigated the power savings a lot of those different modes give.

In order that the NPN be switched, the base needs to be around 650mV more positive than the emitter. The AVR chip will be supplied with 5v when the switch bridges the transistor, which of course means the maximum driving voltage from an AVR output pin is just under 5v. But the NPN transistor requires 5.6v or more to maintain bias when the switch is released. You can probably now see why it won't work!

Numerous circuits probably exist on the web to do this - it will require one more transistor at least to act as a low-side switch. This can pull down to ground a pair of resistors connected between it's collector and the 5v line (the transistor emitter is connected to ground / 0volts of course), which act as biasing chain. The main NPN's base is connected to the junction of these two resistors. When the bridging switch is pressed the Arduino is fed with 5v. An output port pin is set HIGH, this drives the second transistor ON (with a suitable current limit resistor from output pin to the transistor's base). The driver transistor pulls current through the bias chain, which biases the power rail transistor ON for when the switch is released. The Arduino is now feed with around 4.4v when the switch is released and base-emitter voltage drop takes effect.

Jim

Edit: please read 9 volts for 5 volts, etc!

When you park your car in a parking lot, do you take the wheels off to prevent it from moving? Or do you just shift the transmission to "P"?

There are often multiple ways to achieve the same objective; some of them are good, others are stupid. A bootstrap power switch like this is almost always the stupid way.

When someone asks a question like this, don't give them tips to help them take off and put on their tires faster. Tell them to use the parking brake. Teach them the right way.

Jiggy-Ninja:
Teach them the right way.

Who decides the right way? You?
With such simple circuit OP can use the Arduino without damaging it as he wans to use it. When he learns more he may use standalone chip.

Smajdalf:
Who decides the right way?

Physics?

Smajdalf:
Who decides the right way? You?
With such simple circuit OP can use the Arduino without damaging it as he wans to use it. When he learns more he may use standalone chip.

"Without damaging it" is a very low bar to clear, and the results you get from this method are still vastly less than what can be achieved with proper power management techniques.

Sleeping is also far more flexible, since you can have wake up sources that do not require manual intervention to restart the chip.

In my quick attempt to show the OP why his idea would not work, and suggest how it be made to function, I of course missed the obvious. Rather than use an NPN transistor as an emitter follower, replace it with a PNP transistor and swap the positions of the emitter and collector. Strap a high value resistor from emitter to base to prevent a floating base. Now a simple resistor of around 5K from the base to a Arduino out port will allow switching. When power is applied by pressing the bridging switch, take that pin low to pull around 1mA through the transistor base. With only a modest hFE the transistor can source at least 100mA power.

There are many other ways. Hopefully the OP will look at these to learn more techniques. I'm not saying any are the best or worst ways to go, but the fun is in the experimentation and learning after all.

Jim.

Thanks everyone. As I said, I'm very new to electronics so what sounds like a stupid thing to experienced people doesn't sound so stupid to someone who only learned what a capacitor is two weeks ago...

I didn't actually plan to use my (bad) method in a project, I was just curious as to IF it would work, and then WHY it wouldn't.

Jiggy-Ninja:
In order for current to flow through a BJT's collector, the base-emitter junction must be forward biased. For an NPN, that means the base must be about 0.6V higher than the emitter voltage. Without going into details too much, that is impossible in the circuit you have described.

Brilliant, this answers my question, thank you :slight_smile:

Jiggy-Ninja:
More importantly, this is a terrible, horrible, no good very bad day way to extend battery life. The ATmega328P microcontroller used on the Uno board has many options that you can use to shut off peripherals or sleep the CPU to save huge amounts of power without needing to disconnect the voltage source. In the deepest sleep mode (with all peripherals turned off) you can get down to 100 nA, though that's a hard sleep mode to use properly. A more realistic amount is single-digit uAs. Compared to the normal current draw of around 20 mA, that's over 2,000 times less power.

However, an Arduino Uno board has some extra chips on it (primarily the USB->Serial converter and 5V and 3.3V regulators) that will draw over 30 mA of current even if you unplug the main chip. You can't shut those off, they just sit there burning power forever and ever as long as the board's plugged in. Most reasonably-sized batteries will get sucked flat within a few days at most.

Solution: learn to make a "bare bones" Arduino circuit or buy a Pro Mini (and desolder the LEDs). When you learn how to use the power reduction features properly, you can get far more battery life than by just physically shutting off the power.

There's an article in the Useful Links sticky that was written by Nick Gammon, one of the moderators here. He investigated the power savings a lot of those different modes give.

Thanks, I've found the tutorial and will have a read.

james-eton:
In order that the NPN be switched, the base needs to be around 650mV more positive than the emitter. The AVR chip will be supplied with 5v when the switch bridges the transistor, which of course means the maximum driving voltage from an AVR output pin is just under 5v. But the NPN transistor requires 5.6v or more to maintain bias when the switch is released. You can probably now see why it won't work!

Numerous circuits probably exist on the web to do this - it will require one more transistor at least to act as a low-side switch. This can pull down to ground a pair of resistors connected between it's collector and the 5v line (the transistor emitter is connected to ground / 0volts of course), which act as biasing chain. The main NPN's base is connected to the junction of these two resistors. When the bridging switch is pressed the Arduino is fed with 5v. An output port pin is set HIGH, this drives the second transistor ON (with a suitable current limit resistor from output pin to the transistor's base). The driver transistor pulls current through the bias chain, which biases the power rail transistor ON for when the switch is released. The Arduino is now feed with around 4.4v when the switch is released and base-emitter voltage drop takes effect.

Jim

Edit: please read 9 volts for 5 volts, etc!

Cheers for explaining that, although I now know it's a terrible way to do it, at least I understand how it would work :slight_smile:

Jiggy-Ninja:
When you park your car in a parking lot, do you take the wheels off to prevent it from moving? Or do you just shift the transmission to "P"?

There are often multiple ways to achieve the same objective; some of them are good, others are stupid. A bootstrap power switch like this is almost always the stupid way.

When someone asks a question like this, don't give them tips to help them take off and put on their tires faster. Tell them to use the parking brake. Teach them the right way.

Being British and driving a manual, the "right way" is to take it out of gear and put the hand brake, haha :stuck_out_tongue:

Smajdalf:
Who decides the right way? You?

Experience and common sense. But you can only apply common sense when you have wide ranging understanding, ie experience.

cuffbertt:
Thanks everyone. As I said, I'm very new to electronics so what sounds like a stupid thing to experienced people doesn't sound so stupid to someone who only learned what a capacitor is two weeks ago...

Ignorance is not a crime, so don't be afraid to ask questions. You've learned something from this.

Cheers for explaining that, although I now know it's a terrible way to do it, at least I understand how it would work :slight_smile:

I will clarify that this only really applies to trying to self-switch the Arduino's power. It is perfectly fine to use transistor switches to control power to other parts of the circuit that are not needed, such logic chips that don't have EN or SHDN pins, or sensors and analog circuitry that don't need to be constantly active.

For digital chips that have configurable sleep modes though, it is usually better to use those than a transistor switch. It saves you the extra pieces of hardware needed for the switch and potential level-shifting circuitry.

Practice reading datasheets. For any new component you try to use, go to the manufacturer's website and find the datasheet, or search google for "<chip_number> datasheet". For example, the Uno's chip is the ATmega328P made by Atmel (bought by Microchip a while ago), datasheet here. Every datasheet will usually have an intro that explains the purpose of the chip, and 3 other sections: Absolute Maximum Ratings, recommended Operating Conditions, and Typical Characteristics. The rest of the datasheet depends on the specific chip and will basically deal with how to use and operate the chip. Necessary formulas and calculations are usually included, as well as example circuits and in some cases even a recommended layout (if it's a high-speed chip that's sensitive to component placement).

Others things you can find on manufacturers' websites are things like Application Notes and Design Notes. These are gold mines. A lot of them might deal with how to use some obscure feature of one of their proprietary chips, but occasionally you find amazing ones like TI's 51 page Op Amp Circuit Collection that's full of example circuits for the most important and fundamental component in analog circuits. Keep those and treasure them.

cuffbertt:
Cheers for explaining that, although I now know it's a terrible way to do it, at least I understand how it would work :slight_smile:

I must admit my real experience is limited - I am "only hobbyist" but still I don't think it is so terrible way. It is quick and dirty - but whole Arduino thing may be considered quick and dirty. In fact most of EE is not about the most efficient way considering material resources but about most efficient way considering ALL resources. You understand and can use this solution NOW? Does it work as you wish? Then it is GOOD solution! You may find better one. Some day. After some effort. Is it worth it? Who knows...
When you use the transistors to cut off Arduino's power you lose some of it's functionality - most importantly possibility of wake up when some conditions are met. If you use bare chip you lose another functionality - USB communication, voltage regulators, onboard LEDs, filtering capacitors, ceramic resonator... It may take a lot of effort to replace all of this.

Smajdalf:
When you use the transistors to cut off Arduino's power you lose some of it's functionality - most importantly possibility of wake up when some conditions are met. If you use bare chip you lose another functionality - USB communication, voltage regulators, onboard LEDs, filtering capacitors, ceramic resonator... It may take a lot of effort to replace all of this.

Good summary. Yes the ability to wake on an interrupt is the big plus of using sleep modes instead of hardware cutting the power.

Having said that, there's absolutely no reason why completely cutting the power can't work if done correctly. The application might be a little bit more limited, as you need an external device or manual switch to wake it, but I can see possible usage where something just needs to complete a given task and then power itself down when done.

I've never used it on the Arduino, but I've definitely built power down circuits like this before, and they work.

BTW. The following are the sleep mode supply current (from Vin) I've measured on my UNO and (NANO) boards.

Full power : 25 mA (25 mA)
SLEEP_MODE_IDLE 18 mA : (17 mA)
SLEEP_MODE_PWR_DOWN : 9.2 mA (8.2 mA)

So about 37% (UNO) of full load power (and 33% for NANO) for the maximum power saving mode on a complete board. So fair power savings, but nothing close to completely cutting power.

If anyone is interested in the actual circuits, it's pretty much been summarized in various post above, but below are actual circuits I've built and tested in the past (not with the Arduino but other devices).

BTW. I usually I use a P-Mosfet for the main switch device (top device in the diagram), but that has been for higher power applications. So for this example I swapped that for the very low cost BC327, as it will do the job.

A couple of things that can be added to the above is an option to wake on digital pulse from external device, and an optional bootstrap (+ive feedback) resistor to allow it to latch on immediately (instead of having to hold the push button for a few seconds until it gets through the bootloader code).

Be aware however that if you want to use the bootstrap option that you may need to vary the resistor value (150k in the above example) depending on the supply voltage you're using (I was using 12 volts with the above example).

stuart0:
Having said that, there's absolutely no reason why completely cutting the power can't work if done correctly. The application might be a little bit more limited, as you need an external device or manual switch to wake it, but I can see possible usage where something just needs to complete a given task and then power itself down when done.

Adafruit TV-B-Gone has a nice way of doing it. The code does what it needs to (send the IR commands to turn off TVs), then goes into PWR_DOWN mode with no wake up sources enabled. How does it wake up then, you ask?

The button is connected to RESET. The reset signal is a (JARGON ALERT!) nonmaskable interrupt on probably the vast majority of microcontrollers, AVRs included. It cannot be turned off in code like other interrupts can. It overrides everything.

BTW. The following are the sleep mode supply current (from Vin) I've measured on my UNO and (NANO) boards.

Full power : 25 mA (25 mA)
SLEEP_MODE_IDLE 18 mA : (17 mA)
SLEEP_MODE_PWR_DOWN : 9.2 mA (8.2 mA)

So about 37% (UNO) of full load power (and 33% for NANO) for the maximum power saving mode on a complete board. So fair power savings, but nothing close to completely cutting power.

That is not an argument in favor of self-switching power. That's an argument in favor of not using a development board as part of the final assembly. They're good for development (hence the name), but all the extra crap that makes development easy is wasteful when you're running on battery. Get an ISP shield (about $15 I think) and there's not excuse for a barebones system being "too complicated". Download one of the cores that supports setting fuses for the internal oscillator and you don't even need to bother with an external crystal.

Or get cheap Pro Mini clones from ebay with an FTDI board. All the convenience of programming with the bootloader, and almost no wasted power.

Jiggy-Ninja:
That is not an argument in favor of self-switching power. That's an argument in favor of not using a development board as part of the final assembly.

Yes I know, but there are plenty of hobbyists on these forums that are going to use development boards most of the time. It also illustrates the advantage that cutting the power externally can power down entire susb-systems, not just the Arduino itself.

Basically I'm just saying don't completely rule it out, it can have it's place. :slight_smile:

stuart0:
Basically I'm just saying don't completely rule it out, it can have it's place. :slight_smile:

In a few limited circumstances maybe, but to me it usually reads like someone asking "how do I check for button presses when I'm delay()ing all over the place" and people answer by teaching them how to use attachInterrupt instead of teaching how to not use delay()! Delay() sucks. Switching power this way sucks too, and doesn't even provide the best results.

Incidentally, why are your current measurements so much lower than Nick's?

Jiggy-Ninja:
not use delay()! Delay() sucks. Switching power this way sucks too, and doesn't even provide the best results.

Not really a fair analogy in my opinion. Two totally different things.

Jiggy-Ninja:
Incidentally, why are your current measurements so much lower than Nick's?

Not really sure. I see Nick measured about 50 mA versus my 25 mA on the UNO. Could be that mine is a clone using the ch340 usb chip. It also has the 328p micro controller in the smaller surface mount format. I'm not sure if that makes any difference, though it could be a newer revision of the chip. I also had the inbuilt LED off, so a few more mA saved there.

I had lot of voltage overhead (12V supply) so I tested it with a largish 56 ohm series resistor, the results should be fairly accurate. I did only use a multimeter DC voltage measurement to deduce average current, but that usually works well enough.