DEV Community

Cover image for πŸ’‘ Controlling an External LED with ESP32 and Arduino IDE
Mohamed Ahmed
Mohamed Ahmed

Posted on

πŸ’‘ Controlling an External LED with ESP32 and Arduino IDE

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

  1. - ESP32 development board (e.g., ESP32 DevKit C)
  2. - 1 Γ— LED (any color)
  3. - 1 Γ— 220 Ξ© resistor
  4. - Breadboard
  5. - Jumper wires
  6. - 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
}

Enter fullscreen mode Exit fullscreen mode

🧠 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

  1. Connect your ESP32 to your computer.
  2. Select the correct board and port (as before).
  3. Click Upload.
  4. After uploading, open Tools β†’ Serial Monitor.
  5. Set the baud rate to 115200. You should see messages like:

Your IDE should look like this
Your external LED should blink in sync! ✨
_
πŸ’‘ Tip: On some boards, you may need to hold the BOOT button while uploading the code._

Your IDE should look like this

⚑ 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)