Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
đ Ready to explore and create with us? Click [here] and join today!
Lesson 45: Bluetooth Control RGB LEDď
This project is an extension of a previous project(Lesson 44: Bluetooth), adding RGB LED configurations and custom commands such as âled_offâ, âredâ, âgreenâ, etc. These commands allow the RGB LED to be controlled by sending commands from a mobile device using LightBlue.
Required Components
In this project, we need the following components.
Itâs definitely convenient to buy a whole kit, hereâs the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
|---|---|
ESP32 & Development Board (ESP32 Board) |
|
- |
Operation Steps
Build the circuit.
Open the
Lesson_45_Bluetooth_RGB.inofile located in theuniversal-maker-sensor-kit\esp32\Lesson_45_Bluetooth_RGBdirectory, or copy the code into the Arduino IDE.To avoid UUID conflicts, it is recommended to randomly generate three new UUIDs using the Online UUID Generator provided by the Bluetooth SIG, and fill them in the following lines of code.
Note
If you have already generated three new UUIDs in the Lesson 44: Bluetooth project, then you can continue using them.
#define SERVICE_UUID "your_service_uuid_here" #define CHARACTERISTIC_UUID_RX "your_rx_characteristic_uuid_here" #define CHARACTERISTIC_UUID_TX "your_tx_characteristic_uuid_here"
Select the correct board and port, then click the Upload button.
After the code has been successfully uploaded, turn on Bluetooth on your mobile device and open the LightBlue app.
On the Scan page, find ESP32-Bluetooth and click CONNECT. If you donât see it, try refreshing the page a few times. When âConnected to device!â appears, the Bluetooth connection is successful. Scroll down to see the three UUIDs set in the code.
Tap the Send UUID, then set the data format to âUTF-8 Stringâ. Now you can write these commands: âled_offâ, âredâ, âgreenâ, âblueâ, âyellowâ, and âpurpleâ to see if the RGB LED responds to these instructions.
How it works?
This code is an extension of a previous project(Lesson 44: Bluetooth), adding RGB LED configurations and custom commands such as âled_offâ, âredâ, âgreenâ, etc. These commands allow the RGB LED to be controlled by sending commands from a mobile device using LightBlue.
Letâs break down the code step by step:
Add new global variables for the RGB LED pins, PWM channels, frequency, and resolution.
... // Define RGB LED pins const int redPin = 27; const int greenPin = 26; const int bluePin = 25; // Define PWM channels const int redChannel = 0; const int greenChannel = 1; const int blueChannel = 2; ...
Within the
setup()function, the PWM channels are initialized with the predefined frequency and resolution. The RGB LED pins are then attached to their respective PWM channels.void setup() { ... // Set up PWM channels ledcSetup(redChannel, freq, resolution); ledcSetup(greenChannel, freq, resolution); ledcSetup(blueChannel, freq, resolution); // Attach pins to corresponding PWM channels ledcAttachPin(redPin, redChannel); ledcAttachPin(greenPin, greenChannel); ledcAttachPin(bluePin, blueChannel); }
Modify the
onWritemethod in theMyCharacteristicCallbacksclass. This function listens for data coming from the Bluetooth connection. Based on the received string (like"led_off","red","green", etc.), it controls the RGB LED.// Define the BLE characteristic callbacks class MyCharacteristicCallbacks : public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { std::string value = pCharacteristic->getValue(); if (value == "led_off") { setColor(0, 0, 0); // turn the RGB LED off Serial.println("RGB LED turned off"); } else if (value == "red") { setColor(255, 0, 0); // Red Serial.println("red"); } else if (value == "green") { setColor(0, 255, 0); // green Serial.println("green"); } else if (value == "blue") { setColor(0, 0, 255); // blue Serial.println("blue"); } else if (value == "yellow") { setColor(255, 150, 0); // yellow Serial.println("yellow"); } else if (value == "purple") { setColor(80, 0, 80); // purple Serial.println("purple"); } } };
Finally, a function is added to set the RGB LED color.
void setColor(int red, int green, int blue) { // For common-anode RGB LEDs, use 255 minus the color value ledcWrite(redChannel, red); ledcWrite(greenChannel, green); ledcWrite(blueChannel, blue); }
In summary, this script enables a remote control interaction model, where the ESP32 operates as a Bluetooth Low Energy (BLE) server.
The connected BLE client (like a smartphone) can send string commands to change the color of an RGB LED. The ESP32 also gives feedback to the client by sending back the string received, allowing the client to know what operation was performed.