Connecting 2 nano 33 ble using one's accelerometer, magnetometer and gyroscope

hello guys. i have two arduino nano 33 ble and i want one of them to take the data from its sensors (peripheral) and the other one (central) to take the data from the first arduino via bluetooth. i have found the codes below for central and peripheral using the accelerometer data in the three axises. but i need gyroscope's and magnetometer's data too in three axises as well as accelerometer. how can i rearrange my code to add these two sensors also?

central code:

#include <ArduinoBLE.h>
// ------------------------------------------ BLE UUIDs ------------------------------------------
#define BLE_UUID_PERIPHERAL               "19B10000-E8F2-537E-4F6C-D104768A1214"  //please change to a unique value that matches BLE_IMU_PERIPHERAL
#define BLE_UUID_CHARACT_LED              "19B10001-E8F2-537E-4F6C-E104768A1214"  //please change to a unique value that matches BLE_IMU_PERIPHERAL
#define BLE_UUID_CHARACT_ACCX             "29B10001-E8F2-537E-4F6C-a204768A1215"  //please change to a unique value that matches BLE_IMU_PERIPHERAL
#define BLE_UUID_CHARACT_ACCY             "39B10001-E8F2-537E-4F6C-a204768A1215"  //please change to a unique value that matches BLE_IMU_PERIPHERAL
#define BLE_UUID_CHARACT_ACCZ             "49B10001-E8F2-537E-4F6C-a204768A1215"  //please change to a unique value that matches BLE_IMU_PERIPHERAL


// ------------------------------------------ VOID SETUP ------------------------------------------
void setup() {
  Serial.begin(9600);
  while (!Serial);

  // configure the button pin as input
  pinMode(LED_BUILTIN, OUTPUT);

  // initialize the BLE hardware
  BLE.begin();

  Serial.println("BLE Central - Gyroscope control");

  // start scanning for peripherals
  BLE.scanForUuid(BLE_UUID_PERIPHERAL);//
}

// ------------------------------------------ VOID LOOP ------------------------------------------
void loop() {
  // check if a peripheral has been discovered
  BLEDevice peripheral = BLE.available();

  if (peripheral) {
    // discovered a peripheral, print out address, local name, and advertised service
    Serial.print("Found ");
    Serial.print(peripheral.address());
    Serial.print(" '");
    Serial.print(peripheral.localName());
    Serial.print("' ");
    Serial.print("' ");
    Serial.print(peripheral.advertisedServiceUuid());
    Serial.println();

    if (peripheral.localName() != "BLE_IMU") {
      return;
    }

    // stop scanning
    BLE.stopScan();

    LED_IMU(peripheral);

    // peripheral disconnected, start scanning again
    BLE.scanForUuid(BLE_UUID_PERIPHERAL);
  }
}

// ------------------------------------------ FUNCTIONS ------------------------------------------
void LED_IMU(BLEDevice peripheral) {
  // connect to the peripheral
  Serial.println("Connecting ...");

  if (peripheral.connect()) {
    Serial.println("Connected");
  } else {
    Serial.println("Failed to connect!");
    return;
  }

  // discover peripheral attributes
  Serial.println("Discovering attributes ...");
  if (peripheral.discoverAttributes()) {
    Serial.println("Attributes discovered");
  } else {
    Serial.println("Attribute discovery failed!");
    peripheral.disconnect();
    return;
  }

  // retrieve the LED characteristic
  BLECharacteristic ledCharacteristic = peripheral.characteristic(BLE_UUID_CHARACT_LED);
  BLECharacteristic accXCharacteristic = peripheral.characteristic(BLE_UUID_CHARACT_ACCX);
  BLECharacteristic accYCharacteristic = peripheral.characteristic(BLE_UUID_CHARACT_ACCY);
  BLECharacteristic accZCharacteristic = peripheral.characteristic(BLE_UUID_CHARACT_ACCZ);

  // check if an specific BLE characteristic exists
  if (!ledCharacteristic) {
    Serial.println("Peripheral does not have LED characteristic!");
    peripheral.disconnect();
    return;
  } else if (!ledCharacteristic.canWrite()) {
    Serial.println("Peripheral does not have a writable LED characteristic!");
    peripheral.disconnect();
    return;
  }

  
  int buttonState = 0;
  float x, y, z;
  while (peripheral.connected()) {
    // while the peripheral is connected
    // read the gyroscope values
    accXCharacteristic.readValue( &x, 4 );
    accYCharacteristic.readValue( &y, 4 );
    accZCharacteristic.readValue( &z, 4 );
    Serial.print(x);
    Serial.print('\t');
    Serial.print(y);
    Serial.print('\t');       
    Serial.print(z);
    Serial.println('\t');

    // make the LED blink
    if (buttonState == 0)
    {buttonState = 1;}
    else if (buttonState == 1)
    {buttonState = 0;}
    
    digitalWrite(LED_BUILTIN, buttonState);
      if (buttonState == 0) {
        // write 0x01 to turn the LED on
        ledCharacteristic.writeValue((byte)0x01);
      } else {
        // write 0x00 to turn the LED off
        ledCharacteristic.writeValue((byte)0x00);
      }
  }
  Serial.println("Peripheral disconnected");
}

peripheral code:

#include <ArduinoBLE.h>
#include <Arduino_LSM9DS1.h>
//#include <Arduino_LSM6DS3.h> // Uncomment this if your peripheral is the Nano 33 IoT

// ------------------------------------------ BLE UUIDs ------------------------------------------
#define BLE_UUID_PERIPHERAL               "19B10000-E8F2-537E-4F6C-D104768A1214" //please chnage to a unique value that matches BLE_IMU_CENTRAL
#define BLE_UUID_CHARACT_LED              "19B10001-E8F2-537E-4F6C-E104768A1214" //please chnage to a unique value that matches BLE_IMU_CENTRAL
#define BLE_UUID_CHARACT_ACCX             "29B10001-E8F2-537E-4F6C-a204768A1215" //please chnage to a unique value that matches BLE_IMU_CENTRAL
#define BLE_UUID_CHARACT_ACCY             "39B10001-E8F2-537E-4F6C-a204768A1215" //please chnage to a unique value that matches BLE_IMU_CENTRAL
#define BLE_UUID_CHARACT_ACCZ             "49B10001-E8F2-537E-4F6C-a204768A1215" //please chnage to a unique value that matches BLE_IMU_CENTRAL

BLEService LED_IMU_Service(BLE_UUID_PERIPHERAL); // BLE LED Service

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic(BLE_UUID_CHARACT_LED, BLERead | BLEWrite);
BLEFloatCharacteristic accXCharacteristic(BLE_UUID_CHARACT_ACCX, BLERead | BLENotify | BLEWrite);
BLEFloatCharacteristic accYCharacteristic(BLE_UUID_CHARACT_ACCY, BLERead | BLENotify | BLEWrite);
BLEFloatCharacteristic accZCharacteristic(BLE_UUID_CHARACT_ACCZ, BLERead | BLENotify | BLEWrite);


const int ledPin = LED_BUILTIN; // pin to use for the LED
float x, y, z;

// ------------------------------------------ VOID SETUP ------------------------------------------
void setup() {
  Serial.begin(9600); 
  //while (!Serial); //uncomment to view the IMU data in the peripheral serial monitor

  // begin IMU initialization
  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }

  // set LED pin to output mode
  pinMode(ledPin, OUTPUT);

  // begin BLE initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (1);
  }

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

  // add the characteristic to the service
  LED_IMU_Service.addCharacteristic(switchCharacteristic);
  LED_IMU_Service.addCharacteristic(accXCharacteristic);
  LED_IMU_Service.addCharacteristic(accYCharacteristic);
  LED_IMU_Service.addCharacteristic(accZCharacteristic);


  // add service
  BLE.addService(LED_IMU_Service);

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


  // start advertising
  BLE.advertise();

  Serial.println("BLE LED Peripheral");
}

// ------------------------------------------ VOID LOOP ------------------------------------------
void loop() {
  // listen for BLE 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 the remote device wrote to the characteristic,
      // use the value to control the LED:
      if (switchCharacteristic.written()) {
        if (switchCharacteristic.value()) {   // any value other than 0
          Serial.println("LED on");
          digitalWrite(ledPin, HIGH);         // will turn the LED on
        } else {                              // a 0 value
          Serial.println(F("LED off"));
          digitalWrite(ledPin, LOW);          // will turn the LED off
        }
      }

      if (IMU.gyroscopeAvailable()) {
        IMU.readGyroscope(x, y, z);

        accXCharacteristic.writeValue(x);
        accYCharacteristic.writeValue(y);
        accZCharacteristic.writeValue(z);

        Serial.print(x); 
        Serial.print('\t');
        Serial.print(y); 
        Serial.print('\t');         
        Serial.print(z); 
        Serial.println('\t'); 
      }

    }

      // when the central disconnects, print it out:
      Serial.print(F("Disconnected from central: "));
      Serial.println(central.address());
    }
  }

In fact you are reading the gyroscope. There will be similar function calls to these ones in the LSM9DS1 library for the other two sensors. Study the library docs and example code.

      if (IMU.gyroscopeAvailable()) {
        IMU.readGyroscope(x, y, z);

thank you. i am working on it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.