Last modified January 17, 2021
- 1 The doubt or query
- 2 Provisional conclusions
- 2.1 From the point of view of the NodeMCU and the USB port (computer / charger):
- 2.2 From the point of view of what we connect to Vin (sensor, LEDs, buzzer, or whatever):
- 3 Powering CO2 sensors from Vin
- 4 Additional notes
- 5 And what about Arduino?
On many occasions, in our projects, we power our microcontroller through a USB cable connected to a mobile or computer charger and we need power peripherals (sensors, screens, LEDs, etc. that work at 5 volts. Can we do it without problems?
Recently, the user Jesús, made a comment on the blog, specifically in the article about the Home CO2 Meter with wifi, really interesting.
I began to answer the comment and as I wrote, the answer was getting longer and longer and I realized that I needed more resources to answer it (images, video, a richer format, etc). I decided it was a very interesting topic that could go a long way, to be able to cover it minimally (every time I tried to explain a point I realized that I had to explain others in order for it to make sense) and that's where the article you are reading comes from.
Basically the topic was, if we can use the external power input of a NodeMCU to power peripherals operating at 5 volts.
All I explain in the article is also valid for Arduino but with some caveats that I will comment later (because there is not just one Arduino, but many, and you have to see each one, because they are different). It also applies to plates such as Wemos D1 Mini and others like it.
The doubt or query
Specifically, the user said the following in his comment:
“I have been documenting myself and it turns out that the ESP is unable to give 5V. The Vin / GND is to supply it with “unregulated” voltage between 5V and 12V. But not to get 5V out of there. In fact, the power supply when connecting with micro USB is a by-product, not a specification. In fact, in any project that needs 5V (motors), you need a specific power supply, you can't pull Vin (from what I've been reading). With the MH-Z19B we are lucky because its input range is wider (although it is so close to the limit that it is almost certain that some of them are not working and they get jumping values), but not with the MH-Z19C. "
It certainly is a very valid observation, and very useful if we need to power certain sensors or peripherals at 5 volts (such as the MH-Z19C CO2 sensor that it needs to function, according to the manufacturer's specifications, a voltage between 4.9 and 5.1 volts).
My sincere congratulations to Jesús, because he has found a very important point and, furthermore, as he himself indicates, he has no idea about electronics (and, precisely, to those who have no idea about electronics this article is intended; to help them get out of doubt or find something information founded).
As you know, the problem with the affirmations that are read online is that, as with everything in life, there are affirmations of all types and colors and, many times, they are based solely on things that pass by word of mouth without any fairly rigorous technical explanation. Furthermore, people have a tendency to generalize and where one thing is true, in a certain case, there is another similar case in which it is not.
Searching the internet I have found multitude of sites in which the NodeMCU is said to be incapable of providing 5V, stating that the fact that the Vin pin gives voltage when connecting the board via USB is a by-product and not a specification, and other such things.
Little less than some warn you of danger of death if you connect something to the Vin pin while powering the NodeMCU via USB.
The truth is that, not being completely false that the NodeMCU is unable to give 5V, in most cases it will not affect us when connecting peripherals such as sensors and displays.
Luckily the NodeMCU (and the different Arduinos) and its components are very well documented, So it is easy to analyze what is reality and in which cases this is true and in which cases it is not.
The bottom line
The question that arises, in the end, is simple.
The NodeMCU can be powered in two ways:
- Through a USB port (such as a mobile charger, a computer, or similar)
- By a voltage applied to its Vin and GND pins
Most of the time we power the NodeMCU or the Arduino with a USB power supply / charger that we usually use for mobile phones.
Can we, in this case, use the pin that is normally used to power the NodeMCU, or the Arduino, to power other elements that operate at 5 volts without having to find an independent power supply?
Powering the NodeMCU V3
To start analyzing the situation and if there really is a problem or not, let's see how the power of the NodeMCU works.
I am going to refer in this explanation to the NodeMCU V3, which is the most widely used today, and later I will refer to older versions.
This is an extract of the NodeMCU diagram with the circuitry corresponding to the power supply:
The explanation of this scheme is very simple, and I think that any fan can understand it if it is explained with plain and colloquial words (although along the way we miss some hue):
- ESP8266, which is the microcontroller that is mounted on the NodeMCU board, actually is powered at 3.3 volts.
- The USB port connects to a voltage regulator (in practice it is usually an AMS1117) that, regardless of the voltage that enters it (within the limits of the regulator) will draw 3.3 volts to power the ESP8266.
- Pins Vin (positive) and GND (negative) are also connected to the same voltage regulator, except that they do so with an interleaved diode (which only lets current pass one way) so that when you put the voltage through the Vin and GND pins do not exit the USB into the charger or computer and spoil it.
What is explained above has the following effects, peculiarities or things to take into account:
- Regulator forever it brings out a lower tension than it enters. Depending on the chosen regulator (they can be many different in different NodeMCUs) that voltage reduction, or voltage drop, is usually approximately 1.2 volts. Come on, if we put 4.2 volts into the 3.3 volt regulator, the regulator can give us as much 3.0 volts (the 4.2 volts from the input minus the 1.2 volts minimum difference between input and output).
- The diode also has a voltage drop. Depending on the type of diode and the current consumed, it can be between approximately 0.2 and more than 1 volt. This means that if we have 5.0 volts on the USB, on the Vin pin (the diode output) we will have between 4.8 and 4.0 volts.
- The diode is normally of the Schottky type, with a voltage drop of about 0.2 volts.
A special case: the NodeMCU V0.9 power supply
Even though the NodeMCU we find most often is called V3 there are still for sale (and some users will have a NodeMCU version 0.9 in their drawers and it will be convenient for them to know).
The power supply of the NodeMCU version 0.9 is almost the same as the NodeMCU V3 but there is a very important detail (for better and for worse) and that is that, in this version, the 5 volts of the USB and the Vin pin are directly linked. What goes in via USB goes out via Vin and what goes in via Vin goes out via USB.
The good part is that we do not have the voltage drop caused by the diode so the voltage on the Vin pin will be the same as we have on the USB, with no drops (normally 5 volts).
The bad part is that the USB port is not protected. That means that if we put 12 volts in the Vin pin, the NodeMCU will work perfectly, because the regulator will lower the 12 volts to the 3.3 volts that the ESP8266 needs to work, but, those 12 volts will also come out through the USB port and if we have something connected to it (charger, computer, etc) is most likely to break.
With the data we have so far, we can now get some useful conclusions that can be useful to us and that we will refine later.
From the point of view of the NodeMCU and the USB port (computer / charger):
YES we can connect external elements to the Vin and GND pins to feed them without any problem.
Not even the diode will suffer, neither the USB will suffer, nor will anyone get hurt.
Keep in mind that the USB 2.0 specification does not guarantee more than 500 mA current so if the NodeMCU, plus everything you have connected to Vin consume more than 500 mA, there might not be enough current for everything, it will depend on the charger / computer and its capacity to give more than 500 mA.
In the USB 3.0 specification the guaranteed current goes up to 900 mA.
With the USB 3.1 specification with reversible Type-C connectors the guaranteed current rises up to 2 amps.
Currently, due to the high load requirements of modern mobiles, almost all chargers, USB 2.0 and above, and computer ports, USB 2.0 and above, are rated to supply at least 1 amp, and in many cases 2 amps and more.
From the point of view of what we connect to Vin (sensor, LEDs, buzzer, or whatever):
Here comes the key to the issue. The NodeMCU does not care in the least that we connect something to its Vin pin but have we already connected it, for example, a sensor? Do you mind being connected to that pin?
This is going to depend on the voltage requirements that that sensor (or whatever) has.
If it's 5 volts, it's supposed to be 5 volts, right? but, however platitudes this may seem, this is not the case, because the devices do not operate at an exact voltage, but in a range of operating voltage.
Are devices powered from 5 volts to 5 volts? (sic)
When we say that something works at X volts, we really always mean that it works around X volts, or around X volts.
When we are designing, we always have to go to the manufacturer's data sheet or characteristics and check what is its operating voltage range. The manufacturer will always tell us, at least, its typical, maximum and minimum operating voltage and we will be able to move in that range.
Let's look at some examples with different temperature sensors (to stick to something as an example):
|LM35||Data sheet||2.7 ~ 5.5 V|
|DS18B20||Data sheet||3.0 ~ 5.5 V|
|DHT11||Data sheet||3.0 ~ 5.5 V|
|TMP36||Data sheet||2.7 ~ 5.5 V|
As you can see, the manufacturers give some voltage ranges in which the sensor can work.
In general the voltage ranges that we have seen are quite wide, however, not all components have these ranges so great performance.
In the case of the CO2 sensors that we use in our meters, the ranges are as follows:
|MH-Z19||Data sheet||3.6 ~ 5.5 V|
|MH-Z19B||Data sheet||4.5 ~ 5.5 V|
|MH-Z19C||Data sheet||4.9 ~ 5.1 V|
|Senseair S8 LP||Data sheet||4.5 ~ 5.25 V|
As you can see, the operating voltage ranges are much narrower than with temperature sensors, which we had seen before as an example.
In the case of the MH-Z19C sensor the operating voltage range is especially narrow (in fact, it is generally rare for a component to have such a narrow range).
This very narrow range of the MH-Z19C sensor poses a problem for us, and that is that the tolerance with which power supplies are designed are usually higher, so that it is easy that our source of the theoretical 5 volts does not provide a voltage that is between 4.9 and 5.1 volts, but higher or lower.
In the MH-Z19B and Senseair S8 LP sensors we might get to generate a problem, but, as we will see, it is not so easy for it to arise.
Do the 5 volt power supplies give 5 volts? (sic)
Of course not.
The "5 volts" of which we always speak, are a standard, «a name«, A way of understanding each other.
Actually, as we have seen before, when we speak of 5 volts we are not referring to a voltage of exactly 5.00 volts, but to a voltage that is in the range of the 5V.
In the context in which we are, which is that of power through cables that follow USB standards, or USB specifications, as they are often called, we have to look at those specifications to know what that range we are talking about.
According to the official technical specification for a USB 2.0 port, the port should give 5 volts with an allowable variation of 5%, that is: the voltage at the USB connector would be a minimum of 4.75 Volts and a maximum of 5.25 Volts.
Notice I say «the voltage at the port" and not "the voltage at the connector«. This is because the wire that we usually use to connect the port with the device also has a voltage drop and it would not be strange that having 5.00 volts in the port, we had 4.85 volts at the end of a meter of cable connected to that port.
The voltage drop in the cable is an important fact, it can be high (especially in cheap cables that tend to have very fine and impure copper), and manufacturers know it. For this reason, the vast majority of manufacturers of USB ports (the chip that controls the port), USB chargers, etc. they usually design their circuits so that voltage is at the top of the range.
Here we already depart from the standard, for practicality, and we talk about real life. Than we normally find.
Nobody guarantees us these results because the standard offers higher ranges, but in the vast majority of cases this is the case.
To check if this is really the case, I have measured all power supplies, chargers, desktops, laptops, SmartTVs, etc. from my house with a precision USB port meter, and these are the results (not of all, because there have been many, but they have all been around here):
As you see virtually all devices had a voltage between 5.10 and 5.25 volts, except for an LG SmartTV that drew 4.98 volts.
What voltage does the Vin pin offer when powered via USB?
The voltage we have in Vin will depend on the voltage of the USB power supply that we use and the voltage drop of the diode.
This means that we could not have 5 volts on the Vin pin, and have something less.
In the NodeMCU 1.0 and 1.1 (which are the most common, sold as boards V3) the 5 volts from the USB and the Vin pin are linked via a Schottky diode (To avoid that if voltage enters the Vin pin when it is connected to the USB that voltage does not enter the computer, charger or whatever; it could charge it).
That Schottky diode causes a voltage drop of about 0.2 volts, which means that if there is 5 volts on the USB at the Vin pin there will be about 4.8 volts.
This means, if we also consider the 5% of variation in the USB that supports the USB 2.0 specification, it would be a minimum of 4.65 Volts and a maximum of 5.05 Volts, which is within the specification of the MH-Z19, MH-Z19B and Senseair S8 LP sensors.
Powering CO2 sensors from Vin
We have already seen that there is no problem using the Vin pin to power external sensors.
Now we are going to see if we are within the ranges determined by the manufacturers to use each of the sensors taking the power from the Vin pin, knowing that this pin, in practice, should give us a voltage of a minimum of 4.65 Volts and a maximum of 5.05 Volts.
Powering the MH-Z19 via the Vin pin
According to the manufacturer it can be powered with a voltage of 3.6 to 5.5 volts, so we can power it without problems through the Vin pin.
Powering the MH-Z19B via the Vin pin
According to the manufacturer it can be powered with a voltage of 4.5 to 5.5 volts, so we can power it without problems through the Vin pin.
Powering the MH-Z19C via the Vin pin
According to the manufacturer it can be powered with a voltage of 4.9 to 5.1 volts, so we could have problems feeding it through the Vin pin.
Then we will see some ideas and tips to fix it.
Powering the Senseair S8 LP via the Vin pin
According to the manufacturer it can be powered with a voltage of 4.5 to 5.25 volts, so we can power it without problems through the Vin pin.
Does this mean that we cannot use the MH-Z19C feeding it from Vin?
It seems that with the maximum voltage we would walk a little fair because if, by chance, we are joined by a USB power supply that has a high voltage with a Schottky diode with a very low voltage drop (0.1 volts), we could go overboard (although it is not very likely).
We can have the problem with the minimum voltage, which is quite possible that we will fall below the 4.9 volts that it needs as a minimum to work (although probably very little).
Fortunately there are a couple of things we can do, but you will need to have a multimeter to check the measurements (before connecting the MH-Z19C):
If you measure the Vin pin with the multimeter and see that the voltage is too high, simply find another feeder that outputs a lower voltage (you sure have an old one). Plug it in, and measure again.
If the voltage is too low, you can do two things:
- Find a feeder that has a higher voltage.
- Bridge the Schottky diode with a wire (or by removing it from the board and making a bridge).
One of the possible problems could be that some NodeMCUs (it seems that few, fortunately) do not have a Schottky diode between Vin and the USB port, but a normal silicon diode. In this case, the voltage drop across the diode can be 0.6 ~ 0.8 Volts (for currents below 500 mA, which is what we are talking about here, for higher currents the voltage drop can be higher).
Of course, the simplest and most recommendable thing would be to measure the voltage on the Vin pin, but here we are talking about predicting what that voltage will be and whether or not a given design will work, or not, in any situation. mount who mounts it, with sufficient guarantees (at least an acceptable probability index).
If you do not have a multimeter, and the gadget minimally catches your attention, it is best to buy one. I'm not telling you to spend 100 euros, but if you spend 20 or 30 euros on one it will be one of the best investments you make.
And what about Arduino?
The situation in Arduino is very similar, although more complex due to the huge variety of models (and variations of each of the models) that exist.
I'm going to focus on the main Arduino models built around the ATmega328P microcontroller.
Sorry, this part is pending writing. I'll do it as long as I have time.