Esp32 Code Examples: Explore Our Library
The esp32, a low-cost microcontroller with integrated Wi-Fi and dual-mode Bluetooth, is a powerful tool for IoT applications, prototyping, and educational purposes. If you're new to the esp32, you're probably eager to dive into some example codes to get started. This article will guide you through various esp32 examples, helping you understand how to use this versatile microcontroller.
Let's begin by discussing the setup and basic programming of the esp32, then delve into more specific use cases.
Getting Started with esp32
Before we dive into example codes, let's ensure you have the right tools and environment set up.
1. **Hardware**: You'll need an esp32 development board. Popular choices include the NodeMCU, ESP32-WROVER-B, and the official espressif ESP32-PICO-V3. Also, ensure you have the necessary components for your projects, like LEDs, resistors, and jumper wires.
Setting Up the Development Environment
2. **Software**: Install the esp32 board in the Arduino IDE. Go to `File > Preferences`, then enter `https://dl.espressif.com/dl/package_esp32_index.json` in the `Additional Boards Manager URLs` field. Restart the IDE, go to `Tools > Board > Boards Manager`, and search for `esp32` to install it.
3. **Sample Blink Code**: Once set up, let's blink an LED to confirm your esp32 is working. Here's a simple sketch:
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
esp32 Development Boards pins
Familiarize yourself with the esp32 board's GPIO pins, as they are used in programming. Most esp32 boards have an LED connected to GPIO2 and an onboard button connected to GPIO0.
Knowing your development board's pin layout and meanings is crucial for building projects.
esp32 Example Code: Wi-Fi and Networking
One of the esp32's standout features is its integrated Wi-Fi. Let's explore a simple Wi-Fi connection example.
For this, we'll use the `ESP8266WiFi` library (your esp32 needs this too). First, include the library in your sketch, then create a Wi-Fi access point:
#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);
return;
}
Serial.println("Connected to " + WiFi.SSID());
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
This code connects to a Wi-Fi network and prints the IP address via serial monitor.
Interfacing with Other Devices
The esp32's built-in Wi-Fi and Bluetooth allows connection to other devices. For instance, you can create a simple web server with the `ESP8266WebServer` library, or control an esp32 from a smartphone using the `BearSSL` library for MQTT or WebSocket communication.
esp32 Example Code: IoT Projects and Sensors
The esp32's versatility extends to IoT projects. Partnered with sensors and actuators, the esp32 can collect data and interact with the physical world.
esp32 and DHT11 – Temperature and Humidity Sensor
To read temperature and humidity data, connect a DHT11 sensor to your esp32 and use the `DHT` library. Here's a simple sketch:
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read data from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("%, ");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println("°C");
}
With this setup, your esp32 can read temperature and humidity data and print it via serial monitor.
esp32 and a Reed Switch – Motion Detection
For motion detection, connect a reed switch (door/window magnet) to the esp32. When motion triggers the switch, the esp32 can detect it and respond accordingly (like send a notification, trigger an alarm, etc.). Here's a basic sketch:
const int pirPin = 2;
void setup() {
pinMode(pirPin, INPUT);
Serial.begin(115200);
}
void loop() {
int motion = digitalRead(pirPin);
if (motion == HIGH) {
Serial.println("Motion detected!");
}
delay(1000);
}
This simple motion sensor can help you build complex IoT systems, like smart homes, intrusion detection systems, etc.
The esp32's functionality and versatility make it an excellent choice for various IoT applications. Whether it's controlling an LED, creating a Wi-Fi access point, or building a motion sensor, the esp32 always delivers. As you learn and experiment with more esp32 examples, you'll unlock new project possibilities and refine your skills. Keep exploring!