Programming ESP boards with Arduino IDE made easy – beginner’s guide
ESP modules are very popular among IoT project developers. There are different types of esp modules available in the market depending on the features. They are ESP-01, ESP-12E, ESP-12F, NodeMCU and ESP-32. When you buy any of these modules the first thing you have to do is, make it compatible with any IDE and upload a basic code to test it. We are going to make the ESP-01 , NodeMCU and ESP-32 compatible with Arduino IDE and upload basic testing codes. So lets start.
Components required
- ESP-01
- NodeMCU
- ESP-32
- CP2102 USB to UART bridge
- Push button
- Jumper wires (F to F) – 10pcs of different colors
All ESP boards use CP2102 UART bridge to send or receive data from computer. The ESP-32 and NodeMCU have inbuilt CP2102 whereas for ESP-01 we have to connect external CP2102 module.
The computer’s com port must also be able to detect the CP2102 , for that the CP210x driver must be installed. To install the CP210x driver click on this link.
For esp-01 make the connections as per below given circuit diagram
The RXD of ESP-01 is connected to TXD of CP2102 and the TXD of ESP-01 is connected to RXD of CP2102. 3.3V from CP2102 is given to chip enable (CH_PD) pin and VCC pin of ESP module. GND from CP2102 goes to GPIO-0 , GND pin and to RST (reset) pin of esp-01 module. There is a switch between reset and GND.
Making ESP boards compatible with Arduino IDE
To download Arduino IDE click on this link. Go to download options and download as per OS of your device . After installing IDE, open it and follow the below given steps.
- Click on files >> preferences
- In “Additional Boards manager URL’s” copy and paste the below given links. There are two links separated by a comma. One is for esp8266 boards and other is for esp32 boards. Copy the whole text given below and paste it in “Additional Boards manager URL’s”.
Click on “OK”
- Go to Tools >> board >> board manager
- Search for “esp8266” and install the “esp8266 by ESP8266 Community” package
- Search for “esp32” and install the “esp32 by Espressif Systems” package.
Now we have successfully added the esp boards to our Arduino library.
On board led blinking test for esp-01 and NodeMCU
The on board led pin of ESP-01 is GPIO 1 and that of NodeMCU is GPIO 2. Before writing the blinking code we have to select the board and com port. The COM port number can be known by “device manager”. Open device manager in windows.
Expand the “ports” and see the com number of CP2102 UART bridge.
This shows us that our ESP module is connected on COM8. Now we can open Arduino IDE and select the board and port. Board must be “Generic ESP8266 Module”.
Port must be COM8 as we saw it in the device manager.
Now we can upload the on board led blinking code.
Code
int led = 1; // pin 1 is for esp-01 and pin 2 is for node mcu
void setup() {
pinMode(led,OUTPUT);
}
void loop() {
digitalWrite(led,HIGH);
delay(500);
digitalWrite(led,LOW);
delay(500);
}
After uploading, we have to disconnect the GND going to GPIO-0 (for the board to come out of programming mode) and press the reset button. The on-board led will start blinking this means the board is ok.
Now let’s test its WiFi connectivity. The on-board led will start blinking once the WiFi is connected. Enter your WiFi credentials in the code and upload it.
WiFi testing code
#include <ESP8266WiFi.h> // for ESP-32 remove ESP8266 and just keep it <WiFi.h>
const char* ssid = "Your WiFi ssid";
const char* password = "Your WiFi password";
int iled = 1; // for nodemcu change it to 2.
void setup() {
Serial.begin(115200);
pinMode(iled,OUTPUT);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
digitalWrite(iled,HIGH);
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
}
void loop() {
if(WiFi.status()== WL_CONNECTED){
digitalWrite(iled,HIGH);
delay(500);
digitalWrite(iled,LOW);
delay(500);
}
else{
Serial.println("WiFi Disconnected");
digitalWrite(iled,HIGH);
delay(500);
}
}
If the on-board led starts blinking then the ESP-01 module is getting connected to WiFi and is working.
Testing of NodeMCU
In similar way as we tested the ESP-01 we can test the NodeMCU. To program the NodeMCU, all you need is a micro USB cable for connecting it to the COM port. The reset button and CP2102 (USB to UART) is inbuilt.
After connecting the board to COM port. Select the same board type “Generic ESP8266 Module”. Select the port according to device manager.
Led blinking code for NodeMCU is almost same as that of ESP-01. Just change the on-board led pin in code from “1” to “2” since the on-board led on NodeMCU is connected to GPIO-2 of NodeMCU. After uploading the code the led should start blinking. No need to reset as in case of the ESP-01. If the led blinks the board is ok.
WiFi testing of NodeMCU
Upload the same code as that for WiFi testing of esp-01. Just change the on-board led pin number from “1” to “2”. Turn on the WiFi and then upload the code the led will start blinking as soon as the WiFi is connected. This indicates that the board is in good condition and is ready to be used in project.
Testing of ESP-32
There is no on board led on ESP-32. So we will directly do the Wifi test. Upload the same WiFi code as given above. Just change the library name from “ESP8266WiFi.h” to “WiFi.h”. Select the board “ESP-32 dev module”. Select the port as shown in device manager.
Once the code is uploaded open the serial monitor. If the WiFi is turned on then the WiFi connected message along with IP address is displayed this indicates that the ESP-32 board is working fine.
Conclusion
In this way we made ESP boards compatible with Arduino IDE and also tested them. If you have any doubt regarding any part of this blog you can comment it, our team will be there to assist you.
For more interesting projects check out our YouTube channel.