Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Introduction to ESP32 Microcontroller

July 24, 2024

Introduction to ESP32 Microcontroller

ESP32 is a highly popular and powerful microcontroller developed by Espressif Systems. It is equipped with a dual-core processor, Wi-Fi and Bluetooth capabilities, along with a rich set of peripherals. In this blog post, we will explore the features and capabilities of the ESP32 microcontroller, and dive into some examples to understand its programming aspects.

Key Features of ESP32

Dual-Core Processor

Unlike the ESP8266, which has a single-core processor, the ESP32 comes with two powerful Xtensa LX6 CPUs. This enables multitasking, allowing one core to handle tasks such as communication and networking, while the other core focuses on executing the application code.

Wi-Fi and Bluetooth

The ESP32 supports both 2.4 GHz Wi-Fi with built-in antenna, and Bluetooth v4.2 and v5.0. This makes it suitable for a wide range of applications, including IoT devices, home automation, and wearable technology.

Rich Set of Peripherals

The ESP32 offers a rich set of peripherals, such as GPIO pins, UART, SPI, I2C, SD card interface, and more. These peripherals provide flexibility in connecting and interacting with external devices and sensors.

Setting Up the ESP32 Development Environment

To start working with the ESP32, we need to set up the development environment. Here are the steps:

1. Install the Arduino IDE

Download and install the Arduino IDE from the official website.

2. Install the ESP32 Board Package

Open the Arduino IDE and navigate to File > Preferences. In the “Additional Boards Manager URLs” field, add the following URL:

1
https://dl.espressif.com/dl/package_esp32_index.json

Next, go to Tools > Board > Boards Manager. Search for “esp32” and click on “Install” for the “ESP32 by Espressif Systems” package.

3. Select the ESP32 Board and Port

Once the installation is complete, go to Tools > Board and select the appropriate ESP32 board. Then, go to Tools > Port and select the port to which the ESP32 is connected.

Basic Example: Blinking LED

To demonstrate the programming aspect of the ESP32, let’s start with a classic example of blinking an LED using the GPIO pins.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
const int ledPin = 2;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  digitalWrite(ledPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, LOW);
  delay(1000);
}

Here, we define a constant ledPin to hold the GPIO pin number connected to the LED. In the setup() function, we set the ledPin as an output pin using pinMode(). Then, in the loop() function, we toggle the state of the LED by using digitalWrite() and introduce a delay of 1 second between each state change using delay().

Advanced Example: Wi-Fi Web Server

Now, let’s explore a more advanced example involving the ESP32’s Wi-Fi capabilities. We will create a simple web server that can be accessed to control an LED remotely.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <WiFi.h>

const char* ssid = "YourNetworkSSID";
const char* password = "YourNetworkPassword";
WiFiServer server(80);

const int ledPin = 2;

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

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  server.begin();
  pinMode(ledPin, OUTPUT);
}

void loop() {
  WiFiClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        String request = client.readStringUntil('\r');
        if (request.indexOf("/LED=ON") != -1) {
          digitalWrite(ledPin, HIGH);
        } else if (request.indexOf("/LED=OFF") != -1) {
          digitalWrite(ledPin, LOW);
        }

        client.flush();
        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println("");
        client.println("<html><body>");
        client.println("<h1>ESP32 Web Server</h1>");
        client.println("<p>LED State: " + String(digitalRead(ledPin)) + "</p>");
        client.println("<a href='/LED=ON'>Turn On</a>&nbsp;");
        client.println("<a href='/LED=OFF'>Turn Off</a>");
        client.println("</body></html>");
        break;
      }
    }
    client.stop();
  }
}

In this example, we first configure the Wi-Fi connection by providing the network SSID and password. We wait until the ESP32 successfully connects to the Wi-Fi network. Then, we create a WiFiServer object that listens on port 80.

Inside the main loop(), we check for incoming client connections using server.available(). If a client is connected, we read the client’s request, check for specific commands, and control the LED accordingly using digitalWrite(). We then send an HTML response indicating the current state of the LED and providing links to turn it on and off.

Conclusion

The ESP32 microcontroller is a powerful and versatile platform for a wide range of applications. It combines the processing power of a dual-core CPU with built-in Wi-Fi and Bluetooth capabilities. In this blog post, we explored the key features of the ESP32, set up the development environment, and implemented basic and advanced examples to demonstrate its programming aspects. This is just the beginning of what you can achieve with the ESP32, and I encourage you to continue exploring its extensive capabilities. Happy coding with ESP32!

Note: Make sure to refer to the official documentation and online resources for comprehensive information and updates on the ESP32 microcontroller.


➡️ Writing Clean and Maintainable Code in C


⬅️ Continuous Integration and Deployment: Best Practices for DevOps


Go back to Posts.