You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md
+135-113
Original file line number
Diff line number
Diff line change
@@ -73,117 +73,131 @@ In this example, we will use the accelerometer as a "level" that will provide in
73
73
74
74
**1. Setting up**
75
75
76
-
Let's start by opening the Arduino Cloud Editor, click on the **Libraries** tab and search for the **LSM9DS1** library. Then in **> Examples**, open the **SimpleAccelerometer** sketch and once it opens, rename it as **Accelerometer**.
76
+
Let's start by opening the Arduino Cloud Editor, click on the **Libraries** tab and search for the **Arduino_BMI270_BMM150** library. Then, click on **Examples**, and open a new sketch.
77
77
78
78

79
79
80
80
**2. Connecting the board**
81
81
82
-
Now, connect the Arduino Nano 33 BLE to the computer and make sure that the Cloud 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](https://create.arduino.cc/getting-started/plugin/welcome) to install the plugin that will allow the Editor to recognize your board.
83
-
82
+
Now, connect the Arduino Nano 33 BLE to the computer and make sure that the Cloud 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](https://create.arduino.cc/getting-started/plugin/welcome) to install the plugin that will allow the Editor to recognize your board.
84
83
85
84

86
85
87
-
**3. Printing the relative position**
88
-
89
-
Now we will need to modify the code on the example, in order to print the relative position of the board as we move it in different angles.
86
+
**3. Writing the Code**
90
87
91
-
Let's start by initializing the the x, y, z axes as `float` data types, and the `int degreesX = 0;` and `int degreesY = 0;` variables before the `setup()`.
88
+
Now we will write the code to read the accelerometer data, calculate the tilt angles, and print the relative position of the board as we move it at different angles.
92
89
93
-
In the `setup()` we should **remove**the following lines of code:
90
+
-**Include the BMI270 library at the top of your sketch:**
94
91
92
+
```arduino
93
+
#include "Arduino_BMI270_BMM150.h"
94
+
```
95
95
96
-
```arduino
97
-
Serial.println();
98
-
Serial.println("Acceleration in G's");
99
-
Serial.println("X\tY\tZ");
100
-
```
96
+
-**Initialize variables before the `setup()` function:**
101
97
102
-
Since the raw values of the three axes will not be required, we can remove the lines which will print these. Similarly, we should **remove** the following lines from the `loop()`:
98
+
```arduino
99
+
#define MINIMUM_TILT 5 // Threshold for tilt detection in degrees
100
+
#define WAIT_TIME 500 // How often to run the code (in milliseconds)
103
101
102
+
float x, y, z;
103
+
int angleX = 0;
104
+
int angleY = 0;
105
+
unsigned long previousMillis = 0;
106
+
```
104
107
105
-
```arduino
106
-
Serial.print(x);
107
-
Serial.print('\t');
108
-
Serial.print(y);
109
-
Serial.print('\t');
110
-
Serial.println(z);
111
-
```
108
+
-**In the `setup()`, initialize the IMU and start serial communication:**
112
109
113
-
Instead, in the `loop()` we tell the sensor to begin reading the values for the three axes. In this example we will not be using the readings from the Z axis as it is not required for this application to function, therefore you could remove it.
110
+
```arduino
111
+
void setup() {
112
+
Serial.begin(9600);
113
+
while (!Serial);
114
114
115
-
After the `IMU.readAcceleration` initialization, we add four `if` statements for the board's different positions. The statements will calculate the direction in which the board will be tilting towards, as well as provide the axe's degree values.
116
-
117
-
```arduino
118
-
if(x > 0.1){
119
-
x = 100*x;
120
-
degreesX = map(x, 0, 97, 0, 90);
121
-
Serial.print("Tilting up ");
122
-
Serial.print(degreesX);
123
-
Serial.println(" degrees");
124
-
}
125
-
if(x < -0.1){
126
-
x = 100*x;
127
-
degreesX = map(x, 0, -100, 0, 90);
128
-
Serial.print("Tilting down ");
129
-
Serial.print(degreesX);
130
-
Serial.println(" degrees");
131
-
}
132
-
if(y > 0.1){
133
-
y = 100*y;
134
-
degreesY = map(y, 0, 97, 0, 90);
135
-
Serial.print("Tilting left ");
136
-
Serial.print(degreesY);
137
-
Serial.println(" degrees");
138
-
}
139
-
if(y < -0.1){
140
-
y = 100*y;
141
-
degreesY = map(y, 0, -100, 0, 90);
142
-
Serial.print("Tilting right ");
143
-
Serial.print(degreesY);
144
-
Serial.println(" degrees");
115
+
if (!IMU.begin()) {
116
+
Serial.println("Failed to initialize IMU!");
117
+
while (1);
145
118
}
146
119
147
-
```
148
-
149
-
Lastly, we `Serial.print` the results value and add a `delay(1000);`.
150
-
151
-
>**Note:** For the following code to properly work, the board's facing direction and inclination during the initialization of the code, need to be specific. More information will be shared on the "testing it out" section.
120
+
Serial.print("Accelerometer sample rate = ");
121
+
Serial.print(IMU.accelerationSampleRate());
122
+
Serial.println("Hz");
123
+
}
124
+
```
125
+
126
+
-**Write the `loop()` function to read accelerometer data and calculate tilt angles:**
127
+
128
+
```arduino
129
+
void loop() {
130
+
unsigned long currentMillis = millis();
131
+
132
+
if (IMU.accelerationAvailable() && (currentMillis - previousMillis >= WAIT_TIME)) {
133
+
previousMillis = currentMillis;
134
+
135
+
IMU.readAcceleration(x, y, z);
136
+
137
+
// Calculate tilt angles in degrees
138
+
angleX = atan2(x, sqrt(y * y + z * z)) * 180 / PI;
139
+
angleY = atan2(y, sqrt(x * x + z * z)) * 180 / PI;
140
+
141
+
// Determine the tilting direction based on angleX and angleY
142
+
if (angleX > MINIMUM_TILT) { // Tilting up
143
+
Serial.print("Tilting up ");
144
+
Serial.print(angleX);
145
+
Serial.println(" degrees");
146
+
} else if (angleX < -MINIMUM_TILT) { // Tilting down
147
+
Serial.print("Tilting down ");
148
+
Serial.print(-angleX);
149
+
Serial.println(" degrees");
150
+
}
151
+
152
+
if (angleY > MINIMUM_TILT) { // Tilting left
153
+
Serial.print("Tilting left ");
154
+
Serial.print(angleY);
155
+
Serial.println(" degrees");
156
+
} else if (angleY < -MINIMUM_TILT) { // Tilting right
157
+
Serial.print("Tilting right ");
158
+
Serial.print(-angleY);
159
+
Serial.println(" degrees");
160
+
}
161
+
}
162
+
}
163
+
```
152
164
165
+
In this code, we use trigonometric functions to calculate the tilt angles from the accelerometer data. The `MINIMUM_TILT` variable is used to ignore small movements below a certain threshold.
153
166
167
+
> **Note:** For the following code to work properly, the board's facing direction and inclination during the initialization of the code need to be specific. More information will be shared in the "Testing It Out" section.
154
168
169
+
**4. Complete Code**
155
170
156
-
**4. Complete code**
171
+
If you choose to skip the code-building section, the complete code can be found below:
157
172
158
-
If you choose to skip the code building section, the complete code can be found below:
159
173
```arduino
160
174
/*
161
-
Arduino LSM9DS1 - Accelerometer Application
175
+
Arduino BMI270 - Accelerometer Application
162
176
163
177
This example reads the acceleration values as relative direction and degrees,
164
-
from the LSM9DS1 sensor and prints them to the Serial Monitor or Serial Plotter.
178
+
from the BMI270 sensor and prints them to the Serial Monitor.
165
179
166
180
The circuit:
167
181
- Arduino Nano 33 BLE
168
182
169
-
Created by Riccardo Rizzo
170
-
171
-
Modified by Jose García
172
-
27 Nov 2020
183
+
Created by Pedro Lima
173
184
174
185
This example code is in the public domain.
175
186
*/
176
187
177
-
#include <Arduino_LSM9DS1.h>
188
+
#include "Arduino_BMI270_BMM150.h"
189
+
190
+
#define MINIMUM_TILT 5 // Threshold for tilt detection in degrees
191
+
#define WAIT_TIME 500 // How often to run the code (in milliseconds)
178
192
179
193
float x, y, z;
180
-
int degreesX = 0;
181
-
int degreesY = 0;
194
+
int angleX = 0;
195
+
int angleY = 0;
196
+
unsigned long previousMillis = 0;
182
197
183
198
void setup() {
184
199
Serial.begin(9600);
185
200
while (!Serial);
186
-
Serial.println("Started");
187
201
188
202
if (!IMU.begin()) {
189
203
Serial.println("Failed to initialize IMU!");
@@ -196,73 +210,81 @@ void setup() {
196
210
}
197
211
198
212
void loop() {
213
+
unsigned long currentMillis = millis();
214
+
215
+
if (IMU.accelerationAvailable() && (currentMillis - previousMillis >= WAIT_TIME)) {
216
+
previousMillis = currentMillis;
199
217
200
-
if (IMU.accelerationAvailable()) {
201
218
IMU.readAcceleration(x, y, z);
202
219
203
-
}
220
+
// Calculate tilt angles in degrees
221
+
angleX = atan2(x, sqrt(y * y + z * z)) * 180 / PI;
222
+
angleY = atan2(y, sqrt(x * x + z * z)) * 180 / PI;
223
+
224
+
// Determine the tilting direction based on angleX and angleY
225
+
if (angleX > MINIMUM_TILT) { // Tilting up
226
+
Serial.print("Tilting up ");
227
+
Serial.print(angleX);
228
+
Serial.println(" degrees");
229
+
} else if (angleX < -MINIMUM_TILT) { // Tilting down
230
+
Serial.print("Tilting down ");
231
+
Serial.print(-angleX);
232
+
Serial.println(" degrees");
233
+
}
204
234
205
-
if (x > 0.1) {
206
-
x = 100 * x;
207
-
degreesX = map(x, 0, 97, 0, 90);
208
-
Serial.print("Tilting up ");
209
-
Serial.print(degreesX);
210
-
Serial.println(" degrees");
211
-
}
212
-
if (x < -0.1) {
213
-
x = 100 * x;
214
-
degreesX = map(x, 0, -100, 0, 90);
215
-
Serial.print("Tilting down ");
216
-
Serial.print(degreesX);
217
-
Serial.println(" degrees");
218
-
}
219
-
if (y > 0.1) {
220
-
y = 100 * y;
221
-
degreesY = map(y, 0, 97, 0, 90);
222
-
Serial.print("Tilting left ");
223
-
Serial.print(degreesY);
224
-
Serial.println(" degrees");
225
-
}
226
-
if (y < -0.1) {
227
-
y = 100 * y;
228
-
degreesY = map(y, 0, -100, 0, 90);
229
-
Serial.print("Tilting right ");
230
-
Serial.print(degreesY);
231
-
Serial.println(" degrees");
235
+
if (angleY > MINIMUM_TILT) { // Tilting left
236
+
Serial.print("Tilting left ");
237
+
Serial.print(angleY);
238
+
Serial.println(" degrees");
239
+
} else if (angleY < -MINIMUM_TILT) { // Tilting right
240
+
Serial.print("Tilting right ");
241
+
Serial.print(-angleY);
242
+
Serial.println(" degrees");
243
+
}
232
244
}
233
-
delay(1000);
234
245
}
235
-
236
246
```
237
247
238
-
239
248
## Testing It Out
240
-
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:
241
249
242
-

250
+
In order to get correct readings from the board:
243
251
244
-
Now, you can verify and upload the sketch to the board and open the Monitor from the menu on the left.
252
+
1.**Initial Position:**
245
253
246
-
If you tilt the board upwards, downwards, right or left, you will see the results printing every second according to the direction of your movement!
254
+
Before uploading the sketch, place the board on a flat surface with the components facing upwards and the USB port pointing away from you. The image below illustrates the board's position and how it interacts with the X and Y axes:
247
255
256
+

248
257
249
-
Here is a screenshot of the sketch returning these values:
258
+
2.**Uploading the Sketch:**
250
259
251
-

260
+
- Verify and upload the sketch to the board.
261
+
- Open the Serial Monitor from the menu on the left.
262
+
263
+
3.**Interacting with the Board:**
252
264
265
+
-**Tilting Up/Down:** Tilt the board upwards or downwards to see the "Tilting up" or "Tilting down" messages.
266
+
-**Tilting Left/Right:** Tilt the board to the left or right to see the "Tilting left" or "Tilting right" messages.
253
267
268
+
4.**Observing the Output:**
269
+
270
+
The Serial Monitor will display the tilt direction and angle every half-second based on your movements.
271
+
272
+
Here is a screenshot of the sketch returning these values:
273
+
274
+

254
275
255
276
### Troubleshoot
256
277
257
-
Sometimes errors occur, if the code is not working there are some common issues we can troubleshoot:
258
-
- Missing a bracket or a semicolon.
278
+
Sometimes errors occur. If the code is not working, here are some common issues you can troubleshoot:
259
279
280
+
- Missing a bracket or a semicolon.
260
281
- Arduino board connected to the wrong port.
261
282
- Accidental interruption of cable connection.
262
-
- The initial position of the board is not as instructed. In this case you can refresh the page and try again.
263
-
264
-
283
+
- The initial position of the board is not as instructed. In this case, you can reset the board and try again.
284
+
- Ensure the **Arduino_BMI270_BMM150** library is properly installed.
265
285
266
286
## Conclusion
267
287
268
-
In this simple tutorial we learned what an IMU sensor module is, how to use the **LSM9DS1** library, and how to use an Arduino Nano 33 BLE to get data. Furthermore, we utilized the 3-axis accelerometer sensor, in order to measure and print out the degrees and relative position of the board.
288
+
In this simple tutorial, we learned what an IMU sensor module is, how to use the **Arduino_BMI270_BMM150** library, and how to use an Arduino Nano 33 BLE to get data. Furthermore, we utilized the 3-axis accelerometer sensor to measure and print out the degrees and relative position of the board.
0 commit comments