Skip to Content

Arduino

This sample code shows how to use the Nordic UART Service in an Arduino project with the Adafruit Bluefruit nRF52 library.

// Adafruit Bluefruit nRF52 (e.g. Feather nRF52832 / nRF52840) #include <bluefruit.h> // BLEUart implements Nordic UART Service (NUS): // Service 6E400001-..., RX 6E400002-... (central->peripheral), TX 6E400003-... (peripheral->central) BLEUart bleuart; void setup() { Serial.begin(115200); Bluefruit.begin(); Bluefruit.setName("Arduino-NUS"); Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); bleuart.begin(); // Advertise NUS so scanners can find it Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); Bluefruit.Advertising.addTxPower(); Bluefruit.Advertising.addService(bleuart); Bluefruit.ScanResponse.addName(); Bluefruit.Advertising.restartOnDisconnect(true); Bluefruit.Advertising.setInterval(32, 244); Bluefruit.Advertising.start(0); } void loop() { // Forward data from central (NUS RX) to Serial — like onNusRx while (bleuart.available()) { uint8_t buf[64]; int count = bleuart.read(buf, sizeof(buf)); if (count > 0) { Serial.print("NUS RX ("); Serial.print(count); Serial.print(" bytes): "); Serial.write(buf, count); Serial.println(); } } // Every 1s when connected, send a line on NUS TX (notify) static unsigned long last = 0; if (millis() - last >= 1000) { last = millis(); if (Bluefruit.connected()) { bleuart.print("hello from Arduino over NUS\n"); } } }
Last updated on