You can measure voltages using the ADC on the ESP8266. This is practical, for example, to record a battery voltage and thus the charge level of the battery or to record brightness using an LDR and a series resistor.
However, the ADC of the ESP8266 is somewhat special. Unfortunately, without additional external wiring, this can only determine voltages up to a maximum of 1V. It is therefore not possible to directly determine the voltage of a LiPo battery because the voltage range of a healthy LipO is from 3.2 to 4.2V.
In order for the ADC to also be able to measure higher voltages, a trick is used. A voltage divider is used to scale the voltage to be measured down to the possible voltage range of the ADC.
The ADC then measures voltages in the range from 0V to 1V, which represent a higher value on the actual voltage divider. For example, a voltage divider is installed on the board of the Wemos D1 Mini to measure voltages up to 3.3V. That's practical, but unfortunately it's not enough to be able to measure the voltage of a LiPo, for example.
How you can extend this maximum voltage range of the ADC is described in the following article.
Contents
Requirements
Helpful articles:
Before you start with this article, you should have dealt with the basics of soldering. You can find information about this in the following article.
Electronics – My friend the soldering iron
Required tool:
In the following list you will find all the tools you need to implement this article.
Required material:
In the following list you will find all the parts you need to implement this article.
Safety instructions
I know the following notes are always kind of annoying and seem unnecessary. Unfortunately, many people who knew "better" have lost eyes, fingers or other things due to carelessness or injured themselves. Data loss is almost negligible in comparison, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Because even the coolest project is not worth injury or other trouble.
https://www.nerdiy.de/sicherheitshinweise/
Affiliate links/advertising links
The links to online shops listed here are socalled affiliate links. If you click on such an affiliate link and make a purchase via this link, Nerdiy.de will receive a commission from the relevant online shop or provider. The price does not change for you. If you make your purchases via these links, you support Nerdiy.de in being able to offer other useful projects in the future. 🙂
How does a voltage divider work?
A simple voltage divider actually always consists of at least two resistors. In the circuit diagram of the Wemos D1 Mini below you can see a voltage divider in the area marked red.
If you are not interested in the function of a voltage divider, you can simply skip this paragraph. 🙂
There are two resistors installed in series. The upper connection “A0” comes from the connection pin “A0” on the Wemos D1 Mini board. The voltage to be measured (for example your LiPo) is connected to “A0”.
The “ADC” connection then leads to the actual ADC of the ESP8266.
Why are you doing all this?
A property of a series connection of two resistors is used here.
For a series connection of two resistors, the following applies, among other things, to the ratio of the total voltage to an individual voltage at one of the resistors is equal to the ratio of the total resistance to one of the individual resistors. This sounds complicated at first but is explained below. 🙂
First of all: As a formula the whole thing would look like this:
Formula 1:
or
Formula 1.2:
“U1” and “U2” are the voltages that drop across the resistor “R1” and “R2”. “U” is the same as U1 + U2 because in a series connection of (simple ohmic) resistors, the individual voltages add up to the total voltage.
In short, the following applies:
The same applies to the resistances, because the total resistance of a series connection also consists of the sum of the individual resistances.
These formulas can now be used quite well in Formula 1 and you get the following:
“U1” and “U2” are the voltages that drop across the resistor “R1” and “R2”, respectively. “U” is the same as U1+U2 because in a series connection of (simple ohmic) resistors the individual voltages add up to get the total voltage.
To do this, the formula must be changed so that the total voltage “U” can be calculated from the given values. The converted formula results in:
Formula 2:
If you now insert the corresponding values here, the following results:
With the resistance values used in the voltage divider, voltages of up to 3.2V can be measured on the ADC.
The practical thing: If you change these resistance values, you can also measure higher voltages with the ADC. For example, below is the voltage of a LiPo.
Voltage divider on the Wemos D1 Mini
So now you know roughly how a voltage divider works and what it can be used for, among other things. With this knowledge, we now want to find resistance values for the voltage divider in order to be able to measure the voltage of a LiPo battery. Its final charging voltage is a maximum of 4.2V. So that we can reliably measure this upper voltage, it is advisable to choose a slightly higher voltage. Let's just say 4.3V.
Examples of resistors R1 are given in the next paragraph for the usual voltage values of 4.3V, 5V, 9V and 12V. This way you can save yourself the math. 🙂
The goal is now to determine the resistance values for R1 and R2.
At the same time, we already know that U=4.3V and U2=1V have to be. This means that to calculate a value using Formula 2, we only have one value left that we have to determine in order to be able to calculate the last open value. But let's make it easy here and simply set this value. 🙂
We first set R2 to 100kOhm and see what value we get for R1.
The change now results in Formula 1 including the collected knowledge
Formula 3:
In this formula we know all (green) values except R1 (red). So we try to change the formula to R1 and get the following:
simplified and rearranged, this results in:
and the same colored accordingly:
In this formula we now know all the green values and the (red) value we are looking for is neatly on the right side of the equal sign. 🙂
The desired value for R1 can now be calculated by inserting:
This results in a value of 330kOhm for R1. So if we replace the 220kOhm resistor installed on the board of the Wemos D1 Mini with a 330kOhm resistor, we expand the maximum measurable voltage range from 3.3V to 4.3V and can therefore also measure the entire voltage range of a LiPo battery. 🙂
Formula for calculation:
 U2 is always 1V
 U is the desired maximum measurable voltage, e.g. 4.3V
 You should just set R2. Ideally in a range of 10kOhm to 100kOhm to keep the current across the voltage divider low.
Example values for maximum voltages and the associated resistance values
You can use the following resistance values to extend the maximum measurable voltage of the ADC to the specified voltage.
Maximum measurable voltage: 4.3V
R1=330kOhm, R2=100kOhm
Maximum measurable voltage: 5V:
R1=400kOhm, R2=100kOhm
Maximum measurable voltage: 9V:
R1=200kOhm, R2=25kOhm
Maximum measurable voltage: 12V:
R1=275kOhm, R2=25kOhm
You can now simply replace the marked 0805 resistors R1 and R2 with the resistors you want. 🙂
Configuration in ESPEasy
In ESPEasy, the conversion of the ADC into a voltage can be done very easily.
To do this, simply set the device “Analog input – internal” as shown.
If you use a maximum voltage other than 4,300V, you will of course have to adjust this value accordingly. 🙂
Have fun with the project
I hope everything worked as described for you. If not or you have questions or suggestions please let me know in the comments. I will then add this to the article if necessary.
Ideas for new projects are always welcome. 🙂
PS Many of these projects  especially the hardware projects  cost a lot of time and money. Of course I do this because I enjoy it, but if you think it's cool that I share the information with you, I would be happy about a small donation to the coffee fund. 🙂
Hi, just found your site, that's exactly what I'm looking for. How do I build the whole thing if I want to make the resistors external? I don't think I can do that on the Wemos with the resoldering.
Hey Mathias,
For which voltage range do you want to adapt the voltage divider? Theoretically, you can also change the voltage divider by soldering on suitable external resistors. One would have to calculate. maybe it is even enough to simply use another series resistor (for the upper part of the voltage divider). 🙂
Hi and thanks for your reply. I would like to measure a normal 18650 cell on the Wemos that should serve as a battery. Wouldn't like to solder around on the Wemos, since it's very small. Would like to just build two resistors in front of it if that's possible?
Hey Mathias,
you should be able to do that with just a 100k resistor. You then have to switch it like this:
A0–>100k_resistance–>LiPo_plus  LiPo_minus> GND
Then 3.2V should drop across the two series resistors and therefore exactly 1V across “R2”. Long story short: 4.2V on the LiPo would then correspond to the ADC value of 1023. Given the high resistances and the corresponding tolerances, the measurement is probably not super accurate. But it is definitely enough to record the level of the LiPo. 🙂
Remember that the relationship between LiPo voltage and “level” is not completely linear. And I would also “smooth” the measured values a little using a moving average. Otherwise, you could get a “false alarm” when the load is higher (and the battery voltage drops as a result). 🙂
Hope it's explained clearly.
Best regards
Fab
Hi! You really built a great site. Very informative and explained in small steps. I'm working with a Wemos for the first time and came across your tutorial while researching the ADC.
I would like to read a level sensor for my cistern. This measures the water column and lets through 420mA depending on the filling level. It is supplied with 24V. Do you have a tip on how I could remodel the input from the Wemos for this?
All the best, Martin
hey martin,
Thanks. 🙂
Basically you need a suitable resistance. The ADC of the Wemos breakout board can measure voltages from zero to 3.3V. You can use a resistor to convert your sensor’s “current signal” into a voltage. I've looked over it. A 175 ohm resistor should fit.
You would then have to connect it like this: (The zigzag line is supposed to represent the resistance.) I would also recommend switching a highimpedance (eg 10k) resistor in front of the ADC. 🙂
Sensor_signal 420mA

+————–ESP_ADC

\
/ 175 ohms
\
/





GND
If you want, let me know if it worked. 🙂
Oh and on the ADC you should then be able to measure a voltage proportional to the current flow. The formula U=R*I applies (this is Ohm's law). R is the resistance (i.e. 175Ohm), I the current of the sensor (i.e. a value between 420mA) and U is the voltage that is then measured at the ADC or drops across the resistor. You can also test the circuit without having connected the Wemos. Simply measure the voltage with a multimeter. 🙂
Best regards
Fabian
Hi Fabian,
Many thanks for the help!
Used a precision trimmer as a resistor to calibrate the voltage.
Works great so far, but the values of the ADC jump plus/minus 5 points. I've already set a 100n against ground to stabilize, but that only helps to a limited extent.
If I measure the mA of the sensor like this, this value is stable. Depends on the ADC of the Wemo.
Do you have any tips on how I can smooth this out even better?
Best regards Martin
hey martin,
I am glad, that it worked. Good idea with the precision trimmer. 🙂
An idea how you could smooth that even better (in software) would be a moving average.
This is actually a common procedure if you have a signal that you can read in quickly.
To do this, you simply read out the ADC several times (e.g. 100 times) and then calculate the average of it. Small deviations should then actually be compensated for and no longer be noticeable. 🙂
Best regards
Fabian
Hi,
Thanks for your great info page 🙂
Must have been a lot of work.
Can you do that directly in the Wemos in espeasy
convert to percentage?
Greetings Heiko
Hey Heiko,
Thanks very much. Yes, I've already invested an hour or two here. 😀
Yes, that should work. Here's an article that might help you: https://www.letscontrolit.com/wiki/index.php/EasyFormula
Best regards
Fabian
Hi Fabian, Great article here!! well done 🙂
I have a wemos d1 mini with its original smd resistors R1 220kOhm and R2 100kOhm
I want to measure voltages up to 29.4V (7s battery)
so which resistors you think its better to pick?
solution1= R1 300kOhm + R2 10kOhm (up to 32v) least efficient but best accurate
solution2= R1 680kOhm + R2 20kOhm (up to 36V) low efficient medium accuracy
solution3= R1 470kOhm + R2 10kOhm (up to 49v) = most efficient but least accurate
Please do the math and help me pick the best solution.
Thanks 🙂
Hi Nick,
thanks 🙂
To be honest I would go with solution1. Of course the ~9micro amps could be anyoing (depending on your usecase) but in most cases they shouldnt bother that much.
Or do you plan to build something where this 9µA could be problematic? 🙂
Best regards
Fab
I will just use Wemos D1 Mini with tasmota to monitor my 24V (7s battery) that is charged with a solar panel. HUUUGE thanks for the feedback 🙂
Sounds cool, I think in this case you don't have to care so much about the 9µA 🙂
Good luck with the project. 🙂
Best regards
Fab
RE edit
or you can recommend another combination. I have those metallic resistors: 10Ω, 22Ω, 47Ω, 100Ω, 150Ω, 200Ω, 220Ω, 270Ω, 330Ω, 470Ω, 510Ω, 680Ω, 1KΩ, 2KΩ, 2.2K Ω, 3.3KΩ, 4.7KΩ, 5.1KΩ, 6.8KΩ, 10KΩ, 20KΩ, 47KΩ, 51KΩ, 68KΩ, 100KΩ, 220KΩ, 300KΩ, 470KΩ, 680KΩ, 1M
Hello Fab,
I just checked this article, and unfortunately a few errors crept in...
1.
Formula 2 is wrong!
Instead of Rsum / R2 + U2 = U it should read Rsum/R2 * U2 = U
Regardless of the fact that the line underneath is mathematically incorrect.
330/100+1 would be 4.3 ... and the units are not correct either (kOhm/kOhm cancels out)
Furthermore: Where does the 330 kOhm come from? R1=220 kOhm and R2 = 100 kOhm > therefore Rsum = 320 kOhm and not 330 kOhm
With these ohm values (220 and 100 kOhm) you get over 1 volt at the ADC ... namely 1.03 volts
But I can't judge whether this is already harmful for the analogue input.
2.
Formula 3 is ok so far, but it was not derived from formula 1.2 but from formula 1
(Of course, this has no further influence on the further calculation methods.... mentioned only for clarity)
3.
I can't understand the calculated value for the 175 ohms (inquiry from Martin regarding the level sensor). According to my calculations, the value for the resistor would be around 52 ohms...
Please don't misunderstand my comment...
LG Ralph
Hi Ralph,
thanks for your hints. I corrected the errors that I could understand. 🙂
to 1. What do you mean by the units are wrong? Since the kOhms are shortened, V remains above: [kOhm/kOhm*V=V] That's what it says there, isn't it?
to 3. Your calculation refers to a maximum measurable voltage value of 1V. The Wemos D1 Mini (which Martin addresses in the comment) is a breakout board for the ESP8266. A voltage divider is connected upstream of the ADC (which he explicitly did not want to change). That's why the 175 ohms go very well with the extended measuring range (up to 3.3V) of the Wemos D1 Mini. 🙂
Oh no problem. I'm glad if someone finds a mistake and lets me know. 🙂
Thank you and best regards
Fabian
Hello, I know it's already written here a bit, but for me battery monitoring would be important for a motorhome. I would now have used 1MOhm and 470k as resistors. Am I on the right track? I also want to work with Deep Sleep. It's actually just a matter of keeping the battery from being completely discharged and keeping me informed. There are currently no plans to switch off the load. It would be nice if there was a way to charge the battery if necessary.
Hi Sasha,
take a look at the section with example values. I also specified a resistor combination for 12V. 🙂
Best regards
Fabian
Hello, I finally found a good website.
I share many topics with you.
I intend to build a voltage controlled solar excess control.
I am currently measuring the performance of the solar modules and switching a consumer on and off at a certain value. I do this via an ESP 8266 and a Mosfet (IXFK44N50P). I designed the modules so that I can use them to operate heating or switching power supplies (230V). The whole thing without an inverter.
My question: with a voltage divider of 1.1Mo + 47Ko I can get around 1V. at an input voltage of DC 230V. I don't understand the statement that you can only measure a maximum of 42V at the ESP.
Hi Uwe,
What you are doing there is lifethreatening. Particularly high direct voltages (not yet fused) are lifethreatening! You MUST (!) solve this surplus regulation differently.
Even if this were theoretically technically possible, it is lifethreatening and extremely negligent to do so. Please remember that people have already died in such constructions. Even the people who thought they knew better.
I don't know how you got the 42V, but most likely the value refers to the maximum permissible touch voltage. You can find information about this here: https://de.wikipedia.org/wiki/Ber%C3%BChrungsspannung
If you want to build such a surplus control, I recommend a different approach: Measure the current generation output of the panel with a WiFienabled smart socket and then switch the consumers depending on the current power value.
Again: Any handling of high direct voltages is lifethreatening. You definitely shouldn't take this risk!
Best regards
Fabian
Hi,
Thanks for your answer.
I have been doing this hobby since the creation of the transistor, i.e. around 50 years.
Thanks for your safety tips, but no matter how careful you are, it always happens from an ELKO.
Now on to the topic:
There is little readymade electronics for this high DC voltage, so I have to build most of it myself.
I use Fhem and control and monitor everything with it.
The only thing that is new to me is measuring voltage on the ESP 8266.
It's a shame that you don't respond to my question so much.
Hi Uwe,
I have answered your question: namely, that what you are planning to do there is lifethreatening. This is also the reason why there is so little on this topic.
In addition, measuring the output voltage of a solar cell is not suitable for drawing conclusions about its current performance. Output voltage is not linear with power. Especially when an MPP tracker is used, the operating point can shift so that the output voltage even becomes smaller as the power increases.
I don't want to offend you, but the length of time you do something says nothing about how well/confidently you have mastered a topic. Keyword “operational blindness”.
So I would like to ask you once again to solve your problem differently and emphasize once again that what you are doing is lifethreatening.
I would be happy to help you solve the problem differently and safely. To do this, you have to describe in more detail what you actually want to do.
Best regards
Fabian