user42197: 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


user10794: 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.

42197: <p>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:-</p>

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

<p>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.</p>

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

<p>Code follows</p>

<pre class="lang-c++ prettyprint-override"><code>#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&lt;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&lt;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&gt;NUMBER_OF_SAMPLES)
  {
    delay(500000);
  }
}
</code></pre>

<p>And this is the output.</p>

<pre><code>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
</code></pre>
 10794: <p>Assuming you are using an Arduino Uno, I set up your test situation on my bench.</p>

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

<p>I measured 0.670V on the A0 port and 0.666 on the A2 port.</p>

<p>We would expect 0.657V as per the <a href="http://www.ohmslawcalculator.com/voltage-divider-calculator" rel="nofollow noreferrer">Voltage Divider Calculator</a>.</p>

<pre><code>Vout = Vin * R2 / (R1 + R2)
13.8 * 10 / (200 + 10) = 0.657
</code></pre>

<p>As suggested on <a href="http://www.gammon.com.au/adc" rel="nofollow noreferrer">my page about the ADC</a> the formula for converting a reading into a voltage is:</p>

<pre><code>float voltage = ((float) rawADC  + 0.5 ) / 1024.0 * Vref;
</code></pre>

<p>Therefore a reading of 134 gives the correct results:</p>

<pre><code>(134 + 0.5) / 1024 * 5 = 0.657
</code></pre>

<p>My results were:</p>

<pre><code>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
</code></pre>

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

<hr>

<p>The slight differences would be accounted for by noise in the voltage divider circuitry.</p>
