How to Use Wired Communication with an ESP8266
3/24/2021 | By Maker.io Staff
Expansion and breakout boards are a fantastic way to quickly assemble a prototype, evaluate a new sensor, or include a sensor in an existing project. There are a few ways for boards to interface with a microcontroller, such as the ESP8266. This article explores the most important methods and discusses how we can employ them in ESP8266 based projects.
Different Boards and Various Pinouts
Note that the ESP8266 is not a standardized development board such as the Arduino Uno, for example. Instead, the ESP8266 is a connected microcontroller with Wi-Fi capabilities. That means that different manufacturers may choose to produce development boards with a completely non-standard I/O pin configuration. However, the good news is that the NodeMCU layout seems to be a de facto standard that most board manufacturers seem to adhere to, which means that most ESP8266-enabled boards are interchangeable.
Keep in mind that your board might have a different pinout from mine. Therefore, it’s a good idea to consult the silkscreen labels on the board or to have a look at the user’s manual of a particular development board before starting to work with it. For this article, I decided to use the NodeMCU pinout and the Adafruit Feather Huzzah pinout, as those seem to be the most common.
Use the Digital I/O Pins for Communication
We can employ the digital I/O pins of an ESP8266 based development board to communicate with other devices in a project. The simplest way to accomplish this is to connect one of the GPIO of the ESP8266 pins to a digital input pin on the other device, for example, an Arduino. In this example, the Arduino can read the state of the transmitted bit or react to changes with an interrupt. More lines allow the transmission of more bits at once.
This technique will mostly be useful in custom applications or to transmit a single bit of information to enable or disable a circuit. It’s a simple form of communication, and users have to implement a communication protocol and other features, such as error detection and handling, from scratch.
Use an Analog Input
In some cases, the input is not one of two well-defined states such as HIGH and LOW. Instead, the values in between the two extremes are what matters. It’s possible to utilize the ADC0 input to read an analog input signal and translate it to a digital value that can be represented as a numeric value that the program, running on the ESP8266, can then further process. Note that the ADC0 is an input pin only. The ESP8266 doesn’t support real analog output without additional hardware.
UART for Serial Communication with Other Devices
In this type of communication, the sender and receiver are connected via their TX (for outgoing data) and RX pins (incoming data), respectively. Note that the TX pin of the sender connects to the RX pin of the receiver and vice versa. The two parties exchange data directly and without a clock signal to synchronize the data streams. Instead, the sender uses start and stop bits that mark the beginning and end of a message. When the receiver detects a start bit, it starts reading incoming data at the specified baud rate. This rate is typically given in bits per second, and it is a good measure of the transmission speed. The sender and receiver must agree on a baud rate that they both support, and the rates may only differ by around 10% before the communication potentially corrupts the data. Without going into much more detail, here’s an example that shows two development boards connected via their serial ports:
Communicate Via the I2C Pins
I2C is, unlike UART, a communications protocol. In electronics, we work with the I2C protocol often, as it is a form of communication that a large majority of development boards and extension modules support.
Unlike UART, I2C allows more than two devices to communicate over a single bus. In this protocol, devices play the roles of masters and slaves, which are connected over two lines: SDA and SCL. The SCL line carries a single clock signal that synchronizes all communicating parties. The devices use the SDA line to send and receive data. In this protocol, each device has a unique address that it listens to. When sending a new message, a master device includes the address of the device it wants to communicate with and sends that address to all connected devices in the bus. If a slave device receives such a message, it compares its own address to the one that the master transmitted, and if they match, the master and slave can communicate. Again, here’s an example of how to connect a master and a slave via I2C:
Use SPI to Communicate with Other Devices
The communication methods discussed so far were designed in a way that allows a sender to transmit data packets of fixed sizes to one or multiple recipients. With SPI a sender can transmit a continuous stream of an arbitrary number of bits without interruption. SPI, like I2C, employs a master-slave hierarchy. The master controls the communication, while the slave receives instructions from the master and answers accordingly. SPI allows for multiple masters and slaves, and it uses four pins for its communication:
The MOSI and MISO pins are the lines that transmit the data. MOSI stands for Master Out Slave In, and MISO is an acronym for Master In Slave Out. SPI utilizes a shared clock signal, and the master uses the CS (chip select) pin to select which slave he wants to communicate with. Each slave in the communication network has such a CS (sometimes also called SS) pin, and the master selects the slave he wants to communicate with by pulling the device’s CS pin low.
The ESP8266 is an incredibly versatile microcontroller, and it supports all typical communication protocols and methods that one would typically expect. These communication methods are quite a bit simpler compared to communicating over a Wi-Fi network. The communication protocols discussed in this article are typically used to connect external modules and sensors to a microcontroller. Doing so allows for simple and cost-effective hardware, and it reduces the overhead in the communication.