Another voltage divider question..... warning noobie!

Hi all

First post - newbie to Arduino and electronics in general!

I have in mind a project for my mobile astronomical set up. I will be building a power box to supply the various items with 12v power - the mount for the telescope, the CCD camera, the USB hub, the dew prevention strap.

The power box will have 2 x 12v inputs - each one supplying 3 outputs. This enables me to have a different power source for the dew prevention controller, for example, to keep the noise interference on the camera as low as possible. The camera has cooling on it, to a maximum of 38deg below ambient so the power requirements may fluctuate from session to session.

So, I would like to build a unit that measures:
Voltage from each of the supplies - possibly even on each output as well
Current draw on each supply - again, possibly on each output
Humidity and Temperature, and thus calculate the dew point
And eventually to interface to the PC to obtain telescope positional data
All of this will be displayed on LCD/LED screen of some sort

So, I thought I’d start with the basics and measure the voltage. Having understood that I can’t have more then 5vDC going into the Uno, I built a simple voltage divider, but I am getting weird results and I don’t know why…

I’ve attached my Fritzing diagram of my divider - a 1M resistor, a 100K and a lead going to A2 on the Uno.

I’ve used the sketch from Measuring Voltage with Arduino - exactly as is, but updated after measuring my voltage

The Uno is connected via USB only and the 12v input is from a spare transformer I have. This is connected to a variable DC voltage selector, so I could select 12/9/6/5/3 volts and mimic drain on the battery pack.

So, the output I am getting looks like:
0.00 V
0.00 V
0.00 V
0.00 V
0.00 V
1.44 V
23.19 V
63.13 V
63.13 V
63.13 V
63.13 V
63.13 V
63.13 V
63.13 V
63.13 V
63.13 V
63.13 V
44.62 V
0.08 V
0.00 V
0.00 V
0.00 V
0.00 V
0.00 V
0.00 V
0.01 V
7.82 V
37.91 V
63.13 V

I’ve measured the voltage with a digital voltmeter and as far as I can tell, it is stable.

After digging and searching on here, I have found a post (#6 in http://forum.arduino.cc/index.php?topic=255493.0) that suggests a 0.1uF capacitor might help - but I haven’t tried this as yet…

Can anyone see any other reason why I would get such spurious readings?

Thanks and sorry for the long-winded post!

Connect the grounds!!

Those resistor values are awful high, especially with no cap on it to stabilize it. I'd try for lower total resistance and a cap on it to stabilize it. Then go from there.

Oh, and we hate fritzing here. Hand drawn is preferable.

As DrAzzy said. Connect the grounds.

Resistor values are not a problem... if a 100n cap from analogue pin to Arduino ground is added.

Resistor ratio of 1:10 is a bit odd.
Are you measuring a 12volt battery?
A/D is not fully used with 12volt input and default Aref. And 1:10 will give overflow with 1.1volt Aref.
Leo..

So, I thought I'd start with the basics and measure the voltage.

GOOD PLAN! Take baby steps. A lot of beginners try to do the whole thing at once.

Having understood that I can't have more then 5vDC going into the Uno

Try grounding the analog input (and you should read zero volts) and try connecting the Arduino's own 5V to confirm that works...

Current draw on each supply - again, possibly on each output

Current is tricky. You have to measure the voltage across a known series resistor (usually low resistance value). That means you get a (hopefully small) voltage drop, and the bigger issue is that the Arduino normally measures voltage with reference to ground so it's tricky to measure the voltage drop across a resistor unless one end of the resistor is grounded.

And eventually to interface to the PC to obtain telescope positional data
All of this will be displayed on LCD/LED screen of some sort.

That's up to you, but if you're using a computer you might not need the LCD display.

So, I thought I'd start with the basics and measure the voltage. Having understood that I can't have more then 5vDC going into the Uno, I built a simple voltage divider, but I am getting weird results and I don't know why...

Current, voltage, inbuild 12-bit A/D.
Leo..

Thank you all so much - as you might expect from a noobie, I now have more questions!!!

Connect the grounds - do you mean I need to connect the ground of the supply to the GND of the Arduino?

I chose the ratio of 1:10 because that seemed to be what the examples I came across were showing - but thinking about it, a factor of 3 would be sufficient?

As I understand it, the higher resistor values would draw less load from the battery source - but I guess the draw is so low anyway it doesn't really matter...?

'Grounding the analogue input' - not sure what you mean here, or what to do?

Again, thanks for taking the time to help - and noted about Fritzing!! :smiley:

I have also just realised that measuring the voltage across the two circuits will be different - circuit A will also be powering the Arduino, so I think it will need to be different to circuit B....

I have found this thread - http://forum.arduino.cc/index.php?topic=92074.15 , where retrolefty tells how to do this - so this is how I should connect my circuit A...

Circuit B - measuring the voltage on a load that is not connected to the Arduino is then probably where the above question applies to...

So, I will go and play with this idea and post back...

OK, so I think I have this figured out and working now for both Circuit A and B - I have attached a new diagram…

I also modified the sketch to read the 2nd voltage on A5 - and this is working too!

I think the calibration values need a tweak as the voltages that Arduino is reporting are a bit different to my DMM, but actually, for my needs, this is probably not a huge issue…

When the A2 has no load on it, I still see a voltage reading. I’m guessing this is because of the Arduino itself, so I simply subtracted the reference voltage value from the calculation - but not sure this is the right thing to do??

I still have not added the cap to the circuit, as I don’t have any to hand…
@Wawa - you said a 100n would be what I needed - how do you calculate this, please?

So, the sketch:

/*--------------------------------------------------------------
  Original Author:       W.A. Smith, http://startingelectronics.org

  Modified: Darren Jehan, April 2016
  Added calibration values
  Added 2nd input on A5
--------------------------------------------------------------*/

// number of analog samples to take per reading
#define NUM_SAMPLES 10

int sum1 = 0;                    // sum of samples taken
int sum2 = 0;
unsigned char sample_count = 0; // current sample number
float v1 = 0.0;            // calculated voltage
float v2 = 0.0;

void setup()
{
    Serial.begin(9600);
    pinMode(A0,OUTPUT);
    pinMode(A1,OUTPUT);
    pinMode(A2,INPUT);
    pinMode(A3,OUTPUT);
    pinMode(A4,OUTPUT);
    pinMode(A5,INPUT);
    digitalWrite(A0,LOW);
    digitalWrite(A1,LOW);
    digitalWrite(A3,LOW);
    digitalWrite(A4,LOW);
}

void loop()
{
    // take a number of analog samples and add them up
    while (sample_count < NUM_SAMPLES) {
        sum1 += analogRead(A2);
        sum2 += analogRead(A5);
        sample_count++;
        delay(10);
    }
    // calculate the voltage
    // use 5.0 for a 5.0V ADC reference voltage
    // 4.98V is the calibrated reference voltage
    v1 = ((float)sum1 / (float)NUM_SAMPLES * 4.98) / 1024.0;
    v2 = ((float)sum2 / (float)NUM_SAMPLES * 4.98) / 1024.0;

    Serial.print("Circuit A: ");
    Serial.print((v1 * 12.043) - 4.98);
    Serial.println (" V");
    Serial.print("Circuit B: ");
    Serial.print(v2 * 12.043);
    Serial.println (" V");
    sample_count = 0;
    sum1 = 0;
    sum2 = 0;
    delay(500);
}

Again, thanks to everyone that replied and forced me to think a bit more!!

AstroDaz:
@Wawa - you said a 100n would be what I needed - how do you calculate this, please?

The A/D needs a ‘solid’ voltage on the analogue pin to take a sample from.
Something with a <10k impedance is in the chip’s datasheet. You have used 1Megohm.

The internal A/D cap could have some charge left from a previous measurement of another analogue pin.
The (tiny) sample capacitor inside the chip, with a >10k divider, hasn’t got time to fully equalize with the voltage of the divider. The first sample(s) will be bad.
A common fix is to discard the first sample.

A 100n cap on the analogue pin acts as a miniature battery.
When the A/D comes along to take a sample, it sees a solid voltage to sample from.

Still don’t understand the 1:10 divider ratio you use.
With default Aref, you throw away accuracy.

Better to use ~1:13, and 1.1volt Arref.
Stable and smaller voltage steps.
Leo…

As far as connecting grounds together, think of a circuit as something that requires two sides or ends related to each other. Trying to measure a voltage at one point in a circuit when the other side is not connected to a common point is like trying to measure the length of a string when you only have one end. In general, what you want is a common ground point that everything ties back to so that you don't get strange ground loop problems (wire has resistance therefore you get a voltage drop across it when there is a current flowing - if your circuit is connected somewhere in the middle of a length of ground, your circuit is not really seeing the correct ground due to the voltage in the ground wire).

@gpsmikey - thanks, that makes perfect sense now :slight_smile:

@Wawa - The inly reason I used the 1:10 is that what the example at startingelectronics used. I have found a tutorial on using the AREF so I will go and read and see if I can work out how to apply that.

You say to use a ~1:13 ratio, but should I be using the high value resistors, or aim for lower? Again, I understood the preference for high values because of the lower current draw - but more than happy to be guided by experts here!

Final question for now - the capacitor, does it literally just need to sit across the A2 pin and GND?

Appreciate everyone's patience with me!!

AstroDaz:
You say to use a ~1:13 ratio, but should I be using the high value resistors, or aim for lower? Again, I understood the preference for high values because of the lower current draw - but more than happy to be guided by experts here!

Final question for now - the capacitor, does it literally just need to sit across the A2 pin and GND?

1)For max resolution (most A/D values),you calculate the divider so that the maximum battery voltage gives close to 5volt on the analogue pin (for default Aref).

If max battery voltage is 14volt. >> divider ratio is 5:(14-5) >> 5:9

or for 1.1volt Aref (could be 1volt) >> 1:(14-1) >> 1:13

  1. Yes.

Voltage is calculated from: A/D value, number of samples, Aref, divider ratio and calibration factor.
Aref, divider ratio, and calibration factor could be combined to one number (they are fixed) for an easier formula.
Leo…

OK, I think I understand more now - at least I hope so!!

I found tutorials on the AREF and went through those, and have modified my sketch and circuit accordingly.

So, now, I feed the AREF from the Arduino 5v and use the following sketch (taken from tronixstuff - http://tronixstuff.com/2013/12/12/arduino-tutorials-chapter-22-aref-pin/):

int analoginput = 0; // our analog pin
int analogamount = 0; // stores incoming value
float percentage = 0; // used to store our percentage value
float voltage =0; // used to store voltage value

void setup()
{
  analogReference(EXTERNAL); // use AREF for reference voltage
  Serial.begin(9600);
}

void loop()
{
  analogamount=analogRead(analoginput);
  percentage=(analogamount/1024.00)*100;
  voltage=analogamount*4.99; // in millivolts
  Serial.println(analogamount);
  Serial.print("% of AREF: ");
  Serial.println(percentage,2); 
  Serial.print("A0 (mV): ");
  Serial.println(voltage,2);
  delay(2000);
}

My circuit now looks like the attached.

I also hope I’ve understood about the resistor values. I chose 120K and 68K as they give no more than 5v on the analog lines - regardless if it’s a 12v supply (battery) or 13.8v (regulated bench supply)

I’ve added the caps to the circuit, and all the grounds tie back to a common point.

Huge thanks to all - especially Leo!

And of course, the R1, for pin A0 should be connected to the 12v supply line!!
It was a deliberate mistake!!

I have built this on the breadboard and seems to work exactly as I need!

:slight_smile:

AstroDaz:
So, now, I feed the AREF from the Arduino 5v

No…
You won’t have to do, connect, or code anything for default 5volt Aref.
Disconnect that link before you smoke the Arduino.
That resistor ratio is ok for default Aref.

EXTERNAL or INTERNAL is if you want to use a DIFFERENT Aref.
http://www.arduino.cc/en/Reference/AnalogReference

Default Aref is the potentially unstable 5volt power supply.
Good enough for most things, but not so good for accurate/stable voltage measurements.

Arduino also has an inbuild very stable 1.1volt Aref.
If you decide to use that, all you have to do is calculate your divider for ~1volt out.
And enable that 1.1volt Aref in the setup.
The maths line to convert A/D value to voltage also has to be changed from 5 to 1.1.

Example code

/*
0 - ~17volt voltmeter
works with 3.3volt and 5volt Arduinos
uses the internal 1.1volt reference
10k resistor from A1 to ground, and 150k resistor from A1 to +batt
(1k8:27k or 2k2:33k are also valid 1:15 ratios)
100n capacitor from A1 to ground for stable readings
*/
float Aref = 1.075; // change this to the actual Aref voltage of ---YOUR--- Arduino, or adjust to get accurate voltage reading
unsigned int total; // A/D output
float voltage; // converted to volt

void setup() {
  analogReference(INTERNAL); // use the internal ~1.1volt reference  | change (INTERNAL) to (INTERNAL1V1) for a Mega
  Serial.begin(9600); // ---set serial monitor to this value---
}

void loop() {
  analogRead(A0); // one unused reading to clear old sh#t
  for (int x = 0; x < 16; x++) { // 16 analogue readings and 1/16 voltage divider
    total = total + analogRead(A0); // add each value
  }
  voltage = total * Aref / 1024; // convert readings to volt
  // print to serial monitor
  Serial.print("The battery is ");
  Serial.print(voltage);
  Serial.println(" volt");
  total = 0; // reset value
  delay(1000); // readout delay
}

Leo…