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
@@ -68,11 +68,11 @@ An accelerometer is an electromechanical device used to measure acceleration for
68
68
In this example, we will use the accelerometer as a "level" that will provide information about the position of the board. With this application we will be able to read what the relative position of the board is as well as the degrees, by tilting the board up, down, left or right.
69
69
70
70
71
+
71
72
## Creating the Program
72
73
73
74
**1. Setting up**
74
75
75
-
76
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**.
77
77
78
78

@@ -81,125 +81,109 @@ Let's start by opening the Arduino Cloud Editor, click on the **Libraries** tab
81
81
82
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
83
84
-
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.
85
84
86
85

87
86
88
-
### 3. Writing the Code
87
+
**3. Printing the relative position**
89
88
90
-
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.
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.
91
90
92
-
**Include the LSM9DS1 library at the top of your sketch:**
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()`.
93
92
94
-
```arduino
95
-
#include <Arduino_LSM9DS1.h>
96
-
```
93
+
In the `setup()` we should **remove** the following lines of code:
97
94
98
-
**Initialize variables before the `setup()` function:**
99
95
100
96
```arduino
101
-
#define MINIMUM_TILT 5 // Threshold for tilt detection in degrees
102
-
#define WAIT_TIME 500 // How often to run the code (in milliseconds)
103
-
104
-
float x, y, z;
105
-
int angleX = 0;
106
-
int angleY = 0;
107
-
unsigned long previousMillis = 0;
97
+
Serial.println();
98
+
Serial.println("Acceleration in G's");
99
+
Serial.println("X\tY\tZ");
108
100
```
109
101
110
-
**In the `setup()`, initialize the IMU and start serial communication:**
111
-
112
-
```arduino
113
-
void setup() {
114
-
Serial.begin(9600);
115
-
while (!Serial);
116
-
117
-
if (!IMU.begin()) {
118
-
Serial.println("Failed to initialize IMU!");
119
-
while (1);
120
-
}
121
-
122
-
Serial.print("Accelerometer sample rate = ");
123
-
Serial.print(IMU.accelerationSampleRate());
124
-
Serial.println(" Hz");
125
-
}
126
-
```
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()`:
127
103
128
-
**Write the `loop()` function to read accelerometer data and calculate tilt angles:**
129
104
130
105
```arduino
131
-
void loop() {
132
-
unsigned long currentMillis = millis();
106
+
Serial.print(x);
107
+
Serial.print('\t');
108
+
Serial.print(y);
109
+
Serial.print('\t');
110
+
Serial.println(z);
111
+
```
133
112
134
-
if (IMU.accelerationAvailable() && (currentMillis - previousMillis >= WAIT_TIME)) {
135
-
previousMillis = currentMillis;
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.
136
114
137
-
IMU.readAcceleration(x, y, z);
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.
138
116
139
-
// Calculate tilt angles in degrees
140
-
angleX = atan2(x, sqrt(y * y + z * z)) * 180 / PI;
141
-
angleY = atan2(y, sqrt(x * x + z * z)) * 180 / PI;
142
-
143
-
// Determine the tilting direction based on angleX and angleY
144
-
if (angleX > MINIMUM_TILT) { // Tilting up
145
-
Serial.print("Tilting up ");
146
-
Serial.print(angleX);
147
-
Serial.println(" degrees");
148
-
} else if (angleX < -MINIMUM_TILT) { // Tilting down
149
-
Serial.print("Tilting down ");
150
-
Serial.print(-angleX);
151
-
Serial.println(" degrees");
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");
152
124
}
153
-
154
-
if (angleY > MINIMUM_TILT) { // Tilting left
155
-
Serial.print("Tilting left ");
156
-
Serial.print(angleY);
157
-
Serial.println(" degrees");
158
-
} else if (angleY < -MINIMUM_TILT) { // Tilting right
159
-
Serial.print("Tilting right ");
160
-
Serial.print(-angleY);
161
-
Serial.println(" degrees");
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");
162
131
}
163
-
}
164
-
}
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");
145
+
}
146
+
165
147
```
166
148
167
-
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.
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.
168
152
169
-
> **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.
170
153
171
-
**Complete Code**
172
154
173
-
If you choose to skip the code-building section, the complete code can be found below:
174
155
156
+
**4. Complete code**
157
+
158
+
If you choose to skip the code building section, the complete code can be found below:
175
159
```arduino
176
160
/*
177
161
Arduino LSM9DS1 - Accelerometer Application
178
162
179
163
This example reads the acceleration values as relative direction and degrees,
180
-
from the LSM9DS1 sensor and prints them to the Serial Monitor.
164
+
from the LSM9DS1 sensor and prints them to the Serial Monitor or Serial Plotter.
181
165
182
166
The circuit:
183
167
- Arduino Nano 33 BLE
184
168
185
-
Created by Pedro Lima
169
+
Created by Riccardo Rizzo
170
+
171
+
Modified by Jose García
172
+
27 Nov 2020
186
173
187
174
This example code is in the public domain.
188
175
*/
189
176
190
177
#include <Arduino_LSM9DS1.h>
191
178
192
-
#define MINIMUM_TILT 5 // Threshold for tilt detection in degrees
193
-
#define WAIT_TIME 500 // How often to run the code (in milliseconds)
194
-
195
179
float x, y, z;
196
-
int angleX = 0;
197
-
int angleY = 0;
198
-
unsigned long previousMillis = 0;
180
+
int degreesX = 0;
181
+
int degreesY = 0;
199
182
200
183
void setup() {
201
184
Serial.begin(9600);
202
185
while (!Serial);
186
+
Serial.println("Started");
203
187
204
188
if (!IMU.begin()) {
205
189
Serial.println("Failed to initialize IMU!");
@@ -208,43 +192,47 @@ void setup() {
208
192
209
193
Serial.print("Accelerometer sample rate = ");
210
194
Serial.print(IMU.accelerationSampleRate());
211
-
Serial.println("Hz");
195
+
Serial.println("Hz");
212
196
}
213
197
214
198
void loop() {
215
-
unsigned long currentMillis = millis();
216
-
217
-
if (IMU.accelerationAvailable() && (currentMillis - previousMillis >= WAIT_TIME)) {
218
-
previousMillis = currentMillis;
219
199
200
+
if (IMU.accelerationAvailable()) {
220
201
IMU.readAcceleration(x, y, z);
221
202
222
-
// Calculate tilt angles in degrees
223
-
angleX = atan2(x, sqrt(y * y + z * z)) * 180 / PI;
224
-
angleY = atan2(y, sqrt(x * x + z * z)) * 180 / PI;
225
-
226
-
// Determine the tilting direction based on angleX and angleY
227
-
if (angleX > MINIMUM_TILT) { // Tilting up
228
-
Serial.print("Tilting up ");
229
-
Serial.print(angleX);
230
-
Serial.println(" degrees");
231
-
} else if (angleX < -MINIMUM_TILT) { // Tilting down
232
-
Serial.print("Tilting down ");
233
-
Serial.print(-angleX);
234
-
Serial.println(" degrees");
235
-
}
203
+
}
236
204
237
-
if (angleY > MINIMUM_TILT) { // Tilting left
238
-
Serial.print("Tilting left ");
239
-
Serial.print(angleY);
240
-
Serial.println(" degrees");
241
-
} else if (angleY < -MINIMUM_TILT) { // Tilting right
0 commit comments