Accessing Gyroscope Data on Nano 33 BLE Sense

Learn how to measure the direction of force to emulate an object's crash using the Nano 33 BLE Sense.

This tutorial will focus on the 3-axis gyroscope sensor of the LSM9DS1 module, on the Arduino Nano 33 BLE Sense, in order to measure the direction of force on the board to emulate an object's crash. This will be achieved by utilizing the values of the gyroscope'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 data from the gyroscope's sensor.
  • Convert the raw data into direction of force.
  • 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 a bit more of the LSM9DS1 IMU module, which is included in the Arduino Nano 33 BLE Sense 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 Library

The Arduino LSM9DS1 library allows us to use the Arduino Nano 33 BLE Sense 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.

Gyroscope

A gyroscope sensor is a device that can measure and maintain the orientation and angular velocity of an object. Gyroscopes are more advanced than accelerometers, as they can measure the tilt and lateral orientation of an object, whereas an accelerometer can only measure its linear motion.

The gyroscope axes.
The gyroscope axes.

Gyroscope sensors are also called "Angular Rate Sensors" or "Angular Velocity Sensors". Measured in degrees per second, angular velocity is the change in the rotational angle of the object per unit of time.

In this example, we will use the gyroscope as an indicator for the direction of the force that is applied to the board. This will be achieved by swiftly moving the board for an instant in four directions: forward, backward, to the left and to the right. The results will be visible through the Serial Monitor.

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 SimpleGyroscope sketch and once it opens, rename it as Gyroscope.

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 Sense 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 board's direction of force

Now we will need to modify the code on the example, in order to print the board's direction of force 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 plusThreshold = 30;
and
int minusThreshold = -30;
value threshold variables before the
setup()
.

In the

setup()
we should remove the last line of code, as we won't print the raw values of the axes:

1Serial.println("X\tY\tZ");

Next, in the

loop()
we can remove the following, since we initialized in the beginning,

1float x, y, z;

as well as the following lines that won't be required:

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

Instead, we will add four

if
statements, following the already existing one that checks if the sensor is available and then reads the 3 axes.

Here is the code we will add:

1if(y > plusThreshold)
2{
3Serial.println("Collision front");
4delay(500);
5}
6if(y < minusThreshold)
7{
8Serial.println("Collision back");
9delay(500);
10}
11if(x < minusThreshold)
12{
13Serial.println("Collision right");
14delay(500);
15}
16if(x > plusThreshold)
17{
18Serial.println("Collision left");
19delay(500);
20}

These

if
statements will check the x and y values of
plusThreshold
and
minusThreshold
that we defined in the beginning. According to these values the direction of the force that we move our board towards, will be printed.

4. Complete code

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

1/*
2 Arduino LSM9DS1 - Gyroscope Application
3
4 This example reads the gyroscope values from the LSM9DS1 sensor
5 and prints them to the Serial Monitor or Serial Plotter, as a directional detection of
6 an axis' angular velocity.
7
8 The circuit:
9 - Arduino Nano 33 BLE Sense
10
11 Created by Riccardo Rizzo
12
13 Modified by Benjamin Dannegård
14 30 Nov 2020
15
16 This example code is in the public domain.
17*/
18
19#include <Arduino_LSM9DS1.h>
20
21float x, y, z;
22int plusThreshold = 30, minusThreshold = -30;
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 Serial.print("Gyroscope sample rate = ");
34 Serial.print(IMU.gyroscopeSampleRate());
35 Serial.println(" Hz");
36 Serial.println();
37 Serial.println("Gyroscope in degrees/second");
38}
39void loop() {
40
41 if (IMU.gyroscopeAvailable()) {
42 IMU.readGyroscope(x, y, z);
43 }
44 if(y > plusThreshold)
45 {
46 Serial.println("Collision front");
47 delay(500);
48 }
49 if(y < minusThreshold)
50 {
51 Serial.println("Collision back");
52 delay(500);
53 }
54 if(x < minusThreshold)
55 {
56 Serial.println("Collision right");
57 delay(500);
58 }
59 if(x > plusThreshold)
60 {
61 Serial.println("Collision left");
62 delay(500);
63 }
64
65}

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:

Positioning of the board.
Positioning of the board.

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

Now with the board parallel to the ground you can swiftly move it towards one direction: forward, backwards, right or left. According to the movement of your choice, the results will print every second to your monitor!

Here is a screenshot of the sketch returning these values:

Serial Monitor output.
Serial Monitor output.

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.

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 Sense microcontroller, to measure and print out values from a gyroscope sensor. Furthermore, we created an application that detects the direction of force that we can apply to 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.