HowTo: ESPEasy / Wemos D1 Mini – Adapt ADC to another maximum voltage

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. 🙂


Requirements

Helpful articles:
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

Tools required:

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 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/


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. 🙂

Quelle: https://www.reddit.com/r/esp8266/comments/9idpbe/does_anyone_know_where_to_buy_genuine_wemos_d1/

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:

Formula 1:          \frac{R1+R2}{R2}=\frac{U1+U2}{U2} 

or

Formula 1.2:    \frac{R1+R2}{R1}=\frac{U1+U2}{U1}

“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.

Short: U=U1+U2

The same applies to the resistors, because the total resistance of a series connection consists of the sum of the individual resistors.

Rsum=R1+R2

These formulas can now be used quite well in Formula 1 and you get the following:

\frac{Rsum}{R2}=\frac{U}{U2}

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:

Formula 2:       \frac{Rsum}{R2}+U2=U

If you use the corresponding values here, the following results:

\frac{330k}{100k}+1V=3.3V

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

Formula 3: \frac{{\color{Red} R1}+{\color{Green} R2}}{{\color{Green} R2}}=\frac{{\color{Green} U}}{{\color{Green} U2}}

In this formula we know all (green) values except R1 (red). So we try to change the formula to R1 and get the following:

1+\frac{R1}{R2}=\frac{U}{U2}

simplified and converted:

(\frac{U}{U2}-1)*R2=R1

and colored the same accordingly:

(\frac{{\color{Green} U}}{{\color{Green} U2}}-{\color{Green} 1})*{\color{Green} R2}={\color{Red} R1}

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:

(\frac{4.3V}{1V}-1)*100kOhm=330kOhm

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. 🙂

This is the bottom view of the Wemos D1 Mini. The voltage divider for the ADC is also installed there.

Formula for calculation:

(\frac{U}{U2}-1)*R2=R1

  • 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
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. 🙂

Detailed view of the voltage divider on the Wemos D1 Mini

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. 🙂

Fab

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. 🙂

Buy Me a Coffee at ko-fi.com

4 comments

  1. Hi, habe gerade deine Seite gefunden, dass ist genau das was ich suche. Wie baue ich das ganze auf, wenn ich die Widerstände extern machen will? Ich glaube ich bekomme das auf dem Wemos mit dem umlöten nicht hin.

    1. Hey Mathias,
      für welchen Spannungsbereich möchtest du den Spannungsteiler denn anpassen? Man kann den Spannungsteiler theoretisch auch durch das anlöten passender externer Widerstände verändern. Müsste man mal durchrechnen. Vllt. reicht es sogar einfach einen weitere Vorwiderstand (für den oberen Teil des Spannungsteilers) zu verwenden. 🙂

      1. Hi und danke für deine Antwort. Ich möchte einen normale 18650 Zelle am Wemos messen die als Akku dienen soll. Möchte nur ungern am Wemos rum löten, da es ja sehr klein ist. Würde gerne einfach nur zwei Wiederstände davor bauen wenn das geht?

        1. Hey Mathias,
          das müsste man sogar mit nur einem 100k Widerstand hinbekommen. Den musst du dann so verschalten:
          A0–>100k_widerstand–>LiPo_plus | LiPo_minus–> GND
          Dann müssten 3,2V an den beiden Vorwiderständen abfallen und somit genau 1V an dem “R2”. Lange rede kurzer Sinn: 4,2V am LiPo würden dann dem ADC-Wert von 1023 entsprechen. Bei den hohen Widerständen und den entsprechenden Toleranzen ist die Messung wahrscheinlich nicht super genau. Aber es reicht auf jeden Fall aus um den Füllstand des LiPos zu erfassen. 🙂
          Denk dran, dass der Zusammenhang zwischen LiPo Spannung und “Füllstand” nicht komplett linear ist. Und ich würde die Messwerte auch über einen gleitenden Mittelwert etwas “glätten”. Sonst könntest du bei höherer Belastung (und dadurch Spannungseinbrüchen der Akkuspannung) einen “falschen Alarm” bekommen. 🙂
          Hoffe es ist verständlich erklärt.
          Beste Grüße
          Fab

Leave a Reply

Your email address will not be published. Required fields are marked *