Accessing Accelerometer Data on Nano 33 BLE

Learn how to measure the relative position of the Nano 33 BLE through the LSM9DS1 IMU sensor.

This tutorial will focus on the 3-axis accelerometer sensor of the LSM9DS1 module on the Arduino Nano 33 BLE, in order to measure the relative position of the board. This will be achieved by utilizing the values of the accelerometer's axes and later print the return values through the Arduino IDE Serial Monitor.

Goals

The goals of this project are:

  • Understand what an LSM9DS1 module is.
  • Use the LSM9DS1 library.
  • Read the raw data of the accelerometer sensor.
  • Convert the raw data into board positions.
  • Print out live data through the Serial Monitor.

Hardware & Software Needed

  • This project uses no external sensors or components.
  • In this tutorial we will use the Arduino Create Web Editor to program the board.

The LSM9DS1 Inertial Module

IMU stands for: inertial measurement unit. It is an electronic device that measures and reports a body's specific force, angular rate and the orientation of the body, using a combination of accelerometers, gyroscopes, and oftentimes magnetometers. In this tutorial we will learn about the LSM9DS1 IMU module, which is included in the Arduino Nano 33 BLE Board.

The LSM9DS1 sensor.
The LSM9DS1 sensor.

The LSM9DS1 is a system-in-package featuring a 3D digital linear acceleration sensor, a 3D digital angular rate sensor, and a 3D digital magnetic sensor.

The LSM9DS1 Library

The Arduino LSM9DS1 library allows us to use the Arduino Nano 33 BLE IMU module without having to go into complicated programming. The library takes care of the sensor initialization and sets its values as follows:

  • Accelerometer range is set at [-4, +4]g -/+0.122 mg.
  • Gyroscope range is set at [-2000, +2000] dps +/-70 mdps.
  • Magnetometer range is set at [-400, +400] uT +/-0.014 uT.
  • Accelerometer output data rate is fixed at 104 Hz.
  • Gyroscope output data rate is fixed at 104 Hz.
  • Magnetometer output data rate is fixed at 20 Hz.

If you want to read more about the LSM9DS1 sensor module see here.

Accelerometer

An accelerometer is an electromechanical device used to measure acceleration forces. Such forces may be static, like the continuous force of gravity or, as is the case with many mobile devices, dynamic to sense movement or vibrations.

How the accelerometer works.
How the accelerometer works.

In this example, we will use the accelerometer as a "level" that will provide information about the position of the board. With this application we will be able to read what the relative position of the board is as well as the degrees, by tilting the board up, down, left or right.

Creating the Program

1. Setting up

Let's start by opening the Arduino Web Editor, click on the Libraries tab and search for the LSM9DS1 library. Then in > Examples, open the SimpleAccelerometer sketch and once it opens, rename it as Accelerometer.

Finding the library in the Web Editor.
Finding the library in the Web Editor.

2. Connecting the board

Now, connect the Arduino Nano 33 BLE to the computer and make sure that the Web Editor recognizes it, if so, the board and port should appear as shown in the image below. If they don't appear, follow the instructions to install the plugin that will allow the Editor to recognize your board.

Selecting the board.
Selecting the board.

3. Printing the relative position

Now we will need to modify the code on the example, in order to print the relative position of the board as we move it in different angles.

Let's start by initializing the the x, y, z axes as

float
data types, and the
int degreesX = 0;
and
int degreesY = 0;
variables before the
setup()
.

In the

setup()
we should remove the following lines of code:

1Serial.println();
2Serial.println("Acceleration in G's");
3Serial.println("X\tY\tZ");

Since the raw values of the three axes will not be required, we can remove the lines which will print these. Similarly, we should remove the following lines from the

loop()
:

1Serial.print(x);
2Serial.print('\t');
3Serial.print(y);
4Serial.print('\t');
5Serial.println(z);

Instead, in the

loop()
we tell the sensor to begin reading the values for the three axes. In this example we will not be using the readings from the Z axis as it is not required for this application to function, therefore you could remove it.

After the

IMU.readAcceleration
initialization, we add four
if
statements for the board's different positions. The statements will calculate the direction in which the board will be tilting towards, as well as provide the axe's degree values.

1if(x > 0.1){
2 x = 100*x;
3 degreesX = map(x, 0, 97, 0, 90);
4 Serial.print("Tilting up ");
5 Serial.print(degreesX);
6 Serial.println(" degrees");
7 }
8 if(x < -0.1){
9 x = 100*x;
10 degreesX = map(x, 0, -100, 0, 90);
11 Serial.print("Tilting down ");
12 Serial.print(degreesX);
13 Serial.println(" degrees");
14 }
15 if(y > 0.1){
16 y = 100*y;
17 degreesY = map(y, 0, 97, 0, 90);
18 Serial.print("Tilting left ");
19 Serial.print(degreesY);
20 Serial.println(" degrees");
21 }
22 if(y < -0.1){
23 y = 100*y;
24 degreesY = map(y, 0, -100, 0, 90);
25 Serial.print("Tilting right ");
26 Serial.print(degreesY);
27 Serial.println(" degrees");
28 }

Lastly, we

Serial.print
the results value and add a
delay(1000);
.

Note: For the following code to properly work, the board's facing direction and inclination during the initialization of the code, need to be specific. More information will be shared on the "testing it out" section.

4. Complete code

If you choose to skip the code building section, the complete code can be found below:

1/*
2 Arduino LSM9DS1 - Accelerometer Application
3
4 This example reads the acceleration values as relative direction and degrees,
5 from the LSM9DS1 sensor and prints them to the Serial Monitor or Serial Plotter.
6
7 The circuit:
8 - Arduino Nano 33 BLE
9
10 Created by Riccardo Rizzo
11
12 Modified by Jose García
13 27 Nov 2020
14
15 This example code is in the public domain.
16*/
17
18#include <Arduino_LSM9DS1.h>
19
20float x, y, z;
21int degreesX = 0;
22int degreesY = 0;
23
24void setup() {
25 Serial.begin(9600);
26 while (!Serial);
27 Serial.println("Started");
28
29 if (!IMU.begin()) {
30 Serial.println("Failed to initialize IMU!");
31 while (1);
32 }
33
34 Serial.print("Accelerometer sample rate = ");
35 Serial.print(IMU.accelerationSampleRate());
36 Serial.println("Hz");
37}
38
39void loop() {
40
41 if (IMU.accelerationAvailable()) {
42 IMU.readAcceleration(x, y, z);
43
44 }
45
46 if (x > 0.1) {
47 x = 100 * x;
48 degreesX = map(x, 0, 97, 0, 90);
49 Serial.print("Tilting up ");
50 Serial.print(degreesX);
51 Serial.println(" degrees");
52 }
53 if (x < -0.1) {
54 x = 100 * x;
55 degreesX = map(x, 0, -100, 0, 90);
56 Serial.print("Tilting down ");
57 Serial.print(degreesX);
58 Serial.println(" degrees");
59 }
60 if (y > 0.1) {
61 y = 100 * y;
62 degreesY = map(y, 0, 97, 0, 90);
63 Serial.print("Tilting left ");
64 Serial.print(degreesY);
65 Serial.println(" degrees");
66 }
67 if (y < -0.1) {
68 y = 100 * y;
69 degreesY = map(y, 0, -100, 0, 90);
70 Serial.print("Tilting right ");
71 Serial.print(degreesY);
72 Serial.println(" degrees");
73 }
74 delay(1000);
75}

Testing It Out

In order to get a correct reading of the board data, before uploading the sketch to the board hold the board in your hand, from the side of the USB port. The board should be facing up and "pointing" away from you. The image below illustrates the board's position and how it works:

Interacting with the X and Y axes.
Interacting with the X and Y axes.

Now, you can verify and upload the sketch to the board and open the Monitor from the menu on the left.

If you tilt the board upwards, downwards, right or left, you will see the results printing every second according to the direction of your movement!

Here is a screenshot of the sketch returning these values:

Printing out the "tilt condition" of the board.
Printing out the "tilt condition" of the board.

Troubleshoot

Sometimes errors occur, if the code is not working there are some common issues we can troubleshoot:

  • Missing a bracket or a semicolon.

  • Arduino board connected to the wrong port.

  • Accidental interruption of cable connection.

  • The initial position of the board is not as instructed. In this case you can refresh the page and try again.

Conclusion

In this simple tutorial we learned what an IMU sensor module is, how to use the LSM9DS1 library, and how to use an Arduino Nano 33 BLE to get data. Furthermore, we utilized the 3-axis accelerometer sensor, in order to measure and print out the degrees and relative position of the board.

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.