analogRead flaky

I have built a two-output power supply, with continual monitoring and display of voltage and current. This requires doing analogReads on 4 of the Arduino Nano pins, namely A0, A1, A2, A3. I am using a zener diode at 3.98 volts as an external analog reference.

The voltage readings are taken to pins A0, A2, using a voltage dividers to assure that they remain within range. The current readings are taken by using a MAX 472 current sensor, which measures the slight voltage difference across a 0.1 ohm resistor in series with the supply output, and produces a voltage between 1 and 4 volts, linearly related to the current flow.

Now here's the strange problem. The two pins monitoring the voltage outputs provide stable outputs. The two pins devoted to current flow (A1, A3) are reading the voltage outputs of the MAX472's, and provide randomly distributed values about the right value, but with a wide variation. For example, when I'm reading 0.2 amps, associated with 0.7 v. output from the MAX472, I get analogRead values from as low as 80 to as high as 400.

It doesn't matter if I slow down the readings, even to 0.5 sec. between readings, and it doesn't help if I reduce the software to just reading just one pin. Also, my OScope does not see any noise coming from the MAX472. It appears to deliver a stable voltage.

I have carefully inspected the board, and all my solder joints. Any insights?

Thanks in advance for any help.

Using a zener as reference is not that good. Bad regulation and temp drift.
Much better to use the inbuild 1.1volt Aref.
Here is a code example I wrote for a ~17volt voltmeter that uses the internal reference voltage.
It uses a 1:15 divider (1/16) to drop to <=1.1volt, and the same amount of samples (16) for averaging and easier maths.
The tap of the divider can still have <= the supply voltage on it, without damaging the pin.
Because of the larger ratio (1:15 in this case), you also have a better over voltage protection.

You also should then adapt your high side current sensor to output <1.1volt.
Not sure why your curent readings are instable.
Did you try a 100n - 1uF cap from analogue pin to ground.
Leo..

/*
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 (1.000 - 1.200), 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(1); // one unused reading to clear old sh#t
  for (int x = 0; x < 16; x++) { // 16 analogue readings and 1/16 voltage divider = no additional maths
    total = total + analogRead(1); // 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
}

@jrdoner: Please post your code, and your schematic.

Despite what Wawa says, I can't believe an inaccurate voltage reference would cause readings to be out by close to an order of magnitude.

Sure, the flaky current values must have another reason.
Is this with a resistive load.
Does your code average.
Leo..

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom.... :slight_smile:

I admit that my zener drifts by a 0.05 volt or so, but that's about all, and that happens very slowly.

The code is over 1000 lines, so posting it would be impractical, but essentially, when I use analogRead() and immediately print the integer result, I get these drastic variations, so I don't think much in the code could be involved. And yes, the load at this point is just resistive.

The next thing I am going to do, as was suggested above, is to apply a couple caps to the flaky pins. Perhaps the Max 472 chips feeding those pins can't supply enough current during the read operations, although I have used exactly this arrangement in another power supply without any problems at all.

The code is over 1000 lines, so posting it would be impractical ...

You can attach code to posts. We eat 1000 lines for breakfast.

How to use this forum

Perhaps the Max 472 chips feeding those pins can't supply enough current during the read operations ...

I strongly doubt that. However you can test it. Measure the voltage on the Max472 voltage output pin with a voltmeter. What does it read?

Hi,
You problem could be the acquisition time for the AtoD, a common practice is to read an analog input twice in succession and only use the last reading.
This makes sure that you have a valid reading for the input.

Tom.... :slight_smile:

Well, folks,

I appreciate all of the inputs. I did solder a couple of 0.1 uF capacitors to the Nano pins in question, and the problem completely disappeared. I'm afraid I can't provide the theory as to why that cured the problem. I don't think there's any substantial noise in this system, so perhaps the caps supply a little more current at ADC conversion time.

The code is over 1000 lines,

Well, you probably need to reduce your code until you get a stable reading that is expected. Do you have devices connected to your arduino's power bus to cause the voltage to swing? You don't really give enough on your setup, so it could be your hardware wiring, bad code, or other. Depending on your code, you may need to adjust how you take the analog reading.

jrdoner:
I did solder a couple of 0.1 uF capacitors to the Nano pins in question, and the problem completely disappeared. I'm afraid I can't provide the theory as to why that cured the problem. I don't think there's any substantial noise in this system, so perhaps the caps supply a little more current at ADC conversion time.

A cap will give the A/D a "solid" source to sample from if you use a voltage divider with high-ish resistor values.
A pre-read as suggested (look at my code) helps to clear the "ghost charge" from a previous read from another analogue pin.
Leo..