搜索代码

ESP32 Tutorial 50/55 - Control RGB LED from anywhere in the world | SunFounder's ESP32 kit

ESP32 Tutorial 50/55 - Control RGB LED from anywhere in the world | SunFounder's ESP32 kit

In this tutorial, we will learn how to control the color of an RGB LED using the ESP32 microcontroller over Wi-Fi, utilizing the MQTT protocol and Adafruit IO service. This setup allows you to change the color of the RGB LED from anywhere in the world, providing a practical application of IoT technology. We will also explore how to use sliders and a color picker to select the desired color.

esp32-50-RGB-led-mqtt-main

The ESP32 is a powerful microcontroller that has built-in Wi-Fi and Bluetooth capabilities, making it ideal for IoT projects. In this build, we will connect an RGB LED to the ESP32 and control its color through an MQTT broker provided by Adafruit. The tutorial will guide you through the hardware setup, wiring instructions, and the code necessary to make everything work seamlessly (in video at 00:00).

Hardware Explained

esp32-50-RGB-led
esp32-50-RGB-led

For this project, the primary components we will use are the ESP32 microcontroller and the RGB LED. The ESP32 is capable of connecting to Wi-Fi networks, allowing it to communicate with the Adafruit IO service. The RGB LED contains three individual LEDs (red, green, and blue) that can be mixed to create a wide range of colors.

The RGB LED operates on a common anode or common cathode principle, which means that the anode (positive) or cathode (negative) of the individual LEDs must be connected properly for them to work. Each color can be controlled using Pulse Width Modulation (PWM), which adjusts the brightness of each LED by varying the duty cycle.

ES32-38_RGB_LED-wiring

Datasheet Details

Manufacturer SunFounder
Part number RGB LED
Forward voltage (VF) 2.0–3.4 V
Forward current (IF) 20 mA
Peak wavelength (nm) Red: 620, Green: 525, Blue: 465
Package Standard 4-pin
Notes / variants Common anode or common cathode options available

 

  • Use 220 ohm resistors for each LED color to limit current.
  • Ensure correct wiring for common anode or cathode configuration.
  • Check the ESP32's power supply to avoid brownouts.
  • Keep the PWM frequency within the limits for smooth color transitions.
  • Ensure Wi-Fi credentials are correct to connect to the Adafruit IO service.

Wiring Instructions

ES32-38_RGB_LED-wiring

To wire the RGB LED to the ESP32, start by identifying the pins on the RGB LED. The longest pin is the common pin. For a common anode configuration, connect this pin to the positive voltage supply (3.3V). The other three pins correspond to the red, green, and blue LEDs. Connect the red pin to GPIO 27, the green pin to GPIO 26, and the blue pin to GPIO 25. Each of these connections should be made through a 220 ohm resistor to limit the current flowing through the LEDs.

Next, connect the ground (GND) of the ESP32 to the ground line of your circuit. Ensure that the wiring is secure to prevent any intermittent connections. If using a common cathode RGB LED, connect the common pin to ground instead and connect the individual color pins to the positive supply through the resistors. Double-check all connections before powering the circuit.

Code Examples & Walkthrough

In the Arduino code, we start by defining the pins for the red, green, and blue LEDs using the identifiers redPin, greenPin, and bluePin. Additionally, we define the PWM channels for each color using redChannel, greenChannel, and blueChannel. The PWM frequency is set to 5000 Hz with an 8-bit resolution.

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

const int redChannel = 0;
const int greenChannel = 1;
const int blueChannel = 2;

In the setup() function, we initialize the PWM channels and attach the corresponding pins. We also connect to the Wi-Fi network using the defined credentials and set up the MQTT client for communication with Adafruit IO.

void setup() {
  ledcSetup(redChannel, freq, resolution);
  ledcAttachPin(redPin, redChannel);
  // Connect to WiFi
  WiFi.begin(WLAN_SSID, WLAN_PASS);
}

The main loop checks the MQTT connection and processes incoming messages. It also prints the current RGB values to the serial monitor. The color of the LED is updated based on the values received through MQTT subscriptions.

void loop() {
  MQTT_connect();
  mqtt.processPackets(500);
  setColor();
}

For more details on the full code, please refer to the complete code loaded below the article.

Demonstration / What to Expect

Once everything is set up and the code is uploaded, you should see the RGB LED responding to color changes made through the Adafruit IO dashboard. As you adjust the sliders for red, green, and blue, the LED should change its color accordingly. If you experience any issues, ensure that the Wi-Fi connection is stable and that the MQTT topic names match those defined in the code (in video at 17:30).

Common pitfalls include incorrect wiring, mismatched topic names, and forgetting to set the correct Wi-Fi credentials. If the LED does not light up, double-check the resistor connections and ensure that the ESP32 is powered correctly.

Video Timestamps

  • 00:00 Start
  • 2:23 Introduction to the project
  • 4:43 What is MQTT
  • 7:55 Adafruit IO setup
  • 14:09 Wiring explained
  • 16:07 Code explained
  • 27:03 Selecting ESP32 board and COM port on Arduino IDE
  • 29:12 Project demonstration
  • 31:25 What is RGB LED?
  • 35:26 RGB Color

图像

ESP32_rgb_pin
ESP32_rgb_pin
ESP32_RGB_led_wires
ESP32_RGB_led_wires
ES32-38_RGB_LED-wiring
ES32-38_RGB_LED-wiring
esp32-50-RGB-led
esp32-50-RGB-led
esp32-50-RGB-led-mqtt-main
esp32-50-RGB-led-mqtt-main
852-ESP32 Tutorial 50/55- Arduino code to control RGB LED using MQTT service of Adafruit
语言: C++
/*
 * 这是用于ESP32控制RGB LED的Arduino草图,使用Adafruit的MQTT服务  
 * 视频说明 https://youtu.be/-9q1GfGsnr0  
 * 📚⬇️ 下载和资源页面 https://robojax.com/RJT672  
 * 作者 Ahamd Shamshiri  
 * 于2024年2月18日  
 * 
 * Adafruit MQTT库 ESP32 Adafruit IO SSL/TLS示例  
 * 
 * /// 参考: https://www.electronicwings.com/esp32/esp32-mqtt-client  
 * // 定义RGB LED引脚
 */
const int redPin = 27;
const int greenPin = 26;
const int bluePin = 25;

 // 定义PWM通道
const int redChannel = 0;
const int greenChannel = 1;
const int blueChannel = 2;

 // 定义PWM频率和分辨率
const int freq = 5000;
const int resolution = 8;
int colorR, colorG, colorB;

#include <WiFi.h>
#include "WiFiClientSecure.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

/*
 * WiFi接入点 *********************************/
 * 
 * #define WLAN_SSID "Book"
 * #define WLAN_PASS "888-888"
 * 
 * ************************* Adafruit.io 设置
 */

#define AIO_SERVER "io.adafruit.com"

 // 使用端口8883进行MQTTS
#define AIO_SERVERPORT 8883

 // Adafruit IO 账户配置
 // (要获取这些值,请访问 https://io.adafruit.com 并点击“激活密钥”)
 // #define AIO_USERNAME "您的_ADAFRUIT_IO_用户名"
 // #define AIO_KEY "YOUR_ADAFRUIT_IO_KEY"

#define AIO_USERNAME "robojax"
#define AIO_KEY "aio_NBQQ75Rn7liRNcRn5uGUBMsBYmjD"

/*
 * 全球状态(您无需更改此内容!)******************/
 * 
 * // WiFiFlientSecure 用于 SSL/TLS 支持
 * WiFiClientSecure client;
 * 
 * // 通过传入 WiFi 客户端和 MQTT 服务器及登录详细信息设置 MQTT 客户端类。
 * Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
 * 
 * // io.adafruit.com 根 CA
 * const char* adafruitio_root_ca = \
 *  "-----BEGIN CERTIFICATE-----\n"
 *  "MIIEjTCCA3WgAwIBAgIQDQd4KhM/xvmlcpbhMf/ReTANBgkqhkiG9w0BAQsFADBh\n"
 *  "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n"
 *  "d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBH\n"
 *  "MjAeFw0xNzExMDIxMjIzMzdaFw0yNzExMDIxMjIzMzdaMGAxCzAJBgNVBAYTAlVT\n"
 *  "MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\n"
 *  "b20xHzAdBgNVBAMTFkdlb1RydXN0IFRMUyBSU0EgQ0EgRzEwggEiMA0GCSqGSIb3\n"
 *  "DQEBAQUAA4IBDwAwggEKAoIBAQC+F+jsvikKy/65LWEx/TMkCDIuWegh1Ngwvm4Q\n"
 *  "yISgP7oU5d79eoySG3vOhC3w/3jEMuipoH1fBtp7m0tTpsYbAhch4XA7rfuD6whU\n"
 *  "gajeErLVxoiWMPkC/DnUvbgi74BJmdBiuGHQSd7LwsuXpTEGG9fYXcbTVN5SATYq\n"
 *  "DfbexbYxTMwVJWoVb6lrBEgM3gBBqiiAiy800xu1Nq07JdCIQkBsNpFtZbIZhsDS\n"
 *  "fzlGWP4wEmBQ3O67c+ZXkFr2DcrXBEtHam80Gp2SNhou2U5U7UesDL/xgLK6/0d7\n"
 *  "6TnEVMSUVJkZ8VeZr+IUIlvoLrtjLbqugb0T3OYXW+CQU0kBAgMBAAGjggFAMIIB\n"
 *  "PDAdBgNVHQ4EFgQUlE/UXYvkpOKmgP792PkA76O+AlcwHwYDVR0jBBgwFoAUTiJU\n"
 *  "IBiV5uNu5g/6+rkS7QYXjzkwDgYDVR0PAQH/BAQDAgGGMB0GA1UdJQQWMBQGCCsG\n"
 *  "AQUFBwMBBggrBgEFBQcDAjASBgNVHRMBAf8ECDAGAQH/AgEAMDQGCCsGAQUFBwEB\n"
 *  "BCgwJjAkBggrBgEFBQcwAYYYaHR0cDovL29jc3AuZGlnaWNlcnQuY29tMEIGA1Ud\n"
 *  "HwQ7MDkwN6A1oDOGMWh0dHA6Ly9jcmwzLmRpZ2ljZXJ0LmNvbS9EaWdpQ2VydEds\n"
 *  "b2JhbFJvb3RHMi5jcmwwPQYDVR0gBDYwNDAyBgRVHSAAMCowKAYIKwYBBQUHAgEW\n"
 *  "HGh0dHBzOi8vd3d3LmRpZ2ljZXJ0LmNvbS9DUFMwDQYJKoZIhvcNAQELBQADggEB\n"
 *  "AIIcBDqC6cWpyGUSXAjjAcYwsK4iiGF7KweG97i1RJz1kwZhRoo6orU1JtBYnjzB\n"
 *  "c4+/sXmnHJk3mlPyL1xuIAt9sMeC7+vreRIF5wFBC0MCN5sbHwhNN1JzKbifNeP5\n"
 *  "ozpZdQFmkCo+neBiKR6HqIA+LMTMCMMuv2khGGuPHmtDze4GmEGZtYLyF8EQpa5Y\n"
 *  "jPuV6k2Cr/N3XxFpT3hRpt/3usU/Zb9wfKPtWpoznZ4/44c1p9rzFcZYrWkj3A+7\n"
 *  "TNBJE0GmP2fhXhP1D/XVfIW/h0yCJGEiV9Glm/uGOa3DXHlmbAcxSyCRraG+ZBkA\n"
 *  "7h4SeM6Y8l/7MBRpPCz6l8Y=\n"
 *  "-----END CERTIFICATE-----\n";
 * 
 * /****************************** 订阅
 */

 // 设置一个名为“test”的发布馈送和一个名为“test2”的订阅馈送。
 // 注意,AIO 的 MQTT 路径采用以下格式:<用户名>/feeds/<数据源名称>
Adafruit_MQTT_Subscribe COLOR_R = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/rgb-led-color.red");
Adafruit_MQTT_Subscribe COLOR_G = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/rgb-led-color.green");
Adafruit_MQTT_Subscribe COLOR_B = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/rgb-led-color.blue");
Adafruit_MQTT_Subscribe COLOR_RGB = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/color-picker");


资源与参考

尚无可用资源。

文件📁

没有可用的文件。