nRF52832 Breakout Board Hookup Guide
BLE Button Example
This example demonstrates how to use the BLE read and notify features. It monitors the button on pin 6 of the nRF52832 Breakout. When the button state changes a BLE notification is sent.
The Code
Using the BLEPeripheral library, upload this code to your Breakout:
// Import libraries (BLEPeripheral depends on SPI) ////////////// // Hardware // ////////////// /////////////////////// // BLE Advertisments // /////////////////////// const char * localName = "nRF52832 Button"; BLEPeripheral blePeriph; BLEService bleServ("1234"); BLECharCharacteristic btnChar("1234", BLERead | BLENotify); void setup() { Serial.begin(115200); // Set up serial at 115200 baud pinMode(BTN_PIN, INPUT_PULLUP); digitalWrite(7, HIGH); setupBLE(); } void loop() { blePeriph.poll(); // read the current button pin state char buttonValue = digitalRead(BTN_PIN); // has the value changed since the last read bool buttonChanged = (btnChar.value() != buttonValue); if (buttonChanged) { // button state changed, update characteristics btnChar.setValue(buttonValue); } } void setupBLE() { // Advertise name and service: blePeriph.setDeviceName(localName); blePeriph.setLocalName(localName); blePeriph.setAdvertisedServiceUuid(bleServ.uuid()); // Add service blePeriph.addAttribute(bleServ); // Add characteristic blePeriph.addAttribute(btnChar); // Now that device, service, characteristic are set up, // initialize BLE: blePeriph.begin(); }
Use the nRF Connect to Test
Use nRF Connect to connect to your nRF52832 Breakout -- just like last time. This time the name of the device should change to nRF52832 Button (if it's still "LED", try connecting anyway -- sometimes the local ID doesn't change until you've connected to it).
Tap into the "Unknown Service" again, but, this time, try tapping the single-down arrow to read the service's characteristic. This will read the state of the nRF52832 Breakout's pin 6 button. While the button is un-actuated, the value of the property should be 0x01. If you can hold the button down while also tapping the single-down arrow, the value should change to 0x00.
You can also try setting the characteristic to notify, by tapping the triple-down arrow. In this mode, the value should automatically be notified when there's a change in state. Press and release the button to see the value change from 0x00 to 0x01.