I've constructed a little hand held measuring device. Everything is working fine. As I'm improving the software I've had the idea it would be nice if I could display the battery status.
Problem is: I can't think of a way to detect whether the Arduino runs on battery or it is plugged in via USB/FTDI. That would be necessary because it is kinda stupid to display a low battery warning when no battery is required.
Maybe there's something fairly easy that I don't see...
Please help!
spacko:
I've constructed a little hand held measuring device. Everything is working fine. As I'm improving the software I've had the idea it would be nice if I could display the battery status.
Problem is: I can't think of a way to detect whether the Arduino runs on battery or it is plugged in via USB/FTDI. That would be necessary because it is kinda stupid to display a low battery warning when no battery is required.
Maybe there's something fairly easy that I don't see...
Please help!
The most common method used is to wire a two resistor voltage divider from the Vin and a ground pin. Then wire from the junction of the two resistors to a analog input pin. Then performing a analogRead() command in your sketch you can 'measure' the actual battery voltage or read a 0 if only being powered from USB connector.
Also important to think about what your sketch should or can do if the measurement says the battery is low?
Lefty
Lefty,
thank you for your help.
Yes, I was thinking of applying exactly the solution you are suggesting. I am using a 16 digit 2 lines LCD display, so all I wanted was a blinking "BAT" in the upper left corner when the voltage sinks below a certain level. I'd rather have the "BAT" disappearing when external (USB) power is applied, even with the battery inserted. That's not possible, is it?
Also, I am thinking when running on battery as it gets lower the reference voltage of the internal ADC might be different from the ref voltage when I apply USB. I'm using a LP2950 to regulate a 9V block down to 5V. I have not tried out (I'm using an external ADC for measuring) , but I want to make sure to have the same battery level displayed on USB and on battery, if the preferred solution will not work.
Steffen
Why wouldn't you want to display the low battery when the batter is low even if
currently not using the battery?
If you don't you won't ever know the battery needs replacing, until
you really need it.
As an alternative, to "low bat" what about having a "low power" indicator instead?
The indicator lights if the supply voltage to the AVR is below some
threshold regardless of the powersupply.
You can use the internal reference to measure the supply voltage for the AVR
with no added components.
If the voltage is lower than some threshold then put up the "low power" indicator.
--- bill
You can divide the source voltage down and use internal 1.1V ADC reference, that should be steady no matter what the source voltage is.
As to what source is being used, if you have an Uno with auto switching, then you can jumper in the comparator output to tell you whether Vin/2 is > 6.6V and external power is being used, or if i's less and USB port power is being used.
I am sorry for replying so very late, but due to unforseen circumstances I had to leave for a couple of days, no time for Arduino.
Thank you for you input.
I think I've found a solution - a pretty simple one. I am not using a stock Arduino - out of curiosity I built my own one based on a Nano. I'ts got the same output pins but the board design is completely different. Advantage is I can get to the USB lines a little easier.
So what I'm doing measuring the USB + pin between the socket and the FTDI by bridging that and an Arduino pin. If there's Voltage, power is connected. Does not look elegant but it works.
To Retrolefty: can you please post a description of the two-resistor voltage divider wiring? I am a bit of an electronics newbie.
USB 5v - Arduino input 5v.
Wire from the USB to a logic input on the Arduino and check this pin. High will mean USB powered low will mean battery powered.
Fould start at the USB fuse and check if it is only high when powered by USB and not when you hook the battery in.
To be safe i would check the battery at least every time you start up no matter what source you use as batteries do self discharge.
Like a startup check of all systems.
First thing to do wouls be google the Arduino Circuit Diagram.
rhj4: Forum courtesy asks that you don't hitchhike on someone else's question, even if your question is related. Start your own thread! This way people will notice that you are asking a question, and comments to the OP are not confused with comments to you.
rhj4:
To Retrolefty: can you please post a description of the two-resistor voltage divider wiring? I am a bit of an electronics newbie.
If you google the words voltage divider you will get links to pictures, explanations and videos very quickly.