Accessing IMU Data on Nicla Vision

Learn how to access the data from the accelerometer and gyroscope that comes with the LSM6DSOXTR IMU module.


In this tutorial, you will learn how to access the gyroscope and accelerometer that are placed on the Nicla Vision board. For this, you will be using the Arduino_LSM6DSOX library and the Arduino IDE, printing the values in the Serial Monitor of the Arduino IDE.


The goals of this project are:

  • Read accelerometer data
  • Read gyroscope data
  • Print the data in the Serial Monitor

Hardware & Software Needed

IMU (Inertial Measurement Unit)

An IMU is a component that consists of different sensors and can record data such as specific force, angular rate, orientation. The Nicla Visions IMU has a gyroscope and a accelerometer. On the image below you can see exactly where the IMU is located on the board.

Placement of IMU on the Nicla Vision
Placement of IMU on the Nicla Vision

Accelerometer & Gyroscope

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

Illustration of Nicla Vision accelerometer axis.
Illustration of Nicla Vision accelerometer axis.

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, since they can measure the tilt and lateral orientation of an object, whereas an accelerometer can only measure its linear motion. 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.

Illustration of Nicla Vision gyroscope axis.
Illustration of Nicla Vision gyroscope axis.

In this tutorial, you will use the gyroscope as an indicator for the direction of the force that is applied to the board. You will also use the accelerometer as a "level" that will provide information about the position of the board. With this application you will be able to read what the relative position of the board is as well as its orientation, by tilting the board up, down, left or right. The results will be visible through the Serial Monitor.


Setting up the Arduino IDE

Make sure the latest Nicla Core is installed in the Arduino IDE. Tools > Board > Board Manager.... Here you need to look for the Arduino Mbed OS Nicla Boards and install it. Now you have to install the library needed for the IMU. Go to Tools > Manage libraries.., search for Arduino_LSM6DSOX and install it.

IMU Sketch

The full sketch can be found at the end of the Instructions section. Upload the sketch to the board.

To use the IMU you first need to include the library. To simplify the values coming from the IMU, you can create a variable for each axis.

1#include <Arduino_LSM6DSOX.h>
3float Ax, Ay, Az;
4float Gx, Gy, Gz;

To initializes the library you need to call

. When the IMU is initialized, you can quickly check the sample rates of the sensors. Calling
will read the sampling rate of the respective sensor in Hz.

1void setup() {
2 Serial.begin(9600);
4 while(!Serial);
6 if (!IMU.begin()) {
7 Serial.println("Failed to initialize IMU!");
8 while (1);
9 }
11 Serial.print("Accelerometer sample rate = ");
12 Serial.print(IMU.accelerationSampleRate());
13 Serial.println("Hz");
14 Serial.println();
16 Serial.print("Gyroscope sample rate = ");
17 Serial.print(IMU.gyroscopeSampleRate());
18 Serial.println("Hz");
19 Serial.println();

In the loop of the sketch you can check the sensors to see if there is data available on the IMU sensors, using

. Then you can call
IMU.readAcceleration(Ax, Ay, Az)
to read the accelerometer. It will return the value of the x, y and z axis and update the variables
. You can do the same for the gyroscope, formatting it in the Serial Monitor so it will be a bit easier to read the data. The data is being printed with an interval of 500 milliseconds. This can be adjusted by changing the line
at the bottom of the sketch.

1void loop() {
3 if (IMU.accelerationAvailable()) {
4 IMU.readAcceleration(Ax, Ay, Az);
6 Serial.println("Accelerometer data: ");
7 Serial.print(Ax);
8 Serial.print('\t');
9 Serial.print(Ay);
10 Serial.print('\t');
11 Serial.println(Az);
12 Serial.println();
13 }
15 if (IMU.gyroscopeAvailable()) {
16 IMU.readGyroscope(Gx, Gy, Gz);
18 Serial.println("Gyroscope data: ");
19 Serial.print(Gx);
20 Serial.print('\t');
21 Serial.print(Gy);
22 Serial.print('\t');
23 Serial.println(Gz);
24 Serial.println();
25 }

Testing It Out

After successfully uploading the code to the board, you will need to open the Serial Monitor to initialize the program. Once you open it, data will start printing.

Complete Sketch

1#include <Arduino_LSM6DSOX.h>
3float Ax, Ay, Az;
4float Gx, Gy, Gz;
6void setup() {
7 Serial.begin(9600);
9 while(!Serial);
11 if (!IMU.begin()) {
12 Serial.println("Failed to initialize IMU!");
13 while (1);
14 }
16 Serial.print("Accelerometer sample rate = ");
17 Serial.print(IMU.accelerationSampleRate());
18 Serial.println("Hz");
19 Serial.println();
21 Serial.print("Gyroscope sample rate = ");
22 Serial.print(IMU.gyroscopeSampleRate());
23 Serial.println("Hz");
24 Serial.println();
28void loop() {
30 if (IMU.accelerationAvailable()) {
31 IMU.readAcceleration(Ax, Ay, Az);
33 Serial.println("Accelerometer data: ");
34 Serial.print(Ax);
35 Serial.print('\t');
36 Serial.print(Ay);
37 Serial.print('\t');
38 Serial.println(Az);
39 Serial.println();
40 }
42 if (IMU.gyroscopeAvailable()) {
43 IMU.readGyroscope(Gx, Gy, Gz);
45 Serial.println("Gyroscope data: ");
46 Serial.print(Gx);
47 Serial.print('\t');
48 Serial.print(Gy);
49 Serial.print('\t');
50 Serial.println(Gz);
51 Serial.println();
52 }


In this tutorial you have learned how to use the Arduino_LSM6DSOX library to access the IMU on the Nicla Vision. With this you learned how to print the gyroscope and accelerometer data in the Arduino IDE Serial Monitor.

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.