There are several development platforms available for programming the ESP8266. You can go with:
- Arduino IDE – intended for those who are familiar with Arduino
- Espruino – JavaScript SDK and firmware closely emulating Node.js
- Mongoose OS – An operating system for IoT devices that is recommended by Espressif Systems and Google Cloud IoT
- MicroPython – Implementation of Python 3 for microcontrollers
- SDK provided by Espressif – Official SDK to take advantage of all ESP8266 features
When compared to other platforms, the Arduino IDE is the most user-friendly for beginners. While it may not be the ideal platform for working with the ESP8266, it is a program that most people are already familiar with, which makes getting started much easier.
Before you can use the Arduino IDE to program the ESP8266, you must first install the ESP8266 board (also known as the ESP8266 Arduino Core) via the Arduino Board Manager. This guide will walk you through the process of downloading, installing, and testing the ESP8266 Arduino Core.
What is a Core?
The cores are required to make new microcontrollers compatible with your Arduino IDE as well as existing sketches and libraries. Arduino develops the cores for the microcontrollers (Atmel AVR MCUs) used on their boards, but anyone can develop a core for their own boards as long as they follow the rules and requirements set by Arduino.
Some development boards require the installation of an additional core; therefore, Arduino developed the Boards Manager as a tool to add cores to the Arduino IDE.
For more information on how to use the Arduino IDE Boards Manager, check out their tutorial.
Step 1: Installing or Updating the Arduino IDE
The first step in installing the ESP8266 Arduino core is to have the latest version of the Arduino IDE installed on your computer. If you haven’t already, we recommend that you do so right away.
Step 2: Installing the USB-to-Serial Bridge Driver
There are numerous ESP8266-based development boards available. Depending on the design, you may need to install additional drivers for your USB-to-serial converter before you are able to upload code to your ESP8266.
For example, the ESP8266 NodeMCU uses the CP2102 to convert USB signals to UART signals, whereas the WeMos D1 Mini uses the CH340G. The ESP-01, on the other hand, lacks an onboard USB-to-serial converter and requires a separate module.
Make sure to inspect your board carefully to identify the USB-to-serial converter that is present. You’ll probably have either CP2102 or CH340 populated on the board.
If you’ve never installed drivers for these USB-to-serial converters on your computer before, you should do so right now.
CP210x USB to UART Bridge VCP Drivers
Step 3: Installing the ESP8266 Arduino Core
Launch the Arduino IDE and navigate to File > Preferences.
Fill in the “Additional Board Manager URLs” field with the following.
http://arduino.esp8266.com/stable/package_esp8266com_index.json
Then, click the “OK” button.
Now navigate to Tools > Board > Boards Manager…
Filter your search by entering ‘esp8266‘. Look for ESP8266 by ESP8266 Community. Click on that entry, and then choose Install.
Step 4: Selecting the Board and Port
After installing the ESP8266 Arduino Core, restart your Arduino IDE and navigate to Tools > Board to ensure you have ESP8266 boards available.
Now select your board in the Tools > Board menu (in our case, it’s the NodeMCU 1.0 (ESP-12E Module)). If you are unsure which board you have, select the Generic ESP8266 Module.
Finally, connect the ESP8266 NodeMCU to your computer and select the Port.
That’s it! You can now begin writing code for your ESP8266 in the Arduino IDE.
You should make sure you always have the most recent version of the ESP8266 Arduino core installed.
Simply navigate to Tools > Board > Boards Manager, search for ESP8266, and verify the version you have installed. If a newer version is available, you should install it.
Step 5: Testing the Installation
Once you’ve finished the preceding steps, you are ready to test your first program with your ESP8266! Launch the Arduino IDE. If you disconnected your board, plug it back in.
The ESP8266 Arduino core includes several examples that demonstrate everything from scanning for nearby networks to building a web server. To access the example sketches, navigate to File > Examples > ESP8266.
You will see a selection of example sketches. You can choose any of them to load the sketch into your IDE and begin experimenting.
Let’s upload the most basic sketch of all – Blink! Navigate to File > Examples > ESP8266, and open the Blink sketch.
This sketch uses the on-board LED that most ESP8266 development boards have. This LED is typically connected to digital pin D0, and its number may vary from board to board.
void setup() {
pinMode(D0, OUTPUT);
}
void loop() {
digitalWrite(D0, HIGH);
delay(500);
digitalWrite(D0, LOW);
delay(500);
}
If everything worked, the on-board LED on your ESP8266 should now be blinking! To execute the sketch, you may need to press the RST button on your ESP8266.
Congratulations! You have just programmed your first ESP8266!
ESP8266 Example: WiFi Scan
Let’s try to run another ESP8266 example sketch, which demonstrates how to use the ESP8266WiFi library to scan nearby WiFi networks and print the results.
You can find this example under File > Examples > ESP8266WiFi > WiFiScan.
Load the WiFiScan sketch from the example sketches into your Arduino IDE.
#include <ESP8266WiFi.h>
void setup() {
Serial.begin(115200);
Serial.println(F("\nESP8266 WiFi scan example"));
// Set WiFi to station mode
WiFi.mode(WIFI_STA);
// Disconnect from an AP if it was previously connected
WiFi.disconnect();
delay(100);
}
void loop() {
String ssid;
int32_t rssi;
uint8_t encryptionType;
uint8_t* bssid;
int32_t channel;
bool hidden;
int scanResult;
Serial.println(F("Starting WiFi scan..."));
scanResult = WiFi.scanNetworks(/*async=*/false, /*hidden=*/true);
if (scanResult == 0) {
Serial.println(F("No networks found"));
} else if (scanResult > 0) {
Serial.printf(PSTR("%d networks found:\n"), scanResult);
// Print unsorted scan results
for (int8_t i = 0; i < scanResult; i++) {
WiFi.getNetworkInfo(i, ssid, encryptionType, rssi, bssid, channel, hidden);
Serial.printf(PSTR(" %02d: [CH %02d] [%02X:%02X:%02X:%02X:%02X:%02X] %ddBm %c %c %s\n"),
i,
channel,
bssid[0], bssid[1], bssid[2],
bssid[3], bssid[4], bssid[5],
rssi,
(encryptionType == ENC_TYPE_NONE) ? ' ' : '*',
hidden ? 'H' : 'V',
ssid.c_str());
delay(0);
}
} else {
Serial.printf(PSTR("WiFi scan error %d"), scanResult);
}
// Wait a bit before scanning again
delay(5000);
}
Once you have uploaded the sketch, open the serial monitor at baud rate 115200 and press the RST button on the ESP8266. You should see the SSID, RSSI, WiFi channel, and encryption for each discovered network.