Accessing Gyroscope Data on Nano 33 BLE Sense Rev2

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

This tutorial will focus on the 3-axis gyroscope sensor of the IMU system on the Arduino Nano 33 BLE Sense Rev2, 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 how the IMU system on the Arduino Nano 33 BLE Sense Rev2 works.
  • Use the BMI270_BMM150 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 BMI270 & BMM150 Inertial Modules

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 IMU system that is included in the Arduino Nano 33 BLE Sense Rev2 Board.

The IMU system on the Arduino Nano 33 BLE Sense Rev2.
The IMU system on the Arduino Nano 33 BLE Sense Rev2.

The IMU system on the Arduino Nano 33 BLE Sense Rev2 is a combination of two modules, the 6-axis BMI270, and the 3-axis BMM150, that together add up to a combined 9-axis IMU system that can measure acceleration, as well as rotation and magnetic fields all in 3D space.

The Library

The Arduino BMI270_BMM150 library allows us to use the Arduino Nano 33 BLE Sense Rev2 IMU modules 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 sensor modules that make up the IMU system, find the datasheet for the BMI270 and the BMM150 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 BMI270_BMM150 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 Rev2 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 BMI270_BMM150 - Simple Gyroscope
3
4 This example reads the gyroscope values from the BMI270_BMM150
5 sensor and continuously prints them to the Serial Monitor
6 or Serial Plotter.
7
8 The circuit:
9 - Arduino Nano 33 BLE Sense Rev2
10
11 created 10 Jul 2019
12 by Riccardo Rizzo
13
14 This example code is in the public domain.
15*/
16
17#include "Arduino_BMI270_BMM150.h"
18
19float x, y, z;
20
21int plusThreshold = 30, minusThreshold = -30;
22
23void setup() {
24 Serial.begin(9600);
25 while (!Serial);
26 Serial.println("Started");
27
28 if (!IMU.begin()) {
29 Serial.println("Failed to initialize IMU!");
30 while (1);
31 }
32 Serial.print("Gyroscope sample rate = ");
33 Serial.print(IMU.gyroscopeSampleRate());
34 Serial.println(" Hz");
35 Serial.println();
36 Serial.println("Gyroscope in degrees/second");
37}
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 BMI270_BMM150 library, and how to use an Arduino Nano 33 BLE Sense Rev2 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.