When the subject is ESP8266, it is normal that our attention is on the Wifi module. We’ve already discussed some applications in previous articles, and there’s still a lot of cool stuff to see in the future. We’ve looked at how the ESP8266 communicates with the world via wifi, and now we’ll look at how it does it through its pins.
We will make a decision here. The ESP8266 is found in several modules. Each module with its pin provision. ESP-12 modules provide the most features. To make better use of these features and have a more protoboard-friendly module at hand, we will analyze the NodeMCU module with the ESP-12E.
Overview and Electrical Data
The NodeMCU pins are available in two rows with a spacing that allows them to fit on a breadboard (in particular, I find the pins a little thicker than they should be, and I think if they had more length and less width, it would be even more efficient).
- The NodeMCU interferes just a little with ESP-12 pins. It does an untreated exposure of each of them.
It is interesting to see what NodeMCU adds or changes to the ESP-12.
ESP8266 operates on 3.3V, ESP-12 exposes the VDC pin for direct power. The NodeMCU incorporates a voltage regulator (usually the AMS1117) that can receive a 5V to 10V input and deliver a stable 3.3V supply to the ESP-12.
But what advantage does this give us? nodeMCU is a development module. Choosing pins that fit the breadboard shows us this. Being able to receive 5V greatly increases the practicality of the power supply. 5V is the standard used on USB, so even a cell phone charger can keep everything running.
The power can be supplied directly via the USB connector (standard USB = 5V) or the Vin pin (5V to 10V). The regulator provides a maximum of 500mA.
The pins involved in the power are:
- Vin: Positive voltage Operates with voltages from 5V to 10V.
- GND (G): Reference voltage for voltage input and
- 3V (3V): Output voltage with a value of 3.3V
- VU: On some models, this pin is not connected to But Lolin’s V3 model provides a 5V output provided directly by USB.
Some important considerations to keep in mind:
- The 3V3 regulator provides a maximum of So be careful what you feed the 3V3 pins.
- The Module can be powered by either USB or Vin But it is not recommended that it occur for both at the same time.
Serial – USB Adapter
The ESP-12 provides us with two UARTs, with UART0 (pins RXD0 and TXD0) being used in programming. The NodeMCU Module integrates a Serial-USB converter that greatly facilitates our work.
The USB connector can be used for power and data communication via Serial. For this reason, it is a sufficient condition for recording the ESP8266.
A point of attention here: The RXD0 and TXD0 pins are available as GPIO pins (respectively GPIO3 and GPIO1). But they need to be used very carefully. Enabling these pins as GPIO disables USB.
Digital Pins (GPIO)
Digital pins or GPIO (General Purpose Input Output) are pins intended for binary control. It can have a HIGH or LOW state and can operate both, as an input (for reading digital states) and as a digital control (turning on LEDs, for example).
If you take a look at the nodeMCU image, you will notice that it talks about 13 GPIOs (From GPIO0 to GPIO15). So we have 13 pins for input and output control? Not exactly.
Most microcontroller pins have more than one function, this function is defined through registers. In the case of NodeMCU, some GPIOs are already in use. And if we configure it as a digital pin, we lose some functionality.
Let’s look at it on a case-by-case basis:
- GPIO4 and GPIO5: Starting with the easiest. These two have only the GPIO function. It can operate as Input or Output.
- GPIO0 and GPIO2: These pins play a very specific role in the ESP8266 startup. For that reason, they can only operate as
- GPIO1 and GPIO3: As we have seen, these pins are used by USB so they are not recommended for use as GPIO in
- GPIO9 and GPIO10: ESP8266 uses an external FLASH memory chip and communicates with it with a 4-bit SDIO interface. These two pins are on the list. By default, both are used in this But it is possible to set it to 2-bit mode and release these two pins. You gain in the number of pins, but you lose in memory access speed.
- GPIO12, GPIO13, GPIO14, GPIO15: These pins can be used in other modes, but by default, they are free GPIOs for use as Input or
Ah, an important point to mention: ESP’s GPIOs operate at 3.3V.
The Esp8266 only provides one analog input (ADC0 pin), and that’s probably its biggest weakness.
Although the module operates with 3.3V, the analog input operates from 0 to 1V. Which significantly limits the resolution.
Main functions for GPIO in the Arduino IDE
To control the GPIOs, the Arduino IDE provides us with some functions. Among the main ones, times:
pinMode() defines the mode in which the pin will act. The possible modes are:
- INPUT: The pin will be configured as a digital input
- OUTPUT: The pin will be configured as digital output
- INPUT_PULLUP: The pin will be configured as a digital input with a pull-up Here I leave a study recommendation for pull-up and pull-down circuits but simply put. It forces a value when nothing is connected to the pin. If it’s a pull-up, it’s HIGH, and if it’s pull-down, it’s LOW.
As an example, if we want to use gpio4 as an output, we will have:
The digitalWrite() function changes the value of the informed pin. Possible values are: HIGH or LOW.
As an example, if we want to change the value of gpio4 to HIGH, we will have:
One point we need to note is that there is an expected logical sequence to control the pin. First, we define it as OUTPUT and only then send a command to change the state. But what happens if we forget to use pinMode or set it to INPUT?
For these cases, the digitalWrite() command automatically enables the INPUT_PULLUP mode. This procedure protects the microcontroller from short circuits.
The function reads the digital value of the selected pin, returning the HIGH or LOW value. As an example, to read the status of gpio4, we will have:
value = digitalRead(4)
Of course, the value variable needs to be declared before it can be used. And if the pin is not connected to anything, the value will vary randomly (unless pullup is enabled).
This function reads the analog pin. Or almost. The function returns a value between 0 and 1023, with 0 being the equivalent of the GND voltage and 1023 being the value referring to the VCC.
As an example, to read the status of adc0, we will have:
value = digitalRead(A0)
There are some predefined constants to refer to the pins more practically. When using either function, you can use either the gpio number or the related constant.
Differences in other modules
Here we look at the pin arrangement of the nodeMCU Lolin V3 board. There are slight differences on other cards. In Lolin itself there are versions with the ESP-12E module and others with ESP-12F. In this case of Lolin, the most significant difference is a change of position between GPIOs 4 and 5.