Monday, 8 December 2014

Using the ESP8266 WiFi module with an Arduino

So I was pretty excited when I discovered the new cheap ESP8266 wifi module. It seemed too good to be true. I kept feeling that it's got way too many features for it's cost. There had to be a catch somewhere.

But no! I had mine delivered to me a couple of days ago and it's the real thing! Of course, it's true that it's a bit difficult to get it working as the documentation available is mostly in Chinese but there are plenty of people working on a translation. I had a little trouble getting the thing working properly. So I'll give a short tutorial on how to get the module working with an Arduino so that others find it easier.

First of all, here's the pinout of the module:
That's the pinout when looking at the module from the side of the antenna. The antenna is that crazy trace that looks like a square wave.

The module works on 3.3v. So wire up the Vcc pin of the module to the 3.3v pin of the Arduino.
GND goes to ground.
Both RST and CH_PD also goes to 3.3v.

Now here's the tricky bit. The arduino is fine with reading incoming 3.3v signals on its digital input pins but the ESP8266 is NOT ok with incoming 5V signals from the Arduino. So use a simple voltage divider. (1k, 2k resistor) to step down the 5V signal from Arduino to 3.3V.

Another point to note: The RXD of the ESP8266 goes to the RX pin of the Arduino (pin 0) and the TXD of the ESP8266 goes to the TX pin of the Arduino (pin 1). It looks like the Arduino has the labels the other way round. This is because when the Atmega IC is removed, the TX and RX pins are the RX and TX pins of the USB to UART bridge. When the IC is actually in the socket, I suspect that the TX of the module should go to the RX and the RX of the module to the TX of the Arduino board.

I then removed the Atmega328 IC from the Arduino's socket and turned on the power. You should see a red LED light up on the ESP8266 indicating that the board is powered. A blue LED should briefly flash. That blue LED indicates serial communication. Now go to the Arduino IDE and open up the Serial monitor. Set the line ending to "Both NL and CR".

Some ESP8266 modules come with old firmware that requires you to set the baud rate to 115200. Some modules come with the new version of the firmware and a default baud rate of 9600. So try out different baud rates if you get gibberish on the monitor. Once you find the correct baud rate try typing in "AT+RST" into the serial monitor. You should get some output. Here's what I get:

The wifi module is controlled using "AT commands". There is a list of the available commands here. Say you want to connect to your home wifi network.

First type in "AT+CWMODE?". This gives you the current mode the module is operating in. You need to set it to mode 3 if you want to connect to your home router. To set the mode to 3 use the command, "AT+CWMODE=3".

Then type in "AT+CWLAP"(List Access Points). This will give you a list of available wifi networks. Hopefully, you see yours on the list.

To join an Access Point I have to use the command "AT+CWJAP"(Join Access Point).
The syntax is "AT+CWJAP="SSID","password".

And there you have it! You've connected to your router with the ESP8266. There's all sorts of cool things you can do after this. You can set up a TCP or UDP server and start streaming sensor data for example. Maybe I'll write about that in the next blogpost.

No comments: