Skip to content

Commit 52caf7e

Browse files
Revert "Changed tutorials for both revs"
This reverts commit 42d2fd7.
1 parent ef1d0f6 commit 52caf7e

File tree

2 files changed

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

2 files changed

+189
-202
lines changed

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

Lines changed: 93 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -72,127 +72,117 @@ 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 **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**.
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 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+
8283

8384
![Selecting the board.](assets/nano33B_02_board_port.png)
8485

8586
**3. Printing the relative position**
8687

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

89-
First, include the BMI270_BMM150 library at the top of your sketch:
9094

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

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()`:
96102

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)
100103

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);
105110
```
106111

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

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

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+
}
118145
119-
Serial.print("Accelerometer sample rate = ");
120-
Serial.print(IMU.accelerationSampleRate());
121-
Serial.println("Hz");
122-
}
123146
```
124147

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);`.
126149

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.
132151
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-
}
147152

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-
```
160153

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

163155
**4. Complete code**
164156

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

167159
```arduino
168160
/*
169-
Arduino BMI270_BMM150 - Accelerometer Application
161+
Arduino BMI270_BMM150 - Simple Accelerometer
170162
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.
173166
174167
The circuit:
175-
- Arduino Nano 33 BLE Sense Rev2
168+
- Arduino Nano 33 BLE Rev2
176169
177-
Created by Pedro Lima
170+
created 10 Jul 2019
171+
by Riccardo Rizzo
178172
179173
This example code is in the public domain.
180174
*/
181175
182176
#include "Arduino_BMI270_BMM150.h"
183177
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-
187178
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;
191181
192182
void setup() {
193-
194183
Serial.begin(9600);
195184
while (!Serial);
185+
Serial.println("Started");
196186
197187
if (!IMU.begin()) {
198188
Serial.println("Failed to initialize IMU!");
@@ -201,40 +191,47 @@ void setup() {
201191
202192
Serial.print("Accelerometer sample rate = ");
203193
Serial.print(IMU.accelerationSampleRate());
204-
Serial.println("Hz");
194+
Serial.println(" Hz");
205195
}
206196
207197
void loop() {
208-
if (IMU.accelerationAvailable() && millis() - previousMillis >= WAIT_TIME) {
209-
previousMillis = millis();
198+
float x, y, z;
199+
200+
if (IMU.accelerationAvailable()) {
210201
IMU.readAcceleration(x, y, z);
211202
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");
225209
}
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");
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");
216+
}
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");
235230
}
236231
}
237232
}
233+
234+
}
238235
```
239236

240237

0 commit comments

Comments
 (0)