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.
Introduction
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.
Goals
The goals of this project are:
- Read accelerometer data.
- Read gyroscope data.
- Print the data in the Serial Monitor.
Hardware & Software Needed
- Arduino IDE (online or offline).
- LSM6DSOX library.
- Arduino Nano RP2040 Connect.
IMU (Inertial Measurement Unit)
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!
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.
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.
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.
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.
Circuit
Programming the Board
We will now get to the programming part of this tutorial.
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.
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.
We can now take a look at some of the core functions of this sketch:
- initializes the library.IMU.begin()
- reads the sampling rate in Hz.IMU.accelerationSampleRate()
- checks if there's data available from the IMU.IMU.accelerationAvailable()
- reads the accelerometer, and returns the value of the x y and z axis.IMU.readAcceleration(Ax, Ay, Az)
- reads the sampling rate in Hz.IMU.gyroscopeSampleRate()
- checks if there's data available from the IMU.IMU.gyroscopeAvailable()
- reads the accelerometer, and returns the value of the x y and z axis.IMU.readGyroscope(Gx, Gy, Gz)
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
26}27
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
54delay(500);55
56}
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.
The data is being printed with an interval of 500 milliseconds. This can be adjusted by changing the line
delay(500)
at the bottom of the sketch.Troubleshoot
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.
Conclusion
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.
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.