Hello,
I am trying to set up a receiver and a transmitter using the Nano 33 BLE Rev 2. The transmitter does not appear to be working correctly. I was able to use a different code to verify that the receiver is scanning and seeing other devices but it will not connect to the transmitter. Below is the code for the transmitter and receiver respectively. Can you offer any advice?
TRANSMITTER
#include <ArduinoBLE.h>
const int buttonPin = 2; // Digital input pin
BLEService buttonService("180F");
BLEByteCharacteristic buttonChar("2A19", BLERead | BLENotify);
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
while (!Serial);
if (!BLE.begin()) {
Serial.println("Starting BLE failed!");
while (1);
}
BLE.setLocalName("ButtonPeripheral");
BLE.setAdvertisedService(buttonService);
buttonService.addCharacteristic(buttonChar);
BLE.addService(buttonService);
buttonChar.writeValue(0);
BLE.advertise();
Serial.println("BLE Button Peripheral device active");
}
void loop() {
BLEDevice central = BLE.central();
if (central) {
Serial.print("Connected to central: ");
Serial.println(central.address());
while (central.connected()) {
byte buttonState = digitalRead(buttonPin) == LOW ? 1 : 0;
static byte lastState = 255;
if (buttonState != lastState) {
buttonChar.writeValue(buttonState);
Serial.print("Button state changed: ");
Serial.println(buttonState);
lastState = buttonState;
}
delay(100);
}
Serial.println("Disconnected");
}
}
RECEIVER
#include <ArduinoBLE.h>
const int ledPin = 3; // Output controlled by transmitter button
const int signalLostPin = 4; // Output goes HIGH if signal is lost
BLEDevice peripheral;
BLECharacteristic buttonChar;
unsigned long lastReceivedTime = 0;
const unsigned long timeoutDuration = 3000; // 3 seconds without data = "signal lost"
bool connected = false;
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode(signalLostPin, OUTPUT);
digitalWrite(signalLostPin, HIGH); // Assume no signal at startup
Serial.begin(9600);
while (!Serial);
if (!BLE.begin()) {
Serial.println("Starting BLE failed!");
while (1);
}
BLE.scanForUuid("180F"); // Scan for the transmitter's BLE service
}
void loop() {
if (!connected) {
peripheral = BLE.available();
if (peripheral) {
Serial.print("Found ");
Serial.println(peripheral.address());
if (peripheral.connect()) {
Serial.println("Connected to peripheral");
if (peripheral.discoverAttributes()) {
buttonChar = peripheral.characteristic("2A19");
if (!buttonChar) {
Serial.println("Characteristic not found!");
peripheral.disconnect();
return;
}
lastReceivedTime = millis();
digitalWrite(signalLostPin, LOW); // Signal OK
connected = true;
} else {
Serial.println("Attribute discovery failed");
peripheral.disconnect();
}
}
}
}
if (connected && peripheral.connected()) {
if (buttonChar.valueUpdated()) {
byte state;
buttonChar.readValue(state);
Serial.print("Button state: ");
Serial.println(state ? "PRESSED" : "RELEASED");
digitalWrite(ledPin, state ? HIGH : LOW);
lastReceivedTime = millis(); // Reset timeout
digitalWrite(signalLostPin, LOW); // Signal OK
}
// Timeout-based signal loss
if (millis() - lastReceivedTime > timeoutDuration) {
digitalWrite(signalLostPin, HIGH); // No data = signal lost
}
}
// Disconnection-based signal loss
if (connected && !peripheral.connected()) {
Serial.println("Peripheral disconnected");
digitalWrite(signalLostPin, HIGH); // Signal lost
digitalWrite(ledPin, LOW); // Reset controlled output
connected = false;
BLE.scanForUuid("180F"); // Restart scanning
}
BLE.poll();
}