Hamid Sheibani
Published

Key Finder with the Xiao NRF52840 Module

Losing keys can be a frustrating experience that disrupts our daily routines. It's over!

IntermediateFull instructions provided2 hours8,009
Key Finder with the Xiao NRF52840 Module

Things used in this project

Hardware components

Seeed XIAO BLE nRF52840 Sense
Seeed Studio Seeed XIAO BLE nRF52840 Sense
×1
Buzzer
×1
LED
×1

Software apps and online services

nRF Connect SDK
Nordic Semiconductor nRF Connect SDK
Seeed Fusion

Story

Read more

Schematics

KeyFinder

Code

KeyFinder.ino

C/C++
#include <ArduinoBLE.h>

BLEService KeyFinderService("19B10000-E8F2-537E-4F6C-D104768A1214");  // Bluetooth Low Energy KeyFinder Service

// Bluetooth Low Energy Key Finder Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

const int ledPin = LED_BUILTIN;  // pin to use for the LED

void setup() {
  Serial.begin(115200);

  // set LED pin to output mode
  pinMode(ledPin, OUTPUT);
  pinMode(D6, OUTPUT);
  pinMode(D5, OUTPUT);
  pinMode(D7, INPUT);

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting Bluetooth Low Energy module failed!");

    while (1);
  }

  // set advertised local name and service UUID:
  BLE.setLocalName("KeyFinder");
  BLE.setAdvertisedService(KeyFinderService);

  // add the characteristic to the service
  KeyFinderService.addCharacteristic(switchCharacteristic);

  // add service
  BLE.addService(KeyFinderService);

  // set the initial value for the characeristic:
  switchCharacteristic.writeValue(0);

  // start advertising
  BLE.advertise();

  Serial.println("BLE Key Finder Peripheral");
}

void loop() {
  // listen for Bluetooth Low Energy peripherals to connect:
  BLEDevice central = BLE.central();
  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
      if (switchCharacteristic.written()) {
        if (switchCharacteristic.value()) {
          Serial.println("LED on");
          digitalWrite(ledPin, LOW);  // changed from HIGH to LOW
          digitalWrite(D5, HIGH);
          while(1) {
            tone(D6, 500, 500);
            delay(1000);
            if(digitalRead(D7) == 0) {
              Serial.println(F("LED off"));
              digitalWrite(ledPin, HIGH);  // changed from LOW to HIGH
              digitalWrite(D5, LOW);
              digitalWrite(D6, LOW);
              break;
            }
          }
        }
      }
    }
    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

Credits

Hamid Sheibani

Hamid Sheibani

2 projects • 3 followers
Experienced Embedded System Developer with a demonstrated history of working in the research industry.

Comments