ESP32 ADC Non-linear Issues - How do I fix / change Attenuation or width?

vseven
Posts: 21
Joined: Wed Aug 30, 2017 3:22 am

ESP32 ADC Non-linear Issues - How do I fix / change Attenuation or width?

Postby vseven » Fri Sep 01, 2017 1:45 am

I have been trying and failing to get a NodeMCU ESP32-S Dev board to properly read two analog inputs (voltage). It appears the inputs are very non-linear and there is a good amount of noise.

I'm am using the following board for my testing; https://www.amazon.com/gp/product/B0718T232Z/

I have downloaded the firmware repo, built the firmware, and flashed it two days ago so I should be running up to date firmware.

I am using a known good voltage output (which I can set to whatever value) along with testing the voltage to verify its correct.

With the above said I went through and mapped out the voltage vs ADC value and what I found was under around 0.1 v the ADC didn't see anything and above around 3.08v the ADC just reads 4095. I've attached the data and here is the graph:
ESP32-Voltage-vs-ADC-Reading.jpg
ESP32-Voltage-vs-ADC-Reading.jpg (43.42 KiB) Viewed 48623 times
Based on this I'd like to try to switch to the -6db attenuation and also the 11 or 10-bit width and retest. However I don't know how to do this. I can fiddle with Arduino but I can't seem to get the commands right and I'm wondering if I even can from there. I'm assuming I can change the default from the -11db and 12-bit somewhere in the source code and recompile the firmware and reflash it but again I'm not sure how to do that.

Can anyone guide me on the changes I need to make to either add something to my sketch to change these settings on a entire ADC or pin by pin basis or what to change in the firmware code?

-Allan
Attachments
ESP32-Voltage-vs-ADC-Reading.csv
(735 Bytes) Downloaded 2216 times

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: ESP32 ADC Non-linear Issues - How do I fix / change Attenuation or width?

Postby tele_player » Sun Sep 03, 2017 8:32 pm

What a coincidence! That's EXACTLY the same ESP32 I use. I've got two, they work perfectly (so far?)



Width:

/*
* Set the resolution of analogRead return values. Default is 12 bits (range from 0 to 4096).
* If between 9 and 12, it will equal the set hardware resolution, else value will be shifted.
* Range is 1 - 16
*
* Note: compatibility with Arduino SAM
*/
void analogReadResolution(uint8_t bits);

/*
* Sets the sample bits and read resolution
* Default is 12bit (0 - 4095)
* Range is 9 - 12
* */
void analogSetWidth(uint8_t bits);



Attenuation:
typedef enum {
ADC_0db,
ADC_2_5db,
ADC_6db,
ADC_11db
} adc_attenuation_t;

void analogSetAttenuation(adc_attenuation_t attenuation); // all ADC pins
void analogSetPinAttenuation(uint8_t pin, adc_attenuation_t attenuation); // specific pin

vseven
Posts: 21
Joined: Wed Aug 30, 2017 3:22 am

Re: ESP32 ADC Non-linear Issues - How do I fix / change Attenuation or width?

Postby vseven » Mon Sep 04, 2017 2:35 am

Thanks @tele_player . I've tried that code before, verbatim, and I couldn't get it to compile. I copy and pasted your example and at least got a redecloration error since adc_attenuation_t is already defined in the ESP32 files. Removed that declaration and it seemed to compile:

void analogSetWidth(uint8_t bits = 11);
void analogSetAttenuation(adc_attenuation_t ADC_6db); // all ADC pins
//void analogSetPinAttenuation(uint8_t pin, adc_attenuation_t attenuation); // specific pin

I don't know if maybe the copy and paste form the github repo converted stuff that I couldn't see (quotes or otherwise) but copying yours form here seems to work. Thanks.

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: ESP32 ADC Non-linear Issues - How do I fix / change Attenuation or width?

Postby tele_player » Mon Sep 04, 2017 4:16 am

The big question - did tweaking these settings achieve the desired ADC behavior?

vseven
Posts: 21
Joined: Wed Aug 30, 2017 3:22 am

Re: ESP32 ADC Non-linear Issues - How do I fix / change Attenuation or width?

Postby vseven » Mon Sep 04, 2017 8:04 pm

So thats the bad part. It seems that adding the code to my Arduino sketch had zero effect on the controller. The width stayed at 12bit and the attenuation at 11db. Exact same results from my testing also, like the values were the exact same (within 20) no matter the setting which tells me it wasn't taking it. Unless I'm doing something wrong. This is what I tried in order using the defaults for the first test to verify nothing changed:

void analogSetWidth(uint8_t bits = 12);
void analogSetAttenuation(adc_attenuation_t ADC_11db);

Same results as initial testing (without those lines). Then tried:

void analogSetWidth(uint8_t bits = 11);
void analogSetAttenuation(adc_attenuation_t ADC_11db);

Again same results. Then tried:

void analogSetWidth(uint8_t bits = 12);
void analogSetAttenuation(adc_attenuation_t ADC6db);

Expecting the max voltage to be 2.7 per the documentation. But it wasn't....it was still 3.3 (well 3.1 ish) and the ADC still scaled the same. Finally tried:

void analogSetWidth(uint8_t bits = 11);
void analogSetAttenuation(adc_attenuation_t ADC_6db);

And again didn't change anything.

Am I doing something wrong or is it ignoring the commands? I didn't use void analogReadResolution(uint8_t bits); yet but I'm assuming if the attenuation command was ignored that would be too.

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: ESP32 ADC Non-linear Issues - How do I fix / change Attenuation or width?

Postby tele_player » Tue Sep 05, 2017 3:56 pm

Those calls seem to work for me. Note: there's been plenty of discussion of non-linearity of the ADC.
See:
https://github.com/espressif/esp-idf/issues/164

vseven
Posts: 21
Joined: Wed Aug 30, 2017 3:22 am

Re: ESP32 ADC Non-linear Issues - How do I fix / change Attenuation or width?

Postby vseven » Tue Sep 05, 2017 5:01 pm

Yeah, not working at all for me. And yes I'm aware of that post....my reply is at the bottom of it. ;)

I might just hard code the values into the firmware and reflash since it doesn't seem to be taking the changes from the sketch.

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: ESP32 ADC Non-linear Issues - How do I fix / change Attenuation or width?

Postby tele_player » Tue Sep 05, 2017 6:41 pm

Maybe you need a newer version of Arduino core?

I find the versioning of this stuff confusing, at best.

vseven
Posts: 21
Joined: Wed Aug 30, 2017 3:22 am

Re: ESP32 ADC Non-linear Issues - How do I fix / change Attenuation or width?

Postby vseven » Tue Sep 05, 2017 6:44 pm

I'm running the latest version from https://github.com/espressif/arduino-esp32 and have recompiled the latest firmware, multiple times, for the chip. I'm not sure what else I can try.

mnemonix
Posts: 9
Joined: Mon Jan 23, 2017 11:07 pm

Re: ESP32 ADC Non-linear Issues - How do I fix / change Attenuation or width?

Postby mnemonix » Wed Sep 06, 2017 3:21 am

vseven wrote:....I'm not sure what else I can try.
Maybe you can learn a bit of C, for example how to call a function instead of just declaring it? ;)

Here is a stripped down version of what I used to test the ADCs (I've not tested if it works in this minimal version):

Code: Select all

int n = 0;

void setup()
{
    Serial.begin(115200);
    analogSetWidth(10);                           // 10Bit resolution
    analogSetAttenuation((adc_attenuation_t)2);   // -6dB range
}

void loop()    //ADC Test
{
    int av = analogRead(2);                       // ADC12 on GPIO2
    for(n=1; n<8; n++) av += analogRead(2);
    av /= 8;
    Serial.print(" ADC12 = ");
    Serial.println(av,DEC);
    delay(200);
}

Who is online

Users browsing this forum: No registered users and 145 guests