ESP32 First Code: Getting Started with IoT Development
The ESP32 is like an Arduino, but with WiFi and Bluetooth built in. That means you can make things that connect to the internet without adding extra hardware. It's perfect for IoT projects - smart home stuff, sensors that send data online, remote controls, you name it.
If you've never used an ESP32 before, this guide will get you started. By the end, you'll have written and uploaded your first program that connects to WiFi.
What You'll Need
- An ESP32 development board (they're like $5-10 on Amazon)
- A USB cable (most ESP32 boards use micro USB)
- The Arduino IDE (same one you use for Arduino)
- WiFi network (to connect to)
That's it. The ESP32 is pretty self-contained.
Step 1: Install ESP32 Support in Arduino IDE
The Arduino IDE doesn't support ESP32 by default, but it's easy to add. Open Arduino IDE, go to File → Preferences, and in "Additional Boards Manager URLs", add:
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
Then go to Tools → Board → Boards Manager, search for "ESP32", and install "esp32 by Espressif Systems". This takes a few minutes.
Step 2: Select Your Board
Plug in your ESP32. Go to Tools → Board and select your ESP32 board (probably "ESP32 Dev Module" if you're not sure). Then go to Tools → Port and select the COM port that appeared when you plugged it in.
Some ESP32 boards need you to hold the BOOT button while uploading. Check your board's instructions, but most newer ones don't need this.
Step 3: Your First Program - Blink the Built-in LED
Let's start simple - blink the built-in LED. Most ESP32 boards have an LED on pin 2. Here's the code:
void setup() {
pinMode(2, OUTPUT);
}
void loop() {
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, LOW);
delay(1000);
}
Upload this just like you would with Arduino. If the LED blinks, you're good to go!
Step 4: Connect to WiFi
Now the fun part - connecting to WiFi. Here's a basic example:
#include <WiFi.h>
const char* ssid = "YourWiFiName";
const char* password = "YourWiFiPassword";
void setup() {
Serial.begin(115200);
pinMode(2, OUTPUT);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
digitalWrite(2, !digitalRead(2)); // Blink LED while connecting
}
Serial.println("");
Serial.println("WiFi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
digitalWrite(2, HIGH); // LED on when connected
}
void loop() {
// Your code here
}
Replace "YourWiFiName" and "YourWiFiPassword" with your actual WiFi credentials. Upload it, open the Serial Monitor (Tools → Serial Monitor, set to 115200 baud), and you should see it connect.
When it connects, it'll print your IP address. That's your ESP32's address on your network.
Step 5: Create a Simple Web Server
Now let's make it serve a web page. This is where it gets cool - you can control your ESP32 from a browser:
#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "YourWiFiName";
const char* password = "YourWiFiPassword";
WebServer server(80);
void handleRoot() {
server.send(200, "text/html", "<h1>Hello from ESP32!</h1><p>It works!</p>");
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected! IP: ");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.begin();
Serial.println("Web server started!");
}
void loop() {
server.handleClient();
}
Upload this, wait for it to connect, then open a browser and go to the IP address it printed. You should see "Hello from ESP32!" in your browser. Your ESP32 is now a web server!
Common Issues and Fixes
- Won't upload: Try holding the BOOT button while uploading, or check that you selected the right board and port.
- Can't connect to WiFi: Check your credentials, make sure your router supports 2.4GHz (ESP32 doesn't do 5GHz).
- Serial Monitor shows garbage: Make sure baud rate is set to 115200.
- Board not found: Install the ESP32 board support (Step 1) and make sure drivers are installed.
Pro Tip: The ESP32 has two cores, so you can run two things at once. But for beginners, don't worry about that yet. Just get the basics working first.
What's Next?
Now that you can connect to WiFi, try:
- Reading sensors and sending data online
- Controlling things remotely through a web interface
- Connecting to APIs and services
- Building a simple home automation system
The ESP32 is incredibly powerful for its price. Once you get comfortable with the basics, the possibilities are endless.
Common Questions
What's the difference between ESP32 and Arduino?
ESP32 has WiFi and Bluetooth built in, more processing power, and more memory. It's more powerful but also a bit more complex. Arduino is simpler to start with, but ESP32 is better for IoT projects.
Can I use Arduino code on ESP32?
Mostly yes. A lot of Arduino code works on ESP32 with minor changes. But ESP32-specific features (like WiFi) need different libraries.
Do I need to know networking?
Not really. The examples I showed work out of the box. Once you get comfortable, you can learn more about networking, but you don't need it to get started.
Start Building IoT Projects
You've connected your ESP32 to WiFi and created a web server. That's the foundation for tons of cool projects. Experiment, break things, fix them, and learn. That's how you get good at this stuff.