├── ESP32_ADC_Voltage_Measurement.ino └── README.md /ESP32_ADC_Voltage_Measurement.ino: -------------------------------------------------------------------------------- 1 | // 0 - 5 volts range Example 2 | 3 | void setup() { 4 | Serial.begin(115200); 5 | } 6 | 7 | void loop() { 8 | Serial.println(readVoltage(),1); 9 | float BatteryVoltage = readVoltage(); 10 | delay(1000); 11 | } 12 | 13 | float readVoltage(){ 14 | #define constant 5.9; // 0-5v range example 15 | // #define constant 7.1; // 0-6v range 16 | // #define constant 14.6; // 0-12v range 17 | // #define constant 18.5; // 0-15v range 18 | // #define constant 30.1; // 0-24v range 19 | // #define constant 38.1; // 0-30v range 20 | // #define constant 64.5; // 0-50v range 21 | // #define constant 130.0; // 0-100v range 22 | float ADCvoltage = (float)analogRead(36) / 4095 * constant; 23 | return ADCvoltage; // 0-5v range unless constant is changed 24 | } 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP32-ADC-Voltage-Measurement 2 | Using the ADC to measure voltage 3 | 4 | The function that corrects for the ESP32 ADC input impedance loading of an attached voltage divder. I have estimated the ESp32 ADC input impedance to be 1.3Mohms 5 | 6 | #define constant 5.9; 7 | 8 | // 0-5v range example, should be a factor of 5, but loading increases it to 5.9 9 | 10 | // defined constant 5.9 // 0-5v range 11 | 12 | // #define constant 7.1 // 0-6v range 13 | 14 | // #define constant 14.6 // 0-12v range 15 | 16 | // #define constant 18.5 // 0-15v range 17 | 18 | // #define constant 30.1 // 0-24v range 19 | 20 | // #define constant 38.1 // 0-30v range 21 | 22 | // #define constant 64.5 // 0-50v range 23 | 24 | // #define constant 130.0 // 0-100v range 25 | 26 | float readVoltage(){ 27 | 28 | float ADCvoltage = (float)analogRead(36) / 4095 * constant; 29 | 30 | return ADCvoltage * constant; // 0-5v range 31 | } 32 | 33 | // I cannot fully explain why as input voltage increases and effective load on the voltage divider by the ESP32 decreases that the constant offset increases, it should be the opposite! 34 | --------------------------------------------------------------------------------