4 nimh Battery charging through arduino

As a little side function of my robot, i wanted to be able to charge
4 AA 1.2volt 2100 mAh nimh batteries in series. I'm using this to power my Arduino, as well as run 2 motors, so they are eaten up quickly. I was wondering if i could use analogRead to measure the battery capacity. I know there are other people who asked about this, but i couldn't find a definitive answer from any of the other posts.

Yes, you can read the voltage on the battery pack.
If you want to use 4 analog inputs, you could measure all 4.
Or use 2 digital bits to control an analog multiplexer and take turns reading them with a single analog pin.
Or use an external A/D converter and bring in the voltage information serially.
Lots of options.

I'm using this to power my Arduino, as well as run 2 motors, so they are eaten up quickly. I was wondering if i could use analogRead to measure the battery capacity.

Well you can but it will be a meaningless reading in that it will measure they are always fully charged? How could that be? Well if you are powering your Arduino with the 4 AA batteries in series then they becomes the Vcc for the processor chip which by default is also the reference voltage for the analog to digital function on the chip. So by reading the battery via a analog input pin the resulting measurement will always be 1023 counts regardless of the actual voltage of the cells. They will remain true until the battery voltage gets so low that the chip stops functioning. Pretty cool huh?

So what can you do? Well you can read up on the A/D section of the datasheet for the processor. You can consider using internal or external regulated A/D voltage references. There are ways around it but it's best that you try and understand the how and why as there will be various trade-off decision of costs Vs component count, etc.

Lefty

Actually, i planned on having a spdt switch, or a latching relay to the circuit to choose between charging, and operation when plugged in to another powersource.

Actually, i planned on having a spdt switch, or a latching relay to the circuit to choose between charging, and operation when plugged in to another powersource.

What does that have to do with the ability to be able to measure the battery voltage via the Arduino accurately?

Lefty

I have an Arduino Pro Mini 3.3v based LCD remote control which runs off NiMH and includes a small header at the side where I can plug in a power source to charge the battery. The battery feeds Vraw on the Pro Mini so it uses it's own Vref for ADC that's generated from the onboard regulator. The charging power source goes through a LM317 to provide constant current and I control charging using a transistor which turns the 317 on and off. The remote control carries on working while being charged. To measure voltage I use a resistor divider 100k/100k. The Pro Mini controls the charging using 3 stages.

@retrolefty

Basically, i wont be using the batteries that are being charged while reading the battery level.

Basically, i wont be using the batteries that are being charged while reading the battery level.

Understood. However the more fundemental problem is that how the analogRead() function is setup by default you will not be able to read the true battery voltage if it is also directly being used to power the +5v bus of the Arduino, it will always read out as 1023 counts, never change even though the battery voltage is decreasing over time. You will have to read up on alternate voltage references and using resistor voltage dividers. Lots of info on this forum if you search around.

Lefty

ok thanks.. BTW, why would the arduino be reading a decrease of battery level when charging? im only going to use the arduino as a charger, i won't be monitoring the battery level when its in use. how do phones and other devices read their battery level?

how do phones and other devices read their battery level?

By using a A to D reference voltage that does not change with battery voltage.

Lefty

How could one take advantage of the 1.1V reference internal to the ATMega?

How could one take advantage of the 1.1V reference internal to the ATMega?

Simple by using the internal 1.1v reference then one can use an external 2 resistor voltage divider to cut the battery voltage down such that maximum battery voltage is measured as say 1vdc. Software can be used to scale that to any internal value you wish, or just treat it as 1v = 100%. The 1.1v reference will not change as the battery (therefore Vcc) slowly drops as the battery discharges.

Make sense?