Here is a code which enables transmission and reception of data from Arduino Nano 33 BLE board over Bluetooth. nRF Connect App is used to connect to the Arduino Nano 33 BLE board.
We can see the TX Characteristic and RX Characteristic under the Nordic UART service.
#include <ArduinoBLE.h>
// Nordic UART Service (NUS)
BLEService uartService("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");
BLECharacteristic txChar( // Notify characteristic (Arduino → Phone)
"6E400003-B5A3-F393-E0A9-E50E24DCCA9E",
BLENotify,
512
);
BLECharacteristic rxChar( // Write characteristic (Phone → Arduino)
"6E400002-B5A3-F393-E0A9-E50E24DCCA9E",
BLEWrite | BLEWriteWithoutResponse,
512
);
void setup() {
Serial.begin(115200);
while (!Serial);
// Start BLE
if (!BLE.begin()) {
Serial.println("BLE start failed!");
while (1);
}
BLE.setLocalName("Nano33BLE-UART");
BLE.setAdvertisedService(uartService);
uartService.addCharacteristic(txChar);
uartService.addCharacteristic(rxChar);
BLE.addService(uartService);
txChar.writeValue((byte)0x00);
BLE.advertise();
Serial.println("BLE UART Started. Waiting for central...");
}
void loop() {
BLEDevice central = BLE.central();
if (central) {
Serial.print("Connected to: ");
Serial.println(central.address());
while (central.connected()) {
// ➤ 1. Read USB Serial and send over BLE
if (Serial.available()) {
String incoming = Serial.readStringUntil('\n');
txChar.writeValue(incoming.c_str());
Serial.print("Sent over BLE: ");
Serial.println(incoming);
}
// ➤ 2. Read BLE (phone → Arduino)
if (rxChar.written()) {
int len = rxChar.valueLength();
const uint8_t* data = rxChar.value();
String fromBLE = "";
for (int i = 0; i < len; i++) {
fromBLE += (char)data[i];
}
Serial.print("Received from BLE: ");
Serial.println(fromBLE);
}
}
Serial.println("Central disconnected!");
}
}
0 Comments