Control RGB LEDs with Arduino: In this blog post, we’ll guide you through controlling an RGB LED with your Arduino using the Bluetooth module (HC-05) and the BlueBot Controller app. We’ll break down the code, explain how it receives RGB values, and show how the Arduino adjusts the LED’s color based on those values.
Overview of the Setup
In this project, the BlueBot Controller app sends RGB values to the Arduino via Bluetooth (HC-05 module). The Arduino receives these values and uses Pulse Width Modulation (PWM) to control the brightness of the Red, Green, and Blue LEDs, thus changing the color of the RGB LED.
The components involved are:
- Bluetooth Module (HC-05): This module enables Bluetooth communication between the BlueBot app and the Arduino.
- RGB LED: The LED consists of three color channels (Red, Green, Blue) connected to PWM-capable pins on the Arduino.
- Arduino Code: The Arduino code listens for incoming commands, extracts RGB values, and adjusts the LED’s brightness.
Arduino Code Breakdown
Let’s walk through the code and understand how it works.
1. Setting Up Bluetooth Communication
SoftwareSerial BTSerial(2, 3); // RX, TX pins for the Bluetooth module (HC-05)
We set up Bluetooth communication using the SoftwareSerial
library. The Bluetooth module connects to the Arduino through pins 2 (RX) and 3 (TX). The code starts communication at a baud rate of 9600 using BTSerial.begin(9600)
.
2. Pin Setup for RGB LED
int redPin = 9; // Pin for the Red LED (PWM)
int greenPin = 10; // Pin for the Green LED (PWM)
int bluePin = 11; // Pin for the Blue LED (PWM)
We connect the RGB LED to PWM-capable pins 9, 10, and 11 on the Arduino. These pins allow us to control the brightness of the red, green, and blue channels of the LED.
3. Main Loop: Receive and Process Bluetooth Commands
if (BTSerial.available()) {
// Read the incoming Bluetooth data
String command = BTSerial.readStringUntil('\n'); // Read until newline character
Serial.println("Command received: " + command); // Display received command
In the loop()
function, we check if there is any data available from the Bluetooth module using BTSerial.available()
. If data is available, we read it as a string using BTSerial.readStringUntil('\n')
. This command is then printed to the Serial Monitor for debugging purposes.
4. Extract RGB Values from Command
int red = 0, green = 0, blue = 0;
if (command.startsWith("R")) {
red = command.substring(1, command.indexOf(' ', 1)).toInt(); // Extract red value
}
if (command.indexOf('G') != -1) {
green = command.substring(command.indexOf('G') + 1, command.indexOf(' ', command.indexOf('G') + 1)).toInt(); // Extract green value
}
if (command.indexOf('B') != -1) {
blue = command.substring(command.indexOf('B') + 1).toInt(); // Extract blue value
}
After receiving the command, the code extracts individual RGB values. We use substring()
to extract the values after the characters “R”, “G”, and “B”. For example, if the command is R255 G128 B64
, it extracts red as 255, green as 128, and blue as 64.
5. Control the RGB LED
// Map the values to PWM range (0-255)
red = constrain(red, 0, 255);
green = constrain(green, 0, 255);
blue = constrain(blue, 0, 255);
// Control RGB LED brightness using PWM
analogWrite(redPin, red); // Set red LED brightness
analogWrite(greenPin, green); // Set green LED brightness
analogWrite(bluePin, blue); // Set blue LED brightness
The RGB values are constrained to the range of 0-255 using the constrain()
function. This ensures that no value is out of the valid PWM range. Then, the analogWrite()
function is used to adjust the brightness of each LED color by sending the corresponding PWM signal to the respective pins.
6. Send Confirmation Back to the BlueBot App
BTSerial.println("RGB values received and applied: R$red G$green B$blue");
Once the RGB values are applied to the LED, the Arduino sends a confirmation back to the BlueBot app, indicating the colors have been updated successfully.
How the BlueBot Controller App Sends RGB Values
The BlueBot Controller app allows users to adjust the RGB values using sliders or controls. The app sends these values to the Arduino as commands in the format R255 G128 B64
, where the numbers represent the intensity of each color.
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2, 3); // RX, TX pins for the Bluetooth module (HC-05)
int redPin = 9; // Pin for the Red LED (PWM)
int greenPin = 10; // Pin for the Green LED (PWM)
int bluePin = 11; // Pin for the Blue LED (PWM)
void setup() {
// Start serial communication with Bluetooth and monitor
BTSerial.begin(9600); // HC-05 communication at 9600 baud rate
Serial.begin(9600); // Serial monitor at 9600 baud rate
// Set RGB LED pins as output (PWM capable pins)
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.println("Ready to receive commands for RGB control.");
}
void loop() {
if (BTSerial.available()) {
// Read the incoming Bluetooth data
String command = BTSerial.readStringUntil('\n'); // Read until newline character
Serial.println("Command received: " + command); // Display received command
// Parse the command to extract RGB values
int red = 0, green = 0, blue = 0;
if (command.startsWith("R")) {
red = command.substring(1, command.indexOf(' ', 1)).toInt(); // Extract red value
}
if (command.indexOf('G') != -1) {
green = command.substring(command.indexOf('G') + 1, command.indexOf(' ', command.indexOf('G') + 1)).toInt(); // Extract green value
}
if (command.indexOf('B') != -1) {
blue = command.substring(command.indexOf('B') + 1).toInt(); // Extract blue value
}
// Map the values to PWM range (0-255)
red = constrain(red, 0, 255);
green = constrain(green, 0, 255);
blue = constrain(blue, 0, 255);
// Control RGB LED brightness using PWM
analogWrite(redPin, red); // Set red LED brightness
analogWrite(greenPin, green); // Set green LED brightness
analogWrite(bluePin, blue); // Set blue LED brightness
// Send a confirmation back to the Flutter app
BTSerial.println("RGB values received and applied: R$red G$green B$blue");
}
}
Conclusion
Control RGB LEDs with Arduino: This project shows how to control an RGB LED from a mobile device via Bluetooth. The Arduino listens for commands from the BlueBot app, extracts the RGB values, and adjusts the LED’s color using PWM. By experimenting with different RGB values, you can create a variety of colors and effects, making this setup a great introduction to Bluetooth-controlled projects.