Skip to content

Commit ce40551

Browse files
Changed IMU example
1 parent 4e3bd06 commit ce40551

File tree

1 file changed

+135
-113
lines changed
  • content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer

1 file changed

+135
-113
lines changed

content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md

+135-113
Original file line numberDiff line numberDiff line change
@@ -73,117 +73,131 @@ In this example, we will use the accelerometer as a "level" that will provide in
7373

7474
**1. Setting up**
7575

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.
7777

7878
![Finding the library in the Cloud Editor.](./assets/nano33BLE_01_include_library.png)
7979

8080
**2. Connecting the board**
8181

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.
8483

8584
![Selecting the board.](assets/nano33BLE_01_board_port.png)
8685

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**
9087

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.
9289

93-
In the `setup()` we should **remove** the following lines of code:
90+
- **Include the BMI270 library at the top of your sketch:**
9491

92+
```arduino
93+
#include "Arduino_BMI270_BMM150.h"
94+
```
9595

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:**
10197

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)
103101
102+
float x, y, z;
103+
int angleX = 0;
104+
int angleY = 0;
105+
unsigned long previousMillis = 0;
106+
```
104107

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:**
112109

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);
114114
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);
145118
}
146119
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+
```
152164

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.
153166

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.
154168
169+
**4. Complete Code**
155170

156-
**4. Complete code**
171+
If you choose to skip the code-building section, the complete code can be found below:
157172

158-
If you choose to skip the code building section, the complete code can be found below:
159173
```arduino
160174
/*
161-
Arduino LSM9DS1 - Accelerometer Application
175+
Arduino BMI270 - Accelerometer Application
162176
163177
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.
165179
166180
The circuit:
167181
- Arduino Nano 33 BLE
168182
169-
Created by Riccardo Rizzo
170-
171-
Modified by Jose García
172-
27 Nov 2020
183+
Created by Pedro Lima
173184
174185
This example code is in the public domain.
175186
*/
176187
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)
178192
179193
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;
182197
183198
void setup() {
184199
Serial.begin(9600);
185200
while (!Serial);
186-
Serial.println("Started");
187201
188202
if (!IMU.begin()) {
189203
Serial.println("Failed to initialize IMU!");
@@ -196,73 +210,81 @@ void setup() {
196210
}
197211
198212
void loop() {
213+
unsigned long currentMillis = millis();
214+
215+
if (IMU.accelerationAvailable() && (currentMillis - previousMillis >= WAIT_TIME)) {
216+
previousMillis = currentMillis;
199217
200-
if (IMU.accelerationAvailable()) {
201218
IMU.readAcceleration(x, y, z);
202219
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+
}
204234
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+
}
232244
}
233-
delay(1000);
234245
}
235-
236246
```
237247

238-
239248
## 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:
241249

242-
![Interacting with the X and Y axes.](./assets/nano33BLE_01_illustration.png)
250+
In order to get correct readings from the board:
243251

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:**
245253

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:
247255

256+
![Interacting with the X and Y axes.](./assets/nano33BLE_01_illustration.png)
248257

249-
Here is a screenshot of the sketch returning these values:
258+
2. **Uploading the Sketch:**
250259

251-
![Printing out the "tilt condition" of the board.](./assets/nano33BLE_01_printing_values.png)
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:**
252264

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.
253267

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+
![Printing out the "tilt condition" of the board.](./assets/nano33BLE_01_printing_values.png)
254275

255276
### Troubleshoot
256277

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:
259279

280+
- Missing a bracket or a semicolon.
260281
- Arduino board connected to the wrong port.
261282
- 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.
265285

266286
## Conclusion
267287

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.
289+
290+
---

0 commit comments

Comments
 (0)