Friday, March 29, 2024
ESPExplainerHow ToIoT HardwaresIoT Software&ToolsTutorials/DIY

GPIO pins of ESP8266 and How to use efficiently

In this tutorial we focus only on GPIO pins of ESP8266 and How to use efficiently. ESP8266 is most popular development board. ESP8266 comes in many variants most popular is ESP-12 and ESP-01. Use of ESP8266 as just a Serial-to-WiFi bridge with arduino is most common mistake newbies do. NodeMCU is an open source IoT platform. It includes firmware which runs on the ESP8266 Wi-Fi SoC from Espressif Systems, and hardware which is based on the ESP-12 module. The term “NodeMCU” by default refers to the firmware rather than the dev kits.

What is ESP8266?

The ESP8266 series, or family, of Wi-Fi chips is produced by Espressif Systems, a fabless semiconductor company operating out of Shanghai, China. The ESP8266 series presently includes the ESP8266EX and ESP8285 chips.

ESP8266EX (simply referred to as ESP8266) is a system-on-chip (SoC) which integrates a 32-bit Tensilica microcontroller, standard digital peripheral interfaces, antenna switches, RF balun, power amplifier, low noise receive amplifier, filters and power management modules into a small package. It provides capabilities for 2.4 GHz Wi-Fi (802.11 b/g/n, supporting WPA/WPA2), general-purpose input/output (16 GPIO), Inter-Integrated Circuit (I²C), analog-to-digital conversion (10-bit ADC), Serial Peripheral Interface (SPI), I²S interfaces with DMA (sharing pins with GPIO), UART (on dedicated pins, plus a transmit-only UART can be enabled on GPIO2), and pulse-width modulation (PWM). The processor core, called “L106” by Espressif, is based on Tensilica’s Diamond Standard 106Micro 32-bit processor controller core and runs at 80 MHz (or overclocked to 160 MHz). It has a 64 KiB boot ROM, 32 KiB instruction RAM, and 80 KiB user data RAM. (Also, 32  KiB instruction cache RAM and 16 KiB ETS system data RAM.) External flash memory can be accessed through SPI. The silicon chip itself is housed within a 5 mm × 5 mm Quad Flat No-Leads package with 33 connection pads — 8 pads along each side and one large thermal/ground pad in the center.  Read about For Overview, Datasheet, Technical Reference.

ESP8285 is a variation of ESP8266 with 1 MiB of embedded flash memory.

The Modules

Vendors have consequently created a multitude of compact printed circuit board modules based around the ESP8266 chip. Some of these modules have specific identifiers, including monikers such as “ESP-WROOM-02” and and “ESP-01” through “ESP-14”; while other modules might be ill-labeled and merely referred to by a general description — e.g., “ESP8266 Wireless Transceiver.” ESP8266-based modules have demonstrated themselves as a capable, low-cost, networkable foundation for facilitating end-point IoT developments. Espressif’s official modules are presently ESP-WROOM-02 and ESP-WROOM-S2.

NodeMcu – Connect Things EASY

NodeMCU is an open source IoT platform. It includes firmware which runs on the ESP8266 Wi-Fi SoC from Espressif Systems, and hardware which is based on the ESP-12 module. The term “NodeMCU” by default refers to the firmware rather than the development kits. The firmware uses the Lua scripting language. It is based on the eLua project, and built on the Espressif Non-OS SDK for ESP8266.

Features of NodeMCU

  • Arduino-like hardware IO – Advanced API for hardware IO, which can dramatically reduce the redundant work for configuring and manipulating hardware. Code like arduino, but interactively in Lua script.
  • Nodejs style network API – Event-driven API for network applicaitons, which faciliates developers writing code running on a 5mm*5mm sized MCU in Nodejs style. Greatly speed up your IOT application developing process.
  • Lowest cost WI-FI – Less than $2 WI-FI MCU ESP8266 integrated and esay to prototyping development kit. We provide the best platform for IOT application development at the lowest cost.
  • Development Kit – The Development Kit based on ESP8266, integates GPIO, PWM, IIC, 1-Wire and ADC all in one board. Power your developement in the fastest way combinating with NodeMcu Firmware!
NodeLua is the FIRST open source lua based firmware runs on ESP8266. The goal is to make IoT programming easier. Not only an interpreter, but with a Web IDE, Cloud APIs, Mobile App libraries, which makes you creating a real ‘thing’ running on your customers home more robustious and easier. It is currently running on ESP8266 and planned to support more chips, based on the lua language, nodejs-like APIs, but 10 times faster and 100 times smaller than nodejs.

Difference Between ESP8266 (NodeMCU) and Arduino UNO

Specification Arduino ESP8266
RAM 4K Bytes 80 Kilobytes
FLASH memory 32 Kilo bytes 4 Mega Bytes
Speed 16MHz 80MHz
GPIOs 14 11
IO Voltage Level 5V 3.3V
ADC 6 (10-bit) 1 (10-Bit)
Serial 1 1
I2C 1 1
SPI 1 Used by Flash Chip
PWM IOs 6 (8-Bit Resolution) All IO Pins with 10-Bit Resolution
WiFi NO YES 2 MBPS

ESP32 vs ESP8266

Esp32 And ESP8266 both most popular development boards. ESP32 and ESP8266 are cheap Wi-Fi modules perfectly suited for DIY projects in the Internet of Things (IoT) field. These modules come with GPIOs, support for a variety of protocols like SPI, I2C, UART, and more. The best part is that they come with wireless networking included, which makes them apart from other microcontrollers like the Arduino. This means that you can easily control and monitor devices remotely via Wi-Fi for a very low price.

Here you can see a table that adapted by AMICA IO. This table shows main differences between the ESP8266 and the ESP32 processors (table adapted from: AMICA_IO).

ESP8266
ESP32
MCU
Xtensa Single-core 32-bit L106
Xtensa Dual-Core 32-bit LX6 with 600 DMIPS
802.11 b/g/n Wi-Fi
HT20
HT40
Bluetooth
X
Bluetooth 4.2 and BLE
Typical Frequency
80 MHz
160 MHz
SRAM
X
Flash
X
GPIO
17
36
Hardware /Software PWM
None / 8 channels
None / 16 channels
SPI/I2C/I2S/UART
2/1/2/2
4/2/2/2
ADC
10-bit
12-bit
CAN
X
Ethernet MAC Interface
X
Touch Sensor
X
Temperature Sensor
X
Hall effect sensor
X
Working Temperature
-40ºC to 125ºC
-40ºC to 125ºC

GPIO pins of ESP8266

esp8266 Pinout

GPIO pins of ESP8266 and How to use efficiently

NodeMCU Pin Defining

NodeMCU has weird pin mapping. Pin numbers written on the board itself do not correspond to ESP8266 GPIO pin numbers. We have constants defined to make using this board easier:

#define PIN_WIRE_SDA (4)
#define PIN_WIRE_SCL (5)

static const uint8_t SDA = PIN_WIRE_SDA;
static const uint8_t SCL = PIN_WIRE_SCL;

static const uint8_t LED_BUILTIN = 16;
static const uint8_t BUILTIN_LED = 16;

static const uint8_t D0 = 16;
static const uint8_t D1 = 5;
static const uint8_t D2 = 4;
static const uint8_t D3 = 0;
static const uint8_t D4 = 2;
static const uint8_t D5 = 14;
static const uint8_t D6 = 12;
static const uint8_t D7 = 13;
static const uint8_t D8 = 15;
static const uint8_t D9 = 3;
static const uint8_t D10 = 1;

Never use GPIO0 (D3) as input pin, this pin is flash button. If it is low at power on state this will put ESP in programming mode.

How to Use ESP8266/NodeMCU with Arduino IDE

Visit This: Arduino Support for ESP8266 with simple test code

GPIO Pin Description

Just like a normal Arduino, the ESP8266 has digital input/output pins (I/O or GPIO, General Purpose Input/Output pins). These digital input/outputs operate at 3.3V. NEVER CONNECT 5V TO ANY PIN OF ESP8266. The pins are not 5V tolerant, applying more than 3.6V on any pin will kill the chip. The maximum current that can be drawn from a single GPIO pin is 12mA.

ESP8266 has 17 GPIO pins (0-16), however, you can only use 11 of them, because 6 pins (GPIO 6 – 11) are used to connect the flash memory chip. This is the small 8-pin IC right next to the ESP8266. If you try to use one of these pins, you might crash your program.

GPIO 1 and 3 are used as TX and RX of the hardware Serial port (UART), so in most cases, you can’t use them as normal I/O while sending/receiving serial data.

Boot modes

Few I/O pins have a special function during boot: They select 1 of 3 boot modes:

GPIO15 GPIO0 GPIO2 Mode
0V 0V 3.3V Uart Bootloader
0V 3.3V 3.3V Boot sketch (SPI flash)
3.3V X X SDIO mode (not used for Arduino)

Note: Pull down of 1KOhm is required on GPIO0 and external pull-up resistor on GPIO2 is not required the internal pullup is enabled at boot.

  • GPIO15 is always pulled low, so you can’t use the internal pull-up resistor. You have to keep this in mind when using GPIO15 as an input to read a switch or connect it to a device with an open-collector (or open-drain) output, like I²C.
  • GPIO0 is pulled high during normal operation.
  • GPIO2 can’t be low at boot, so you can’t connect a switch to it.

Recommended: ESP8266 Static IP Address Using Arduino Example

Internal pull-up/-down resistors

GPIO 0-15 all have a built-in pull-up resistor, just like in an Arduino. GPIO16 has a built-in pull-down resistor.

Digital I/O

You can set the function of a pin using pinMode(pin, mode); where pin is the GPIO number*, and mode can be either INPUT, which is the default, OUTPUT, or INPUT_PULLUP to enable the built-in pull-up resistors for GPIO 0-15. To enable the pull-down resistor for GPIO16, you have to use INPUT_PULLDOWN_16.

(*) NodeMCU uses a different pin mapping, read more here. To address a NodeMCU pin, e.g. pin 5, use D5: for instance: pinMode(D5, OUTPUT);

To set an output pin high (3.3V) or low (0V), use digitalWrite(pin, value); where pin is the digital pin, and value either 1 or 0 (or HIGH and LOW).

To read an input, use digitalRead(pin);

PWM

ESP8266 supported PWM on all digital pins. The default PWM range is 10-bits @ 1kHz, but this can be changed. To enable PWM on a certain pin, use analogWrite(pin, value); where pin is the digital pin, and value a number between 0 and 1023. You can change the range (bit depth) of the PWM output by using analogWriteRange(range); The frequency can be changed by using analogWriteFreq(frequency);. frequency should be between 100 and 1000Hz.

Read This : ESP8266 PWM Example

Analog input

The ESP8266 has a single analog input, with an input range of 0 – 1.0V. If you supply 3.3V, for example, you will damage the chip. Some boards like the NodeMCU have an on-board resistive voltage divider, to get an easier 0 – 3.3V range. The ADC (analog to digital converter) has a resolution of 10 bits. The ESP can also use the ADC to measure the supply voltage (VCC). To do this, include ADC_MODE(ADC_VCC); at the top of your sketch, and use ESP.getVcc(); to actually get the voltage. If you use it to read the supply voltage, you can’t connect anything else to the analog pin.

Serial Communication

The ESP8266 has two hardware UARTS (Serial ports):
UART0 on pins 1 and 3 (TX0 and RX0 resp.), and UART1 on pins 2 and 8 (TX1 and RX1 resp.), however, GPIO8 is used to connect the flash chip. This means that UART1 can only transmit data.

UART0 also has hardware flow control on pins 15 and 13 (RTS0 and CTS0 resp.). These two pins can also be used as alternative TX0 and RX0 pins.

To use UART0 (TX = GPIO1, RX = GPIO3), you can use the Serial object, just like on an Arduino: Serial.begin(baud).

To use the alternative pins (TX = GPIO15, RX = GPIO13), use Serial.swap() after Serial.begin.

To use UART1 (TX = GPIO2), use the Serial1 object.

WiFi Communication

You can use ESP as a HotSpot mode and it can connect to hot spot. Both mode can work at a time.

How To us ESP8266 as a MQTT Broker

Visit this : ESP8266 as a MQTT Broker | How To Make ESP8266 as a MQTT Broker


In this tutorial we’ve shown you GPIO pins of ESP8266 and How to use efficiently. I hope you like this post “GPIO pins of ESP8266 and How to use efficiently”. Do you have any questions? Leave a comment down below!

Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog.

We have other tutorials with ESP32 that you may find useful:

You may like also:

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

One thought on “GPIO pins of ESP8266 and How to use efficiently

Leave a Reply

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