Nano 33 BLE Rev 2 Bluetooth Transmitter

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();
}

So you have two Nano 33 BLE Rev 2, one to be a transmitter and the other a receiver. Is that correct?
Are your sketches samples from the hardware library or some other samples from the Files menu?

Yes I have 2 nano 33 BLE rev 2, I want one to send the status of a digital input to the other but I cant even get them to connect. I didnt see a good example of 2 arduinos talking to each other. This code was weitten by chat gpt but I looked through it and i dont see why it wouldnt work unless there is something about the actual arduino that wont let it work the way I want it to.

Can you find and connect to the peripheral devoce (which you are calling the transmitter) with a phone using LightBlue or nrfConnect. If all is good then focus on the central device code. If not,then run the library example for the peripheral aka server.

Very poor choice. Start with the provided library examples for central and/or peripheral device, as mentioned above.

Ok I will try looking into those examples. I dont have a ton of experience writing code and I currently dont have a ton of time so I was hoping Chat GPT was going to provide me with a quick solution but I guess I will have to put more work into it. Thanks for the responses thus far

So I tried loading the example code at this link:

https://docs.arduino.cc/tutorials/nano-33-ble-sense/ble-device-to-device/#testing-the-code

Its meant to connect the Nano 33 BLE to the Nano 33 BLE Sense and using two Nano 33 BLEs not one being a Sense. I believe the connection process should still work and there will be errors because there is a sensor on the Sense board that the Nano 33 BLE doesnt have.

I tried the code exactly as it is provided in the link and I cant get them to connect and I have tried removing anything to do with the sensor that I don't have and they still don't connect.

I also tried using nrfConect and I don't see the peripheral.

I have not looked at the central, but there is an issue with the peripheral and the use of a long local name.

BLE.setLocalName("Arduino Nano 33 BLE (Peripheral)");

I'm not clear if there is an advertising packet size issue or a library limitation but if you test for the status of .setLocalName()

bool BLEAdvertisingData::setLocalName(const char *localName)
{
  int previousLength = (_localName && strlen(_localName) > 0) ? (strlen(_localName) + AD_FIELD_OVERHEAD) : 0;
  bool success = updateRemainingLength(previousLength, (strlen(localName) + AD_FIELD_OVERHEAD));
  if (success) {
    _localName = localName;
  }
  return success;
}

Add the test to the setup of your code and you can see this for a long and a short local name.

Status with Long Name: 0
Status with Short Name: 1
Nano 33 BLE (Peripheral Device)
void setup() {
  Serial.begin(9600);
  while (!Serial);

  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);

  digitalWrite(LEDR, HIGH);
  digitalWrite(LEDG, HIGH);
  digitalWrite(LEDB, HIGH);
  digitalWrite(LED_BUILTIN, LOW);


  if (!BLE.begin()) {
    Serial.println("- Starting Bluetooth® Low Energy module failed!");
    while (1);
  }

  byte status = BLE.setLocalName("Arduino Nano 33 BLE (Peripheral)");
  Serial.print("Status with Long Name: ");
  Serial.println(status);

  status = BLE.setLocalName("BLE (Peripheral)");
  Serial.print("Status with Short Name: ");
  Serial.println(status);

  BLE.setAdvertisedService(gestureService);
  gestureService.addCharacteristic(gestureCharacteristic);
  BLE.addService(gestureService);
  gestureCharacteristic.writeValue(-1);
  BLE.advertise();

  Serial.println("Nano 33 BLE (Peripheral Device)");
  Serial.println(" ");
}

nrfConnect can find and connect when using the short name.