Header Ads Widget

Responsive Advertisement

How to Blink an RGB LED with ESP32 in Different Colors

 

How to Blink an RGB LED with ESP32 in Different Colors

Introduction

RGB LEDs allow you to create multiple colors by mixing red, green, and blue light. In this tutorial, we will use an ESP32 to control an RGB LED, making it blink in different colors.

Components Required

  • ESP32 board
  • Common cathode RGB LED
  • Jumper wires
  • Breadboard

Circuit Connection

RGB LED PinESP32 Pin
Red (R)GPIO 25
Green (G)GPIO 26
Blue (B)GPIO 27
GNDGND

Arduino Code for Blinking RGB LED



#define RED_PIN 25 #define GREEN_PIN 26 #define BLUE_PIN 27 void setup() { pinMode(RED_PIN, OUTPUT); pinMode(GREEN_PIN, OUTPUT); pinMode(BLUE_PIN, OUTPUT); } void loop() { setColor(255, 0, 0); // Red delay(1000); setColor(0, 255, 0); // Green delay(1000); setColor(0, 0, 255); // Blue delay(1000); setColor(255, 255, 0); // Yellow delay(1000); setColor(0, 255, 255); // Cyan delay(1000); setColor(255, 0, 255); // Magenta delay(1000); setColor(255, 255, 255); // White delay(1000); setColor(0, 0, 0); // OFF delay(1000); } void setColor(int red, int green, int blue) { analogWrite(RED_PIN, red); analogWrite(GREEN_PIN, green); analogWrite(BLUE_PIN, blue); }

_____________________________________________________________

Explanation

  • setColor(r, g, b) sets the brightness of the red, green, and blue LEDs using PWM.
  • The program cycles through 7 different colors (Red, Green, Blue, Yellow, Cyan, Magenta, White) and then turns the LED off.
  • delay(1000) keeps each color on for 1 second.

Conclusion

This simple program makes an RGB LED blink in different colors using an ESP32. You can modify it to create fading effects or use sensors for color changes.

🔧 Try it out and share your thoughts in the comments! 🚀

Post a Comment

0 Comments