I want to know when the batteries of a model are getting low.
The 5 AA batteries are over 5 volts so I cannot check the end voltage.
I make a tap after the second AA battery and take it to pin A0.
Unfortunately a meter shows a voltage of 2.39 but A0 shows 1.18!!
What is the correct way to get a real reading?
void doshowvoltage()
{
adcValue = analogRead(A0); // Convert analog of pin A0 to digital
voltage = adcValue * (5.0 / 1023.0); // Calculate voltage according to digital
Serial.println(adcValue);
Serial.println(voltage);
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
matrix.textFont(Font_4x6);
matrix.beginText(0, 1, 0xFFFFFF);
matrix.println(voltage);
matrix.endText(0);
matrix.endDraw();
}
2.39V is pretty low... are the batteries empty?
Or is a large motor running off the batteries?
How do you feed the battery voltage into arduino?
Via 5V pin or via vin?
If via vin, did you check the 5V pin? It should be at 5V...
Yes I know the batteries are getting low hence the reason I wanted a checking system.
I use Vin plug and socket to power up the Arduino - the current voltage is 6.101V at the plug and 5.065 on the board.
Then it is not equivalent to a Uno WiFI.
Was there a special board package/core you were supposed to download and install?
Which board do you have selected from the board manager?
What processor is on the board?
Maybe show a picture of the board.
From the Arduino documentation:
analogReference(type)
Arduino megaAVR Boards (Uno WiFi Rev2)
DEFAULT: a built-in 0.55V reference
INTERNAL: a built-in 0.55V reference
VDD: Vdd of the ATmega4809. 5V on the Uno WiFi Rev2
INTERNAL0V55: a built-in 0.55V reference
INTERNAL1V1: a built-in 1.1V reference
INTERNAL1V5: a built-in 1.5V reference
INTERNAL2V5: a built-in 2.5V reference
INTERNAL4V3: a built-in 4.3V reference
EXTERNAL: the voltage applied to the AREF pin (0 to 5V only) is used as the reference
For measuring batteries, it would be best to use the Arduino's interval voltage reference. Using Vdd, which is the default, won't give an accurate reading when the batteries get low.
The interval voltage reference might be 1.1V or some other voltage depending on the model of Arduino you are using.
You then need a voltage divider to reduce the battery voltage down to less than 1.1V (or whatever for your Arduino model).
If you want to measure, for example, up to about 8.5V and the interval reference voltage is 1.1V, then you could make a voltage divider using a 10K and a 1.5K resistor.
You could then calculate the voltage like this:
voltage = adcValue * (8.44/ 1023.0); // Calculate voltage according to digital
The 8.44V is calculated like this:
1.1V * (10K + 1.5K) / 1.5K = 8.44V
There is a difference beteen the Uno WiFi and the Uno R4 Wifi but in any case if you set the reference to AR_DEFAULT as @kmin suggested it should read correctly.
The only thing I can think of is to add analogReadResolution(10) to setup to make sure it's set to 10 bits