By ran | 19 December 2024 | 0 Comments
HOW to Use Bluetooth Control LED with ESP32
Components
Wiring Diagram
Test Code
//**********************************************************************************
/*
* Filename : Bluetooth Control LED
* Description : The phone controls esp32's led via bluetooth.
When the phone sends "LED_on," ESP32's LED lights turn on.
When the phone sends "LED_off," ESP32's LED lights turn off.
* Auther : http//www.keyestudio.com
*/
#include "BluetoothSerial.h"
#include "string.h"
#define LED 15
BluetoothSerial SerialBT;
char buffer[20];
static int count = 0;
void setup() {
pinMode(LED, OUTPUT);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.begin(115200);
Serial.println("\nThe device started, now you can pair it with bluetooth!");
}
void loop() {
while(SerialBT.available())
{
buffer[count] = SerialBT.read();
count++;
}
if(count>0){
Serial.print(buffer);
if(strncmp(buffer,"led_on",6)==0){
digitalWrite(LED,HIGH);
}
if(strncmp(buffer,"led_off",7)==0){
digitalWrite(LED,LOW);
}
count=0;
memset(buffer,0,20);
}
}
//**********************************************************************************
Test Result
Connect the wires according to the experimental wiring diagram, compile and upload the code to the ESP32. After uploading successfully,we will use a USB cable to power on. The APP operation is the same as the project 60.1. To make the external LED on and off, simply change the sending content to “led_on” and “led_off”. Moving the APP to send data:
The serial monitor will display as follows:
LED Circumstance
Note:
If the sent content is not “led-on ‘or” led-off “, the status of the LED will not change. If the LED is on, it remains on when irrelevant content is received; if the LED is off, it continues to be off when irrelevant content is received.
Leave a Reply
Your email address will not be published.Required fields are marked. *
CATEGORIES