The ESP8266 is a small WiFi module built around the ESP8266 chip that can connect your microcontroller to the internet wirelessly for a very small cost. It can be a great option for Internet of Things (IoT) projects, but can be difficult to work with for beginner hobbyists who do not have prior experience with the module. In this tutorial, we hope to show you how to interface the ESP8266 with an Arduino and perform some basic functions like connecting it to a WiFi network.
The first feature to notice about the ESP8266 is its awkwardly spaced header pins. The module has 8 pins that serve different functions, but they are packed in a 4×2 arrangement that makes plugging the module into a breadboard impossible. This means that to prototype projects on a breadboard, you’ll need male-female jumper wires to connect the pins on the ESP8266 to rows on the breadboard. If you’d like to make your prototyping more compact, you can also purchase breadboard breakouts for the ESP8266 such as this one. For prototyping, I chose to just use jumper wires.
The pinout for the ESP8266’s pins are according to the following diagram:
VCC: Connection to 3.3V Vcc (Vcc cannot exceed 3.3V!)
RESET: Reset pin (pull down to reset)
CH_PD: Chip enable and power down pin
TX: UART serial communication transmit pin
Note that the maximum voltage input for the ESP8266 is 3.3V. Any input voltage greater than 3.3V will damage the module! To program settings on the ESP8266, we’ll first need to connect it to a serial terminal on a computer through which we can send it special commands. Settings that we’ll have to program include, for example, the SSID and password for the wifi network the module will be connected to. To connect the ESP8266 to a computer and configure its settings, we’ll need a USB to serial adapter with 3.3V logic, along with a serial terminal program. Fortunately for us, we have the Arduino and the Arduino IDE’s serial monitor! This means that we’ll just have to connect the ESP8266 module to the Arduino and upload a custom sketch to the Arduino.
Use the following diagram to connect the ESP8266 module to the Arduino:
Figure 3. Connection between ESP8266 and Arduino Uno
You will need the following parts:
Arduino Uno or similar module
ESP8266 ESP-01 module
2 x 1kΩ resistors for CH_PD and RESET pull-up
3 x 220Ω resistors for serial line voltage divider
3.3V regulated power supply for ESP8266 (you cannot plug the power line into the Arduino’s 3.3V out pin because the ESP8266 draws a lot of current!)
A couple of features of this circuit stand out immediately.
First, the ESP8266 module is powered by a 3.3V regulated power supply. As described earlier, the ESP8266 has a maximum voltage input rating of 3.3 volts. Using a power supply rated higher than this recommended 3.3 volts could fry the module! When connecting the ESP8266 to this power supply, you should not only connect the Vcc and Ground pins to the power supply lines, but also connect the power supply’s ground line to the Arduino’s ground pin. Since the Arduino will be powered by the USB connection to the laptop, creating a common ground essentially creates a common reference (you can think of it as a baseline) to compare voltages and thereby interpret digital high and low signals.
Second, the receive (RX) line for the ESP8266 module is connected to the output of a resistor voltage-divider circuit. We do this to shift the serial communication logic level (the highs and lows of the digital signals that make up the serial communications) from a logic high of 5 volts on the Arduino to a logic high of 3.3 volts on the ESP8266 module. Again, the ESP8266 is specified with 3.3V logic, so connecting the module’s receive line directly to the Arduino’s transmit line could damage the device. We do not need to shift levels on the ESP8266’s transmit line because the module’s 3.3V logic high is a high enough voltage to also register as a logic high on the Arduino.
Finally, there are two pull-up resistors on the ESP8266’s CH_PD and RESET pins. These are specified by the module’s creators to ensure that the device functions properly while it is being used. To reset the ESP8266 or disable it, these pins must be pulled down, but since we do not want those processes to occur, we will instead pull these pins high through some 10kΩ pull-up resistors.
After you have made all the hardware connections, upload the following sketch to the Arduino. The sketch copies commands typed into the Serial Monitor and sends them to the ESP8266, and displays responses from the ESP8266 in the Serial Monitor window. We will be using the Arduino Serial Monitor both to send commands to the module and to view responses that it sends back! For the very first run of the sketch, you may need to uncomment the block that says “you may need to uncomment this block for the first run.” The ESP8266 modules come pre-set out of the factory at different BAUD rates, depending on the manufacturer. My module came set at 115200 BAUD out of the box. Because we are going to use SoftwareSerial to communicate with the module, we can slow the BAUD rate down to 9600 to ensure reliable communication. That setting is enforced with the AT+IPR=9600 command sent to the device (keep reading to know what this means!)
constbyterxPin=2;// Wire this to Tx Pin of ESP8266
constbytetxPin=3;// Wire this to Rx Pin of ESP8266
// We'll use a software serial interface to connect to ESP8266
// You may need to uncomment this block for the first run:
// ESP8266.begin(115200); // Change this to the factory baudrate used by ESP8266
// Serial.println("Setting BAUDRATE to 9600");
Serial.println("Command Sent: "+command);
Okay, so we’ve connected the ESP8266 module to the Arduino. Now we can view and configure settings on the ESP8266 as we like. To do this, we must send a specific string command that the module can interpret and respond to. The string commands that we send are called “AT commands” because they all begin with the string “AT” and because they call ATtention to the device. A full list of all the AT commands you can use with the ESP8266 module can be found here: https://cdn.sparkfun.com/assets/learn_tutorials/4/0/3/4A-ESP8266__AT_Instruction_Set__EN_v0.30.pdf
I’ll focus on a couple of examples for this tutorial. You can configure the rest of your ESP8266’s settings by referencing the AT commands in the documentation!
The first command to run is “AT+CWLAP”, the command to scan and list the wifi networks available to join. Typing in the string into the Serial Monitor and hitting enter produces this result!
Figure 4. “AT+CWLAP” command in Serial Monitor
I’ve blanked out the IP Addresses for the networks that the ESP8266 listed, but as you can see, the device finds several networks and lists their information neatly in rows. This will be useful when we want to connect to a specific network! We can do so through the command “AT+CWJAP_CUR”.
In the Serial Monitor, type in AT+CWJAP_CUR, followed by the SSID of the network you want to join in double quotes, followed by the password to that network in double quotes. The picture below shows a failed attempt followed by a successful one.
Great work! Now that you successfully have your ESP8266 module running and communicating with both the WiFi networks and your Arduino, you can send it some other configuration commands as well. The documentation provides a detailed list of all the commands you can send the module. In the next tutorial, we’ll show you how to use the ESP8266 module and the commands you just learned in a complete Arduino project!