Step-by-Step Tutorial: Setting Up ESP32 Board in Arduino IDE

by AKP in Circuits > Arduino

2059 Views, 32 Favorites, 0 Comments

Step-by-Step Tutorial: Setting Up ESP32 Board in Arduino IDE

esp32-arduino-ide-tutorial.jpg

There are various development platforms available for programming the ESP32. You have the following options:

  • Arduino IDE – Designed for users familiar with Arduino programming.
  • Espruino – JavaScript SDK and firmware that closely emulates Node.js.
  • Mongoose OS – An IoT operating system recommended by Espressif Systems and Google Cloud IoT.
  • MicroPython – A Python 3 implementation tailored for microcontrollers.
  • Espressif SDK – The official SDK provided by Espressif to leverage all ESP32 features.

Among these platforms, the Arduino IDE is considered the most beginner-friendly. While it may not be the optimal choice for ESP32 development, many people are already acquainted with Arduino, making it easier to get started.

To use the Arduino IDE for ESP32 programming, you need to install the ESP32 board (also known as the ESP32 Arduino Core) through the Arduino Board Manager. This guide will provide step-by-step instructions on how to download, install, and test the ESP32 Arduino Core.

Installing or Updating the Arduino IDE

To begin the process of installing the ESP32 Arduino core, it is essential to have the latest version of the Arduino IDE installed on your computer. If you haven’t already done so, we highly recommend installing or updating the Arduino IDE to the most recent version before proceeding.

Installing the USB-to-Serial Bridge Driver

CH340G-On-WeMos-ESP32-Lite.jpg
CP2102-On-ESP32-Dev-Kit-V1.jpg

To ensure proper communication between your computer and the ESP32 development board, you may need to install the appropriate USB-to-serial bridge driver. The specific driver required depends on the USB-to-serial converter chip used on your ESP32 board.

Different ESP32 boards utilize different USB-to-serial converters. For example, the CP2102 or CH340G chips are commonly used. It is important to identify the USB-to-serial converter chip on your board before proceeding.

If you have not installed the driver for the USB-to-serial converter on your computer before, follow the instructions provided by the manufacturer to download and install the driver. This will enable your computer to recognize and communicate with the ESP32 board through the USB connection.

Ensure that the USB-to-serial bridge driver is installed correctly before attempting to upload code to your ESP32 board. This will ensure a smooth programming experience and proper functionality of the board.

Installing the ESP32 Arduino Core

Arduino-IDE-Boards-Manager.jpg
Arduino-IDE-ESP32-Json-URL.jpg
Arduino-IDE-ESP32-Package.jpg
Arduino-IDE-Preferences.jpg
ESP32-and-ESP8266-Board-Manager-URLs.jpg

To utilize the ESP32 with the Arduino IDE, you need to install the ESP32 Arduino Core. Follow these steps to install the core:

Selecting the Board and Port

Arduino-IDE-Boards-Manager-ESP32-Option.jpg
Arduino-IDE-Boards-Manager-ESP32-Boards.jpg
Arduino-IDE-ESP32-Port-Selection.jpg

After successfully installing the ESP32 Arduino Core, follow these steps to select the board and port settings in the Arduino IDE:

Restart the Arduino IDE to apply the changes made during the installation.

ESP32 Example: WiFi Scan

ESP32-WiFi-Scan-Sketch.jpg
ESP32-WiFi-Scan-Sketch-Output.jpg

Let’s try running an ESP32 example sketch that demonstrates how to scan nearby WiFi networks using the WiFi library and print the results.

To access this example, go to File > Examples > WiFi > WiFiScan in the Arduino IDE.

Load the WiFiScan sketch from the examples into your Arduino IDE. The code for the sketch is as follows:

#include "WiFi.h"

void setup()
{
Serial.begin(115200);

// Set WiFi to station mode and disconnect from an AP if it was previously connected.
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);

Serial.println("Setup done");
}

void loop()
{
Serial.println("Scan start");

// WiFi.scanNetworks will return the number of networks found.
int n = WiFi.scanNetworks();
Serial.println("Scan done");
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
Serial.println("Nr | SSID | RSSI | CH | Encryption");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.printf("%2d",i + 1);
Serial.print(" | ");
Serial.printf("%-32.32s", WiFi.SSID(i).c_str());
Serial.print(" | ");
Serial.printf("%4d", WiFi.RSSI(i));
Serial.print(" | ");
Serial.printf("%2d", WiFi.channel(i));
Serial.print(" | ");
switch (WiFi.encryptionType(i))
{
case WIFI_AUTH_OPEN:
Serial.print("open");
break;
case WIFI_AUTH_WEP:
Serial.print("WEP");
break;
case WIFI_AUTH_WPA_PSK:
Serial.print("WPA");
break;
case WIFI_AUTH_WPA2_PSK:
Serial.print("WPA2");
break;
case WIFI_AUTH_WPA_WPA2_PSK:
Serial.print("WPA+WPA2");
break;
case WIFI_AUTH_WPA2_ENTERPRISE:
Serial.print("WPA2-EAP");
break;
case WIFI_AUTH_WPA3_PSK:
Serial.print("WPA3");
break;
case WIFI_AUTH_WPA2_WPA3_PSK:
Serial.print("WPA2+WPA3");
break;
case WIFI_AUTH_WAPI_PSK:
Serial.print("WAPI");
break;
default:
Serial.print("unknown");
}
Serial.println();
delay(10);
}
}
Serial.println("");

// Delete the scan result to free memory for code below.
WiFi.scanDelete();

// Wait a bit before scanning again.
delay(5000);
}

Troubleshooting

Failed-to-connect-to-ESP32-Error-while-Uploading-Sketch-in-Arduino-IDE.jpg

If you encounter the “A fatal error occurred: Failed to connect to ESP32… Timed out waiting for packet header…” error when uploading code to your ESP32, it indicates that your board is not in flashing or uploading mode. You can follow the steps below to resolve this issue:

  • Press and hold the BOOT button on your ESP32 board.
  • Click the Upload button in the Arduino IDE to initiate the sketch upload process.
  • Keep holding the BOOT button until you see the message “Writing at 0x00001000… (100%)” in the Arduino IDE log after the “Connecting…” message.
  • Release the BOOT button at this point.
  • You should now see the “Done uploading” message, indicating that the code has been successfully uploaded.

That’s it! You can now press the EN button on the ESP32 board to restart it and run the newly uploaded sketch.

Please note that you will need to repeat this button sequence every time you want to upload a new sketch to your ESP32.

See Full Article Here