Introduction
Welcome back, makers! π
In the previous post we successfully uploaded our first program β the classic βBlinkβ β to make the built-in LED on the ESP32 blink.
Now, weβll take it a step further by connecting and controlling an external LED.
This is your first step into hardware interaction β where your ESP32 begins to control real-world devices!
π§° What Youβll Need
- - ESP32 development board (e.g., ESP32 DevKit C)
- - 1 Γ LED (any color)
- - 1 Γ 220 Ξ© resistor
- - Breadboard
- - Jumper wires
- - USB cable connected to your computer
βοΈ Step 1: Breadboard/circuit Connections
Component | ESP32 pin | Description |
---|---|---|
LED (long leg β anode) | GPIO 4 | Digital output pin |
LED (short leg β cathode) | GND | Ground |
Resistor (220 Ξ©) | In series with LED | Limits current to protect LED |
You can use GPIO 5, 13, or 14 β just make sure to update the code accordingly.
π‘ Tip: You can use GPIO 5, 13, or 14 instead β just update the code accordingly.
Always use a resistor in series with your LED to prevent burning it out!
π» Step 2: Write the Code
In the Arduino IDE, open a new sketch and paste this:
// Control an external LED using ESP32
int ledPin = 4; // The GPIO pin connected to the LED
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as output
Serial.begin(115200); // Start the Serial Monitor
Serial.println("External LED Test Started");
}
void loop() {
digitalWrite(ledPin, HIGH); // Turn LED on
Serial.println("LED is ON");
delay(1000); // Wait 1 second
digitalWrite(ledPin, LOW); // Turn LED off
Serial.println("LED is OFF");
delay(1000); // Wait 1 second
}
π§ Explanation:
- pinMode(ledPin, OUTPUT) tells the ESP32 that GPIO 4 will send signals.
- digitalWrite(ledPin, HIGH/LOW) turns the LED on and off.
- The Serial Monitor prints real-time messages so you can see what the board is doing.
π₯οΈ Step 3: Upload and Monitor
- Connect your ESP32 to your computer.
- Select the correct board and port (as before).
- Click Upload.
- After uploading, open Tools β Serial Monitor.
- Set the baud rate to 115200. You should see messages like:
Your external LED should blink in sync! β¨
_π‘ Tip: On some boards, you may need to hold the BOOT button while uploading the code._
β‘ Troubleshooting
1.LED not lighting up?
- Check your wiring (make sure anode and cathode arenβt reversed).
- Try a different GPIO pin and update the code.
- Verify that your resistor is not too large (220 Ξ©β330 Ξ© works best).
2.No Serial Output?
- Ensure you opened the Serial Monitor at 115200 baud.
- Make sure the code includes Serial.begin(115200);.
π― Whatβs Next
β
Youβve now controlled an external LED with your ESP32!
In the next post, weβll add a button so you can turn the LED on and off manually. πΉοΈ
Get ready to make your first interactive circuit!
π¬ Your Turn!
Did your external LED blink? π΄π‘
Share your success (or any issues you faced) in the comments β Iβd love to see your first working circuit!
Top comments (0)