90Views15Replies

Author Options:

Having trouble measuring voltage with arduino. Answered

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

Tags:Arduino

15 Replies

user

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)

Select as Best AnswerUndo Best Answer

user

Sorry for the late response, had exams. So basicly you are trying to say i should also buffer the vef?

This is causing the strange voltage peeks?

Select as Best AnswerUndo Best Answer

user

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.

Select as Best AnswerUndo Best Answer

user

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.

Select as Best AnswerUndo Best Answer

user

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.

Select as Best AnswerUndo Best Answer

user
iceng (author)faraz ahmed khan2018-01-05

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 ?

Select as Best AnswerUndo Best Answer

user
faraz ahmed khan (author)iceng2018-01-05

The 2.5 vref goes to aref pin on my arduino.

ill try the the cap

Select as Best AnswerUndo Best Answer

user
faraz ahmed khan (author)2018-01-06

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.

Select as Best AnswerUndo Best Answer

user

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

Select as Best AnswerUndo Best Answer

user

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 ?

Select as Best AnswerUndo Best Answer

user

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

Select as Best AnswerUndo Best Answer

user
seandogue (author)2018-01-05

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.

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)2018-01-04

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

Select as Best AnswerUndo Best Answer

user
iceng (author)2018-01-04

Your circuit schematic please..

Select as Best AnswerUndo Best Answer