You can measure voltages using the ADC on the ESP8266. This is useful, for example, to measure a battery voltage and thus the state of charge of the battery or a brightness using an LDR and a series resistor.
The ADC of the ESP8266 is somewhat special. Without additional external wiring, this can unfortunately only handle voltages up to max. 1V. It is therefore not possible to determine the voltage of a LiPo battery directly, because the voltage range of a healthy LiPo ranges from 3.2 to 4.2V.
So that the ADC can also 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 stand for a higher value on the actual voltage divider. For example, a voltage divider is also installed on the circuit board of the Wemos D1 Mini to measure voltages up to 3.3V. This is practical, but unfortunately it is not enough to measure the voltage of a LiPo, for example.
How you can expand this maximum voltage range of the ADC is described in the following article.
Hints for our lovely english readers: Basically, many of the articles on Nerdiy.de are translations from the original german articles. Therefore, it may happen here and there that some illustrations are not available in english and that some translations are weird/strange/full of mistakes or generally totaly wrong. So if you find some obvious (or also not obvious) mistakes don't hesitate to leave us a hint about that in the comment section.
Also please don't get confused, that instead of a "dot" often a "comma" is used as decimal separator. 🙂
Before you start this article, you should have worked on the basics of soldering. You can find information on this in the following article.
Electronics – My friend the soldering iron
In the following list you will find all the tools you need to implement this article.
In the following list you will find all the parts you need to implement this article.
I know the following hints are always a bit annoying and seem unnecessary. But unfortunately, many people who knew it "better" from carelessness lost their eyes, fingers or other things or hurt themselves. In comparison, a loss of data is almost not worth mentioning, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Even the coolest project is worth no injury or other annoyance. https://www.nerdiy.de/sicherheitshinweise/
The links to online shops listed here are so-called affiliate links. If you click on such an affiliate link and shop via this link, Nerdiy.de receives a commission from the online shop or provider concerned. The price doesn't change for you. If you do your purchases via these links, you will support Nerdiy.de in being able to offer further 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 below of the Wemos D1 Mini you can see a voltage divider in the red marked area.
If you are not interested in the function of a voltage divider, you can simply skip this part. 🙂
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 do you do this?
We are using a property of a series connection of two resistors here.
For a series connection of two resistors, it is true, among other things, that 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. That sounds complicated at first but is explained in the following. 🙂
In advance: As a formula, the whole thing would look like this:
“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.
The same applies to the resistors, because the total resistance of a series connection consists of the sum of the individual resistors.
These formulas can now be used quite well in Formula 1 and you get the following:
With this formula you can now understand why these resistance values were selected in the circuit diagram of the Wemos D1 Mini shown above. R1 has the value 220kOhm and R2 the value 100kOhm. With the knowledge that at the point “ADC” (here the ADC of the ESP8266 is connected) a maximum of 1V can be applied, for which maximum voltage the voltage divider can be used.
To do this, the formula must be changed so that the total voltage “U” can be calculated from the given values. The changed formula results in:
If you use the corresponding values here, the following results:
With the resistance values used in the voltage divider, voltages up to 3.3V can be measured at the ADC.
The practical: If you change these resistance values, you can also measure higher voltages with the ADC. In the following, for example, 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. With this knowledge, we now want to find resistance values for the voltage divider to be able to measure the voltage of a LiPo battery. Its final charge voltage is a maximum of 4.2V. So that we can measure this upper voltage safely, it is advisable to choose a slightly higher voltage. Let’s just say 4.3V.
For the usual voltage values of 4.3V, 5V, 9V and 12V, examples for the resistors R1 are already given in the next paragraph. So you can save yourself the math. 🙂
The goal is to determine the resistance values for R1 and R2.
At the same time, we already know that U = 4.3V and U2 = 1V. This leaves only one unknown value to calculate with formula 2, which we have to determine in order to be able to calculate the last open value. But we make it easy for us here and simply set this value. 🙂
We first set R2 to 100kOhm and see what value we get for R1.
When converted, Formula 1.2 now includes the knowledge gathered
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 converted:
and colored the same accordingly:
In this formula, we now know all the green values and the (red) value we are looking for is right on the right side of the equal sign. 🙂
The desired value for R1 can now be calculated by inserting:
The value for R1 is 330kOhm. So if we replace the 220kOhm resistor on the circuit board of the Wemos D1 Mini with a 330kOhm resistor, the maximum measurable voltage range is expanded from 3.3V to 4.3V and can therefore also measure the complete 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 select a value for R2. Ideally in a range from 10kOhm to 100kOhm to keep the current through the voltage divider low.
Example values for maximum voltages and the associated resistance values
You can use the following resistance values to expand the maximum measurable voltage of the ADC to the specified voltage.
Maximum measurable voltage: 4,3V
Maximum measurable voltage: 5V:
Maximum measurable voltage: 9V:
Maximum measurable voltage: 12V:
You can now simply replace the marked 0805 resistors R1 and R2 with the resistors you want. 🙂
Configuration in ESPEasy
The conversion of the ADC into a voltage can be done very comfortably in ESPEasy.
Simply set the device “Analog input – internal” as shown.
If you use a maximum voltage other than 4,300V, you must of course adjust this value accordingly. 🙂
I hope everything worked as described. If not or you have any other questions or suggestions, please let me know in the comments. Also, ideas for new projects are always welcome. 🙂
P.S. 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 appreciate it that I share these information with you, I would be happy about a small donation to the coffee box. 🙂