arduino esp32 code examples

Arduino ESP32 Code Examples

By Robert Moore
AD

Embarking on your journey with Arduino, the ESP32, an affordable and powerful microcontroller, brings a wealth of possibilities to your project. Let's delve into the world of ESP32 code examples, showcasing its versatility and guidance for your next IoT or embedded project.

Firstly, let's understand what ESP32 offers. This system-on-chip includes a 240MHz dual-core processor, Wi-Fi and Bluetooth connectivity, and 520KB SRAM, all packed into a minuscule form factor. Now, let's explore some code examples that highlight its capabilities.

Blink an LED: A Classic ESP32 Example

Kickstarting your ESP32 journey, blinking an LED is a staple among Arduino enthusiasts, and now ESP32 developers too. Here's a basic example:

AD
10 ESP32 Skills Every Maker Should Learn
10 ESP32 Skills Every Maker Should Learn
Simulate ESP32 Projects in VS Code with Wokwi (Beginner Guide!)
Simulate ESP32 Projects in VS Code with Wokwi (Beginner Guide!)
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
```cpp void setup() { pinMode(2, OUTPUT); } void loop() { digitalWrite(2, HIGH); delay(1000); digitalWrite(2, LOW); delay(1000); } ```

This code uses the digitalWrite() function to turn ON and OFF the LED connected to GPIO2, with a 1-second interval.

Here's an enhancement, adding a pulsing effect with varying delays:

Anatomy esp32
Anatomy esp32
Custom Arduino Library for ESP32 📦⚙️
Custom Arduino Library for ESP32 📦⚙️
Forget the Arduino IDE: 5 Awesome Arduino IDE Alternatives - Learn Robotics
Forget the Arduino IDE: 5 Awesome Arduino IDE Alternatives - Learn Robotics
```cpp void setup() { pinMode(2, OUTPUT); } int delayTime = 100; void loop() { digitalWrite(2, HIGH); delay(delayTime); digitalWrite(2, LOW); delay(delayTime); if (delayTime == 1000) delayTime = 100; else delayTime += 50; } ```

Connect to Wi-Fi

ESP32's built-in Wi-Fi capabilities enable effortless connectivity. Here's a simple example:

```cpp #include "ESP8266WiFi.h" const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); } void loop() { if (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } else { Serial.println("Connected to WiFi"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); delay(10000); } } ```

This code connects to your specified Wi-Fi network and prints your IP address once connected.

AD

Next, let's explore another powerful feature of ESP32...

the esp32 wi - fi is shown with fireworks in the background
the esp32 wi - fi is shown with fireworks in the background
🌐 ESP32 Static IP Address – Assign a Fixed IP in Arduino IDE!
🌐 ESP32 Static IP Address – Assign a Fixed IP in Arduino IDE!
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 to Proteus: Step-by-Step Tutorial with Full Code
ESP32 to Proteus: Step-by-Step Tutorial with Full Code
how to send sms from esp32 on the breadboard with an esp23
how to send sms from esp32 on the breadboard with an esp23
Better Debugging for Arduino IDE: SerialDebug Library | Random Nerd Tutorials
Better Debugging for Arduino IDE: SerialDebug Library | Random Nerd Tutorials

The Use Case: Controlling appliances via MQTT

MQTT, a lightweight publish/subscribe messaging protocol, is perfect for IoT projects. Here's an example of controlling an LED using an MQTT server:

Subscribe to the MQTT server

ESP32 connects to the MQTT server and subscribes to a topic (led_status). When a message arrives, it updates the LED status accordingly.

AD
```cpp #include #include const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; const char* mqtt_server = "your_mqtt_server"; const char* mqtt_username = "your_mqtt_username"; const char* mqtt_password = "your_mqtt_password"; const char* topic = "led_status"; WiFiClient espClient; PubSubClient client(espClient); void setup() { Serial.begin(115200); WiFi.begin(ssid, password); client.setServer(mqtt_server, 1883); pinMode(2, OUTPUT); } void loop() { if (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } if (!client.connected()) { reconnect(); } client.loop(); } void reconnect() { while (!client.connected()) { Serial.print("Attempting MQTT connection..."); if (client.connect("ESP32_Client", mqtt_username, mqtt_password)) { client.subscribe(topic); Serial.println("connected"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); delay(5000); return; } } } void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived in topic: "); Serial.println(topic); for (int i = 0; i < length; i++) { Serial.print((char) payload[i]); } Serial.println(); if ((char)payload[0] == '1') { digitalWrite(2, HIGH); } else { digitalWrite(2, LOW); } } ```

With this example, you've harnessed the power of ESP32 for IoT projects, controlling appliances wirelessly with MQTT.

As you tqdm through your ESP32 learning journey, these examples merely scratch the surface. Explore ESP32's rich APIs, bolstered by a vast online community, to unlock its full potential. So, wire up, code, and shape your IoT future with Arduino's ESP32!

🔧 Install ESP32 in Arduino IDE – Quick & Easy Setup Guide!
🔧 Install ESP32 in Arduino IDE – Quick & Easy Setup Guide!
ESP32 PIR Motion Sensor – Detect Motion with Arduino IDE
ESP32 PIR Motion Sensor – Detect Motion with Arduino IDE
Siri + Arduino
Siri + Arduino
Best Arduino Simulators- – Perfect for Testing Your Code!
Best Arduino Simulators- – Perfect for Testing Your Code!
Best Arduino Projects with Code
Best Arduino Projects with Code
ESP32 PWM with Arduino IDE (Analog Output) | Random Nerd Tutorials
ESP32 PWM with Arduino IDE (Analog Output) | Random Nerd Tutorials
🤖 Build an AI Attendance System using ESP32! 📡📝
🤖 Build an AI Attendance System using ESP32! 📡📝
How to Power the ESP32 Correctly ⚡
How to Power the ESP32 Correctly ⚡
🌐 Build an ESP32 Web Server to Control LEDs (Step-by-Step Guide!)
🌐 Build an ESP32 Web Server to Control LEDs (Step-by-Step Guide!)
📁 Upload Files to ESP32 SPIFFS Without Arduino IDE!
📁 Upload Files to ESP32 SPIFFS Without Arduino IDE!
ESP32 Tutorials for Beginners
ESP32 Tutorials for Beginners
50+ Arduino Projects with Code - 30+ Arduino Projects for Beginners Inside
50+ Arduino Projects with Code - 30+ Arduino Projects for Beginners Inside
AD