[i=s] 本帖最后由 无垠的广袤 于 2025-3-27 11:22 编辑 [/i]
【BW21-CBV-Kit 开发套件测评】蓝牙小彩灯
本文介绍了安信可 BW21-CBV-Kit 开发套件实现蓝牙小彩灯的项目设计。
介绍
结合示例程序 PWM_over_BLEUart
,实现手机应用蓝牙串口传输,进而控制开发板引脚 PWM 输出,以更改 RGB 三色 LED 的亮度实现无极调光。
硬件连接
如图所示,将 RGB-LED 模块的红、绿、蓝引脚分别连接至开发板的 Arduino 1、2、3 引脚,
这里使用共阴极 LED,公共引脚连接至 GND;若采用共阳极则接至 3.3V 即可。


三色 LED 模块原理图


RGB-LED 模块详见:https://oshwhub.com/jinleili/rgb-led-module
代码解析
#include "BLEDevice.h"
// Choose the RGB LED type
// #define AnodeRGB
#define CathodeRGB
// Choose 3 PWM pins for RGB LED pins refer to the pinmap of using board
#define LED_RED 1
#define LED_GRN 2
#define LED_BLU 3
#define UART_SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
#define STRING_BUF_SIZE 100
BLEService UartService(UART_SERVICE_UUID);
BLECharacteristic Rx(CHARACTERISTIC_UUID_RX);
BLECharacteristic Tx(CHARACTERISTIC_UUID_TX);
BLEAdvertData advdata;
BLEAdvertData scndata;
bool notify = false;
void readCB(BLECharacteristic* chr, uint8_t connID)
{
printf("Characteristic %s read by connection %d \n", chr->getUUID().str(), connID);
}
void writeCB(BLECharacteristic* chr, uint8_t connID)
{
printf("Characteristic %s write by connection %d :\n", chr->getUUID().str(), connID);
uint16_t datalen = chr->getDataLen();
if (datalen > 0) {
if (chr->readData8() == '!') {
uint8_t command[datalen];
chr->getData(command, datalen);
if (command[1] == 'C') {
// print hex
printf("Color command R = %x G = %x B = %x \n", command[2], command[3], command[4]);
// print decimal
// printf("Color command R = %d G = %d B = %d \n", command[2], command[3], command[4]);
#if defined(CathodeRGB)
analogWrite(LED_RED, command[2]);
analogWrite(LED_GRN, command[3]);
analogWrite(LED_BLU, command[4]);
#elif defined(AnodeRGB)
analogWrite(LED_RED, (255 - command[2]));
analogWrite(LED_GRN, (255 - command[3]));
analogWrite(LED_BLU, (255 - command[4]));
#else
printf("Error, please choose the RGB LED type \n");
#endif
}
} else {
Serial.print("Received string: ");
Serial.print(chr->readString());
Serial.println();
}
}
}
void notifCB(BLECharacteristic* chr, uint8_t connID, uint16_t cccd)
{
if (cccd & GATT_CLIENT_CHAR_CONFIG_NOTIFY) {
printf("Notifications enabled on Characteristic %s for connection %d \n", chr->getUUID().str(), connID);
notify = true;
} else {
printf("Notifications disabled on Characteristic %s for connection %d \n", chr->getUUID().str(), connID);
notify = false;
}
}
void setup()
{
Serial.begin(115200);
pinMode(LED_RED, OUTPUT);
pinMode(LED_GRN, OUTPUT);
pinMode(LED_BLU, OUTPUT);
analogWrite(LED_RED, 255);
analogWrite(LED_GRN, 255);
analogWrite(LED_BLU, 255);
advdata.addFlags(GAP_ADTYPE_FLAGS_LIMITED | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED);
advdata.addCompleteName("AMEBA_BLE_DEV");
scndata.addCompleteServices(BLEUUID(UART_SERVICE_UUID));
Rx.setWriteProperty(true);
Rx.setWritePermissions(GATT_PERM_WRITE);
Rx.setWriteCallback(writeCB);
Rx.setBufferLen(STRING_BUF_SIZE);
Tx.setReadProperty(true);
Tx.setReadPermissions(GATT_PERM_READ);
Tx.setReadCallback(readCB);
Tx.setNotifyProperty(true);
Tx.setCCCDCallback(notifCB);
Tx.setBufferLen(STRING_BUF_SIZE);
UartService.addCharacteristic(Rx);
UartService.addCharacteristic(Tx);
BLE.init();
BLE.configAdvert()->setAdvData(advdata);
BLE.configAdvert()->setScanRspData(scndata);
BLE.configServer(1);
BLE.addService(UartService);
BLE.beginPeripheral();
}
void loop()
{
if (Serial.available()) {
Tx.writeString(Serial.readString());
if (BLE.connected(0) && notify) {
Tx.notify(0);
}
}
delay(100);
}
详见瑞昱官方链接: https://www.amebaiot.com/en/amebapro2-arduino-ble-pwm/
编译上传
- 打开示例程序
PWM_over_BLEUart
;


- 按住 BOOT(下载) 按钮的同时按一下 EN(复位)按钮,进入下载模式;
- 选择目标串口对应的端口号,点击 Download 按钮,待上传成功提示 Success,完成固件上传;
- 再次短按 EN 键复位,执行程序。
测试流程
手机安装并打开 Adafruit Bluefruit LE Connect 应用;


打开手机蓝牙开关,扫描周围蓝牙设备,连接目标 AMEBA_BLE_DEV
;


依次选择 Controller
- Color Picker
- 点击色环及 SELECT
显示目标颜色。
效果展示


总结
本文介绍了安信可 BW21-CBV-Kit 开发套件实现蓝牙小彩灯的项目设计,包括项目介绍、代码分析、操作流程、效果展示等,为无线控制等物联网应用的开发设计提供了参考。