This is the part 1 of 3 instructables to help you to use the ESP8266 with Arduino. In this This first tutorial you will learn how to set-up and test the module connected to an Arduino.
The ESP8266 is perhaps the most versatile serial module to connect "things" at the Internet, that why it is so popular in the world of IoT. It is a complete module, which includes a microprocessor which can be programmed directly via the Arduino IDE (C++), or in other environments to build (usually using a high level language itself, the "LUA"). To control "things" there is no need to have the Arduino itself to the interface because the ESP8266 has 2 GPIOs (two "pin" input / output). A lot of inportant info is possible to be found at ESP82 Forum:
Link to ESP8266 Forum:
- 802.11 b / g / n
- Wi-Fi Direct (P2P), soft-AP
- Integrated TCP / IP protocol stack
- Integrated TR switch, balun, LNA, power amplifier and matching network
- Integrated PLLs, regulators, DCXO and power management units
- + 19.5dBm output power in 802.11b mode
- Power down leakage current of <10uA
- 1MB Flash Memory
- Integrated low power 32-bit CPU Could be used the application processor
- SDIO 1.1 / 2.0, SPI, UART STBC, 1 × 1 MIMO, MIMO 2 × 1
- A-MPDU & A-MSDU aggregation & 0.4ms guard interval
- Wake up and transmit packets in <2ms
- Standby power consumption of <1.0mW (DTIM3)
The above spec includes a lot of technical stuff that you do not really need in the great part of your projects, but it is good to have a hand "just in case".
Until today, I only studied the ESP8266 connected to the Arduino, replacing the need for a relatively expensive Wifi shield (Shield, is a PCB that you install at a top of an Arduino to expand its capabilities).
Step 1: The ESP 8266 Specifications and Pins
Well, the first thing you have to do is test the module with AT commands, which is the standard of communication. In general , the module comes from manufactures speaking at a 115,200 baud rate. Sometimes this is complicated, as in the case of Arduino UNO, once only for HW Serial "0" (pins 0 and 1) can work at that speed. The problem is that the PC Serial Monitor also use the same serial port (PC is used here as a generic term, but I hope my Mac does not listen to me ;-). The solution for Arduino UNO is to use the "SoftwareSerial" library to designate other two generic pins (GPIOs) to be used as a serial port (SW). This works OK as long as the transmission speed is less than 19,200 baud. Perfect! But how to do in the case of ESP8266 come programmed at a faster rate? The way is to reprogram it, of course! BUT, not all Firmware that is loaded into from factory, accept reprogramming module. So the ideal is to upgrade the FW first. Several posts at internet explain how to do it. Here, not to wrap your head around with right speed, firmware, etc., I will simplify it using an Arduino MEGA, that has 4 HW serial ports. So, no worries.
The ports of MEGA:
- TX0 / RX0 ==> Pin 1, 0 (same as the UNO) ==> "Serial 0"
- TX1 / RX1 ==> Pin 18, 19 ==> "Serial1"
- TX2 / RX2 ==> Pin 16, 17 ==> "Serial2"
- TX3 / RX3 ==> Pin 14, 15 ==> "Serial3"
For my tests, I will use the Serial 2 (pins 16 and 17)
Let's have a close view at the module:
- Power Source: 3.3V This is very important because the module does not work with 5V and can burn if you insist (rhymed!). The input pins also do not support 5V and when receiving an Arduino signal, it is important to use first a "voltage level converter" (cute name for the good and old resistors voltage divider). Another important point is to have an independent source of 3.3V. Not always the Arduino can supply the required current for the correct module operation.
- The module has 6 pins:
- TX: that it will be connected to RX2's MEGA (can be connected directly, since MEGA has no problem understanding 3.3V as HIGH)
- RX : TX2 connected to the MEGA via a Level Converter
- VCC : 3.3V
- GND : Ground. It is worth remembering that you must connect the GND of ESP8266 to the GND of the MEGA.
- CH_PD (*) : connected to pin 4 of the MEGA SW reset to start communication
- RST : Reset generally connects to VCC
- GPIO0 : open
- GPIO2 : open
(*) In several sites on the Internet, this pin is directly connected to VCC. In my case, without the "reset" by SW (puts the pin momentarily LOW), the ESP8266 did not work.
There are adapters on the market to turn the module breadboard friendly, as the physical distance between the ESP8266 pins are not compatible with breadboard holes. I used a simple "Male / Female" cable type FTDI (see below) for the connection. The colors shown are compatible with the connection diagram.
Step 2: The Circuit to Be Used in Tests
Nothing special in HW diagram. The red plaque is an independent 3.3V source that is coupled to the breadboard. Observe that the yellow wire connected to the TX2 of Arduino, goes through the voltage divider (1K and 2.2K resistors), so when TX2 is HIGH (5V), the ESP8266 receive approx. 3.3V (HIGH for it).
Step 3: TESTING ESP8266 WITH AT COMMANDS
The idea of this sketch is to run tests and set-up the module, allowing you to enter AT commands via the Serial Monitor and see the answer:
In the part of "comments", are listed the most basic AT commands. When loading the program you will see a lot of trash at Serial Monitor, after the name of the module and finally the word "ready". Do not worry, at this time you can send AT commands.
Start with simple "AT", the module should return "OK", test other commands and so on.
The picture shows what should appear in the Serial Monitor after you test various commands.
- AT commands for test examples:
- AT =====> ESP8266 returns OK
- AT + RST =====> ESP8266 restart and returns OK
- AT + GMR =====> ESP8266 returns AT Version; SDK version; id; OK
- AT + CWMODE? => ESP8266 returns mode type
- AT + CWLAP ===> ESP8266 returns close access points
- AT + CIFSR ===> ESP8266 returs designed IP
- AT + CIPMUX = 1 ==> Set ESP8266 is multiples connections
- AT + CIOBAUD = 9600 ==> Change Baudrate ==> ESP8266 returs OK
- AT + CIPSERVER = 1.80 ==> set mode SERVER port: 4040
- AT + CWMODE = 3 ==> Connect ESP8266 as combined mode (Access Point (2) and Server (1))
- AT + CWSAP = "Acc_Point_name", "password", wifi_Channel, cript # ==> j.
- AT + CWSAP = "ESP_8266_AP," 1234 ", 3.0
- AT + CWJAP = "SSID", "password" ==> Connect to WiFi network
* = AT + CWJAP "ROVAI TIMECAP", "-1 mjr747"
Bellow the documents related to the tests: the Arduino sketch, and two documents in PDF (one with a complete list of commands and the other with a more complete guide for the module set-up as different modes: web server, access point, etc.):