/* Read an analog voltage using an ESP32 Potentiometer wiper is connected to GPIO 36 (Analog ADC1_CH0) + to Vin via resistor to limit Vmax to 900mV and - to 0V calibration and zero offset corrections are required for even approximate readings */ const int ADC1_CH = 36; //signal input pin NB ADC2 can not be used when using WiFi int AnalogValue = 0; // variable for storing the averaged reading int Reading = 0; //present reading int loopCount = 64; //how many times to take reading for averaging int calibration = 3200; //entered from testing int zcorr = 200; //entered from testing float mvolts; //measurement in millivolts void setup() { Serial.begin(115200); delay(1000); analogReadResolution(12); //Can be a value between 9 (0 – 511) and 12 bits (0 – 4095). Default is 12-bit resolution. analogSetAttenuation(ADC_2_5db); //sets the input attenuation for all ADC pins. Default is ADC_11db /* ADC_0db: sets no attenuation. ADC can measure up to approximately 800 mV (1V input = ADC reading of 1088). ADC_2_5db: range of measurement to up to approx. 1100 mV. (1V input = ADC reading of 3722). ADC_6db: range of measurement to up to approx. 1350 mV. (1V input = ADC reading of 3033). ADC_11db: range of measurement to up to approx. 2600 mV. (1V input = ADC reading of 1575). */ } void loop() { // Reading input voltage; take an average over loopCount readings. AnalogValue = 0; for (int i = 0; i < loopCount; i++) { Reading = analogRead(ADC1_CH); AnalogValue += Reading; } AnalogValue = AnalogValue / loopCount; //average of readings over loopCount measurements mvolts = (AnalogValue + zcorr) * 1000.0 / calibration; // apply zero and range corrections Serial.printf("reading is %d and voltage is %5.0f millivolts \n", AnalogValue, mvolts); delay(500); }