Where To Buy? | ||||
---|---|---|---|---|
No. | Components | Distributor | Link To Buy | |
1 | ESP8266 | Amazon | Buy Now |
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.
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).
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:
Some important considerations to keep in mind:
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 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:
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.
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:
As an example, if we want to use gpio4 as an output, we will have:
pinMode(4, OUTPUT)
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:
digitalWrite(4, HIGH);
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.
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.