Accessing IMU data on Nano RP2040 Connect

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


In this tutorial, we will learn how to access the gyroscope and accelerometer onboard the Nano RP2040 Connect. For this, we will be using the Arduino_LSMDS63 library.

Note: if you need help setting up your environment to use your Arduino Nano RP2040 board, please refer to this installation guide.


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)

The LSM6DSOXTR sensor.
The LSM6DSOXTR sensor.

An IMU is a component that exists of different sensors that records data such as specific force, angular rate, orientation. On the Nano RP2040 Connect, there is one gyroscope and one accelerometer. Let's take a look at how they work!


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.


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.


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.


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.


Plug in the Nano RP2040 Connect to your computer.
Plug in the Nano RP2040 Connect to your computer.

Programming the board

We will now get to the programming part of this tutorial.

  1. First, let's make sure we have the drivers installed. If we are using the Web Editor, we do not need to install anything. If we are using an offline editor, we need to install it manually. This can be done by navigating to Tools > Board > Board Manager.... Here we need to look for the Arduino Mbed OS Nano Boards and install it.

  2. Now, we need to install the libraries needed. If we are using the Web Editor, there is no need to install anything. If we are using an offline editor, simply go to Tools > Manage libraries.., and search for Arduino_LSM6DS3 and install it.

  3. We can now take a look at some of the core functions of this sketch:

  • IMU.begin()
    - initializes the library.
  • IMU.accelerationSampleRate()
    - reads the sampling rate in Hz.
  • IMU.accelerationAvailable()
    - checks if there's data available from the IMU.
  • IMU.readAcceleration(Ax, Ay, Az)
    - reads the accelerometer, and returns the value of the x y and z axis.
  • IMU.gyroscopeSampleRate()
    - reads the sampling rate in Hz.
  • IMU.gyroscopeAvailable()
    - checks if there's data available from the IMU.
  • IMU.readGyroscope(Gx, Gy, Gz)
    - reads the accelerometer, and returns the value of the x y and z axis.

The sketch can be found in the snippet below. Upload the sketch to the board.

1#include <Arduino_LSM6DSOX.h>2
3float Ax, Ay, Az;4float Gx, Gy, Gz;5
6void setup() {7  Serial.begin(9600);8
9  while(!Serial);10
11  if (!IMU.begin()) {12    Serial.println("Failed to initialize IMU!");13    while (1);14  }15
16  Serial.print("Accelerometer sample rate = ");17  Serial.print(IMU.accelerationSampleRate());18  Serial.println("Hz");19  Serial.println();20
21  Serial.print("Gyroscope sample rate = ");  22  Serial.print(IMU.gyroscopeSampleRate());23  Serial.println("Hz");24  Serial.println();25
28void loop() {29
30  if (IMU.accelerationAvailable()) {31    IMU.readAcceleration(Ax, Ay, Az);32
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  }41
42  if (IMU.gyroscopeAvailable()) {43    IMU.readGyroscope(Gx, Gy, Gz);44    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  }53

Testing it out

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

Accelerometer and gyroscope data printed in the Serial Monitor.
Accelerometer and gyroscope data printed in the Serial Monitor.

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.


If the code is not working, there are some common issues we can troubleshoot:

  • We have not installed the Arduino_LSM6DSOX library.
  • We have a faulty USB cable.


In this tutorial we have learned how to retrieve data from the IMU onboard our Nano RP2040 Connect board. With the gyroscope and accelerometer data, we can create a lot of interesting projects, such as gesture control, orientation and more.

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.