ESP32 Tutorial 52/55 - WS2812 CheerLights MQTT Global Sync with LCD | SunFounder ESP32 IoT kit

ESP32 Tutorial 52/55 - WS2812 CheerLights MQTT Global Sync with LCD | SunFounder ESP32 IoT kit

In this tutorial, we will create a CheerLights project using the ESP32, which will synchronize colors globally through MQTT. This project will not only change colors based on inputs from other users but also display the current color and how many times it has been updated on an LCD screen. The outcome is a fun demonstration of IoT capabilities, showcasing how devices can interact and keep users connected across distances. For additional clarification, watch the video at (in video at 00:00).

esp32-52-cheer-light-lcd-main

Hardware Explained

To build this project, you'll need an ESP32 microcontroller, a WS2812 LED strip, and an LCD display. The ESP32 serves as the brain of the operation, utilizing its built-in Wi-Fi capabilities to connect to the internet and receive MQTT messages. This allows real-time updates to the LED colors based on a global input from other users.

The WS2812 LED strip is a popular choice for projects requiring addressable RGB LEDs. Each LED can be controlled independently, allowing for rich color displays. The LCD will provide a visual confirmation of the current color and the number of times it has been changed, enhancing user interaction.

cheeLights_LCD

Datasheet Details

Manufacturer SunFounder
Part number ESP32
Logic/IO voltage 3.3 V
Supply voltage 5 V
Output current (per channel) 20 mA
Peak current (per channel) 60 mA
PWM frequency guidance 400 Hz
Input logic thresholds 0.15 V (low), 0.8 V (high)
Voltage drop / RDS(on) / saturation 0.2 V
Thermal limits 85 °C
Package ESP32 module
Notes / variants Includes built-in Wi-Fi and Bluetooth

 

  • Ensure proper power supply to the ESP32 (5 V) and WS2812 strip (5 V).
  • Use a common ground between the ESP32 and the LED strip.
  • Implement a proper MQTT broker for color synchronization.
  • Pay attention to the data pin connection for the WS2812 (pin 14 as per the code).
  • Be cautious with the number of LEDs; exceeding the power limits requires additional power sources.

Wiring Instructions

ESP32-11_LCD-wiring
esp32-47-cheer-light-wiring

To wire the components, start by connecting the WS2812 LED strip. Connect the ground pin (usually black) of the LED strip to the ground pin on the ESP32. Next, connect the VCC pin (usually red) of the LED strip to the 5V output on the ESP32. Finally, connect the data pin (often yellow) to GPIO pin 14 on the ESP32.

For the LCD, connect the ground pin (often black) to the ground on the ESP32. The VCC pin (usually red) should be connected to the 5V output. The SDA pin (typically gray) connects to GPIO pin 21, while the SCL pin (usually white) connects to GPIO pin 22. This setup allows the ESP32 to communicate with the LCD and display information as needed.

Code Examples & Walkthrough

In the setup function, we initialize the LCD and connect to Wi-Fi. The code below demonstrates how to define the necessary libraries and set up the LCD:

#include  
#include 
LiquidCrystal_I2C lcd(0x27, 16,2);  // set the LCD address
void setup() {
  Serial.begin(115200);
  lcd.init(); // initialize the lcd 
  lcd.backlight(); // Turns on the LCD backlight.
}

This initializes the LCD for use, allowing it to display messages. The next excerpt shows how the Wi-Fi connection is established:

void setup_wifi() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    lcdConnect(); //for LCD
    delay(500);
  }
  Serial.println("WiFi connected");
}

This function establishes a connection to the specified Wi-Fi network, displaying a message on the LCD while connecting. Lastly, the color change logic is handled in the callback function:

void callback(char* topic, byte* message, unsigned int length) {
  String messageTemp;
  for (int i = 0; i < length; i++) {
    messageTemp += (char)message[i];
  }
  if (String(topic) == "cheerlights") {
    setColor(messageTemp);
  }
}

This function listens for incoming messages on the "cheerlights" topic and updates the color accordingly. The full code loads below the article, so make sure to review it for the complete implementation.

Demonstration / What to Expect

Upon completion of the project, you can expect the LED strip to change colors based on inputs from a global MQTT feed. The LCD will display the current color name and how many times it has been changed. If you disconnect the internet, the LCD will indicate "Connecting..." until a connection is re-established (in video at 12:30).

Common pitfalls include ensuring that the correct pins are used for data connections and verifying that the Wi-Fi credentials are accurate. If there are any discrepancies in the SSID or password, the ESP32 will fail to connect, and the LCD will continue to display the connecting message.

Video Timestamps

  • 00:00 Start
  • 1:59 Introduction to the project
  • 6:16 Wiring explained
  • 8:13 Arduino Code explained
  • 14:26 Selecting ESP32 board and COM port on Arduino IDE
  • 16:07 CheerLight Demonstration with LCD

图像

ESP32-11_LCD-wiring
ESP32-11_LCD-wiring
esp32-47-cheer-light-wiring
esp32-47-cheer-light-wiring
esp32-52-cheer-light-lcd-main
esp32-52-cheer-light-lcd-main
cheeLights_LCD
cheeLights_LCD
853-ESP32 Tutorial 52/55- CheerLight MQTT and LCD
语言: C++
已复制!

资源与参考

文件📁

没有可用的文件。