Skip to content

Commit 35cafe5

Browse files
Merge pull request #2185 from arduino/Pedromsousalima/Nano33BLE/IMUTutorial
[MKC-1855] IMU tutorial Revised
2 parents 731c494 + c8e0d2f commit 35cafe5

File tree

2 files changed

+200
-185
lines changed
  • content/hardware/03.nano/boards
    • nano-33-ble/tutorials/imu-accelerometer
    • nano-33-ble-rev2/tutorials/imu-accelerometer

2 files changed

+200
-185
lines changed

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

Lines changed: 96 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -72,117 +72,127 @@ In this example, we will use the accelerometer as a "level" that will provide in
7272

7373
**1. Setting up**
7474

75-
Let's start by opening the Arduino Cloud Editor, clicking on the **Libraries** tab and searching for the **BMI270_BMM150** library. Then in **> Examples**, open the **SimpleAccelerometer** sketch and once it opens, rename it as **Accelerometer**.
75+
Let's start by opening the Arduino Cloud Editor, clicking on the **Libraries** tab, and searching for the **Arduino_BMI270_BMM150** library. Then, in **Examples**, open the **SimpleAccelerometer** sketch and once it opens, rename it as **Accelerometer**.
7676

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

7979
**2. Connecting the board**
8080

81-
Now, connect the Arduino Nano 33 BLE Rev2 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.
82-
81+
Now, connect the Arduino Nano 33 BLE Sense Rev2 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.
8382

8483
![Selecting the board.](assets/nano33B_02_board_port.png)
8584

8685
**3. Printing the relative position**
8786

88-
Now we will need to modify the code on the example, to print the relative position of the board as we move it in different angles.
89-
90-
Let's start by initializing the x, y, and z axes as `float` data types, and the `int degreesX = 0;` and `int degreesY = 0;` variables before the `setup()`.
91-
92-
In the `setup()` we should **remove** the following lines of code:
87+
Now we will need to modify the code in the example to print the relative position of the board as we move it at different angles.
9388

89+
First, include the BMI270_BMM150 library at the top of your sketch:
9490

9591
```arduino
96-
Serial.println();
97-
Serial.println("Acceleration in G's");
98-
Serial.println("X\tY\tZ");
92+
#include "Arduino_BMI270_BMM150.h"
9993
```
10094

101-
Since the raw values of the three axes will not be required, we can remove the lines that will print these. Similarly, we should **remove** the following lines from the `loop()`:
102-
95+
Then, initialize variables before the `setup()` function:
10396

10497
```arduino
105-
Serial.print(x);
106-
Serial.print('\t');
107-
Serial.print(y);
108-
Serial.print('\t');
109-
Serial.println(z);
110-
```
98+
#define MINIMUM_TILT 5 // Threshold for tilt detection in degrees
99+
#define WAIT_TIME 500 // How often to run the code (in milliseconds)
111100
112-
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.
101+
float x, y, z;
102+
int angleX = 0;
103+
int angleY = 0;
104+
unsigned long previousMillis = 0;
105+
```
113106

114-
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, as well as provide the axe's degree values.
107+
In the `setup()`, initialize the IMU and start serial communication:
115108

116109
```arduino
117-
if(x > 0.1){
118-
x = 100*x;
119-
degreesX = map(x, 0, 97, 0, 90);
120-
Serial.print("Tilting up ");
121-
Serial.print(degreesX);
122-
Serial.println(" degrees");
123-
}
124-
if(x < -0.1){
125-
x = 100*x;
126-
degreesX = map(x, 0, -100, 0, 90);
127-
Serial.print("Tilting down ");
128-
Serial.print(degreesX);
129-
Serial.println(" degrees");
130-
}
131-
if(y > 0.1){
132-
y = 100*y;
133-
degreesY = map(y, 0, 97, 0, 90);
134-
Serial.print("Tilting left ");
135-
Serial.print(degreesY);
136-
Serial.println(" degrees");
137-
}
138-
if(y < -0.1){
139-
y = 100*y;
140-
degreesY = map(y, 0, -100, 0, 90);
141-
Serial.print("Tilting right ");
142-
Serial.print(degreesY);
143-
Serial.println(" degrees");
144-
}
110+
void setup() {
111+
Serial.begin(9600);
112+
while (!Serial);
113+
114+
if (!IMU.begin()) {
115+
Serial.println("Failed to initialize IMU!");
116+
while (1);
117+
}
145118
119+
Serial.print("Accelerometer sample rate = ");
120+
Serial.print(IMU.accelerationSampleRate());
121+
Serial.println("Hz");
122+
}
146123
```
147124

148-
Lastly, we print the values in the serial monitor add a `delay(1000);`.
125+
In the `loop()` function, we will read the accelerometer data and calculate the tilt angles:
149126

150-
>**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.
127+
```arduino
128+
void loop() {
129+
if (IMU.accelerationAvailable() && millis() - previousMillis >= WAIT_TIME) {
130+
previousMillis = millis();
131+
IMU.readAcceleration(x, y, z);
151132
133+
// Calculate tilt angles in degrees
134+
angleX = atan2(x, sqrt(y * y + z * z)) * 180 / PI;
135+
angleY = atan2(y, sqrt(x * x + z * z)) * 180 / PI;
136+
137+
// Determine the tilting direction based on angleX and angleY
138+
if (angleX > MINIMUM_TILT) { // Tilting up
139+
Serial.print("Tilting up ");
140+
Serial.print(angleX);
141+
Serial.println(" degrees");
142+
} else if (angleX < -MINIMUM_TILT) { // Tilting down
143+
Serial.print("Tilting down ");
144+
Serial.print(-angleX);
145+
Serial.println(" degrees");
146+
}
152147
148+
if (angleY > MINIMUM_TILT) { // Tilting right
149+
Serial.print("Tilting right ");
150+
Serial.print(angleY);
151+
Serial.println(" degrees");
152+
} else if (angleY < -MINIMUM_TILT) { // Tilting left
153+
Serial.print("Tilting left ");
154+
Serial.print(-angleY);
155+
Serial.println(" degrees");
156+
}
157+
}
158+
}
159+
```
153160

161+
> **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.
154162
155163
**4. Complete code**
156164

157165
If you choose to skip the code-building section, the complete code can be found below:
158166

159167
```arduino
160168
/*
161-
Arduino BMI270_BMM150 - Simple Accelerometer
169+
Arduino BMI270_BMM150 - Accelerometer Application
162170
163-
This example reads the acceleration values from the BMI270_BMM150
164-
sensor and continuously prints them to the Serial Monitor
165-
or Serial Plotter.
171+
This example reads the acceleration values as relative direction and degrees,
172+
from the BMI270 sensor and prints them to the Serial Monitor.
166173
167174
The circuit:
168-
- Arduino Nano 33 BLE Rev2
175+
- Arduino Nano 33 BLE Sense Rev2
169176
170-
created 10 Jul 2019
171-
by Riccardo Rizzo
177+
Created by Pedro Lima
172178
173179
This example code is in the public domain.
174180
*/
175181
176182
#include "Arduino_BMI270_BMM150.h"
177183
184+
#define MINIMUM_TILT 5 // Threshold for tilt detection in degrees
185+
#define WAIT_TIME 500 // How often to run the code (in milliseconds)
186+
178187
float x, y, z;
179-
int degreesX = 0;
180-
int degreesY = 0;
188+
int angleX = 0;
189+
int angleY = 0;
190+
unsigned long previousMillis = 0;
181191
182192
void setup() {
193+
183194
Serial.begin(9600);
184195
while (!Serial);
185-
Serial.println("Started");
186196
187197
if (!IMU.begin()) {
188198
Serial.println("Failed to initialize IMU!");
@@ -191,47 +201,40 @@ void setup() {
191201
192202
Serial.print("Accelerometer sample rate = ");
193203
Serial.print(IMU.accelerationSampleRate());
194-
Serial.println(" Hz");
204+
Serial.println("Hz");
195205
}
196206
197207
void loop() {
198-
float x, y, z;
199-
200-
if (IMU.accelerationAvailable()) {
208+
if (IMU.accelerationAvailable() && millis() - previousMillis >= WAIT_TIME) {
209+
previousMillis = millis();
201210
IMU.readAcceleration(x, y, z);
202211
203-
if(x > 0.1){
204-
x = 100*x;
205-
degreesX = map(x, 0, 97, 0, 90);
206-
Serial.print("Tilting up ");
207-
Serial.print(degreesX);
208-
Serial.println(" degrees");
209-
}
210-
if(x < -0.1){
211-
x = 100*x;
212-
degreesX = map(x, 0, -100, 0, 90);
213-
Serial.print("Tilting down ");
214-
Serial.print(degreesX);
215-
Serial.println(" degrees");
212+
// Calculate tilt angles in degrees
213+
angleX = atan2(x, sqrt(y * y + z * z)) * 180 / PI;
214+
angleY = atan2(y, sqrt(x * x + z * z)) * 180 / PI;
215+
216+
// Determine the tilting direction based on angleX and angleY
217+
if (angleX > MINIMUM_TILT) { // Tilting up
218+
Serial.print("Tilting up ");
219+
Serial.print(angleX);
220+
Serial.println(" degrees");
221+
} else if (angleX < -MINIMUM_TILT) { // Tilting down
222+
Serial.print("Tilting down ");
223+
Serial.print(-angleX);
224+
Serial.println(" degrees");
216225
}
217-
if(y > 0.1){
218-
y = 100*y;
219-
degreesY = map(y, 0, 97, 0, 90);
220-
Serial.print("Tilting left ");
221-
Serial.print(degreesY);
222-
Serial.println(" degrees");
223-
}
224-
if(y < -0.1){
225-
y = 100*y;
226-
degreesY = map(y, 0, -100, 0, 90);
227-
Serial.print("Tilting right ");
228-
Serial.print(degreesY);
229-
Serial.println(" degrees");
226+
227+
if (angleY > MINIMUM_TILT) { // Tilting right
228+
Serial.print("Tilting right ");
229+
Serial.print(angleY);
230+
Serial.println(" degrees");
231+
} else if (angleY < -MINIMUM_TILT) { // Tilting left
232+
Serial.print("Tilting left ");
233+
Serial.print(-angleY);
234+
Serial.println(" degrees");
230235
}
231236
}
232237
}
233-
234-
}
235238
```
236239

237240

0 commit comments

Comments
 (0)