IOT

How to Interface ESP8266 with Arduino

The ESP8266-01 has been a fantastic module for all of our IoT projects. Since its first release, it has grown into a popular Wi-Fi module that is simple to use, inexpensive, and powerful. Another open-source platform that has gained a lot of attraction is Arduino, which has a lot of projects developed around it. Combining these two platforms will allow for a wide range of new ideas, therefore we’ll learn how to connect the ESP8266-01 module to Arduino in this tutorial. We’ll be able to send and receive data between the Arduino and the Internet this way.

What Is ESP8266?

The ESP8226 is a low-cost Wi-Fi microchip with a fully functional Wifi and TCP/IP protocol stack that can provide access to your Wi-Fi network to any microcontroller. This can either allow you to host an application on your device or allow you to offload all Wi-Fi networking activities to another application processor. A single-chip CPU, GPIO pins, analogue pins, I2C and SPI pins are all included in this module. The L106 32-bit RISC CPU is utilised in this module, and it runs at 80 MHz according to Tensilica xtensa Dimond’s specifications. 

Each ESP8266 module comes with pre-programmed AT command set software, so you can just connect it to your Arduino and have nearly the same amount of Wi-Fi functionality. These AT commands are specific instructions for working with the onboard microprocessor on the ESP8266. When used with an Arduino, however, the default firmware is replaced.First, let’s have a look at some of its unique features.

Features of ESP8266

  • It has 32- bit microcontroller
  • CPU: 80 MHz (default) or 160 MHz
  • Memory: 32 KiB instruction RAM, 80 KiB user data RAM, 32 KiB instruction cache RAM, 16 KiB ETS system – data RAM.
  • Input: 16 GPIO pins
  • Processor: L106 32-bit RISC microprocessor core based on the Tensilica Xtensa Diamond standards 106 Micro running at 80 MHz.
  • External QSPI flash: up to 16 MiB is supported (512 KiB to 4 MiB typically included)
  • The new version of the ESP8266 Wi-Fi module has increased the flash disk size from 512KB to 1MB.
  • IC (software implementation)
  • IS interfaces with DMA (sharing pins with GPIO)
  • 10-bit ADC (successive approximation ADC).

What is Arduino Uno?

The Arduino Uno is an ATmega328-based microcontroller board. Arduino is an open-source prototyping tool that is great for both hobbyists and professionals due to its simplicity. The Arduino Uno contains 14 digital input/output pins, 6 analogue inputs, a 16 MHz crystal oscillator, a USB connection, a power jack, an ICSP header, and a reset button. It comes with everything you’ll need to get started with the microcontroller; simply plug it into a computer with a USB cable or power it with an AC-to-DC adapter or battery.

This board has a USB interface, which means it can be connected to a computer through a USB cable, and it can be programmed using the Arduino IDE (Integrated Development Environment) software.

Features of the Arduino UNO:

  • Microcontroller: ATmega328
  • Operating Voltage: 5V
  • Input Voltage (recommended): 7-12V
  • Input Voltage (limits): 6-20V
  • Digital I/O Pins: 14 (of which 6 provide PWM output)
  • Analog Input Pins: 6
  • DC Current per I/O Pin: 40 mA
  • DC Current for 3.3V Pin: 50 mA
  • Flash Memory: 32 KB of which 0.5 KB used by bootloader
  • SRAM: 2 KB (ATmega328)
  • EEPROM: 1 KB (ATmega328)
  • Clock Speed: 16 MHz

Why should we interface the ESP8266 Wi-Fi module with Arduino?

We use mobile phones, which are wireless communication devices, to communicate with one another today. It enables us to communicate with our peers from anywhere at any time. Consider wirelessly sending data to a machine from distant. This is feasible with the help of the ESP8266 Wi-Fi module. Accessibility is the root of the issue. Control of the internet is a current trend in the IoT sector. And Wifi is a major enabler of this capability.

Components required 

  • Arduino Uno
  • ESP8266 Wi-Fi Module (ESP-01)
  • 1 x LED
  • A Push Button
  • Resistors: 330 Ω, 1 KΩ, 2 KΩ. 
  • Breadboard.
  • Jumper wires – as required.

Circuit diagram to interface ESP8266 with Arduino Uno

You’ll need to connect the ESP8266 Wi-Fi module to your Arduino via the serial communication ports for this experiment. Because the ESP-01 uses serial communication protocols. Remember what we learnt in the Arduino communication protocols post about two communicating devices utilising the same protocols? As a result, serial connection from the Arduino is also required. As a result, we use the Arduino Uno’s TX and RX pins. 

  • Connect the Arduino’s digital pin 2 (D2) to the ESP8266’s RX.
  • Connect the Arduino’s digital pin 3 (D3) to the ESP8266’s TX.
  • A level connection circuit can be used to regulate the voltage at the transmitter and receiver pins. At the Arduino’s RX pin connection, a level converter is required.
  • A 1K resistor and a 2.2 resistor can be connected to the ESP8266’s transmitter and reception pins, respectively.
  • The Arduino board’s D11 pin is connected to an LED.
  • The input power supply should be 3.3 V, therefore connect the ESP8266’s Vcc pin to the Arduino’s 3.3V Vout pin. Connect the ground pin to the GND pin of the microcontroller in the same way.
  • A push button can be connected between the ESP8266’s RESET and GND.
  • The Wi-Fi module’s GPIO pins (GPIO0 & GPIO2) are not used in this experiment.

Arduino programming to connect the ESP-01 with the Arduino Uno

For interfacing the ESP8266 with your Arduino, you’ll need the ESP8266Wifi.h library file. The Arduino environment is where Arduino programming takes place. However, you can install CodeBlocks to operate with your Arduino as well. But there’s a whole different bucket of worms that you should avoid at this point.

First, we must find an appropriate port to link the Arduino environment to our Arduino Uno board. Two functions should be included in the Arduino code: void setup() and void loop (). In void setup, all pin-mode declarations are made (). The function to be performed as well as the computation process are both coded in the void loop ().

Code for interfacing an ESP8266 with an Arduino Uno

The code allows you to connect your Arduino to the ESP8266 Wi-Fi module and instructs the ESP8266 to receive data given by the client. The Arduino will then operate the LED in accordance with the command.

#include <ESP8266WiFi.h>
const char WiFiPassword[] = “LED12345”;
const char AP_NameChar[] = “LEDControl” ;
 
WiFiServer server(80);
 
String header = “HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n”;
String html_1 = “<!DOCTYPE html><html><head><meta name=’viewport’ content=’width=device-width, initial-scale=1.0’/><meta charset=’utf-8′><style>body {font-size:140%;} #main {display: table; margin: auto;  padding: 0 10px 0 10px; } h2,{text-align:center; } .button { padding:10px 10px 10px 10px; width:100%;  background-color: #4CAF50; font-size: 120%;}</style><title>LED Control</title></head><body><div id=’main’><h2>LED Control</h2>”;
String html_2 = “<form id=’F1′ action=’LEDON’><input class=’button’ type=’submit’ value=’LED ON’ ></form><br>”;
String html_3 = “<form id=’F2′ action=’LEDOFF’><input class=’button’ type=’submit’ value=’LED OFF’ ></form><br>”;
String html_4 = “</div></body></html>”;
 
String request = “”;
int LED_Pin = D1;
 
void setup() 
{
    pinMode(LED_Pin, OUTPUT); 
 
    boolean conn = WiFi.softAP(AP_NameChar, WiFiPassword);
    server.begin();
 
}
 
 
 
void loop() 
{
 
    // Check if a client has connected
    WiFiClient client = server.available();
    if (!client)  {  return;  }
 
    // Read the first line of the request
    request = client.readStringUntil(‘\r’);
 
    if       ( request.indexOf(“LEDON”) > 0 )  { digitalWrite(LED_Pin, HIGH);  }
    else if  ( request.indexOf(“LEDOFF”) > 0 ) { digitalWrite(LED_Pin, LOW);   }
 
    client.flush();
 
    client.print( header );
    client.print( html_1 );
    client.print( html_2 );
    client.print( html_3 );
    client.print( html_4);
 
    delay(5);
  // The client will actually be disconnected when the function returns and ‘client’ object is detroyed
 
} // void loop()

Connect to the ESP8266 with your mobile phone’s wifi after uploading this code. Then, using the IP address, open a browser on your phone and connect to the ESP8266. You may also use a tool called Advanced IP scanner to find the IP address of your Wi-Fi module. Then, using this tool, locate the module’s IP address and paste it into the webpage’s IP address bar. The web app’s HTML code may be found here.

<head>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″/>
<meta charset=”utf-8″>
<style>
body {font-size:140%;} 
#main {display: table; margin: auto;  padding: 0 10px 0 10px; } 
h2,{text-align:center; } 
.button { padding:10px 10px 10px 10px; width:100%;  background-color: #4CAF50; font-size: 120%;}
</style>
<title>LED Control</title>

You may now control the LED on your webpage by touching the on/off button. You may use the same process to control any other device, as well as control them over the internet from anywhere. You may also use AT instructions to operate the ESP8266 Arduino Wi-Fi module. You can accomplish that without any additional setups, however.

Few basic AT commands are listed below,

  • AT – response OK
  • AT+CWLAP-It will let you know nearby Wi-fi networks
  • AT+GMP- Using this you can check the firmware version
  • AT+CWJAP- Let you join with the Wi-fi network using “<access point name>,””<password>.”
  • AT+CIFSR- Let you know to get the currently allocated IP address.

Common problems and troubleshooting when connecting the ESP8266 with the Arduino Uno

The ESP8266 module does not support 5-3V logic shifting, hence an external logic level converter will be required. As we saw in previous essay on the Arduino power supply, your Arduino Uno has 5V and 3.3V power out features. However, it is strongly recommended that the ESP8266 be powered from a separate 3.3V source.

The Arduino Uno is used to communicate with the ESP8266. Ascertain that the RX is connected to the TX and that the TX is connected to the RX. RX-RX and TX-TX connections work with the ESP8266 if you want to talk with it.

Applications of the ESP8266

ESP8266 has a wide range of applications in IoT based projects. Hence, some cool applications are listed below,

  • You can turn On /Off any device using Windows remote Arduino.
  • Wi-Fi controlled remote car.
  • Home management system.
  • Drone control.
  • Smart house applications.

Leave a Reply

Your email address will not be published.