Arduino BLE Accelerometer Tutorial – Introduction

In the previous post, I showed you how you can send Battery status to your phone using Arduino Nano 22 BLE Sense. In this post, I will show you how you can send Arduino Nano 33 IOT Sensor Data to your phone. If you haven’t seen that post yet, or if you want to know more about the basics of Arduino and BLE, check out the link below.

Arduino BLE Example 1 – Battery Level Indicator

Arduino Nano 33 BLE Sense

Today, I am here with a new version of Arduino Nano – Arduino Nano 33 BLE Sense, Nano with BLE connectivity focussing on IOT, which is packed with a wide variety of sensors such as 9 axis Inertial Measurement Unit, pressure, light, and even gestures sensors and a microphone.

It is powered by Nina B306 module that support BLE as well as Bluetooth 5 connection. The inbuilt Bluetooth module consumes very low power and can be easily accessed using Arduino libraries. This makes it easier to program and enable wireless connectivity to any of your projects in no time.You won’t have to use external Bluetooth modules to add Bluetooth capability to your project. Save space and power.

Example 2 – Arduino BLE Accelerometer Tutorial

Step 0 – Try Example 1 (Optional)

In the previous example, I showed you a very easy example to show you jpw you can send simple data via Bluetooth. If you are new to this, try this example first. It will help to give you a better understanding of the BLE.

Step 1 – Installing Libraries

For this example, we will need two libraries –

ArduinoBLE – To Send Data via Bluetooth

LSM9DS1 – To Read data from inbuilt Accelerometer

Both these libraries are available in library manager. Simply search for that using the name and click on install.

Step 2 – Test Accelerometer Code (Optional)

Now we will try running the accelerometer code to make sure that these data are being read properly. For that, use the below code.

#include <Arduino_LSM9DS1.h> void setup() { Serial.begin(9600); while (!Serial); Serial.println("Started"); if (!IMU.begin()) { Serial.println("Failed to initialize IMU!"); while (1); } Serial.print("Accelerometer sample rate = "); Serial.print(IMU.accelerationSampleRate()); Serial.println(" Hz"); Serial.println(); Serial.println("Acceleration in G's"); Serial.println("XtYtZ"); } void loop() { float x, y, z; if (IMU.accelerationAvailable()) { IMU.readAcceleration(x, y, z); Serial.print(x); Serial.print('t'); Serial.print(y); Serial.print('t'); Serial.println(z); } }

Once uploaded, start the serial monitor. You will see the data being populated. Try tilting the board in all direction and you will see the value changes accordingly.

Step 3 – Upload the Code

Now its time to upload the complete code. Copy the code below and paste it in the IDE.

#include <ArduinoBLE.h> #include <Arduino_LSM9DS1.h> int accelX=1; int accelY=1; float x, y, z; BLEService customService("1101"); BLEUnsignedIntCharacteristic customXChar("2101", BLERead | BLENotify); BLEUnsignedIntCharacteristic customYChar("2102", BLERead | BLENotify); void setup() { IMU.begin(); Serial.begin(9600); while (!Serial); pinMode(LED_BUILTIN, OUTPUT); if (!BLE.begin()) { Serial.println("BLE failed to Initiate"); delay(500); while (1); } BLE.setLocalName("Arduino Accelerometer"); BLE.setAdvertisedService(customService); customService.addCharacteristic(customXChar); customService.addCharacteristic(customYChar); BLE.addService(customService); customXChar.writeValue(accelX); customYChar.writeValue(accelY); BLE.advertise(); Serial.println("Bluetooth device is now active, waiting for connections..."); } void loop() { BLEDevice central = BLE.central(); if (central) { Serial.print("Connected to central: "); Serial.println(central.address()); digitalWrite(LED_BUILTIN, HIGH); while (central.connected()) { delay(200); read_Accel(); customXChar.writeValue(accelX); customYChar.writeValue(accelY); Serial.print("At Main Function"); Serial.println(""); Serial.print(accelX); Serial.print(" - "); Serial.println(accelY); Serial.println(""); Serial.println(""); } } digitalWrite(LED_BUILTIN, LOW); Serial.print("Disconnected from central: "); Serial.println(central.address()); } void read_Accel() { if (IMU.accelerationAvailable()) { IMU.readAcceleration(x, y, z); accelX = (1+x)*100; accelY = (1+y)*100; } }

Select the right port and board. Click on upload.

Step 4 – Testing Arduino BLE Accelerometer

In your Android smartphone, install the app “nRF Connect”. Open it and start the scanner. You will see the device “Arduino Accelerometer” in the device list. Now tap on connect and a new tab will be opened.

Go to that and you will see the services and characteristics of the device.

Tap on Unknown Service and you will see the accelerometer values being read from the Arduino

In the next post, I will show you how you can send inbuilt sensor values such as accelerometer, gyroscope, color sensor and gesture sensor from the Arduino to your phone as well as another Arduino via BLE.