Getting I2C regulated 0-10Volt out of an Arduino or ESP8266

The max Output of an Arduino or ESP8266 is roughly 5, respectively 3.3 Volt. The Arduino has the possibility to output a varying voltage on its Analog output pins, whereas the ESP8266 can emulate a varying voltage on a PWM addressed pin, with the help of a low pass filter (basically an RC filter [4k7/10uF]). There are instances where it would be handy/necessary to have a 10 Volt regulated output: Some dimmable lights require a control signal of 0-10Volt as do some commercial Power-motor controllers.
It is not hard to get such a signal from the mentioned microcontrollers, by amplifying the output.

The circuit shows such an amplifier. It is a simple Opamp non-inverting amplifier. When the Op Amp receives and input on the non-inverting output,  it will raise or lower it’s output until the level on the inverting output is matched.
As we are using a voltage divider to feedback the output voltage, we can make the opamp to drive the output higher than the input on the non-inverting pin.
The output voltage is thus defined as:
Vo=Vin x (1+(Rtop/Rbottom)), in which Rtop is R1+the top fraction of  P2 and Rbottom is R2+the bottom fraction of P2.
the amplification thus can vary between 1+(10/20) and 1+(20/10) or: between 1.5 and 3.
Ideally, with the variable resistor in the exact middle, the amplification equals 2 and the Output -when driven by the Arduino- should be 10 Volt, however, the Arduino may not output exactly 5 Volt, in which case the output can be trimmed with the variable resistor.
As the LM358 is not a rail to rail opamp, the Vcc needs to be higher than the required output. As the datasheet is a bit unclear on this, I have chosen 12 Volt, which turns out to work fine.
When using the ESP8266, the maximum output of the circuit will be 3×2.2=9.9Volt. In practice however the output on the ESP8266 pins will probably not reach 3v3, also, making an analog output with PWM and a low passfilter is not ideal.

Enter, the MCP4725
The ‘problems’ mentioned above, can largely be remedied by using  a proper DAC (Digital Analog Converter). The MCP4725 is a cheap, easy to use DAC that is available on break out boards at various chinese webshops.
The MCP4725 is a 12 bit, rail-to-rail DAC, which means that if you feed it with 5 or 3V3, that will come out of it (well…almost). It also has on board EEPROM for storage of setup, but for this build that is not really necessary. There is a library available in the Arduino IDE library manager. The Vcc pin requires an appropriate bypass capacitor of about 0.1 µF (ceramic) to ground. An additional 10 µF capacitor (tantalum) in parallel is also recommended to further attenuate high frequency noise present in application boards.

As the MCP4725 is a 12 bit DAC, the range goes from 0-4095. Writing 4095 to the MCP 4725 to the DAC therefore would give the max output. A program for the MCP4725 thus would look like something like this:

#include <Wire.h>
#include <Adafruit_MCP4725.h>
Adafruit_MCP4725 dac;

void setup(void) {
dac.begin(0x60); // This may vary between 0x60 and 0x65 for your board

void loop() {
// 1V Out from MC4725 -> 2V Out from LM358
dac.setVoltage(819, false); // false meaning "do not write to EEPROM"
// 2V Out from MC4725 -> 4V Out from LM358
dac.setVoltage(1638, false);
// 2.5V Out from MC4725 -> 5V out from LM358
dac.setVoltage(2043, false);
// 3V Out from MC4725 -> 6V Out from LM358
dac.setVoltage(2457, false);
// 4V Out from MC4725 -> 8V Out from LM358
dac.setVoltage(3276, false);
// 5V Out from MC4725 -> 10V Out from LM358
dac.setVoltage(4095, false);

The max output current of the LM358 is a bit obscure from the datasheet, it is probably around 20-40mA which is sufficient for most control signals. If you need more current (maybe you directly want to drive a motor), consider the LM675 that has a max output of 3 Amps. Be careful though as the LM675 can easily oscillate at amplification <10. Oscillation will lead to output going to (almost) rail even at no input and the chip warming up). Use decoupling capacitors – preferably a ceramic (100nF) and tantalum (10uF) type in parallel- and keep the leads short. A a small capacitor (on the order of 50 pF to 500 pF) across the circuit input, helps preventing the input leads to function as antenna.
A better choice might be the OPA544T, though that only has a 2 amp output, but is more stable at low amplifications. It is also more expensive.

Another way to get more current is with a power transistor, such as a BD137 (1A) or even a good old 2N3055/TIP3055 (15A).  However,the hFE starts to play an important role in this circuit. take for instance the BD137 that has a worst case hFE of 40. When it needs to deliver 1A, it will require  25mA as its base current. That is on the edge of what an LM358 seems to be able to deliver. If we add a BC547 that has a hFE of 110-800, a much smaller current will be required from the 358.
Other possible transistors in this range are 2SD882 (2Amp hFE 60), BD135, BD137, BD139 (1.5 A, hFE 40)
In case of the 2N3055, that has a worst case hFE of 20. If 15A is required, a base current of 750mA is required. Adding a BC547 would bring that down substantially, but it can’t deliver 750mA. A 2N2222 could, but it’s hFE is only around 30 at that current so you would need a BC547 AND a 2N2222  AND a 2N3055

In this light a TIP120 or TIP122  is a better choice as those already are powerdarlingtons with an hFE of about 1000 and a max current of 5A

Connecting an ESP8266 to the outside World

I admit that this is a subject that is really really basic and no doubt explained at many places. Yet, I still see the question posed time after time at various fora and sadly the replies are often not on the ball. Things like “You need REST API”, “Just use Blynk”, “Just use IFTTT” or “Why would you want to do that”, surely will not help the struggling novice on their way.
So, rather than having to repeat my explanation again and again, it is better, I just write an article, explaining it…..for the novices amongst us.

Finding 2 IP addresses
OK, so you have an ESP8266 that reads a sensor and you want to be able to get the reading when you are away from home, using internet. It is quite simple, but you need to know 2 distinct IP addresses.
1- you need to know the IP address that connects your computer to the internet. You can easily find that on websites like ““. It is also called ‘Public IP’ or ‘WAN IP’
2- you need to know the IP address that your ESP8266 has in your local network. You need to check the DHCP table in your router, but many ESP8266 programs actually print that number as well in the serial port. These numbers almost always looks like “192.168.x.yyy”. You can also use a phone app like ‘port authority’ to find the number for you.

Port forwarding
So what happens when you are on your holiday address in Reykjavik or Kabul and you want to know what your sensor back home says? You open a browser, type in the IP address you found under “1”. well that connects you via the internet to your router back home. However, your router when it gets that call, doesn’t have a clue what to do with it, let alone that it knows it should forward that call to the ESP8266 that is connected to it, because you didn’t tell it what to do with it.

So when you get back home you have to instruct your router to forward the calls to your ESP8266 and that is done via “IP forwarding” or “Port forwarding”.
What makes that a bit complicated is that it is done differently in various routers, so you would have to check your specific router for it. Look for “IP forwarding” or “Port forwarding” and in some routers it is called ‘virtual server’.
When you found that you usually have to fill out 2 things. The port that is being forwarded and the IP address it has to be forwarded to. For the port you choose “80” and for the IP address to forward to, you use the local ip address, you found under point “2”.It is the address that started with “192.168.”
If it doesnt allow you to forward a specific port, but it does allow you to forward a service, choose ‘http’ for that service.
If you really cant find the port forwarding, but You happen to find something that is called “DMZ” choose that to forward to the ip address of your ESP8266 (the address starting with “192.168”) that is less elegant, but it will work.

Setting up a Webserver
So you are back on holiday and you want to check again. You fill out the internet IP number of your home system (the number you found under “1” and yes, this time the call comes in to your router and that one knows exactly what to do,so it forwards the call to your ESP8266. The ESP duly receives the call and……….has no idea what to do with it because chances are you didnt tell it what to so with it.
Your ESP8266 needs a program that presents the data in “Webpage”. That is not hard, there are plenty of programs who do that. These programs usually are called “Webservers”. Plenty of those available. (Look in your Arduino IDE examples or check e.g. randomnerdstutorial.)

It works!!!! for now: MAC and Address reservation
So, you now have programmed your ESP8266 with a webserver program that presents the sensor data on its own little webpage.
In order to test it when you are at home you open your browser and fill out the Local IP number (that you found under “2” starting with 192.168) and yes, you see the webpage. To make sure you also fill out the address you found under “1” (thus the address that your system has on the worldwide interweb) and great it all works. You go back on holiday and check from the other side of the world and great still works. Mission accomplished!!!

…..or is it. Suddenly it doesn’t work anymore. What on earth is wrong????? Well chances are that you have restarted your ESP8266 a few times and suddenly it has received a different IP address in your Local network. Say it once had, but now you find out it has Your router however still sends the incoming call to the first IP number because that is what you told it to do. Now it is hardly feasible that you constantly check if the local IP is still the same and if it isnt that you keep changing your forward instructions.
Fortunately, most routers allow you to reserve a specific IP number in your network, for a specific device, and this is how you do it:
When you earlier found the IP number of your ESP8266 (the one starting with 192.168), you probably also found its MAC number. A MAC number (or addres rather) looks something like: “5D:CF:7F:AC:61:66”
Now you need to look in your router that is called “MAC binding” or “Address reservation”. The object here is that you give the MAC number, as well as the IP number (192.168.xx.yyy) that should always be reserved just for the device with that specific MAC stands for “media access control”.

Yes, works again
OK you got that done,so now whenever you go to the internet address of your system back home, the call comes into your router, the router knows what internal address to send it to, that address is always the same, and the device on that address knows what to do: it presents (“serves”) a webpage to the computer half across the world behind which you are sitting. That webpage can present data, but it may just as well receive commands from you, e.g. to turn a lamp on or off. Realuse though that anybody who knows your public IP address can also check that webpage. That might not be so bad if it just presents temperature, but it might give a problem if strangers can switch a lamp off and on in your home. So once you set it all up, you may consider making a password  protected webpage.

Let me compare the situation with making a phonecall to a hotel: you need the hotel’s phonenumber (compare to the internet IP) and you need to know what room someone is in (the Local IP). The person in the room needs to know what to do with your questions (serving a webpage). That all works well as long as the person is in the same room (the address reservation: when Mr MAC comes to the hotel, he always gets room

A final touch: DNS providers
So, everything works now………..but what if there is a rezoning and the hotel’s phonenumber changes???? Yes, you could look up the new number, but it is hardly practical.
But what does that mean in internet terms? Well, most of us get a so called ‘Dynamic IP address’ from our provider. Usually those addresses stay the same for a long time, but when you happen to restart your router you could get a different internet IP address. Sure you could check your internet address regularly, but that is only possible when you are at home. What to do when you are away from home????
The solution is a (free) DNS provider.
DNS providers provide you with an internet name, that you can use rather than an IP number. The essence though is that you get to put a tiny bit of software on your computer, that regularly checks your internet address and if that changes, it tells your DNS provider what the new number is. So you only have to provide a domain name to your browser and the DNS provider then knows the most up to date IP number to connect to.
A popular freeDNS provider for instance was “”, but there are more.

The HTU21D on an ESP8266-01, with MQTT and graphics

Limited as the ESP8266-01 might seem regarding the number of pins, it is still a very capable little device that is sometimes wrongfully underrated.

It is perfect however for projects that demand little I/O, such as reading a single sensor.

I am using the HTU21D temperature and humidity sensor. The ESP01 needs constructing a small board that contains the connectors and a small 1117-33 voltage regulator (soldered on the bottom) so the circuit can be fed with 5 Volt. As I am using an ESP8266-01S -that already has 10k pullups on GPIO0 and GPIO2, I do not have to add resistors for an undisturbed startup and use of I2C. If you are using the ‘old’  ESP8266-01, you have to add 4k7 -10k pullups to GPIO0 and GPIO2. If your HTU21 module already contains pullups, those will function as proper startup resistors as well.

The software (download here) is not too complicated, it first connects to your LAN, but it chooses the strongest of two LANs if available.

It then connects to an MQTT server and sends the sensor readings via MQTT every minute.

I also find it convenient when the software tells me what program it is, so I have added that too.

Finally, as in future I may want to include some time dependent signaling, I included a clock that is being synchronized through the internet.

Should you check your DHCP list, the device identifies as “HTU21”

MQTT Output:

If you like a graphical presentation of your data, I have made another file that will exactly do that. It is based on code by Rui and Sara Santos from (randomnerdtutorials) that I discussed in an earlier  post. It will work with a 512k/32k SPIFFS setting as well as with a 1M/64k setting.
Beware though that many ESP8266-01 (and especially the ESP8266-01S) modules now come with a PUYA memory chip that has been causing some issues when using SPIFFS. The userdata.h file contains 2 defines that need to be set or commented out depending on whether you have or do not have the PUYA chip (2 defines to cover different cores). If you are still on the 2.4.1 core, you will need a patch. That you will have to put here: C:/Users/

Using the ESP8285 M2, with a warning

Next to the previously discussed M3, there is also an ESP8285 M2 (in fact, there is also an M1). Like the M3 it is based on an ESP8285 that has 1Mb on board.
It has 11 I/O pins (including Rx and Tx) as well as an ADC.

An advantage though is that it is available on a development board that has a Vcc and Grnd connection per I/O pin (but read the caveat below), whereas e.g. the Wemos D1 mini only has 1 ground and 3V3 pin.
The board can be programmed directly from the Arduino IDE, but it does not have a circuit present that puts it automatically in flash mode. Therefore it is necessary to push the Flash button, hold it and then press the Reset button and only then release the Flash button.

There is a big caveat with this board though: The row of positive pins opposite the I/O pins, is connected to the INPUT voltage, so even if you feed this board via USB, you get 5Volt on the “V” pins. If you feed your board 3.3 Volt via the 3V3 connector……there is no voltage at all on the ‘V’ pins. Whether this is a design flaw or intentional I do not know.

Using the ESP8285 M3

Chinese webshops sell the ESP-M3. This is a tiny module containing an ESP8285. In short, an ESP8285 is like an ESP8266 but with 1Mb memory on chip. It is for instance the chip that has been used in the SonOff basic till now. The M3 is not a new module,it surfaced in 2016 and was primarily meant for wearables I think.

Sadly it is not really breadboard friendly. 6 pins have a 0.1″ pitch with throughholes, but 6 other pins have a 0.1″ pitch with edge-pads. The alignment between the through-hole and edge pins is hwoever not a standard 0.1″ pitch.
The module has the following pins.

ESP-M3 Compares with Wemos/NodeMCU
GPIO2 (Tx1)(apparently connects to an onboard blue LED D4
GPIO13 (Mosi) D7
Vcc Vcc
Gnd Gnd

The function of the pins is equal to that of the ESP8266. In comparison with the Wemos D1/ESP8266 (11 I/O pins, one ADC pin) the ESP-M3 has only 7 I/O pins, and no ADC. The ESP8285 HAS more pins than that, but these are not all broken out on the ESP-M3 module. All in all that is still 3 pins more than the ESP8266-01.

The use of the ESP-M3 is similar to that of the ESP8266. In order to go into Flash mode, the GPIO0 pin has to be made LOW. For ‘normal use’ the GPIO0 has to be pulled HIGH. The EN pin has to be pulled high. Choose 8285 board for upload.

void setup() {
pinMode(2, OUTPUT);

void loop() {
digitalWrite(2, HIGH);
// turn LED on 
digitalWrite(2, LOW);
// turn LED off

Showing ESP8266 battery voltage in a graph

In a previous post, I dabbled around with presenting sensor readings with the Highcharts graphic library, based on a post on the randomnerdtutorials website.

It might be educational to add another channel to it that reads battery voltage and presents that in a graph as well.
In the picture above there are two colours: when the voltage is above 3.3Volt, the colour is green, when it goes below 3.3Volt the colour turns red.

This is how to do it:
First, grab the Arduino Sketch file from the randomnerdsturorial website. Open it up and add two procedures:

1 locate the procedure called readBME280Humidity() and right above that, add the following lines:

String readBatt(){
float v=analogRead(A0);
return String(v);

2 locate the section that says // Start server.
right above that add:

server.on("/voltage", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", readBatt().c_str());

That’s all we need to do in the Arduino Sketch. Now get the index.html file from the randomnerdstutorial website.
Open up the file in an editor like Notepad++ and find the </script> tag.
Right above that tag, add the following code.

var chartV = new Highcharts.Chart({
chart:{ renderTo:'chart-voltage' },
title: { text: 'LipoVoltage' },
series: [{
showInLegend: false,
name: 'Voltage',
data: [],

zones: [{
value: 0,
color: '#0000ff'
}, {
value: 3.3,
color: '#ff0000'
}, {
color: '#00ff00'

plotOptions: {
line: { animation: false,
dataLabels: { enabled: true }
xAxis: {
type: 'datetime',
dateTimeLabelFormats: { second: '%H:%M:%S' }
yAxis: {

title: { text: 'Voltage (V)' },
min: 0,
credits: { enabled: false }

setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var x = (new Date()).getTime(),
y = parseFloat(this.responseText);
if(chartV.series[0].data.length > 40) {
chartV.series[0].addPoint([x, y], true, true, true);
} else {
chartV.series[0].addPoint([x, y], true, false, true);
};"GET", "/voltage", true);
}, 30000 ) ;

Next, locate the following section in the file, and add the indicated line

Save the html file in the data folder of your Arduino sketch.

Now we have to take care of the hardware section.
The ADC of the ESP8266 chip can only read a maximum of 1Volt, whereas a lipo battery will have a max voltage of 4.2 Volt. So we need a voltage divider that makes 1 Volt uit of 4.2 Volt. If you are using the Wemos D1 mini, that already has a voltage divider in place. It connects the A0 of the Wemos with the ADC of the ESP8266 chip.

A 220k over 100k however will not be giving the required ratio. We will need to add an extra 100k resistor between the battery and the A0 pin. That gives us a ratio of 100/(100+220+100)=100/420=1/4.2.
If you happen to use the new, version 1.3 battery shield, I think that already has an extra 130k in place, which gives a slightly different division factor of 100/(130+220+100)=1/4.5.

Upload the Arduinosketch to an ESP8266 and upload the HTML file to the SPIFFS. Open up your IDE terminal window to check the ip nr and open a browser to that ip nr.

ESP8266 Plot Sensor Readings in Real Time Charts on a Web Server

Until recent, I had not bothered much about adding graphics to my ESP8266 programs, as I was using Openhab and Influx/Grafana for graphical presentation of data. In fact I never fully realized what was possible on an ESP8266. Then I saw some great graphics for a BME280 on the randomnerdtutorials site from Rui and Sara Santos.
Their approach is to use the ‘Highcharts’ graphic javascript library that is available on line on cloudflare, something that in all honesty I had never thought of.
Anyway, they create 3 separate graphs, for each of the modalities (temperature, humidity and pressure) of the BME280. That works wonder well. I however was more interested in having various (temperature) results in one graph. Before I go further, the fact that Sara and Rui make 3 requests, makes the program easy to follow. They could ofcourse have made one request, receive the temperature, humidity and air pressure in one json message and parse that. Anyway, back to my adaptation of the code.
I found some examples that were using a different library: Chart.js, but I found the program that Sara and Rui presented cleaner and clearer. Also, they fully separate the HTML file from the ino sketch (storing the HTML in SPIFFS) and that was something I wanted too, so I set about to adapt their program to my needs. I must confess that I am not an expert on javascript, asynchronous websites, websockets and AJAX programming, but this seemed like a great way to dive deeper.
I started with identifying where in the program the data was sent and where it was further processed
In the BME280 program 3 separate calls are made in which subsequently temperature, humidity and pressure are sent to the server.
For the temperature this goes as follows:

server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", readBME280Temperature().c_str());

The request->send_P is a way to send an entire webpage from PROGMEM, but in fact, only the result of the temperature reading is being sent. It seemed to me the best way to add temperature readings (from several DS18B20’s) they needed to be added here somehow. It seemed most sensible to do that with a JSON format.

I found the data was received/processed in the HTML file by the setInterval(function ( ) in the line y = parseFloat(this.responseText);. Obviously I needed to add one or more variables here and instead of using parsefloat, I would need to parse the JSON like this:

var myObj = JSON.parse(this.responseText);
var y=myObj.Temp[0];
var z=myObj.Temp[1];

OK, so that seemed covered, now the Highcharts library needed some further digging in to for me in order to actually add the values to a second and third line in the chart. There are some examples on the cloudflare and jsfiddle website, but those seemed to have little in common with the structure I already had, so obviously this needed some trial and error.

To keep things manageble as an example, I will add 1 channel to the temperature chart that Sara and Rui Santos made and will leave the Humidity and AirPressure graphs untouched.

So first lets adapt the ino file.
Go to the server.on("/temperature" procedure and replace that one by:

server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
     String payload = "{\"Temp\":[" + String(readBME280Temperature()) + "," + String(readDS18B20()) + "]}";
    request->send(200, "text/plain", payload.c_str());

The readDS18B20() should be your procedure to read the result of a DS18B20 (or any other sensor). and yes, I am using ‘send‘, not ‘send_P.’
The result -i.e. the JSON, can be checked on /temparature20190807_200243.jpg

The HTML file needs various changes,not only in the AJAX-serverrequest section, but also in the Highcharts definitions, so I best just post that here in totality.
You could play around with the various Highchart settings. You may have noticed that the colors of both lines are the same. Might be a good excercise for you to try and define a different color . A hint: in the index.html file, as far as I understand Highcharts, the color statement that Rui&Sara use (series: { color: '#059e8a' }) under ‘plot options’ is a general one for the entire graph (1 of the 3). That is a possibility with more than one line too, but you can also define colors per line. Try it, it is not so hard. (I believe that if you do not define any color at all, Highcharts will use default colours, but I did not try that.)


It is also easy to change the line markers,BME4

or make a dotted line, give it a try.


or display negative results in a different color:


(Yes, I know I am not giving you the exact code for that, trying to stimulate, and really, only takes minutes. You may find this link of interest)