This is just a very quick example on how to add a Voltage Divider to the ESP8266 Internal ADC input to increase the ADC input range.For more theory behind, please take a look at the previous related article: ESP8266 – Internal ADC
Today project:
Measure a voltage input range from 0-5V with the ESP8266 internal ADC
What do we need:
- 2 Resistors for the voltage divider, R1=105.6k, R2=24.08k. I am using here precision resistors and the values are measured values with a proper calibrated bench meter.
- a good, trustable, calibrated ok Multimeter.
- some wires to connect all together.
I will not insist on connections, take a deeper look at the previous article about
Voltage divider schematic and Vout (ADC input voltage) formula
|
How do we do it :
1. Measure your Max desired input voltage
In my case, Max Input Voltage (Vmax) = 5.1919 (measured)
2. Measure Resistors values:
R1 = 105.6k
R2 = 24.08k
3. Check if Full Scale Value at the voltage divider output is inside the ADC defined domain:
- Calculated : Vout = (R2/(R1+R2))*Vin = 0.964072733 V
- Measured = 0.96038V
Good enough for the precision we are looking for. ESP8266 ADC is 10bit only and not exactly the most accurate in town
4. Calculate Voltage Divider Ratio:
- Vdivider Ratio= Max Input Voltage/Fullscale value = 5.405976676
5. Read ADC value :
adcr = adc.read(0) print(" ReadADC : "..adcr)
Average result for adcr = 1017
THIS IS IMPORTANT !! It tell us that we are inside the ADC domain as adcr < 1024 !
IF adcr > 1023 then you need to adjust your voltage divider resistors to fit inside ADC domain!
6. Calculate LSB in 2 ways to cross check that we have the right value:
- LSB = Input Voltage read by multimeter/ADC readed Value =
= 5.1919 / 1017 = 0.005105113 V OR
- LSB = (ADC Input pin read Voltage by multimeter/ADC readed Value/)*Vdivider Ratio =
= (0.96038/1017)*5.405976676 = 0.005105113 V It looks that we have the right LSB Value for our exercise!
If you want to know also the ADC LSB, then
LSB = ADC Input pin read Voltage by multimeter/ADC readed Value =
= 0.96038/1017 = 0.000944346 V
Guess what’s happening if you multiply ADC LSB with Vdivider ratio 🙂
7. Software Implementation for the ADC read function:
function readADC() ad = 0 LSB = 0.005105113 --calibrate based on your voltage divider AND Vref! adcr = adc.read(0) ad= adcr*LSB print(" ReadADC : "..adcr) print(" Read Voltage : "..ad) return ad end
and some results in the terminal window:
> SENT: readADC() readADC() ReadADC : 1017 Read Voltage : 5.191899921 >
1 Comment
Michal · February 13, 2019 at 5:48 pm
Thank you for this article, it really is the easy way example! I followed you step by step, only changed Vmax to 4.2V and successfully made voltage divider and have workign Li-Pol battery voltage measuring on my old Wifi Witty ESP8266 board running on battery, monitoring CO2, temperature, humidity and air pressure in my living room.