Go Down

Topic: [SOLVED] Analog read from PSU is higher than actual value (Read 619 times) previous topic - next topic

Bjerknez

I will try that. Thanks :)

But is there any way i can lets say allow all voltages to A0 up to 15V but not more? To protect the analog output?

johnerrington

OK so the reference is the 1.1V bandgap on the Wemos, with a potential divider giving Rin=320k and Vin=3.3V

The OP's potential divider becomes 12k : 3k3 // 330k or about 12k : 3k27

giving a full scale reading of about 16.3 volts.

None of this explains the OP's question why a 1.5V cell or a 9V battery give the correct values as measured with his multimeter, while measurements from the Wemos of his PSU output are "incorrect."

However I may have an idea - voltages from the batteries are true DC. 

IS it possible ripple on his psu is causing this error?  A multimeter will show the "correct" average value, while the arduino doing spot measurements may not.

The OP is not doing any reading averaging in his code.

I'm trying to help. If I find your question interesting I'll give you karma. If you find my input useful please give me karma (I need it)

Bjerknez

OK so the reference is the 1.1V bandgap on the Wemos, with a potential divider giving Rin=320k and Vin=3.3V

The OP's potential divider becomes 12k : 3k3 // 330k or about 12k : 3k27

giving a full scale reading of about 16.3 volts.

None of this explains the OP's question why a 1.5V cell or a 9V battery give the correct values as measured with his multimeter, while measurements from the Wemos of his PSU output are "incorrect."

However I may have an idea - voltages from the batteries are true DC. 

IS it possible ripple on his psu is causing this error?  A multimeter will show the "correct" average value, while the arduino doing spot measurements may not.

The OP is not doing any reading averaging in his code.


Finally someone that try to troubleshoot my real question! Thanks :) To understand B i have to understand A. In my mind all works perfectly fine unless the readings from my PSU.

I can live with that since it is batteries i'm going to read anyway. Therefore in my eyes i allready have an circuit that works.

But i just don't know why the readings from my PSU shows in serial 0.3-0.4V higher, when my batteries shows correct value. :)

johnerrington

Thanks Bjerknez (wheres my karma??)

Have a look at the sketch you will find on this page - the "readInput" function  will show you what I mean about averaging - which you should almost invariably do when measuring an analog input.
I'm trying to help. If I find your question interesting I'll give you karma. If you find my input useful please give me karma (I need it)

pourduino

You said you tested with AA and 9v batteries and 12v with PSU.
ESP's ADC is not accurate (here, here) and the error gets more pronounced the larger the voltage is. Do you get the same error with 9v battery and 9v PSU?

Do you by any chance power the ESP differently when measuring with PSU? ADC is very sensitive to input power. Maybe add a 1000uF cap to ESP and test again.

Also read this. Are your wires that you measure the PSU with longer perhaps?

Bjerknez

You said you tested with AA and 9v batteries and 12v with PSU.

Read post #4 :)

Bjerknez

Thanks Bjerknez (wheres my karma??)

Have a look at the sketch you will find on this page - the "readInput" function  will show you what I mean about averaging - which you should almost invariably do when measuring an analog input.
Karma is given :)

johnerrington

And to you for posing an interesting and chanllenging problem!
I'm trying to help. If I find your question interesting I'll give you karma. If you find my input useful please give me karma (I need it)

Bjerknez

I decided to try out ESP32 instead with higher resolution. But this did not work either. With the code under, my PSU reads actually spot on this time. But when i meassure my bateries they are way off. My 9V brand new battery gives me 9.61V when i check with multimeter. My AA battery gives me 1.62V on the multimeter.

But my serial readings from ESP32 gives me 8.25V from the 9V battery and 0.9V from AA battery. But as i said, my PSU is adjustet to 15V blank. Meassured with multimeter. When i connect it to serial, i actually get 14.99 - 15.02V. Spot on.

I just don't get it...

I'm use 100K and 27K as voltage divider.

Her is my code i tested with.

Code: [Select]
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

int vPin = 33;
int LED_1 = 5;
float realVoltage;

char auth[] = "*****";

char ssid[] = "*****";
char pass[] = "*****";

BlynkTimer timer;

void setup() {
  Blynk.begin(auth, ssid, pass);
  Serial.begin(9600);
  pinMode(vPin, INPUT);
  pinMode(LED_1, OUTPUT);
  timer.setInterval(100L, readVoltage);
  timer.setInterval(100L, ledBar);
}

void readVoltage(){
  int voltageValue = analogRead(vPin);
  realVoltage = (15./4096)*voltageValue;

  Blynk.virtualWrite(V1, realVoltage);
  Serial.print("Battery Voltage: ");
  Serial.print(realVoltage);
  Serial.println("V");
}

void ledBar(){
  if(realVoltage >9){
    digitalWrite(LED_1, HIGH);
  }
  else{
    digitalWrite(LED_1, LOW);
  }
}

void loop() {
  Blynk.run();
  timer.run();
}

Bjerknez

I think i have decided to just make the voltage reading only to work with 12V battery and fine tune it with playing with the calculation. The most important is that the volt readings compares to my Multimeter readings from 12V batteries.

I think its not possible to espect a microcontroller gives correct prints og any voltage. I do not know exactly why, but i just think this is the right way to go.

raschemmel

#25
Oct 06, 2020, 07:00 am Last Edit: Oct 06, 2020, 07:01 am by raschemmel
This is all academic until you tell us what a meter reads at the A0 pin.
Without that, nothing matters. You need to know the actual voltage at the A0 pin.

johnerrington

You will still need to do some averaging as I showed you, to eliminate noise and voltage fluctuations with load and on charge.  If you do that I'll bet the difference nbetween battery and psu voltages will be greatly reduced.

@raschemmel - you cant rely on a meter to show a "correct" value for a rapidly changing signal, that will correspond with a spot measurement.
More detail on these pages
http://www.skillbank.co.uk/SignalConversion/index.htm
I'm trying to help. If I find your question interesting I'll give you karma. If you find my input useful please give me karma (I need it)

raschemmel

#27
Oct 06, 2020, 07:17 am Last Edit: Oct 06, 2020, 07:20 am by raschemmel
Quote
you cant rely on a meter to show a "correct" value for a rapidly changing signal, that will correspond with a spot measurement.
Well then how or why exactly are any of the meter readings reported by the OP relevant.
How exactly is the signal 'rapidly changing ' if it's a battery or a PSU ? That makes no sense.
If you are going to accept the OP's voltage measurements of his batteries and PSU, why can't
you accept the voltage reading on the A0 pin ? How is that exempt ?
You know very well the analog reading is a function of the power supply used by the MCU.
If you don't know that voltage , then you don't know anything.

Bjerknez

This is all academic until you tell us what a meter reads at the A0 pin.
Without that, nothing matters. You need to know the actual voltage at the A0 pin.
The voltage on the A0 pin is exactly 3V. Meassured with multimeter.

pourduino

Try the same code and the exact same setup with an Arduino too. See if that gives correct readings.

(This might be a very unpopular opinion, but in my humble nooby opinion the only thing ESP is good at is being cheap. It's an unreliable toy. It's not worth spending your time figuring out why it's giving out weird reading unless an Atmel chip is doing it)

Go Up