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
+93-96Lines changed: 93 additions & 96 deletions
Original file line number
Diff line number
Diff line change
@@ -72,127 +72,117 @@ 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 **Arduino_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 **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 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.
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
+
82
83
83
84

84
85
85
86
**3. Printing the relative position**
86
87
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.
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:
88
93
89
-
First, include the BMI270_BMM150 library at the top of your sketch:
90
94
91
95
```arduino
92
-
#include "Arduino_BMI270_BMM150.h"
96
+
Serial.println();
97
+
Serial.println("Acceleration in G's");
98
+
Serial.println("X\tY\tZ");
93
99
```
94
100
95
-
Then, initialize variables before the `setup()` function:
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()`:
96
102
97
-
```arduino
98
-
#define MINIMUM_TILT 5 // Threshold for tilt detection in degrees
99
-
#define WAIT_TIME 500 // How often to run the code (in milliseconds)
100
103
101
-
float x, y, z;
102
-
int angleX = 0;
103
-
int angleY = 0;
104
-
unsigned long previousMillis = 0;
104
+
```arduino
105
+
Serial.print(x);
106
+
Serial.print('\t');
107
+
Serial.print(y);
108
+
Serial.print('\t');
109
+
Serial.println(z);
105
110
```
106
111
107
-
In the `setup()`, initialize the IMU and start serial communication:
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.
108
113
109
-
```arduino
110
-
void setup() {
111
-
Serial.begin(9600);
112
-
while (!Serial);
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.
113
115
114
-
if (!IMU.begin()) {
115
-
Serial.println("Failed to initialize IMU!");
116
-
while (1);
117
-
}
116
+
```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
+
}
118
145
119
-
Serial.print("Accelerometer sample rate = ");
120
-
Serial.print(IMU.accelerationSampleRate());
121
-
Serial.println("Hz");
122
-
}
123
146
```
124
147
125
-
In the `loop()` function, we will read the accelerometer data and calculate the tilt angles:
148
+
Lastly, we print the values in the serial monitor add a `delay(1000);`.
126
149
127
-
```arduino
128
-
void loop() {
129
-
if (IMU.accelerationAvailable() && millis() - previousMillis >= WAIT_TIME) {
130
-
previousMillis = millis();
131
-
IMU.readAcceleration(x, y, z);
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.
132
151
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
-
}
147
152
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
-
```
160
153
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.
162
154
163
155
**4. Complete code**
164
156
165
157
If you choose to skip the code-building section, the complete code can be found below:
166
158
167
159
```arduino
168
160
/*
169
-
Arduino BMI270_BMM150 - Accelerometer Application
161
+
Arduino BMI270_BMM150 - Simple Accelerometer
170
162
171
-
This example reads the acceleration values as relative direction and degrees,
172
-
from the BMI270 sensor and prints them to the Serial Monitor.
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.
173
166
174
167
The circuit:
175
-
- Arduino Nano 33 BLE Sense Rev2
168
+
- Arduino Nano 33 BLE Rev2
176
169
177
-
Created by Pedro Lima
170
+
created 10 Jul 2019
171
+
by Riccardo Rizzo
178
172
179
173
This example code is in the public domain.
180
174
*/
181
175
182
176
#include "Arduino_BMI270_BMM150.h"
183
177
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
-
187
178
float x, y, z;
188
-
int angleX = 0;
189
-
int angleY = 0;
190
-
unsigned long previousMillis = 0;
179
+
int degreesX = 0;
180
+
int degreesY = 0;
191
181
192
182
void setup() {
193
-
194
183
Serial.begin(9600);
195
184
while (!Serial);
185
+
Serial.println("Started");
196
186
197
187
if (!IMU.begin()) {
198
188
Serial.println("Failed to initialize IMU!");
@@ -201,40 +191,47 @@ void setup() {
201
191
202
192
Serial.print("Accelerometer sample rate = ");
203
193
Serial.print(IMU.accelerationSampleRate());
204
-
Serial.println("Hz");
194
+
Serial.println("Hz");
205
195
}
206
196
207
197
void loop() {
208
-
if (IMU.accelerationAvailable() && millis() - previousMillis >= WAIT_TIME) {
209
-
previousMillis = millis();
198
+
float x, y, z;
199
+
200
+
if (IMU.accelerationAvailable()) {
210
201
IMU.readAcceleration(x, y, z);
211
202
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");
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");
225
209
}
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