ESP-8266 is an easy and low-cost alternative to the expensive Arduino WiFi shields. While those shields can cost over USD 50, you can find an ESP module for less that USD 3 at ebay
There are several ESP module models around there. All of them are based on the same IC. This article is based on the ESP-01, which is, probably, the most common model.
All ESP-8266 modules are programable. You can use the default factory firmware or you can write your own firmware and upload it to your module. My module came with the firmware version 0018000902-AI03.
The default firmware provides a serial communication that you can use to send AT commands to your module, just like in the old telephone/modem days. These commands provide everything you need to connect to an WiFi router and send/receive data.
Connecting the ESP to an Arduino
The logic connections between Arduino and ESP are very simple: ESP-Rx goes to Arduino Tx, ESP-Tx goes to Arduino Rx. However, all ESP-8266 run on 3.3V, while Arduino pins run on 5V. Before connecting them, you shall provide a way to adapt these voltages, or you could damage your ESP.
I'm using an CD4050 to adjust the voltage between the Tx/Rx pins. Check it:
Note that I'm using the CD4050 only to adapt the Arduino-Tx to ESP-Rx. There's no need to adjust the ESP-Tx to Arduino-Rx, since the Arduino port can handle 3.3V.
Also note that the Arduino 3.3V regulated output (50 mA max) can't drive the current requested by the ESP (Up to 200 mA). Some tutorials on the Internet use the Arduino 3.3V to power the ESP, but it didn't worked for me. I'm using an external power supply (An old 5V Android charger).
I'm using a diode and resistor (17 kOhms, but any large value will work) to decrease my 5V power supply (Here depicted as a battery pack). Depending on the quality of your power supply, you can use one or two diodes. As mine wasn't a good one, a single diode was enough to get 3.7V when the circuit was powered on.
I made a PCB with this circuit. You'll find the link at the end of this post
Important:
- Never power your ESP with 5V. You'll destroy it. The same goes for any logic input (In this case, the Rx pin)
- If your ESP receives less than 3.3V, you'll end up with a lot of transmission errors (You'll see a lot of garbage on your serial communication)
- When using an external power, do not forget of interconnecting both Arduino-GND and power supply GND, to keep the voltage reference. If you don't do that, you'll end up with a lot of transmission errors, again.
ESP-8266-01 terminals
The terminals for this ESP models are depicted here:
- VCC shall be connected to the 3.3V power supply
- GPIO0 and GPIO2 are general purpose digital ports. GPIO0 also controls the module mode (programming or normal operation). In our case (normal operation), it shall be connected to 3.3V (high). GPIO2 is not used in this example, so I put it on 3.3V to simplify the connections
- CH_PD: Chip enable. Keep it on high (3.3V) for normal operation
- RST: Reset. Keep it on high (3.3V) for normal operation. Put it on 0V to reset the chip.
- Tx: Goes to Arduino Rx
- Rx: Goes to Arduino Tx (But needs a voltage adjusting)
- GND is ground
You can see that, in this example, all pins go to VCC (3.3V), except GND, Rx and Tx.
Note that the module also has two-color led (or two separate leds, depending on your vendor)
- Red led indicates the board is powered
- Blue led indicates module activity (Initializing, Rx/Tx, etc)
Test code
In order to test your ESP, you'll need two serial ports:
- One dedicated serial port, connected between Arduino and ESP, where Arduino will send AT commands to ESP.
- A second serial port, connected between Arduino and the computer, where you can type your AT commands.
So, the best Arduino model for this task is Mega, since it provides up to 4 serial ports. You can still try other models, using the SoftSerial lib, but this library has some speed limitations, and it may not work with some ESP models.
You can test your ESP connection with the following simple code:
void setup() { Serial.begin(9600); Serial1.begin(9600); } void loop() { while (Serial1.available()) { Serial.write(Serial1.read()); } while (Serial.available()) { Serial1.write(Serial.read()); } } |
This code simply relays data from both Arduino serial. It gets the AT commands you type at your computer and sends them directly to your ESP. It also gets ESP return data and sends to your computer.
Once you upload this code to your Arduino, you'll see this on your terminal:
Note that, as there are some differences among modules from different manufacturers, the information available at Internet is, several times, contradictory. If your module doesn't work, try other different baud rates
The "ready" text on your terminal means that your ESP is ready to receive your AT commands. You can get a list of them at the bottom of this page.
However, you'd rather use a high level library, since using AT commands may be tricky. A good one is also available at the bottom links. An example of what you can achieve with this library:
The code for this example can be found at the bottom (Portuguese tutorial)