ESP32 Tutorial 38/55 - Controling RGB LED from your mobile phone | SunFounder's ESP32 IoT Learning kit

ESP32 Tutorial 38/55 - Controling RGB LED from your mobile phone | SunFounder's ESP32 IoT Learning kit

In this tutorial, we'll explore how to control an RGB LED using an ESP32 module from the SunFounder ESP32 learning kit. By sending commands from your mobile device, you can change the LED color or turn it off entirely. This project harnesses the capabilities of the ESP32, leveraging built-in Wi-Fi and Bluetooth features for seamless connectivity and control.

ESP32_RGB_led_wires
ESP32_rgb_pin

The RGB LED consists of three individual LEDs: red, green, and blue, which can be mixed to create various colors. In this project, you will learn how to wire the RGB LED correctly and program the ESP32 to respond to Bluetooth commands. The tutorial will also guide you through the necessary code components to achieve this functionality (in video at 02:15).

Hardware Explained

The primary components for this project include the ESP32 microcontroller and the RGB LED. The ESP32 is a powerful module with built-in Wi-Fi and Bluetooth, making it ideal for IoT applications. In this project, it will act as a server to receive commands from a mobile device and control the RGB LED accordingly.

The RGB LED has four pins: one common pin (either anode or cathode) and three pins for the individual colors. The common pin connects to either the power source or ground, while the other three pins connect to the ESP32's GPIO pins through resistors to limit the current and protect the LEDs. This setup allows for precise control of each color's brightness, creating a wide range of colors.

Datasheet Details

Manufacturer SunFounder
Part number RGB LED
Common pin type Common anode / Common cathode
Forward voltage (V) 2.0 - 3.2 V
Max forward current (A) 20 mA
Typical current (A) 15 mA
Color resolution 8 bit (0-255)
Package Through-hole / SMD

 

  • Ensure proper resistor values (typically 220 Ohm) to limit current through each LED channel.
  • Check the common pin configuration (anode or cathode) before wiring.
  • Use PWM for dimming and color mixing by adjusting the signal sent to each LED.
  • Be cautious with wiring to avoid short circuits; connect one pin at a time.
  • Test each color individually after setup to confirm correct wiring.

Wiring Instructions

ES32-38_RGB_LED-wiring

To wire the RGB LED to the ESP32, start by placing the RGB LED on a breadboard. The longer pin is the common pin, which you will connect to either the positive voltage (for common anode) or ground (for common cathode). If you are using common anode, connect the long pin to the 3.3V pin on the ESP32. For common cathode, connect it to the GND pin.

Next, take three 220 Ohm resistors and connect one end of each resistor to the corresponding RGB pins of the LED. Connect the other ends of the resistors to the ESP32 GPIO pins: connect the red pin of the LED to GPIO 27, the green pin to GPIO 26, and the blue pin to GPIO 25. Finally, ensure that the common pin is appropriately connected based on your configuration (anode or cathode).

Code Examples & Walkthrough

The code for this project begins with defining the pins connected to the RGB LED. The following excerpt shows how the pins are declared:

const int redPin = 27;
const int greenPin = 26;
const int bluePin = 25;

Here, redPin, greenPin, and bluePin are assigned specific GPIO numbers on the ESP32 for each color channel of the RGB LED.

In the setup function, the Bluetooth is initialized, and the PWM settings are applied. This excerpt demonstrates this initialization:

void setup() {
  Serial.begin(115200);      // Initialize the serial port
  setupBLE();                // Initialize the Bluetooth BLE

  ledcAttach(redPin, freq, resolution);
  ledcAttach(greenPin, freq, resolution);
  ledcAttach(bluePin, freq, resolution);
}

This code initializes the serial communication and sets up the Bluetooth functionality while attaching the RGB LED pins to PWM channels for control.

Finally, the loop function checks for received Bluetooth messages and adjusts the LED color accordingly:

if (value == "red") {
  setColor(255, 0, 0); // Red
  Serial.println("red");
}

In this section, if the received value is "red", the LED will be set to full red brightness using the setColor function.

For a complete understanding of the code, it's recommended to watch the video tutorial where the full code is loaded below the article.

Demonstration / What to Expect

Once everything is wired and the code is uploaded, you should be able to control the RGB LED from your mobile device via Bluetooth. By sending commands like "red", "green", "blue", etc., you will see the LED change colors accordingly. If you send "LED_off", the RGB LED will turn off. Make sure to check the serial monitor for any debugging messages to confirm that commands are being received correctly (in video at 10:45).

Video Timestamps

  • 00:00 Start
  • 1:59 What's RGB LED?
  • 6:01 RGB Color explained
  • 10:01 Documentation page
  • 11:19 Wiring explained
  • 13:34 Selecting ESP32 board and COM port in Arduino IDE
  • 15:15 Arduino Code
  • 18:02 Demonstration of controlling RGB LED using your phone

图像

ESP32_rgb_pin
ESP32_rgb_pin
ESP32_RGB_led_wires
ESP32_RGB_led_wires
ES32-38_RGB_LED-wiring
ES32-38_RGB_LED-wiring
839-ESP32 Tutorial 38/55- Arduino code for controlling RGB LED using bluetooth app
语言: C++
已复制!

资源与参考

文件📁

没有可用的文件。