|
| 1 | +--- |
| 2 | +title: 'Accessing IMU Data on Nicla Vision' |
| 3 | +difficulty: easy |
| 4 | +compatible-products: [nicla-vision] |
| 5 | +description: 'Learn how to access the data from the accelerometer and gyroscope that comes with the LSM6DSOXTR IMU module.' |
| 6 | +tags: |
| 7 | + - Gyroscope |
| 8 | + - Accelerometer |
| 9 | +author: 'Benjamin Dannegård' |
| 10 | +libraries: |
| 11 | + - name: Arduino LSM6DSOX |
| 12 | + url: https://www.arduino.cc/en/Reference/ArduinoLSM6DSOX |
| 13 | +hardware: |
| 14 | + - hardware/05.nicla/boards/nicla-vision |
| 15 | +software: |
| 16 | + - ide-v1 |
| 17 | + - ide-v2 |
| 18 | + - web-editor |
| 19 | +--- |
| 20 | + |
| 21 | +## Introduction |
| 22 | + |
| 23 | +In this tutorial, we will learn how to access the gyroscope and accelerometer that is on the Nicla Vision board. For this, we will be using the [Arduino_LSMDS63](https://www.arduino.cc/en/Reference/ArduinoLSM6DSOX) library and the Arduino IDE. Printing the values in the serial monitor of the Arduino IDE. |
| 24 | + |
| 25 | +## Goals |
| 26 | + |
| 27 | +The goals of this project are: |
| 28 | + |
| 29 | +- Read accelerometer data. |
| 30 | +- Read gyroscope data. |
| 31 | +- Print the data in the Serial Monitor. |
| 32 | + |
| 33 | +## Hardware & Software Needed |
| 34 | + |
| 35 | +- Arduino IDE ([online](https://create.arduino.cc/) or [offline](https://www.arduino.cc/en/main/software)). |
| 36 | +- [LSM6DSOX library](https://github.com/arduino-libraries/Arduino_LSM6DSOX) |
| 37 | +- Nicla Vision board (<https://store.arduino.cc/products/nicla-vision>) |
| 38 | + |
| 39 | +## IMU (Inertial Measurement Unit) |
| 40 | + |
| 41 | +An IMU is a component that exists of different sensors that records 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 on the board the IMU is located. |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +### Accelerometer & Gyroscope |
| 46 | + |
| 47 | +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. |
| 48 | + |
| 49 | +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. |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +In this tutorial, we will use the gyroscope as an indicator for the direction of the force that is applied to the board. We will also 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. The results will be visible through the Serial Monitor. |
| 54 | + |
| 55 | +## Instructions |
| 56 | + |
| 57 | +### Setting up the Ardunio IDE |
| 58 | + |
| 59 | +Make sure the latest Nicla Core is installed in the Arduino IDE. **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 library needed for the IMU. Go to **Tools > Manage libraries..**, and search for **Arduino_LSM6DS3** and install it. |
| 60 | + |
| 61 | +### IMU Sketch |
| 62 | + |
| 63 | +The full sketch can be found at the end of the **Instructions** section. Upload the sketch to the board. |
| 64 | + |
| 65 | +To use the IMU we first include the library. To make it easier with the values from the IMU, we create a variable for each axis. |
| 66 | + |
| 67 | +```arduino |
| 68 | +#include <Arduino_LSM6DSOX.h> |
| 69 | +
|
| 70 | +float Ax, Ay, Az; |
| 71 | +float Gx, Gy, Gz; |
| 72 | +
|
| 73 | +``` |
| 74 | + |
| 75 | +To initializes the library we need to call `IMU.begin()`. When the IMU is inistialized, we can quickly check the sameple rates of the sensors. Calling `IMU.accelerationSampleRate()` and `IMU.gyroscopeSampleRate()` will read the sampling rate of the respective sensor in Hz. |
| 76 | + |
| 77 | +```arduino |
| 78 | +void setup() { |
| 79 | + Serial.begin(9600); |
| 80 | +
|
| 81 | + while(!Serial); |
| 82 | +
|
| 83 | + if (!IMU.begin()) { |
| 84 | + Serial.println("Failed to initialize IMU!"); |
| 85 | + while (1); |
| 86 | + } |
| 87 | + |
| 88 | + Serial.print("Accelerometer sample rate = "); |
| 89 | + Serial.print(IMU.accelerationSampleRate()); |
| 90 | + Serial.println("Hz"); |
| 91 | + Serial.println(); |
| 92 | +
|
| 93 | + Serial.print("Gyroscope sample rate = "); |
| 94 | + Serial.print(IMU.gyroscopeSampleRate()); |
| 95 | + Serial.println("Hz"); |
| 96 | + Serial.println(); |
| 97 | +
|
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +In the loop of the sketch we can check the sensors to see if there is data available on the IMU sensors, using `IMU.accelerationAvailable()` and `IMU.gyroscopeAvailable()`. Then we 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 `Ax`, `Ay` and `Az`. We 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 `delay(500)` at the bottom of the sketch. |
| 102 | + |
| 103 | +```arduino |
| 104 | +void loop() { |
| 105 | +
|
| 106 | + if (IMU.accelerationAvailable()) { |
| 107 | + IMU.readAcceleration(Ax, Ay, Az); |
| 108 | +
|
| 109 | + Serial.println("Accelerometer data: "); |
| 110 | + Serial.print(Ax); |
| 111 | + Serial.print('\t'); |
| 112 | + Serial.print(Ay); |
| 113 | + Serial.print('\t'); |
| 114 | + Serial.println(Az); |
| 115 | + Serial.println(); |
| 116 | + } |
| 117 | +
|
| 118 | + if (IMU.gyroscopeAvailable()) { |
| 119 | + IMU.readGyroscope(Gx, Gy, Gz); |
| 120 | + |
| 121 | + Serial.println("Gyroscope data: "); |
| 122 | + Serial.print(Gx); |
| 123 | + Serial.print('\t'); |
| 124 | + Serial.print(Gy); |
| 125 | + Serial.print('\t'); |
| 126 | + Serial.println(Gz); |
| 127 | + Serial.println(); |
| 128 | + } |
| 129 | +
|
| 130 | +delay(500); |
| 131 | +
|
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +### Testing It Out |
| 136 | + |
| 137 | +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. |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | +### Complete Sketch |
| 142 | + |
| 143 | +```arduino |
| 144 | +#include <Arduino_LSM6DSOX.h> |
| 145 | +
|
| 146 | +float Ax, Ay, Az; |
| 147 | +float Gx, Gy, Gz; |
| 148 | +
|
| 149 | +void setup() { |
| 150 | + Serial.begin(9600); |
| 151 | +
|
| 152 | + while(!Serial); |
| 153 | +
|
| 154 | + if (!IMU.begin()) { |
| 155 | + Serial.println("Failed to initialize IMU!"); |
| 156 | + while (1); |
| 157 | + } |
| 158 | + |
| 159 | + Serial.print("Accelerometer sample rate = "); |
| 160 | + Serial.print(IMU.accelerationSampleRate()); |
| 161 | + Serial.println("Hz"); |
| 162 | + Serial.println(); |
| 163 | +
|
| 164 | + Serial.print("Gyroscope sample rate = "); |
| 165 | + Serial.print(IMU.gyroscopeSampleRate()); |
| 166 | + Serial.println("Hz"); |
| 167 | + Serial.println(); |
| 168 | +
|
| 169 | +} |
| 170 | +
|
| 171 | +void loop() { |
| 172 | +
|
| 173 | + if (IMU.accelerationAvailable()) { |
| 174 | + IMU.readAcceleration(Ax, Ay, Az); |
| 175 | +
|
| 176 | + Serial.println("Accelerometer data: "); |
| 177 | + Serial.print(Ax); |
| 178 | + Serial.print('\t'); |
| 179 | + Serial.print(Ay); |
| 180 | + Serial.print('\t'); |
| 181 | + Serial.println(Az); |
| 182 | + Serial.println(); |
| 183 | + } |
| 184 | +
|
| 185 | + if (IMU.gyroscopeAvailable()) { |
| 186 | + IMU.readGyroscope(Gx, Gy, Gz); |
| 187 | + |
| 188 | + Serial.println("Gyroscope data: "); |
| 189 | + Serial.print(Gx); |
| 190 | + Serial.print('\t'); |
| 191 | + Serial.print(Gy); |
| 192 | + Serial.print('\t'); |
| 193 | + Serial.println(Gz); |
| 194 | + Serial.println(); |
| 195 | + } |
| 196 | +
|
| 197 | +delay(500); |
| 198 | +
|
| 199 | +} |
| 200 | +``` |
| 201 | + |
| 202 | +## Conclusion |
| 203 | + |
| 204 | +In this tutorial we have learned how to use the **Arduino_LSM6DSOX** library to access the IMU on the Nicla Vision. With this we learned how to print the gyroscope and accelerometer data in the Arduino IDE serial monitor. |
0 commit comments