0

I'm new to Arduino, and trying to build a project that will measure the voltage and current from two, unconnected, 12v batteries. The present test environment comprises:-

  • a 13.8v power supply (run from the mains (240v)) which is powering two separate voltage dividers;
  • a take-off from one voltage divider going to A0, the take-off to the second going to A3;
  • a connection from Arduino GND to the -ve rail on the bread-board;

The voltage dividers are identical - both comprising 10kOhm and 200kOhm resistors - so I am expecting that an analogRead of A0 and A3 would return similar results, which they don't. A0 is up around the 344 mark, A3 is down around 133. So, the problem is that identical voltage dividers are not giving the same readings from the same input voltage.

Code and results follow, and I would be grateful for any hints, tips and further education!

Code follows

#define NUMBER_OF_SAMPLES 10
int looped = 0;

void setup() 
{
  Serial.begin(57600);  Serial.println("Test sketch starting");
}

void loop() 
{
  int temp1=0;
  int temp2=0;     
  int temp3=0;

  looped++;

  for(int count=0; count<NUMBER_OF_SAMPLES; count++)
  {
    temp1 = analogRead(A0);
    temp3 += temp1;

    Serial.print("temp1 = ");
    Serial.print(temp1);    Serial.print(",");
    delay(200);               
  }   
  Serial.println(temp3);
  temp3 = 0;
  for(int count=0; count<NUMBER_OF_SAMPLES; count++)
  {

    temp2 = analogRead(A2);
    temp3 += temp2;
    Serial.print("temp2 = ");
    Serial.print(temp2);    Serial.print(",");  
    delay(200);
  }
  Serial.println(temp3); 
  Serial.println("");   

  if(looped>NUMBER_OF_SAMPLES)
  {
    delay(500000);
  }
}

And this is the output.

Test sketch starting
temp1 = 345,temp1 = 345,temp1 = 345,temp1 = 345,temp1 = 345,temp1 = 345,temp1 = 345,temp1 = 345,temp1 = 345,temp1 = 345,3450
temp2 = 129,temp2 = 125,temp2 = 137,temp2 = 137,temp2 = 126,temp2 = 136,temp2 = 134,temp2 = 136,temp2 = 132,temp2 = 132,1324

temp1 = 345,temp1 = 345,temp1 = 345,temp1 = 345,temp1 = 345,temp1 = 345,temp1 = 345,temp1 = 345,temp1 = 345,temp1 = 345,3450
temp2 = 132,temp2 = 133,temp2 = 136,temp2 = 137,temp2 = 135,temp2 = 129,temp2 = 132,temp2 = 137,temp2 = 131,temp2 = 130,1332

temp1 = 345,temp1 = 345,temp1 = 345,temp1 = 345,temp1 = 345,temp1 = 345,temp1 = 345,temp1 = 345,temp1 = 344,temp1 = 344,3448
temp2 = 133,temp2 = 134,temp2 = 132,temp2 = 128,temp2 = 133,temp2 = 134,temp2 = 131,temp2 = 125,temp2 = 130,temp2 = 129,1309

temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 345,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 345,3442
temp2 = 132,temp2 = 133,temp2 = 135,temp2 = 132,temp2 = 131,temp2 = 133,temp2 = 134,temp2 = 133,temp2 = 131,temp2 = 130,1324

temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,3440
temp2 = 133,temp2 = 133,temp2 = 131,temp2 = 133,temp2 = 132,temp2 = 133,temp2 = 133,temp2 = 130,temp2 = 133,temp2 = 138,1329

temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,3440
temp2 = 133,temp2 = 135,temp2 = 133,temp2 = 133,temp2 = 131,temp2 = 133,temp2 = 133,temp2 = 133,temp2 = 133,temp2 = 133,1330

temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,temp1 = 344,3440
temp2 = 131,temp2 = 134,temp2 = 128,temp2 = 134,temp2 = 134,temp2 = 132,temp2 = 132,temp2 = 132,temp2 = 132,temp2 = 133,1322

temp1 = 344,temp1 = 343,temp1 = 344,temp1 = 344,temp1 = 343,temp1 = 344,temp1 = 343,temp1 = 343,temp1 = 343,temp1 = 344,3435
temp2 = 142,temp2 = 141,temp2 = 135,temp2 = 135,temp2 = 135,temp2 = 133,temp2 = 134,temp2 = 133,temp2 = 133,temp2 = 132,1353

temp1 = 343,temp1 = 344,temp1 = 344,temp1 = 343,temp1 = 343,temp1 = 343,temp1 = 343,temp1 = 343,temp1 = 343,temp1 = 343,3432
temp2 = 135,temp2 = 130,temp2 = 136,temp2 = 135,temp2 = 141,temp2 = 130,temp2 = 135,temp2 = 133,temp2 = 134,temp2 = 131,1340

temp1 = 343,temp1 = 343,temp1 = 343,temp1 = 343,temp1 = 343,temp1 = 343,temp1 = 343,temp1 = 343,temp1 = 343,temp1 = 343,3430
temp2 = 129,temp2 = 133,temp2 = 129,temp2 = 131,temp2 = 133,temp2 = 130,temp2 = 131,temp2 = 135,temp2 = 134,temp2 = 133,1318

temp1 = 343,temp1 = 343,temp1 = 343,temp1 = 343,temp1 = 343,temp1 = 343,temp1 = 343,temp1 = 343,temp1 = 343,temp1 = 343,3430
temp2 = 131,temp2 = 135,temp2 = 130,temp2 = 128,temp2 = 137,temp2 = 136,temp2 = 136,temp2 = 131,temp2 = 134,temp2 = 134,1332
14
  • 2
    You are reading A2 instead of A3. Commented Feb 17, 2018 at 18:53
  • 1
    Well that's a schoolboy error! Thanks for that, and reformatting the original question. Now I've fixed the code I see that the result returned by A0 starts at 345 and drops to 341, while that for A3 starts at 324 and drops to 289 - both over 20 iterations. A0 returns 320 six times consecutively, while A3 just drops consistently and the gap between the two widens. Any ideas how I might correct this?
    – user42197
    Commented Feb 17, 2018 at 19:19
  • What resistors are you using?
    – Majenko
    Commented Feb 17, 2018 at 19:35
  • The divider on A0 is 9840 and 200000, that on A3 is 9770 and 199800 - both measured on a UT203 digital multimeter, so nothing particularly special but, hopefully, not garbage either. The resistors were from Maplins, so probably 5%, but would that make the difference?
    – user42197
    Commented Feb 17, 2018 at 20:14
  • 1
    Which Arduino? Please add the appropriate tag to the question. Also please edit the question to make absolutely clear what port you are using. As it stands the question says you are using A3 but the code says A2.
    – Nick Gammon
    Commented Feb 17, 2018 at 22:34

1 Answer 1

1

Assuming you are using an Arduino Uno, I set up your test situation on my bench.

I modified the prints as jsotola suggested to move the "temp1 = " out of the loop.

I measured 0.670V on the A0 port and 0.666 on the A2 port.

We would expect 0.657V as per the Voltage Divider Calculator.

Vout = Vin * R2 / (R1 + R2)
13.8 * 10 / (200 + 10) = 0.657

As suggested on my page about the ADC the formula for converting a reading into a voltage is:

float voltage = ((float) rawADC  + 0.5 ) / 1024.0 * Vref;

Therefore a reading of 134 gives the correct results:

(134 + 0.5) / 1024 * 5 = 0.657

My results were:

Test sketch starting
temp1 = 133,134,133,134,134,134,134,134,134,134,1338
temp2 = 133,132,133,134,132,133,133,134,133,133,1330

temp1 = 134,134,134,134,133,134,134,134,134,134,1339
temp2 = 134,133,133,132,133,133,133,134,133,133,1331

temp1 = 134,134,134,133,134,134,134,133,134,134,1338
temp2 = 133,133,133,134,133,133,133,133,133,132,1330

temp1 = 134,134,133,135,134,134,134,134,134,134,1340
temp2 = 133,133,133,133,133,133,132,133,133,134,1330

temp1 = 134,134,134,134,134,134,134,134,134,134,1340
temp2 = 133,133,132,133,133,134,133,133,134,133,1331

temp1 = 134,133,133,134,134,133,134,134,134,134,1337
temp2 = 132,133,133,133,134,133,133,133,133,133,1330

temp1 = 134,134,134,135,134,133,134,134,134,134,1340
temp2 = 133,133,133,134,133,133,133,133,133,132,1330

This therefore shows that your sketch is working properly, and that there must be some electrical issue in your voltage divider or your connections.


The slight differences would be accounted for by noise in the voltage divider circuitry.

1
  • Excellent, thanks Nick, that gives me the confidence to try this again with soldered connections, rather than on a breadboard.
    – user42197
    Commented Feb 18, 2018 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.