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-rev2/tutorials/imu-accelerometer/content.md
+96-93Lines changed: 96 additions & 93 deletions
Original file line number
Diff line number
Diff line change
@@ -72,117 +72,127 @@ In this example, we will use the accelerometer as a "level" that will provide in
72
72
73
73
**1. Setting up**
74
74
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**.
76
76
77
77

78
78
79
79
**2. Connecting the board**
80
80
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.
83
82
84
83

85
84
86
85
**3. Printing the relative position**
87
86
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.
93
88
89
+
First, include the BMI270_BMM150 library at the top of your sketch:
94
90
95
91
```arduino
96
-
Serial.println();
97
-
Serial.println("Acceleration in G's");
98
-
Serial.println("X\tY\tZ");
92
+
#include "Arduino_BMI270_BMM150.h"
99
93
```
100
94
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:
103
96
104
97
```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)
111
100
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
+
```
113
106
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:
115
108
116
109
```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
+
}
145
118
119
+
Serial.print("Accelerometer sample rate = ");
120
+
Serial.print(IMU.accelerationSampleRate());
121
+
Serial.println("Hz");
122
+
}
146
123
```
147
124
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:
149
126
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);
151
132
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
+
}
152
147
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
+
```
153
160
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.
154
162
155
163
**4. Complete code**
156
164
157
165
If you choose to skip the code-building section, the complete code can be found below:
158
166
159
167
```arduino
160
168
/*
161
-
Arduino BMI270_BMM150 - Simple Accelerometer
169
+
Arduino BMI270_BMM150 - Accelerometer Application
162
170
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.
166
173
167
174
The circuit:
168
-
- Arduino Nano 33 BLE Rev2
175
+
- Arduino Nano 33 BLE Sense Rev2
169
176
170
-
created 10 Jul 2019
171
-
by Riccardo Rizzo
177
+
Created by Pedro Lima
172
178
173
179
This example code is in the public domain.
174
180
*/
175
181
176
182
#include "Arduino_BMI270_BMM150.h"
177
183
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
+
178
187
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;
181
191
182
192
void setup() {
193
+
183
194
Serial.begin(9600);
184
195
while (!Serial);
185
-
Serial.println("Started");
186
196
187
197
if (!IMU.begin()) {
188
198
Serial.println("Failed to initialize IMU!");
@@ -191,47 +201,40 @@ void setup() {
191
201
192
202
Serial.print("Accelerometer sample rate = ");
193
203
Serial.print(IMU.accelerationSampleRate());
194
-
Serial.println("Hz");
204
+
Serial.println("Hz");
195
205
}
196
206
197
207
void loop() {
198
-
float x, y, z;
199
-
200
-
if (IMU.accelerationAvailable()) {
208
+
if (IMU.accelerationAvailable() && millis() - previousMillis >= WAIT_TIME) {
209
+
previousMillis = millis();
201
210
IMU.readAcceleration(x, y, z);
202
211
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");
216
225
}
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
0 commit comments