Having trouble measuring voltage with arduino.

I have posted this question before but I’ve made a few changes with my circuit with no luck. I’m trying to make a variable lab bench power supply with an lcd screen to read voltage and current while adjusting them. I’m using TL431 to have a 2.5v Vref and  a op amp in voltage follower configuration with 10M and 1M trimmer (Divider) on the input to finely calibrate 38v down to 2.5. The op amp is acting like a buffer.

This is the code I've come up with:

float volt;
int sum = 0;                  
unsigned char sample_count = 0;
#define NUM_SAMPLES 10

void setup() {
  analogReference(EXTERNAL);
  Serial.begin(9600);
}

void loop() {

  while (sample_count < NUM_SAMPLES) {
   sum += analogRead(A0);
   sample_count++;
   delay(10);
 
  }

   volt = ((float)sum / (float)NUM_SAMPLES * 2.5) / 1023.0;
    Serial.println(volt * 15.2 ,3);
    sample_count = 0;
    sum = 0;
}

Parts list:
LM358 (for now, will upgrade to a unity gain stable op amp)
10M 5% resistor
1M trimmer
Arduino Mega (Will use a pro mini in the final circuit)
Breadboard (Will this effect my readings?)
TL431 (2.5v Vref)

Yes I now the LM358 is not suited for this application but I don’t think this will affect my readings by much, about 5% maybe. Will it?

This code is gives me OK values at start but when I start to decrease/Increase the voltage. It lags behind and offsets by 1-2 volts on the reading. My goal with this project is to read (LIVE, meaning while changing voltage with buttons and see it change without any offset/lag) Voltage with a resolution of +-5/10mv accuracy and also read current (But thats for a later day). One last thing, I’m 16 so a lot of electronic terms tend to go over my head so please while giving an answer keep it simple if possible. Thanks :D

sort by: active | newest | oldest
faraz ahmed khan (author) 15 days ago

Here you go :)

New-Schematic-2 (2).png

fwiw, often times, it helps to buffer an offboard reference so that it presents a stiff reference to the A/D circuit. A non-stiff (**high output impedance) reference can lead to "odd" values once acquired.

***edited to correct low to "high". A low impedance reference is what one wants to present to an A/D converter, because (generally) these A/D converters are capacitive in nature, so when the ref is sampled by the A/D converter, if it's not stiff (ie not low impedance), it can't source enough current to charge the cap sufficeintly and provide an accurate reference (same goes analog inputs btw)

And your reference is also grounded to the Arduino Agnd ?

Incindentally, in your code

volt = ((float)sum / (float)NUM_SAMPLES * 2.5) / 1023.0;
Serial.println(volt * 15.2 ,3);

This is more efficient.

volt=float(sum)*0.000594 //a single multiply to replace all the other FP ops.

I think we need to see more of your schematic. Your choice of op-amp is very good, your use of it in unity gain mode is also a great choice. Can you actually WATCH the output of the amp with a meter to check that it is actually stable, in AC and DC ?

Iceng's idea is good. If you can find a scope, watch the output of the circuit and make sure it isn't oscillating.

faraz ahmed khan (author)  steveastrouk15 days ago

The output is stable in DC according to my meter, But haven't checked it in AC. I dont think ill have access to a scope.

I would definitely place a 0.1uF disc or mylar capacitor across the positive input to ground..

I assume the 38v is your signal source..

Where do you place the the 2.5v ref ?

faraz ahmed khan (author)  iceng15 days ago

The 2.5 vref goes to aref pin on my arduino.

ill try the the cap

faraz ahmed khan (author) 14 days ago

Here are some pictures.

(These readings are 200ms apart)

Focus of the last 4 readings.

At thwstart no problems at 38v, everything seems fine right? NO! at 30v things go out of hand (Offset) as you can see and at lower voltage same problem.

The meter measures my voltage just fine and doesn't jump in values.

The last picture is my circuit. Added the cap you said ice.

20180106_151909.jpg20180106_151946.jpg20180106_152033.jpg20180106_152106.jpg

AND no these dont settle over time. I left the 5v run for 30mins but with same offset problem.

You're doing very well, and are posting extremely smart answers to our questions. I still have to wonder about how you have grounded your signal to the arduino ?

faraz ahmed khan (author)  steveastrouk12 days ago

my signal grounds connects to my op amp. The op amps ground is connected to my arduino GND same thing?

seandogue15 days ago

The lm358 isn't a "bad" am persay. However, if you're using it as a direct gain reducer, I'd recommend a second stage v-divider over embedding the reduction.

That is,

v-ref ---- lm358(unity buffer)---v-divider---- vref in on arduino

In this case, one should choose resistances for the v-divider that are relatively low to keep the reference stiff

If that's not its purpose, (to stabilize the arduino's reference) and the opamp is being used on the signal source, then v-divider first followed by the unity gain buffer

signal --- v-divider --- unity gain lm358 --- arduino analog input

in this case, the resistances used for the v-divider should be kept relatively high to reduce parasitic deviations from unknown source voltage output impedance.

Hmm . Sounds like something's floating. As Iceng says "Schematic please" ;-)

iceng16 days ago

Your circuit schematic please..