Programming ESP8266 WiFi Module with Arduino IDE

Table of Contents

Programming ESP8266 WiFi Module with Arduino IDE

ESP8266 is a Wi-Fi module suitable for IoT and home automation projects. It allows you to control inputs and outputs like you would with an Arduino series of boards, but with Wi-Fi. Therefore, it is well suited for home automation/IoT applications.

Comparing the ESP8266 with other Wi-Fi solutions on the market, it is an excellent choice for most “Internet of Things” projects. In this article, we’ll introduce the basic concepts of the ESP8266 board, it’s pinout, and programming examples.

Introduction to ESP8266

ESP8266 is a powerful and versatile Wi-Fi enabled microcontroller that has gained popularity in the world of electronics and IoT (Internet of Things). Developed by Espressif Systems, the ESP8266 offers a cost-effective solution for connecting devices to the internet and enabling them to communicate with each other. It features a built-in Wi-Fi module, a powerful processor, and GPIO pins for connecting external components. The ESP8266 can be programmed using various programming languages, making it accessible to both beginners and experienced developers. Its low power consumption and compact size make it ideal for a wide range of applications, including home automation, wireless sensor networks, and smart devices. For example, the ESP8266 can be used to create a smart thermostat that allows users to control the temperature of their homes remotely.

ESP8266 Pinout

ESP8266 Pinout
ESP8266 Pinout

In the pin diagram, some pins have other uses besides GPIO. For example, the D1 pin is not only GPIO5, but also an SCL interface, which can be connected to the corresponding SCL pin of the LCD screen.

The following table shows the correspondence between the labels on the onboard identification and GPIO numbers, and which pins are best used in projects, and which pins require caution. Pins highlighted in green are ready for use. Those highlighted in yellow can be used, but require attention as they may have unexpected behavior, mainly at startup. Pins highlighted in red are not recommended for use as inputs or outputs.

LabelGPIOInputOutputNotes
D0GPIO16No InterruptNo PWM or I2C supportHIGH at boot used to wake up from deep sleep
D1GPIO5OKOKCommonly used as SCL (I2C)
D2GPIO4OKOKCommonly used as SDA (I2C)
D3GPIO0Pull-upOKConnected to FLASH button, boot fails if pulled LOW
D4GPIO2Pull-upOKHIGH at boot connected to on-board LED, boot fails if pulled LOW
D5GPIO14OKOKSPI (SCLK)
D6GPIO12OKOKSPI (MISO)
D7GPIO13OKOKSPI (MOSI)
D8GPIO15Pull-downOKSPI (CS) Boot fails if pulled HIGH
RXGPIO3OKRXHIGH at boot
TXGPIO1TXOKHIGH at boot debug output at boot, boot fails if pulled LOW
A0ADC0AnalogXAnalog pin

How to Program ESP8266 with Arduino IDE?

There are many ways to program ESP8266:

  1. Use Arduino IDE to develop, follow the development style of Arduino, easy to use;
  2. Use ESP8266 official SDK development;
  3. Use MicroPython to develop, follow the programming style of Python (this method needs to burn a special firmware for the board first);

Here we choose the first one, because Arduino programming is simple and easy to operate.

Step 1: Install the Arduino IDE

Download IDE from Arduino official website: https://www.arduino.cc/en/software. The version I am using now is Arduino IDE 1.8.3. (Noted that the latest version of Arduino IDE 2.1.1 cannot install the ESP8266 resource package online.)

Install Arduino IDE 1.8.3
Install Arduino IDE 1.8.3

Step 2: Install the ESP8266 Package

Because the Arduino IDE is used for the Arduino development board by default, there are only relevant development resources for the Arduino development board by default. To use ESP8266 in Arduino IDE, we need to manually install the ESP8266 development package. There are two installation methods: online installation and offline package installation, here we choose the first one.

First, find the “File -> Preferences” in the Arduino IDE menu: 

Open Preferences in Arduino IDE
Open Preferences in Arduino IDE

And then, enter the below link in the “Additional Boards Manager URLs”:

http://arduino.esp8266.com/stable/package_esp8266com_index.json

Setting Additional boards manager URLs in Preferences
Setting Additional boards manager URLs in Preferences

Next, click to open the: Tools -> Board -> Board Manager.

Open Boards Manager in Arduino
Open Boards Manager in Arduino

In the field, input and search “esp8266”, you will see an esp8266 package, click “Install” to start online installation.

Search and Install the ESP8266 Board
Search and Install the ESP8266 Board
Installing ESP8266 packages
Installing ESP8266 packages

After the installation is complete, you can find all resources about the esp8266 board in the “Tools->Board” menu.

ESP8266 Boards Resource (3.1.2 Version)
ESP8266 Boards Resource (3.1.2 Version)

Step 3: ESP8266 Programming

Here we write a simple network connection test program for the ESP8266 board, the program code is as follows:

				
					void setup() {
  Serial.begin(9600);
  Serial.println("esp8266 test");
  initWiFi();

}

void loop() {
  Serial.println("hello esp8266");
  delay(1000);

}

#include <ESP8266WiFi.h>

constchar ssid[] = "xxxxx";      //WiFi Name
constchar pass[] = "xxxxx";       //WiFi Password

WiFiClient client;

//Initialize WIFI
void initWiFi()
{
   Serial.print("Connecting WiFi...");
   WiFi.mode(WIFI_STA); //Configure WIFI as Station mode
   WiFi.begin(ssid, pass); //Incoming ssid and password of WIFI hotspot
   while (WiFi.status() != WL_CONNECTED) //Waiting for successful connection
   {
     delay(500);
     Serial. print(".");
   }

   Serial.println("");
   Serial.println("WiFi connected");
   Serial.println("IP address: ");
   Serial.println(WiFi.localIP()); //print your own IP address
}
				
			

Notes:
The setup function is used for serial port initialization. For example, “Serial.begin(9600);” means that the initial baud rate of the serial port is 9600.

The loop function is similar to the while(1){} in the main function in the development of a single-chip microcomputer, where we can write program logic codes.

To use the WIFI function, you need to include the header file “ESP8266WiFi.h”.

The process of WIFI initialization includes 3 steps:

  1. Configure WIFI to Station mode, that is, our ESP8266 is used as a WIFI device;
  2. Pass in the ssid and password of the WIFI hotspot, so that ESP8266 can connect to the hotspot;
  3. Read the WIFI connection status and wait for the connection to be successful.

Select the model and serial port number of your ESP8266 development board:

Choose Board NodeMCU 1.0 (ESP-12E Module)
Choose Board NodeMCU 1.0 (ESP-12E Module)

Now, we can compile and burn the code into the ESP8266WiFi board.

Compile and Upload the code to ESP8266 WiFi Module
Compile and Upload the code to ESP8266 WiFi Module

Subscribe

Join our subscribers list to get monthly blog updates, technology news, case studies. We will never send spam, and you can unsubscribe at any time.

About Author

Aidan Taylor
Aidan Taylor

I am Aidan Taylor and I have over 10 years of experience in the field of PCB Reverse Engineering, PCB design and IC Unlock.

Need Help?

Don't hesitate - Get in touch today to find out how we can help take your project to the next level.

Table of Contents

Scroll to Top
welldone pcb manufacturer

Start to Achieve Your PCB Project