◆Smart Home Ideas Journal

Mastering ESP32: Top Programming Examples

By

Discover the power of the ESP32, a low-cost, low-power system on a chip (SoC) microcontroller with integrated Wi-Fi and dual-mode Bluetooth. This chip is designed for IoT applications, making it an excellent choice for a broad range of projects. To get started, let's explore some practical ESP32 programming examples.

The ESP32 can be programmed using various languages and development platforms. The Arduino IDE, with added ESP32 board definitions, is a popular choice due to its simplicity and extensive community support. Before diving into programming, ensure you've set up your development environment and have the necessary hardware, including an ESP32 board and a USB-TTL converter for programming.

Setting Up and Blinking an LED

Let's start with the classic "Hello, World!" of embedded programming: blinking an LED. This example will help you understand the basic concepts of programming the ESP32.

10 ESP32 Skills Every Maker Should Learn
10 ESP32 Skills Every Maker Should Learn
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
electronics mini projects for engineering – ESP32 in Proteus 8 Full Tutorial
electronics mini projects for engineering – ESP32 in Proteus 8 Full Tutorial

To blink an LED connected to one of the ESP32's GPIO pins, you'll need to utilize the built-in LEDc library for pulse-width modulation (PWM) control. Here's a simple sketch to get you started:

```cpp #include #define LED_PIN 2 #define FREQUENCY 1000 #define LED_CHANNEL 0 void setup() { LEDCSetup(LED_CHANNEL, FREQUENCY, 8); LEDCAttachPin(LED_PIN, LED_CHANNEL); LEDCWrite(LED_CHANNEL, 128); } void loop() { delay(500); LEDCWrite(LED_CHANNEL, 50); delay(500); LEDCWrite(LED_CHANNEL, 128); } ```

Understanding the LEDC Library

The LEDC library is essential for controlling LEDs and other PWM devices on the ESP32. It allows you to configure and control up to 16 independent PWM channels.

The `LEDCSetup` function configures a channel with the given frequency, bit resolution (8-bit in this case), and output GPIO pin. The `LEDCAttachPin` function attaches the chosen LED pin to the specified channel. Finally, `LEDCWrite` sets the duty cycle for the attached pin.

Modifying the Blink Rate

Now that you've set up the basics, let's modify the blink rate by introducing a variable to store the interval. Update your sketch as follows:

```cpp #include #define LED_PIN 2 #define FREQUENCY 1000 #define LED_CHANNEL 0 #define BLINK_INTERVAL 1000 void setup() { LEDCSetup(LED_CHANNEL, FREQUENCY, 8); LEDCAttachPin(LED_PIN, LED_CHANNEL); LEDCWrite(LED_CHANNEL, 128); } void loop() { delay(BLINK_INTERVAL); LEDCWrite(LED_CHANNEL, 50); delay(BLINK_INTERVAL); LEDCWrite(LED_CHANNEL, 128); } ```

With this modification, you can easily adjust the blink interval by changing the `BLINK_INTERVAL` constant at the top of the sketch.

Connecting to Wi-Fi and Controlling a Remote Device

Now that you've mastered the basics, let's explore a more advanced example: connecting the ESP32 to a Wi-Fi network and controlling another device over the network.

In this scenario, we'll use a simple HTTP server to control an LED on another device. For this example, you'll need two ESP32 boards and a USB-to-serial converter for each.

Setting up the Server

First, configure one ESP32 as an HTTP server with routes to control an LED. Add the following code to your sketch:

```cpp #include #include #define LED_PIN 2 #define SSID "your_network_SSID" #define PASSWORD "your_network_PASSWORD" #define PORT 80 WebServer server(PORT); void handleRoot() { String webpage = "
Turn On

Turn Off
"; server.send(200, "text/html", webpage); } void handleOn() { digitalWrite(LED_PIN, HIGH); server.send(200, "text/plain", "LED turned on"); } void handleOff() { digitalWrite(LED_PIN, LOW); server.send(200, "text/plain", "LED turned off"); } void setup() { pinMode(LED_PIN, OUTPUT); WiFi.begin(SSID, PASSWORD); while (WiFi.status() != WL_CONNECTED) { delay(500); } server.on("/", handleRoot); server.on("/on", handleOn); server.on("/off", handleOff); server.begin(); } void loop() { server.handleClient(); } ```

The sketch initializes the Wi-Fi client and creates a simple web server with three routes: a root route that displays a form to control the LED and two action routes ("on" and "off") that control the LED's state. Replace `'your_network_SSID'` and `'your_network_PASSWORD'` with your network's credentials.

Controlling the Server

Now that you have the server set up, configure the second ESP32 to connect to the Wi-Fi network and control the server using its HTTP client library. Here's a basic sketch to get you started:

```cpp #include #include #define SSID "your_network_SSID" #define PASSWORD "your_network_PASSWORD" #define SERVER_IP "192.168.1.100" // IP address of the server ESP32 #define PORT 80 WiFiClient client; void setup() { WiFi.begin(SSID, PASSWORD); while (WiFi.status() != WL_CONNECTED) { delay(500); } } void loop() { if (WiFi.status() == WL_CONNECTED) { // Use HTTPClient to control the server // ... } } ```

Once connected to the Wi-Fi network, use the `HTTPClient` library to send HTTP requests to the server's IP address and control its LED state.

The possibilities for ESP32 programming are practically endless. With its powerful features and low cost, it's no surprise that the ESP32 has become a popular choice for IoT projects. So dive into the vast world of ESP32 programming and start exploring, building, and connecting today!

ESP32 Pinout Guide: GPIO, Buttons & Ports Explained
ESP32 Pinout Guide: GPIO, Buttons & Ports Explained
Anatomy esp32
Anatomy esp32
How to send SMS using ESP32
How to send SMS using ESP32
an open laptop computer sitting on top of a white surface with the words, simulation esp
an open laptop computer sitting on top of a white surface with the words, simulation esp
an electronic device is shown with the words mott on it and two different components
an electronic device is shown with the words mott on it and two different components
Introduction to ESP32 Programming Series - The Engineering Projects
Introduction to ESP32 Programming Series - The Engineering Projects
How to Power the ESP32 Correctly ⚡
How to Power the ESP32 Correctly ⚡
ESP32 ESP8266 Publish Sensor Readings to Google Sheets | Random Nerd Tutorials
ESP32 ESP8266 Publish Sensor Readings to Google Sheets | Random Nerd Tutorials
Wokwi in VS Code: How To Simulate ESP32 Projects in VS Code!
Wokwi in VS Code: How To Simulate ESP32 Projects in VS Code!
ESP8266 0.96 inch OLED Display with Arduino IDE | Random Nerd Tutorials
ESP8266 0.96 inch OLED Display with Arduino IDE | Random Nerd Tutorials
ESP32: 10 Easy Projects for Beginners | Random Nerd Tutorials
ESP32: 10 Easy Projects for Beginners | Random Nerd Tutorials
ESP32 Explained: The Powerful IoT Board
ESP32 Explained: The Powerful IoT Board
Beginner Guide to ESP32 in Proteus 8 (Free Tutorial)
Beginner Guide to ESP32 in Proteus 8 (Free Tutorial)
esp 32 is a machine that can make you an engineer
esp 32 is a machine that can make you an engineer
🌐 Build an ESP32 Web Server to Control LEDs (Step-by-Step Guide!)
🌐 Build an ESP32 Web Server to Control LEDs (Step-by-Step Guide!)
ESP32 PWM with Arduino IDE (Analog Output) | Random Nerd Tutorials
ESP32 PWM with Arduino IDE (Analog Output) | Random Nerd Tutorials
🌐 ESP32 + IFTTT = Smart Home Magic! ✨
🌐 ESP32 + IFTTT = Smart Home Magic! ✨
Programming the ESP32 With an ARM Cortex-M USB CDC Gateway
Programming the ESP32 With an ARM Cortex-M USB CDC Gateway
ESP32 to Proteus: Step-by-Step Tutorial with Full Code
ESP32 to Proteus: Step-by-Step Tutorial with Full Code