Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADC Values Inaccurate and different from NodeMCU #2672

Closed
lawrence-jeff opened this issue Nov 8, 2016 · 20 comments
Closed

ADC Values Inaccurate and different from NodeMCU #2672

lawrence-jeff opened this issue Nov 8, 2016 · 20 comments

Comments

@lawrence-jeff
Copy link

lawrence-jeff commented Nov 8, 2016

Hardware: Multiple NodeMCU boards (Amico and Lolin) (with ESP-12 AIThinker module)
Core Version: 2.3.0

Description

The ADC Results via analogread (TOUT) don't seem to be correct, not wildly inaccurate but enough to throw some sensors.
Without changes to the setup they are also different than what is reported by the nodemcu lua firmware (which seems more accurate)
Even simple tests show it
Compare
Measured/Calculated values on Tout pin (factoring in voltage divider in nodemcu boards) and measured by a decent meter

1.02v (Pin fed via 3.32 source) - Nodemcu reads 1024 Arduino 1002
.502v - Nodemcu reads 530, Arduino 485
0 v - Both read 1

I can replace devices and reflash and reboot and the measurements are consistent. I never see the Arduino go to 1024 even when it has over 1 volt on the pin which seems very odd. Nodemcu firmware reports the correct value of 1024 consistently

Any ideas?

Example connect A0 on nodemcu to 3.3 volt pin then run both:

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

void loop(void) {
float reading;

reading = analogRead(A0);

Serial.print("Analog reading ");
Serial.println(reading);
}

in lua

print(adc.read(0))

@igrr
Copy link
Member

igrr commented Nov 8, 2016

Can you try adding the following line somewhere before your setup function?

ADC_MODE(0);

@lawrence-jeff
Copy link
Author

I added it and it didn't result in any noticeable change. Measuring the pin at 1.03 still results in around 1002 in Arduino and 1024 in nodemcu (what I would expect if the range is 0-1v)

Let me know if there is anything else I should look at.
Jeff

@lrmoreno007
Copy link
Contributor

How are you doing to measure in Arduino side?
Because Arduino A0 range are 0-5V

In NodeMCU:

int val = analogRead(A0);
val = map(val, 0, 1023, 0, 1000);
Serial.print(val);
Serial.println(" mV");

In Arduino:

int val = analogRead(A0);
val = map(val, 0, 1023, 0, 5000);
Serial.print(val);
Serial.println(" mV");

@lawrence-jeff
Copy link
Author

lrmoreno007,
Not sure exactly what you are asking - I'm using the arduino software not a physical board and the Arduino ESP port seems adjusted to measure 0-1v which is correct for an ESP ADC.

The repro process is pretty basic
Setup an ESP with 1 volt to the ADC pin (on a nodemcu board this means connect the A0 pin to the 3.3v regulated output which results in 1.03 volts at the module pin (measured)
This should result in a 1024 value in all software since it is at the top of the 1v range

Now flash with Nodemcu Lua and read the ADC = you get 1024 as expected
Don't touch any hardware but flash with arduino port
Do an Analogread and you get ~1000

Not a huge difference in this example but doing the same test at other levels shows larger discrepancies between the analog reads. Whatever Nodemcu Lua does seems to provide more accurate numbers

Not exactly relevant but if you use a real arduino board with the same code (and use the Aref pin and connect to the 3.3v rail) and run the same code you also get 1024 as expected.

So it doesn't seem to be the hardware or a problem inherent in the Arduino core itself - but something in the ESP Arduino port.

@lawrence-jeff
Copy link
Author

Also interesting that Issue #338 while not focused on this component noted the same thing in the initial report - that person was getting 909 in the arduino port and 1024 in nodemcu. The issue focused more on why the grounded value was wrong but it seems they were also seeing the general discrepency.

@lrmoreno007
Copy link
Contributor

lrmoreno007 commented Nov 9, 2016

OK, I understood that you were measuring with a Arduino Hardware. I didn't read you was using NodeMcu Lua firmware.

#338 are solved.

@lrmoreno007
Copy link
Contributor

lrmoreno007 commented Nov 9, 2016

Can you do the following? And tell us the result.

ADC_MODE(ADC_VCC);

void setup() {
float voltaje = ESP.getVcc();
Serial.print(voltaje/102.40f);
Serial.println(" <--- Must be 33");
}

void loop() {

}

@lawrence-jeff
Copy link
Author

32.71 <--- Must be 33

@lrmoreno007
Copy link
Contributor

lrmoreno007 commented Nov 9, 2016

In Vcc reading mode everything seems fine and the applied voltage is adequate.

Sorry to disagree with Igrr but the line before setup should not be? ...
ADC_MODE(ADC_TOUT);

Or

ADC_MODE(ADC_TOUT_3V3);

Or

ADC_MODE(33);

From Esp.h:
' enum ADCMode {
ADC_TOUT = 33,
ADC_TOUT_3V3 = 33,
ADC_VCC = 255,
ADC_VDD = 255
};

#define ADC_MODE(mode) int __get_adc_mode(void) { return (int) (mode); }'

@lawrence-jeff
Copy link
Author

So I tried both ADC_MODE(33) and ADC_MODE(255)
33 still results in 1002 instead of 1024 and 255 results in 65535

So is this something with only my boards - since it such an easy test does the max value on the pin result in 1024 as expected for everyone else?

@lrmoreno007
Copy link
Contributor

lrmoreno007 commented Nov 16, 2016

Sorry but so far I have not had any NodeMCU free.

I have tested with NodeMCU firmware and Arduino firmware and both give me the same result, with 3.3v in A0.

NodeMCU 0.9.5 build 20150318 powered by Lua 5.1.4
lua: cannot open init.lua

print(adc.read(0))
1024

Arduino with last git version:
ADC_MODE(ADC_TOUT);
void setup() {
Serial.begin(115200);
}
void loop() {
float voltaje = analogRead(A0);
Serial.println(voltaje);
delay(1000);
}

Result:
1024
1024
...

@lawrence-jeff
Copy link
Author

Well your Lua code agrees with mine but when I run that same arduino code (with a direct jumper between A0 and 3.3v) and I get
1001.00
1001.00
1001.00
1001.00

I have tried 4 different nodemcu boards so far, all similar.
Since you declare it as a float why does your output not have a decimal component? What version of the arduino esp8266 are you using. I am using 2.3.0

@lawrence-jeff
Copy link
Author

lawrence-jeff commented Nov 19, 2016

Followed the GIT instructions to load the latest and still get 1001.00.
Not sure what I changed but after uploading one more time with the git version - my board starting consistently reading 1024. Flashed all the other ones with an issue and they are all reading 1024 now as well. So it seems whatever this was is no longer present in the latest version

@lawrence-jeff
Copy link
Author

Reproduced on another system - using ESParduino 2.3.0 the top value is 1002 or so, replaced the contents of the 2.3.0 folder with the latest from this repo and recompiled and the top is now 1024 so one problem is fixed. The midpoint value is still off

Arduino and LUA both read the exact midpoint (validated with meter on the pin as .501v) as 530 instead of 512. If I take a real arduino add to the same circuit (using nodemcu board power rails) and use the 3.3v off nodemuc as external VREF on the real arduino it measures the same point as 510 (which would be basically correct as 1024/2 = 512)

So better but still oddly off ...since its present in both LUA and ESPArduino probably something in the underyling SDK, so I guess this can be closed

@jourjine
Copy link

jourjine commented Nov 19, 2016

maximum voltage is 1.06 not 1.01
rawADC 978 = 1v
etc

about this

PS
i have good working oven for more 6 month with 100k NTC
PID regulation +-0.5C

@lawrence-jeff
Copy link
Author

jourjine - I read through that article but I don't think it explains what I am seeing on my modules. The original problem was that the ESPArduino Analog read seemed to top out at around 1000 even when the same circuit in LUA measured 1024. According to the datasheet the range is 0-1v so it should have been 1023/1024 like LUA. This issues is resolved by using the GIT version of the ESP arduino code and now I have a top of 1024. From measurement it seems though that this top is reached now at around .97 volts - so it has gone too far the other way. What I did was use a pot to adjust voltage on the pin for every .1 v from 0-1v. After a bit of math dividing the ADC value by 1055 seems to correct it to reflect reality in volts. So if my ADC reads 800 that translates to .76 volts. If it reads 1023 that translates to .97 volts. This seems to give me nice results that work across the 3 boards i have.

If yours is working - be careful about compiling or changing your code - you could find the behavior changes more than you would expect.

@universam1
Copy link

Hi I can confirm that this problem exists with the 1.5.3 SDK and is gone with the GIT version of Arduino with SDK 1.54!
Any explanation for this?

@brianjmurrell
Copy link

brianjmurrell commented Dec 21, 2017

I too am having issues with inaccurate ADC values on my ESP board and Arduino. Looking in the board manager in the Arduino IDE it seems I also have version 2.3.0.

So I updated to the 2.4.0-rc2 and while 3.3V on the ADC (pin A0) now reads the full 1024, when I put the Vout from a 100K/100K voltage divider (that reads exactly 1.65 on my multimeter) I'm only reading 485 on A0 rather than the 512 it should be.

@lawrence-jeff
Copy link
Author

I would open a new issue (this one is closed), if you can get better results using NodeMCU Lua firmware that might help get attention

@brianjmurrell
Copy link

@lawrence-jeff Much thanks. I already did. #4009. :-)

if you can get better results using NodeMCU Lua firmware

That would be interesting for sure. But this is all pretty new to me. I jumped immediately to using Arduino with it. Any docs/guides/advise you can give about how I would test this with NodeMDU Lua firmware now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants