G6EJD / ESP32-ADC-Accuracy-Improvement-function

A function that improves the default ADC reading accuracy to within 1%

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

wifi noise (may be)

jay6621 opened this issue · comments

Setup : 1k pot b/w 5v & GND
Board : Devkit 1
Actual ADC INPUT : 0.243V

Sketch :

#include <WiFi.h>
#define csensor1  36

const char* ssid = "3391";
const char* password = "JAY@6621";

void setup() {
Serial.begin(115200);
delay(7000);
}

void loop() {
      Serial.println("TEST START\n"); 
      
      Serial.println("without wi-fi");
      for(int i=0;i<20;i++){
      Serial.println(ReadVoltage(csensor1),3);
      delay(100);
      }
      
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
      }
      Serial.println("");
      Serial.println("WiFi connected");
  
      Serial.println("with wi-fi");
      for(int i=0;i<20;i++){
      Serial.println(ReadVoltage(csensor1),3);
      delay(100);
      }
      Serial.println("TEST END");
      while(1);
}

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;
}

Serial Output

TEST START

without wi-fi
0.184
0.184
0.182
0.182
0.181
0.182
0.181
0.182
0.182
0.184
0.182
0.181
0.184
0.186
0.184
0.176
0.182
0.184
0.181
0.182
..
WiFi connected
with wi-fi
0.192
0.202
0.201
0.199
0.204
0.202
0.202
0.203
0.202
0.202
0.203
0.201
0.162
0.204
0.201
0.202
0.201
0.199
0.203
0.204
TEST END

I got many troubles from ESP while using ADC , I found it a junk. I don't know if I am wrong. After watching your video I tried above method using function you created. I noticed If Wifi is ON it gives different result.
It is on ADC 1 still. Is it a issue ? I don't know. but for anyone who reads this mind the wifi while using ESP ADC.
also if anyone have solution to above problem (?) please leave a comment.

Thank you
JAY

commented

You cannot use ADC2 when the WiFi is in use, See: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/adc.html
ADC2 is used by the Wi-Fi driver. Therefore the application can only use ADC2 when the Wi-Fi driver has not started.
Change to ADC1 and all OK.