voltage divider function works only when arduino ic connected to computer

Hello,
I want to know when my battery volt is going below 29 volts and turn the red led on.
So I connected a voltage divider to the arduino analog pin.
The problem is that it works only when the arduino is connected by usb to the computer, I see in the serial monitor that the volt is 28 and the red led is on, but immediately when disconnecting it from usb the red led is off and green led is on.
What is the problem, and what influence does the computer have on arduino?

Thank you!

The function code is below:

#define Red_Led 9
#define Green_Led 13

#define Z1 22000
#define Z2 3600

pinMode(Red_Led, OUTPUT);
pinMode(Green_Led, OUTPUT);

void battery_check()
{

Vout = analogRead(5) * (5.0/1023.0);
Vin = ((Z1 + Z2)/(Z2) )* Vout;

Serial.println(Vin); /////prints battery volt

if (Vin<29)
{

digitalWrite(Red_Led, HIGH);
digitalWrite(Green_Led, LOW);
}

else if(Vin>=30)
{

digitalWrite(Green_Led, HIGH);
digitalWrite(Red_Led, LOW);
}

}

Hi,

How have you got your arduino powered without the USB connected?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your complete sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom… :slight_smile:

Hi,

Sounds as though, however you are powering the Arduino when disconnected from the PC, the supply voltage is slightly different. The problem is this line in your code:

Vout = analogRead(5) * (5.0/1023.0);

This assumes the supply voltage is exactly 5.0V, which is probably not exactly correct on either USB power or your other supply. For example one might be 5.1V and the other 4.9V. Do you have a multimeter to measure them?

There is a better way. The Arduino has a built-in voltage reference which you can use to compare with your battery voltage. This means that the Ardino's supply voltage will not affect the reading. What kind of Arduino are you using?

Read here about analogReference().

You will need to change your voltage divider to get the battery voltage down to less than the internal reference voltage.

Paul

How is your potential divider connected up?
Is it relying on having the USB connected in order to get it's GND connection?

I have found that analogue measurement are often more accurate when the Arduino is powered from an external supply. This is due to the output of the voltage regulator being very close to 5.0V, whereas the voltage from a USB port can be anywhere between 4.75V and 5.25V.

I know that this isn't the answer to your problem, and will only make a 0.1% difference, but you should divide by 1024, not 1023 when calculating the voltage.
(You don't divide by 99 when you calculate a percentage do you?)

JohnLincoln:
I know that this isn't the answer to your problem, and will only make a 0.1% difference, but you should divide by 1024, not 1023 when calculating the voltage.
(You don't divide by 99 when you calculate a percentage do you?)

Ah, but does the ADC give a 1024 count when you apply 5V?

Or - exactly what voltage corresponds to a count of 1023?

Hi,
ADC gives 11 1111 1111 count at 5V = 1023

Tom.... :slight_smile:

The maximum count is indeed 1023, but there are 1024 different values.
You have to include zero as a possible value.

If you look in the ATmega328P datasheet, you will find on page 261 :

23.7 ADC Conversion Result
After the conversion is complete (ADIF is high), the conversion result can be found in the ADC
Result Registers (ADCL, ADCH).
For single ended conversion, the result is

VIN ⋅ 1024
ADC = ------------------
VREF

where VIN is the voltage on the selected input pin and VREF the selected voltage reference (see
Table 23-3 on page 262 and Table 23-4 on page 263). 0x000 represents analog ground, and
0x3FF represents the selected reference voltage minus one LSB.

The problem with the internal reference is although it is stable and independent
of the actual supply voltage, it has a big spread (ie different chips will have different
reference voltages due to manufacturing variation) The datasheet says this is
about 10% spread IIRC, which means you will have to calibrate the reference for
your Arduino and store it in EEPROM or something.

The on-board regulator is probably accurate enough.

USB supplies are much more varied and hard to predict in practice due to losses in the
thin wires in typical USB leads and sharing the power via hubs and so forth.

USB power is usually very noisy too.

The upshot of the previous comments is that it makes absolutely no difference whatsoever whether you divide by 1023 or 1024.

However, division by 1024 is far simpler.