Page 1 of 1

ESP32 ADC Accuracy

Posted: Thu Apr 05, 2018 9:02 am
by Daniel
The Rev1 and Rev0 versions of the ESP32 has a couple of issues with the ADC. There is a topic on noise pickup viewtopic.php?f=12&t=8816 that describes that issue. In addition to that issue the ADC is not as linear as it should be but the non-linearity is repeatable and can be removed with some numerical manipulation. The code below corrects the raw readings from the ADC and returns a corrected, linearized reading. The code is not particularly fast because it uses floating point functions. If you care about speed you can put the function into a spreadsheet and generate a lookup table.

Code: Select all

#define ADC_Ch 36
void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.println( ReadVoltage( ADC_Ch ), 3 ) ;
  Serial.println( analogRead( ADC_Ch ) ) ;
  delay( 1000 ) ;
}

double ReadVoltage(byte  pin )
{
  double reading = analogRead( pin ) ; // Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095
  if( reading < 1 || reading > 4095) {
    return 0 ;
   }
  // return -0.000000000009824 * pow( reading,3 )  +  0.000000016557283 * pow( reading, 2 ) + 0.000854596860691 * reading + 0.065440348345433 ;
  return -0.000000000000016 * pow( reading, 4 ) + 0.000000000118171 * pow( reading, 3 )- 0.000000301211691 * pow( reading,2 )+ 0.001109019271794 * reading + 0.034143524634089 ;
} // More complex, improved polynomial or simpler polynomial, use either

/* ADC readings v voltage
// Polynomial curve fit, based on this raw data:
 *   464     0.5
 *  1088     1.0
 *  1707     1.5
 *  2331     2.0
 *  2951     2.5 
 *  3775     3.0
 */