Giter Site home page Giter Site logo

esp32-adc's Introduction

ESP32-ADC

ESP32 How to correct the ADC

It is annoying:
The results of the ESP32 Analog to Digital Converter are really bad !



See: Original_Output.jpg

Is it possible to get precise results from ESP32-ADC ?

Here we build a LookUpTable to correct this behavior

Sense is:

Read value Y and correct it like:
Y = ADC_LUT[Y]

FOR ME IT WORKS VERY WELL. (See: Correct_with_LUT.jpg)

I estimate the ESP32-DAC produces correct values.

====================== NOTE ===================================================

We know that is not true, but it is linear upto (about) output-value = 200)
To be more precise: measure the ouput voltage of 200 with a good external voltmeter
and calculate a slope factor. See: ESP32_DAC_WITH_MY_LUT.ino

With that I get good real world values up to 2.5 V !

===============================================================================

Then I want to build a LUT:

  1. Send a value X to DAC
  2. Read back value Y from ADC. (pins 25 and 35 are connected)
  3. Value Y (0..4095) mostly is too low. We need a LUT (0..4095) to correct the measured value.
  4. Correct the value: Y =LUT[Y]
  5. Plot the value Y which should match X now. (Attention: Y should be > about 70 !)

Because the DAC uses 8 bit I have to interpolate to get the LookUpTable 0..4095.
But the errors caused by that are much less than the usual noise.

I am really surprised how good the results are. Look at the image.

Here is the program. How to use: (Arduino_IDE, ESP32, Esp32_Correct_DA2.ino)

  1. Connect pin25 and pin 35
  2. Start the program
  3. Start the Serial Plotter
  4. Wait some minutes - you will see garbage (from the printout of results)
  5. See the corrected values as image (Correct_with_LUT.jpg). The correction is made for YOUR ESP32.

    Second run:
  6. Comment out #define GRAPH <<<<<<<<<<<<<<<<<<
  7. Start the program again
  8. Wait some minutes.
  9. When the program stops you get 2 arrays to include in your sources (well, use only one of them):
    float ADC_LUT[4096] = { .... with best precision and int ADC_LUT[4096] = { ... less precision, but with less memory footprint

In your source: (Y: 0...4095)

Y=readAnalog(); Y=ADC_LUT[Y]; (look at the program)

ESP32_WITH_MY_LUT.ino is a demo program (to use with Serial Plotter).
It contains MY corrections. If you start it you may see the differences of YOUR ESP32 to mine.

Then use it with your own built and replaced ADC_LUT.
Here you can set your measured value for DAC(200) and then all results will give you real world voltages
with a precision of +/- 0.01V upto 2.5 V

Advantages of a LookUpTable: Using it is much faster than using a fitted polynom and it should show better precision

esp32-adc's People

Contributors

macleod-d avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

esp32-adc's Issues

Code questions

Hi Helmut,
I stumbled across this and analyzed the code in ESP32-correct-da2 and have a few questions. I did not debug nor run the code.

  1. Line #78: you divide by (float)10.0. Is this correct, I would have expected to divide by (float)5.0?

  2. Line #91: I do not understand what the diff value represents. It’s subtracting a voltage value from the index i? Should this read something like “diff=abs((float)Results[i] - Res2[j]);” ?

Application of your LUT approach

Hi,
I've run the program and have successfully gotten the two tables: float ADC_LUT[4096], int ADC_LUT[4096].

My project is measuring multiple input voltages for a number of sensors on pins: 39,36,35,34,33,32.
Question: Do I need to create a LUT for each of these pins?
Thanks,

driver/dac.h

Hello , where can i find this file?
dac.h

Best regards
Peter

On Results array out-of-boundary corner case...

Helmut, I first saw your post on expressif discussion forum before finding your github. I have two questions that may need your clarification.

  1. I used the code you posted on the expressif forum https://esp32.com/viewtopic.php?f=19&t=2881&start=30#p47663, I noticed that this particular section of the code on the forum
for (int i=0; i<200; i++) {
        if ((i%100)==0) Serial.println(i);
        Dotest(i);
    }

It is different from the code in this github that uses i<500 instead of 200. Is the code on the forum incorrect?

  1. The Results array is declared as Results[4096], and on the interpolation calculation, it take the Results[i+1] value to minus the Results[i] value for generating the interpolation values.
for (int i=0; i<256; i++) {
       for (int j=1; j<16; j++) {
          Results[i*16+j] = Results[i*16] + (Results[(i+1)*16] - Results[(i)*16])*(float)j / (float)16.0;
       }
    }

This formula works fine except of the last iteration when Results[(i+1)*16] = Results[4096] which will be out of boundary and get an unknown value. Can you verify that?

DAC linearity

I am struggling to use your code to achieve the desired results and I believe the issue is the output from my DAC is also non linear. I have taken these measurements with an external voltmeter on the pins for various DAC output values:
200 -> 2.52v
100 -> 1.31v
50 -> 0.71v

When running the test, the program declares Should be: 1.26 measured: 1.26 by setting the output pin to 100 and halving the known voltage at 200, but when if I measure externally the reading is 1.31v.

I wonder if this program maps the characteristics of the DAC to the ADC but as the DAC is non linear, we still do not have accurate readings from the ADC?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.