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?
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?
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?
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.