The project I am looking to make is to have 1 Arduino Nano 33 rev2 BLE labeled Remote (Central) scan and connect to another Arduino Nano 33 BLE rev2 labeled Base (Peripheral). Once connected, a button on Remote can trigger an event on the Base. In my coding, I was successful in having Remote scan for Base, but the issue I keep having is I can't get the 2 boards to connect/pair, or for the few times I was able to get them to connect/pair, the connection is lost after 3 seconds and they will disconnect. How can I make the connection stick? Is there maybe another library I should be using with ArduinoBLE? If it is not clear as to the LED on pin 12, its to have a visual that a connection has been made. Thank you for your time.
Remote Code:
#include <ArduinoBLE.h>
bool deviceFound = false; // Flag to track if the device is found
void setup() {
Serial.begin(9600); // Start serial monitor
while (!Serial); // Wait for serial to connect
Serial.println("Serial Monitor is working");
// Set pin 12 as an output for the LED
pinMode(12, OUTPUT);
digitalWrite(12, LOW); // Ensure the LED is initially off
// Start the Bluetooth Low Energy (BLE) device
if (!BLE.begin()) {
Serial.println("Starting Bluetooth® Low Energy module failed!");
while (1);
}
Serial.println("Starting Bluetooth® Low Energy module is online!");
Serial.println("Searching for Base...");
}
void loop() {
// Start scanning for BLE devices with the name "Base"
BLE.scanForName("Base");
// Check if a device is found
BLEDevice foundDevice = BLE.available(); // Retrieve the first available BLE device
if (foundDevice && !deviceFound) {
// Check if the found device's name is "Base"
if (foundDevice.localName() == "Base") {
Serial.print("Found: ");
Serial.println(foundDevice.localName());
// Set the flag to true so it doesn't print again
deviceFound = true;
// Attempt to connect to the found device (which will handle pairing automatically)
Serial.println("Attempting to connect to Base...");
bool connected = foundDevice.connect(); // Attempt the connection
if (connected) {
Serial.println("Connected to Base!");
digitalWrite(12, HIGH); // LED on
} else {
Serial.println("Failed to connect to Base. Retrying...");
delay(2000); // Wait before retrying connection
}
}
}
// If device is not found, make sure LED is off
if (!deviceFound) {
digitalWrite(12, LOW); // LED off
}
// Add a short delay to avoid flooding the serial monitor
delay(1000);
}
Remote Serial Monitor Printout:
Serial Monitor is working
Starting Bluetooth® Low Energy module is online!
Searching for Base...
Found: Base
Attempting to connect to Base...
Failed to connect to Base. Retrying...
Base Code:
#include <ArduinoBLE.h>
void setup() {
Serial.begin(9600); // Start serial monitor
while (!Serial); // Wait for serial to connect
Serial.println("Serial Monitor is working");
// Initialize BLE
if (!BLE.begin()) {
Serial.println("Starting Bluetooth® Low Energy module failed!");
while (1); // Stay here if BLE initialization fails
}
Serial.println("Starting Bluetooth® Low Energy module is online!");
// Set the local Bluetooth name to "Base"
const char* localName = "Base"; // Local name as a C-string
BLE.setLocalName(localName); // Set the Bluetooth localName
// Create a service
BLEService myService("180D"); // UUID needed for iPhone to see advertising name for verification that advertising is working
BLE.setAdvertisedService(myService); // Advertise the service
// Print localName to Serial Monitor, for debugging to verify correct localName is being advertised
Serial.print("Advertising Local Name: ");
Serial.println(localName); // Print the advertising name (local name)
// Start BLE advertising
BLE.advertise(); // Start advertising over Bluetooth
Serial.println("Advertising... Waiting for Remote to connect");
}
void loop() {
// Keep the BLE stack running
BLE.poll();
}
Base Serial Monitor Printout:
Serial Monitor is working
Starting Bluetooth® Low Energy module is online!
Advertising Local Name: Base
Advertising... Waiting for Remote to connect