0% found this document useful (0 votes)
24 views4 pages

Bluetooth RGB LED Control with ESP32

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Bluetooth RGB LED Control with ESP32

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Utilize the ThingZkit Mini, an Arduino-based wireless

[Link].9
development module, to create a Bluetooth-enabled toggle
switch for a multicolor LED (RGB), ensuring that the LED’s
DATE
on/off status is transmitted and monitored effectively.

AIM: To program the WDM to create a Bluetooth-enabled toggle switch for a multicolored
LED, ensuring that the LED’s on/off status is transmitted and monitored effectively.

Coding:
#include "BluetoothSerial.h"
#include <BluetoothSerial.h>

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)


#error Bluetooth is not enabled! Please run make menuconfig to and
enable it
#endif

BluetoothSerial SerialBT;

char receivedChar;
const char turnON_LED1 = 'a';
const char turnOFF_LED1 = 'b';
const char turnON_LED2 = 'c';
const char turnOFF_LED2 = 'd';
const char turnON_LED3 = 'e';
const char turnOFF_LED3 = 'f';

const int LEDpin1 = 2; // First LED

const int LEDpin2 = 4; // Second LED

const int LEDpin3 = 5; // Third LED

void setup() {
[Link](9600);
[Link]("ESP32_dev1"); // Set your device's Bluetooth name
[Link]("The device started, now you can pair it with
Bluetooth!");
[Link]("To control the LEDs, send the following:");
[Link]("LED 1 ON: 'a', LED 1 OFF: 'b'");
[Link]("LED 2 ON: 'c', LED 2 OFF: 'd'");
[Link]("LED 3 ON: 'e', LED 3 OFF: 'f'");

// Set pin modes for LEDs


pinMode(LEDpin1, OUTPUT);
pinMode(LEDpin2, OUTPUT);
pinMode(LEDpin3, OUTPUT);
}

void loop() {
receivedChar = (char)[Link]();

if ([Link]()) {
[Link]([Link]());
}

if ([Link]()) {
[Link]("Received: ");
[Link](receivedChar); // Write on BT app
[Link]("Received: ");
[Link](receivedChar); // Print on serial monitor

// Control LED 1
if (receivedChar == turnON_LED1) {
[Link]("LED 1 ON");
[Link]("LED 1 ON");
digitalWrite(LEDpin1, HIGH);// Turn LED 1 ON
}
if (receivedChar == turnOFF_LED1) {
[Link]("LED 1 OFF");
[Link]("LED 1 OFF");
digitalWrite(LEDpin1, LOW); // Turn LED 1 OFF
}
// Control LED 2
if (receivedChar == turnON_LED2) {
[Link]("LED 2 ON");
[Link]("LED 2 ON");
digitalWrite(LEDpin2, HIGH); // Turn LED 2 ON
}
if (receivedChar == turnOFF_LED2) {
[Link]("LED 2 OFF");
[Link]("LED 2 OFF");
digitalWrite(LEDpin2, LOW); // Turn LED 2 OFF
}
// Control LED 3
if (receivedChar == turnON_LED3) {
[Link]("LED 3 ON");
[Link]("LED 3 ON");
digitalWrite(LEDpin3, HIGH); // Turn LED 3 ON
}
if (receivedChar == turnOFF_LED3) {
[Link]("LED 3 OFF");
[Link]("LED 3 OFF");
digitalWrite(LEDpin3, LOW); // Turn LED 3 OFF
}
}
delay(20); // Short delay to avoid overloading the loop
}
Theory:
This code is for controlling an RGB LED using an ESP32 microcontroller based thingZkit
Mini WDM module via Bluetooth communication. The RGB LED cycles through different
colors when a specific command is received through Bluetooth. Here is a detailed breakdown:
• #include "BluetoothSerial.h": This includes the library needed for Bluetooth
communication on the ESP32.
• Preprocessor check (#if !defined(...)): Ensures that Bluetooth is enabled on the
ESP32. If not, an error is thrown.
• pinMode(): Configures the RGB pins as outputs.
• [Link](9600): Initializes serial communication for debugging at 9600 baud rate.
• [Link]("ESP32_dev1"): Initializes Bluetooth communication with the name
"ESP32_dev1".
• The [Link]() calls provide debugging information to the Serial Monitor.
• BluetoothSerial SerialBT: Object used for Bluetooth communication.
• receivedChar: Stores the last character received via Bluetooth.
• const char ...: Defines specific characters that will be used to control the LEDs.
• const int LEDpin1, LEDpin2, LEDpin3: Pins assigned for controlling the three LEDs.

Explanation:

• Reading Data: The receivedChar variable captures the character received from the
Bluetooth connection using [Link]().
• Echoing Data: If data is available on the serial monitor, it is written back to the
Bluetooth for feedback using [Link]().
• Controlling LEDs:
o When a specific character (e.g., 'a', 'c', 'e') is received, the corresponding
LED is turned on.
o When the character for turning off (e.g., 'b', 'd', 'f') is received, the
respective LED is turned off.
o Each control action is logged both to the Bluetooth device and the serial
monitor for debugging.

Output:

• Character 'a' turns LED 1 on, and 'b' turns it off.


• Character 'c' turns LED 2 on, and 'd' turns it off.
• Character 'e' turns LED 3 on, and 'f' turns it off.
• Each command's result is printed on both the serial monitor and Bluetooth app for
confirmation.

Delay:

• delay(20): A brief delay to prevent the loop from running too fast, ensuring stable
performance.

You might also like