From ce4055100208a0f0be278ac0d9946e75b247195a Mon Sep 17 00:00:00 2001 From: pedromsousalima <32345730+pedromsousalima@users.noreply.github.com> Date: Mon, 23 Sep 2024 10:44:31 +0200 Subject: [PATCH 1/9] Changed IMU example --- .../tutorials/imu-accelerometer/content.md | 248 ++++++++++-------- 1 file changed, 135 insertions(+), 113 deletions(-) diff --git a/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md b/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md index ddd0bd7552..7c90ceb40f 100644 --- a/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md +++ b/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md @@ -73,117 +73,131 @@ In this example, we will use the accelerometer as a "level" that will provide in **1. Setting up** -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**. +Let's start by opening the Arduino Cloud Editor, click on the **Libraries** tab and search for the **Arduino_BMI270_BMM150** library. Then, click on **Examples**, and open a new sketch. ![Finding the library in the Cloud Editor.](./assets/nano33BLE_01_include_library.png) **2. Connecting the board** -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. - +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. ![Selecting the board.](assets/nano33BLE_01_board_port.png) -**3. Printing the relative position** - -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. +**3. Writing the Code** -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()`. +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. -In the `setup()` we should **remove** the following lines of code: +- **Include the BMI270 library at the top of your sketch:** + ```arduino + #include "Arduino_BMI270_BMM150.h" + ``` -```arduino -Serial.println(); -Serial.println("Acceleration in G's"); -Serial.println("X\tY\tZ"); -``` +- **Initialize variables before the `setup()` function:** -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()`: + ```arduino + #define MINIMUM_TILT 5 // Threshold for tilt detection in degrees + #define WAIT_TIME 500 // How often to run the code (in milliseconds) + float x, y, z; + int angleX = 0; + int angleY = 0; + unsigned long previousMillis = 0; + ``` -```arduino -Serial.print(x); -Serial.print('\t'); -Serial.print(y); -Serial.print('\t'); -Serial.println(z); -``` +- **In the `setup()`, initialize the IMU and start serial communication:** -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. + ```arduino + void setup() { + Serial.begin(9600); + while (!Serial); -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. - -```arduino -if(x > 0.1){ - x = 100*x; - degreesX = map(x, 0, 97, 0, 90); - Serial.print("Tilting up "); - Serial.print(degreesX); - Serial.println(" degrees"); - } - if(x < -0.1){ - x = 100*x; - degreesX = map(x, 0, -100, 0, 90); - Serial.print("Tilting down "); - Serial.print(degreesX); - Serial.println(" degrees"); - } - if(y > 0.1){ - y = 100*y; - degreesY = map(y, 0, 97, 0, 90); - Serial.print("Tilting left "); - Serial.print(degreesY); - Serial.println(" degrees"); - } - if(y < -0.1){ - y = 100*y; - degreesY = map(y, 0, -100, 0, 90); - Serial.print("Tilting right "); - Serial.print(degreesY); - Serial.println(" degrees"); + if (!IMU.begin()) { + Serial.println("Failed to initialize IMU!"); + while (1); } -``` - -Lastly, we `Serial.print` the results value and add a `delay(1000);`. - ->**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. + Serial.print("Accelerometer sample rate = "); + Serial.print(IMU.accelerationSampleRate()); + Serial.println("Hz"); + } + ``` + +- **Write the `loop()` function to read accelerometer data and calculate tilt angles:** + + ```arduino + void loop() { + unsigned long currentMillis = millis(); + + if (IMU.accelerationAvailable() && (currentMillis - previousMillis >= WAIT_TIME)) { + previousMillis = currentMillis; + + IMU.readAcceleration(x, y, z); + + // Calculate tilt angles in degrees + angleX = atan2(x, sqrt(y * y + z * z)) * 180 / PI; + angleY = atan2(y, sqrt(x * x + z * z)) * 180 / PI; + + // Determine the tilting direction based on angleX and angleY + if (angleX > MINIMUM_TILT) { // Tilting up + Serial.print("Tilting up "); + Serial.print(angleX); + Serial.println(" degrees"); + } else if (angleX < -MINIMUM_TILT) { // Tilting down + Serial.print("Tilting down "); + Serial.print(-angleX); + Serial.println(" degrees"); + } + + if (angleY > MINIMUM_TILT) { // Tilting left + Serial.print("Tilting left "); + Serial.print(angleY); + Serial.println(" degrees"); + } else if (angleY < -MINIMUM_TILT) { // Tilting right + Serial.print("Tilting right "); + Serial.print(-angleY); + Serial.println(" degrees"); + } + } + } + ``` +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. +> **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. +**4. Complete Code** -**4. Complete code** +If you choose to skip the code-building section, the complete code can be found below: -If you choose to skip the code building section, the complete code can be found below: ```arduino /* - Arduino LSM9DS1 - Accelerometer Application + Arduino BMI270 - Accelerometer Application This example reads the acceleration values as relative direction and degrees, - from the LSM9DS1 sensor and prints them to the Serial Monitor or Serial Plotter. + from the BMI270 sensor and prints them to the Serial Monitor. The circuit: - Arduino Nano 33 BLE - Created by Riccardo Rizzo - - Modified by Jose García - 27 Nov 2020 + Created by Pedro Lima This example code is in the public domain. */ -#include +#include "Arduino_BMI270_BMM150.h" + +#define MINIMUM_TILT 5 // Threshold for tilt detection in degrees +#define WAIT_TIME 500 // How often to run the code (in milliseconds) float x, y, z; -int degreesX = 0; -int degreesY = 0; +int angleX = 0; +int angleY = 0; +unsigned long previousMillis = 0; void setup() { Serial.begin(9600); while (!Serial); - Serial.println("Started"); if (!IMU.begin()) { Serial.println("Failed to initialize IMU!"); @@ -196,73 +210,81 @@ void setup() { } void loop() { + unsigned long currentMillis = millis(); + + if (IMU.accelerationAvailable() && (currentMillis - previousMillis >= WAIT_TIME)) { + previousMillis = currentMillis; - if (IMU.accelerationAvailable()) { IMU.readAcceleration(x, y, z); - } + // Calculate tilt angles in degrees + angleX = atan2(x, sqrt(y * y + z * z)) * 180 / PI; + angleY = atan2(y, sqrt(x * x + z * z)) * 180 / PI; + + // Determine the tilting direction based on angleX and angleY + if (angleX > MINIMUM_TILT) { // Tilting up + Serial.print("Tilting up "); + Serial.print(angleX); + Serial.println(" degrees"); + } else if (angleX < -MINIMUM_TILT) { // Tilting down + Serial.print("Tilting down "); + Serial.print(-angleX); + Serial.println(" degrees"); + } - if (x > 0.1) { - x = 100 * x; - degreesX = map(x, 0, 97, 0, 90); - Serial.print("Tilting up "); - Serial.print(degreesX); - Serial.println(" degrees"); - } - if (x < -0.1) { - x = 100 * x; - degreesX = map(x, 0, -100, 0, 90); - Serial.print("Tilting down "); - Serial.print(degreesX); - Serial.println(" degrees"); - } - if (y > 0.1) { - y = 100 * y; - degreesY = map(y, 0, 97, 0, 90); - Serial.print("Tilting left "); - Serial.print(degreesY); - Serial.println(" degrees"); - } - if (y < -0.1) { - y = 100 * y; - degreesY = map(y, 0, -100, 0, 90); - Serial.print("Tilting right "); - Serial.print(degreesY); - Serial.println(" degrees"); + if (angleY > MINIMUM_TILT) { // Tilting left + Serial.print("Tilting left "); + Serial.print(angleY); + Serial.println(" degrees"); + } else if (angleY < -MINIMUM_TILT) { // Tilting right + Serial.print("Tilting right "); + Serial.print(-angleY); + Serial.println(" degrees"); + } } - delay(1000); } - ``` - ## Testing It Out -In order to get a correct reading of the board data, before uploading the sketch to the board hold the board in your hand, from the side of the USB port. The board should be facing up and "pointing" away from you. The image below illustrates the board's position and how it works: -![Interacting with the X and Y axes.](./assets/nano33BLE_01_illustration.png) +In order to get correct readings from the board: -Now, you can verify and upload the sketch to the board and open the Monitor from the menu on the left. +1. **Initial Position:** -If you tilt the board upwards, downwards, right or left, you will see the results printing every second according to the direction of your movement! + Before uploading the sketch, place the board on a flat surface with the components facing upwards and the USB port pointing away from you. The image below illustrates the board's position and how it interacts with the X and Y axes: + ![Interacting with the X and Y axes.](./assets/nano33BLE_01_illustration.png) -Here is a screenshot of the sketch returning these values: +2. **Uploading the Sketch:** -![Printing out the "tilt condition" of the board.](./assets/nano33BLE_01_printing_values.png) + - Verify and upload the sketch to the board. + - Open the Serial Monitor from the menu on the left. + +3. **Interacting with the Board:** + - **Tilting Up/Down:** Tilt the board upwards or downwards to see the "Tilting up" or "Tilting down" messages. + - **Tilting Left/Right:** Tilt the board to the left or right to see the "Tilting left" or "Tilting right" messages. +4. **Observing the Output:** + + The Serial Monitor will display the tilt direction and angle every half-second based on your movements. + +Here is a screenshot of the sketch returning these values: + +![Printing out the "tilt condition" of the board.](./assets/nano33BLE_01_printing_values.png) ### Troubleshoot -Sometimes errors occur, if the code is not working there are some common issues we can troubleshoot: -- Missing a bracket or a semicolon. +Sometimes errors occur. If the code is not working, here are some common issues you can troubleshoot: +- Missing a bracket or a semicolon. - Arduino board connected to the wrong port. - Accidental interruption of cable connection. -- The initial position of the board is not as instructed. In this case you can refresh the page and try again. - - +- The initial position of the board is not as instructed. In this case, you can reset the board and try again. +- Ensure the **Arduino_BMI270_BMM150** library is properly installed. ## Conclusion -In this simple tutorial we learned what an IMU sensor module is, how to use the **LSM9DS1** library, and how to use an Arduino Nano 33 BLE to get data. Furthermore, we utilized the 3-axis accelerometer sensor, in order to measure and print out the degrees and relative position of the board. +In this simple tutorial, we learned what an IMU sensor module is, how to use the **Arduino_BMI270_BMM150** library, and how to use an Arduino Nano 33 BLE to get data. Furthermore, we utilized the 3-axis accelerometer sensor to measure and print out the degrees and relative position of the board. + +--- \ No newline at end of file From e344d7e3fd33b43b60afd81f799c0b9d3b13c625 Mon Sep 17 00:00:00 2001 From: pedromsousalima <32345730+pedromsousalima@users.noreply.github.com> Date: Mon, 23 Sep 2024 10:45:50 +0200 Subject: [PATCH 2/9] Linter --- .../boards/nano-33-ble/tutorials/imu-accelerometer/content.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md b/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md index 7c90ceb40f..7b546a4ce8 100644 --- a/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md +++ b/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md @@ -285,6 +285,4 @@ Sometimes errors occur. If the code is not working, here are some common issues ## Conclusion -In this simple tutorial, we learned what an IMU sensor module is, how to use the **Arduino_BMI270_BMM150** library, and how to use an Arduino Nano 33 BLE to get data. Furthermore, we utilized the 3-axis accelerometer sensor to measure and print out the degrees and relative position of the board. - ---- \ No newline at end of file +In this simple tutorial, we learned what an IMU sensor module is, how to use the **Arduino_BMI270_BMM150** library, and how to use an Arduino Nano 33 BLE to get data. Furthermore, we utilized the 3-axis accelerometer sensor to measure and print out the degrees and relative position of the board. \ No newline at end of file From 85b3ac9f2e93ae548092eb8c248bc3fd1658f714 Mon Sep 17 00:00:00 2001 From: pedromsousalima <32345730+pedromsousalima@users.noreply.github.com> Date: Mon, 23 Sep 2024 10:49:15 +0200 Subject: [PATCH 3/9] linter --- .../tutorials/imu-accelerometer/content.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md b/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md index 7b546a4ce8..8d0d6097b0 100644 --- a/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md +++ b/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md @@ -275,14 +275,15 @@ Here is a screenshot of the sketch returning these values: ### Troubleshoot -Sometimes errors occur. If the code is not working, here are some common issues you can troubleshoot: - +Sometimes errors occur, if the code is not working there are some common issues we can troubleshoot: - Missing a bracket or a semicolon. + - Arduino board connected to the wrong port. - Accidental interruption of cable connection. -- The initial position of the board is not as instructed. In this case, you can reset the board and try again. -- Ensure the **Arduino_BMI270_BMM150** library is properly installed. +- The initial position of the board is not as instructed. In this case you can refresh the page and try again. + + ## Conclusion -In this simple tutorial, we learned what an IMU sensor module is, how to use the **Arduino_BMI270_BMM150** library, and how to use an Arduino Nano 33 BLE to get data. Furthermore, we utilized the 3-axis accelerometer sensor to measure and print out the degrees and relative position of the board. \ No newline at end of file +In this simple tutorial we learned what an IMU sensor module is, how to use the **LSM9DS1** library, and how to use an Arduino Nano 33 BLE to get data. Furthermore, we utilized the 3-axis accelerometer sensor, in order to measure and print out the degrees and relative position of the board. \ No newline at end of file From 05edf566f82df66a8f66b4230d7e62112af810ff Mon Sep 17 00:00:00 2001 From: pedromsousalima <32345730+pedromsousalima@users.noreply.github.com> Date: Mon, 23 Sep 2024 11:06:14 +0200 Subject: [PATCH 4/9] removed nested --- .../tutorials/imu-accelerometer/content.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md b/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md index 8d0d6097b0..ca09b3e09a 100644 --- a/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md +++ b/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md @@ -87,13 +87,13 @@ Now, connect the Arduino Nano 33 BLE to the computer and make sure that the Clou 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. -- **Include the BMI270 library at the top of your sketch:** + **Include the BMI270 library at the top of your sketch:** ```arduino #include "Arduino_BMI270_BMM150.h" ``` -- **Initialize variables before the `setup()` function:** + **Initialize variables before the `setup()` function:** ```arduino #define MINIMUM_TILT 5 // Threshold for tilt detection in degrees @@ -105,7 +105,7 @@ Now we will write the code to read the accelerometer data, calculate the tilt an unsigned long previousMillis = 0; ``` -- **In the `setup()`, initialize the IMU and start serial communication:** + **In the `setup()`, initialize the IMU and start serial communication:** ```arduino void setup() { @@ -123,7 +123,7 @@ Now we will write the code to read the accelerometer data, calculate the tilt an } ``` -- **Write the `loop()` function to read accelerometer data and calculate tilt angles:** + **Write the `loop()` function to read accelerometer data and calculate tilt angles:** ```arduino void loop() { @@ -166,7 +166,7 @@ In this code, we use trigonometric functions to calculate the tilt angles from t > **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. -**4. Complete Code** +**Complete Code** If you choose to skip the code-building section, the complete code can be found below: @@ -249,23 +249,23 @@ void loop() { In order to get correct readings from the board: -1. **Initial Position:** + **Initial Position:** Before uploading the sketch, place the board on a flat surface with the components facing upwards and the USB port pointing away from you. The image below illustrates the board's position and how it interacts with the X and Y axes: ![Interacting with the X and Y axes.](./assets/nano33BLE_01_illustration.png) -2. **Uploading the Sketch:** + **Uploading the Sketch:** - Verify and upload the sketch to the board. - Open the Serial Monitor from the menu on the left. -3. **Interacting with the Board:** + **Interacting with the Board:** - **Tilting Up/Down:** Tilt the board upwards or downwards to see the "Tilting up" or "Tilting down" messages. - **Tilting Left/Right:** Tilt the board to the left or right to see the "Tilting left" or "Tilting right" messages. -4. **Observing the Output:** + **Observing the Output:** The Serial Monitor will display the tilt direction and angle every half-second based on your movements. From 83640ba545db42e2de3683d025912034ad656abe Mon Sep 17 00:00:00 2001 From: pedromsousalima <32345730+pedromsousalima@users.noreply.github.com> Date: Wed, 25 Sep 2024 14:05:44 +0200 Subject: [PATCH 5/9] Added both revs --- .../tutorials/imu-accelerometer/content.md | 191 +++++++++--------- .../tutorials/imu-accelerometer/content.md | 169 ++++++++-------- 2 files changed, 177 insertions(+), 183 deletions(-) diff --git a/content/hardware/03.nano/boards/nano-33-ble-rev2/tutorials/imu-accelerometer/content.md b/content/hardware/03.nano/boards/nano-33-ble-rev2/tutorials/imu-accelerometer/content.md index 7bc4c47481..f230320808 100644 --- a/content/hardware/03.nano/boards/nano-33-ble-rev2/tutorials/imu-accelerometer/content.md +++ b/content/hardware/03.nano/boards/nano-33-ble-rev2/tutorials/imu-accelerometer/content.md @@ -72,85 +72,93 @@ In this example, we will use the accelerometer as a "level" that will provide in **1. Setting up** -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**. +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**. ![Finding the library in the Cloud Editor.](./assets/nano33B_02_include_library.png) **2. Connecting the board** -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. - +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. ![Selecting the board.](assets/nano33B_02_board_port.png) **3. Printing the relative position** -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. - -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()`. - -In the `setup()` we should **remove** the following lines of code: +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. +First, include the BMI270_BMM150 library at the top of your sketch: ```arduino -Serial.println(); -Serial.println("Acceleration in G's"); -Serial.println("X\tY\tZ"); +#include "Arduino_BMI270_BMM150.h" ``` -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()`: - +Then, initialize variables before the `setup()` function: ```arduino -Serial.print(x); -Serial.print('\t'); -Serial.print(y); -Serial.print('\t'); -Serial.println(z); -``` +#define MINIMUM_TILT 5 // Threshold for tilt detection in degrees +#define WAIT_TIME 500 // How often to run the code (in milliseconds) -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. +float x, y, z; +int angleX = 0; +int angleY = 0; +unsigned long previousMillis = 0; +``` -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. +In the `setup()`, initialize the IMU and start serial communication: ```arduino -if(x > 0.1){ - x = 100*x; - degreesX = map(x, 0, 97, 0, 90); - Serial.print("Tilting up "); - Serial.print(degreesX); - Serial.println(" degrees"); - } - if(x < -0.1){ - x = 100*x; - degreesX = map(x, 0, -100, 0, 90); - Serial.print("Tilting down "); - Serial.print(degreesX); - Serial.println(" degrees"); - } - if(y > 0.1){ - y = 100*y; - degreesY = map(y, 0, 97, 0, 90); - Serial.print("Tilting left "); - Serial.print(degreesY); - Serial.println(" degrees"); - } - if(y < -0.1){ - y = 100*y; - degreesY = map(y, 0, -100, 0, 90); - Serial.print("Tilting right "); - Serial.print(degreesY); - Serial.println(" degrees"); - } +void setup() { + Serial.begin(9600); + while (!Serial); + + if (!IMU.begin()) { + Serial.println("Failed to initialize IMU!"); + while (1); + } + Serial.print("Accelerometer sample rate = "); + Serial.print(IMU.accelerationSampleRate()); + Serial.println("Hz"); +} ``` -Lastly, we print the values in the serial monitor add a `delay(1000);`. +In the `loop()` function, we will read the accelerometer data and calculate the tilt angles: ->**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. +```arduino +void loop() { + if (IMU.accelerationAvailable() && millis() - previousMillis >= WAIT_TIME) { + previousMillis = millis(); + IMU.readAcceleration(x, y, z); + // Calculate tilt angles in degrees + angleX = atan2(x, sqrt(y * y + z * z)) * 180 / PI; + angleY = atan2(y, sqrt(x * x + z * z)) * 180 / PI; + + // Determine the tilting direction based on angleX and angleY + if (angleX > MINIMUM_TILT) { // Tilting up + Serial.print("Tilting up "); + Serial.print(angleX); + Serial.println(" degrees"); + } else if (angleX < -MINIMUM_TILT) { // Tilting down + Serial.print("Tilting down "); + Serial.print(-angleX); + Serial.println(" degrees"); + } + if (angleY > MINIMUM_TILT) { // Tilting right + Serial.print("Tilting right "); + Serial.print(angleY); + Serial.println(" degrees"); + } else if (angleY < -MINIMUM_TILT) { // Tilting left + Serial.print("Tilting left "); + Serial.print(-angleY); + Serial.println(" degrees"); + } + } +} +``` +> **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. **4. Complete code** @@ -158,31 +166,33 @@ If you choose to skip the code-building section, the complete code can be found ```arduino /* - Arduino BMI270_BMM150 - Simple Accelerometer + Arduino BMI270_BMM150 - Accelerometer Application - This example reads the acceleration values from the BMI270_BMM150 - sensor and continuously prints them to the Serial Monitor - or Serial Plotter. + This example reads the acceleration values as relative direction and degrees, + from the BMI270 sensor and prints them to the Serial Monitor. The circuit: - - Arduino Nano 33 BLE Rev2 + - Arduino Nano 33 BLE Sense Rev2 - created 10 Jul 2019 - by Riccardo Rizzo + Created by Pedro Lima This example code is in the public domain. */ #include "Arduino_BMI270_BMM150.h" +#define MINIMUM_TILT 5 // Threshold for tilt detection in degrees +#define WAIT_TIME 500 // How often to run the code (in milliseconds) + float x, y, z; -int degreesX = 0; -int degreesY = 0; +int angleX = 0; +int angleY = 0; +unsigned long previousMillis = 0; void setup() { + Serial.begin(9600); while (!Serial); - Serial.println("Started"); if (!IMU.begin()) { Serial.println("Failed to initialize IMU!"); @@ -191,47 +201,40 @@ void setup() { Serial.print("Accelerometer sample rate = "); Serial.print(IMU.accelerationSampleRate()); - Serial.println(" Hz"); + Serial.println("Hz"); } void loop() { - float x, y, z; - - if (IMU.accelerationAvailable()) { + if (IMU.accelerationAvailable() && millis() - previousMillis >= WAIT_TIME) { + previousMillis = millis(); IMU.readAcceleration(x, y, z); -if(x > 0.1){ - x = 100*x; - degreesX = map(x, 0, 97, 0, 90); - Serial.print("Tilting up "); - Serial.print(degreesX); - Serial.println(" degrees"); - } - if(x < -0.1){ - x = 100*x; - degreesX = map(x, 0, -100, 0, 90); - Serial.print("Tilting down "); - Serial.print(degreesX); - Serial.println(" degrees"); + // Calculate tilt angles in degrees + angleX = atan2(x, sqrt(y * y + z * z)) * 180 / PI; + angleY = atan2(y, sqrt(x * x + z * z)) * 180 / PI; + + // Determine the tilting direction based on angleX and angleY + if (angleX > MINIMUM_TILT) { // Tilting up + Serial.print("Tilting up "); + Serial.print(angleX); + Serial.println(" degrees"); + } else if (angleX < -MINIMUM_TILT) { // Tilting down + Serial.print("Tilting down "); + Serial.print(-angleX); + Serial.println(" degrees"); } - if(y > 0.1){ - y = 100*y; - degreesY = map(y, 0, 97, 0, 90); - Serial.print("Tilting left "); - Serial.print(degreesY); - Serial.println(" degrees"); - } - if(y < -0.1){ - y = 100*y; - degreesY = map(y, 0, -100, 0, 90); - Serial.print("Tilting right "); - Serial.print(degreesY); - Serial.println(" degrees"); + + if (angleY > MINIMUM_TILT) { // Tilting right + Serial.print("Tilting right "); + Serial.print(angleY); + Serial.println(" degrees"); + } else if (angleY < -MINIMUM_TILT) { // Tilting left + Serial.print("Tilting left "); + Serial.print(-angleY); + Serial.println(" degrees"); } } } - -} ``` @@ -263,4 +266,4 @@ Sometimes errors occur, if the code is not working there are some common issues ## Conclusion -In this simple tutorial, we learned what an IMU sensor module is, how to use the **BMI270_BMM150** library, and how to use an Arduino Nano 33 BLE Rev2 to get data. Furthermore, we utilized the 3-axis accelerometer sensor, in order to measure and print out the degrees and relative position of the board. +In this simple tutorial, we learned what an IMU sensor module is, how to use the **BMI270_BMM150** library, and how to use an Arduino Nano 33 BLE Rev2 to get data. Furthermore, we utilized the 3-axis accelerometer sensor, in order to measure and print out the degrees and relative position of the board. \ No newline at end of file diff --git a/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md b/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md index ca09b3e09a..333db7c673 100644 --- a/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md +++ b/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md @@ -68,99 +68,101 @@ An accelerometer is an electromechanical device used to measure acceleration for 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. - ## Creating the Program **1. Setting up** -Let's start by opening the Arduino Cloud Editor, click on the **Libraries** tab and search for the **Arduino_BMI270_BMM150** library. Then, click on **Examples**, and open a new sketch. + +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**. ![Finding the library in the Cloud Editor.](./assets/nano33BLE_01_include_library.png) **2. Connecting the board** +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. + 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. ![Selecting the board.](assets/nano33BLE_01_board_port.png) -**3. Writing the Code** +### 3. Writing the Code 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. - **Include the BMI270 library at the top of your sketch:** +**Include the LSM9DS1 library at the top of your sketch:** + +```arduino +#include +``` + +**Initialize variables before the `setup()` function:** + +```arduino +#define MINIMUM_TILT 5 // Threshold for tilt detection in degrees +#define WAIT_TIME 500 // How often to run the code (in milliseconds) - ```arduino - #include "Arduino_BMI270_BMM150.h" - ``` +float x, y, z; +int angleX = 0; +int angleY = 0; +unsigned long previousMillis = 0; +``` - **Initialize variables before the `setup()` function:** +**In the `setup()`, initialize the IMU and start serial communication:** - ```arduino - #define MINIMUM_TILT 5 // Threshold for tilt detection in degrees - #define WAIT_TIME 500 // How often to run the code (in milliseconds) +```arduino +void setup() { + Serial.begin(9600); + while (!Serial); + + if (!IMU.begin()) { + Serial.println("Failed to initialize IMU!"); + while (1); + } + + Serial.print("Accelerometer sample rate = "); + Serial.print(IMU.accelerationSampleRate()); + Serial.println(" Hz"); +} +``` + +**Write the `loop()` function to read accelerometer data and calculate tilt angles:** + +```arduino +void loop() { + unsigned long currentMillis = millis(); - float x, y, z; - int angleX = 0; - int angleY = 0; - unsigned long previousMillis = 0; - ``` + if (IMU.accelerationAvailable() && (currentMillis - previousMillis >= WAIT_TIME)) { + previousMillis = currentMillis; - **In the `setup()`, initialize the IMU and start serial communication:** + IMU.readAcceleration(x, y, z); - ```arduino - void setup() { - Serial.begin(9600); - while (!Serial); + // Calculate tilt angles in degrees + angleX = atan2(x, sqrt(y * y + z * z)) * 180 / PI; + angleY = atan2(y, sqrt(x * x + z * z)) * 180 / PI; - if (!IMU.begin()) { - Serial.println("Failed to initialize IMU!"); - while (1); + // Determine the tilting direction based on angleX and angleY + if (angleX > MINIMUM_TILT) { // Tilting up + Serial.print("Tilting up "); + Serial.print(angleX); + Serial.println(" degrees"); + } else if (angleX < -MINIMUM_TILT) { // Tilting down + Serial.print("Tilting down "); + Serial.print(-angleX); + Serial.println(" degrees"); } - Serial.print("Accelerometer sample rate = "); - Serial.print(IMU.accelerationSampleRate()); - Serial.println("Hz"); - } - ``` - - **Write the `loop()` function to read accelerometer data and calculate tilt angles:** - - ```arduino - void loop() { - unsigned long currentMillis = millis(); - - if (IMU.accelerationAvailable() && (currentMillis - previousMillis >= WAIT_TIME)) { - previousMillis = currentMillis; - - IMU.readAcceleration(x, y, z); - - // Calculate tilt angles in degrees - angleX = atan2(x, sqrt(y * y + z * z)) * 180 / PI; - angleY = atan2(y, sqrt(x * x + z * z)) * 180 / PI; - - // Determine the tilting direction based on angleX and angleY - if (angleX > MINIMUM_TILT) { // Tilting up - Serial.print("Tilting up "); - Serial.print(angleX); - Serial.println(" degrees"); - } else if (angleX < -MINIMUM_TILT) { // Tilting down - Serial.print("Tilting down "); - Serial.print(-angleX); - Serial.println(" degrees"); - } - - if (angleY > MINIMUM_TILT) { // Tilting left - Serial.print("Tilting left "); - Serial.print(angleY); - Serial.println(" degrees"); - } else if (angleY < -MINIMUM_TILT) { // Tilting right - Serial.print("Tilting right "); - Serial.print(-angleY); - Serial.println(" degrees"); - } + if (angleY > MINIMUM_TILT) { // Tilting left + Serial.print("Tilting left "); + Serial.print(angleY); + Serial.println(" degrees"); + } else if (angleY < -MINIMUM_TILT) { // Tilting right + Serial.print("Tilting right "); + Serial.print(-angleY); + Serial.println(" degrees"); } } - ``` +} +``` 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. @@ -172,10 +174,10 @@ If you choose to skip the code-building section, the complete code can be found ```arduino /* - Arduino BMI270 - Accelerometer Application + Arduino LSM9DS1 - Accelerometer Application This example reads the acceleration values as relative direction and degrees, - from the BMI270 sensor and prints them to the Serial Monitor. + from the LSM9DS1 sensor and prints them to the Serial Monitor. The circuit: - Arduino Nano 33 BLE @@ -185,7 +187,7 @@ If you choose to skip the code-building section, the complete code can be found This example code is in the public domain. */ -#include "Arduino_BMI270_BMM150.h" +#include #define MINIMUM_TILT 5 // Threshold for tilt detection in degrees #define WAIT_TIME 500 // How often to run the code (in milliseconds) @@ -206,7 +208,7 @@ void setup() { Serial.print("Accelerometer sample rate = "); Serial.print(IMU.accelerationSampleRate()); - Serial.println("Hz"); + Serial.println(" Hz"); } void loop() { @@ -245,34 +247,23 @@ void loop() { } ``` -## Testing It Out -In order to get correct readings from the board: - - **Initial Position:** - - Before uploading the sketch, place the board on a flat surface with the components facing upwards and the USB port pointing away from you. The image below illustrates the board's position and how it interacts with the X and Y axes: - - ![Interacting with the X and Y axes.](./assets/nano33BLE_01_illustration.png) - - **Uploading the Sketch:** - - - Verify and upload the sketch to the board. - - Open the Serial Monitor from the menu on the left. +## Testing It Out +In order to get a correct reading of the board data, before uploading the sketch to the board hold the board in your hand, from the side of the USB port. The board should be facing up and "pointing" away from you. The image below illustrates the board's position and how it works: - **Interacting with the Board:** +![Interacting with the X and Y axes.](./assets/nano33BLE_01_illustration.png) - - **Tilting Up/Down:** Tilt the board upwards or downwards to see the "Tilting up" or "Tilting down" messages. - - **Tilting Left/Right:** Tilt the board to the left or right to see the "Tilting left" or "Tilting right" messages. +Now, you can verify and upload the sketch to the board and open the Monitor from the menu on the left. - **Observing the Output:** +If you tilt the board upwards, downwards, right or left, you will see the results printing every second according to the direction of your movement! - The Serial Monitor will display the tilt direction and angle every half-second based on your movements. Here is a screenshot of the sketch returning these values: ![Printing out the "tilt condition" of the board.](./assets/nano33BLE_01_printing_values.png) + + ### Troubleshoot Sometimes errors occur, if the code is not working there are some common issues we can troubleshoot: @@ -286,4 +277,4 @@ Sometimes errors occur, if the code is not working there are some common issues ## Conclusion -In this simple tutorial we learned what an IMU sensor module is, how to use the **LSM9DS1** library, and how to use an Arduino Nano 33 BLE to get data. Furthermore, we utilized the 3-axis accelerometer sensor, in order to measure and print out the degrees and relative position of the board. \ No newline at end of file +In this simple tutorial we learned what an IMU sensor module is, how to use the **LSM9DS1** library, and how to use an Arduino Nano 33 BLE to get data. Furthermore, we utilized the 3-axis accelerometer sensor, in order to measure and print out the degrees and relative position of the board. From 51b09e01344e72c48a224441bd8d0e981bef80a0 Mon Sep 17 00:00:00 2001 From: pedromsousalima <32345730+pedromsousalima@users.noreply.github.com> Date: Thu, 26 Sep 2024 10:17:32 +0200 Subject: [PATCH 6/9] Update content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Karl Söderby <35461661+karlsoderby@users.noreply.github.com> --- .../boards/nano-33-ble/tutorials/imu-accelerometer/content.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md b/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md index 333db7c673..ac85a5f505 100644 --- a/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md +++ b/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md @@ -89,7 +89,7 @@ Now, connect the Arduino Nano 33 BLE to the computer and make sure that the Clou 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. -**Include the LSM9DS1 library at the top of your sketch:** +Include the LSM9DS1 library at the top of your sketch: ```arduino #include From 6154e4743609e1084c5751aecdb2146135ef751e Mon Sep 17 00:00:00 2001 From: pedromsousalima <32345730+pedromsousalima@users.noreply.github.com> Date: Thu, 26 Sep 2024 10:19:27 +0200 Subject: [PATCH 7/9] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Karl Söderby <35461661+karlsoderby@users.noreply.github.com> --- .../boards/nano-33-ble/tutorials/imu-accelerometer/content.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md b/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md index ac85a5f505..4e2eef0a73 100644 --- a/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md +++ b/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md @@ -95,7 +95,7 @@ Include the LSM9DS1 library at the top of your sketch: #include ``` -**Initialize variables before the `setup()` function:** +Initialize variables before the `setup()` function: ```arduino #define MINIMUM_TILT 5 // Threshold for tilt detection in degrees @@ -107,7 +107,7 @@ int angleY = 0; unsigned long previousMillis = 0; ``` -**In the `setup()`, initialize the IMU and start serial communication:** +In the `setup()`, initialize the IMU and start serial communication: ```arduino void setup() { From eb1198496625d15b78047999c6867526ca856233 Mon Sep 17 00:00:00 2001 From: pedromsousalima <32345730+pedromsousalima@users.noreply.github.com> Date: Thu, 26 Sep 2024 16:29:23 +0200 Subject: [PATCH 8/9] Fix RUN check --- .../nano-33-ble-rev2/tutorials/imu-accelerometer/content.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/hardware/03.nano/boards/nano-33-ble-rev2/tutorials/imu-accelerometer/content.md b/content/hardware/03.nano/boards/nano-33-ble-rev2/tutorials/imu-accelerometer/content.md index f230320808..a34db94f6e 100644 --- a/content/hardware/03.nano/boards/nano-33-ble-rev2/tutorials/imu-accelerometer/content.md +++ b/content/hardware/03.nano/boards/nano-33-ble-rev2/tutorials/imu-accelerometer/content.md @@ -72,7 +72,7 @@ In this example, we will use the accelerometer as a "level" that will provide in **1. Setting up** -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**. +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**. ![Finding the library in the Cloud Editor.](./assets/nano33B_02_include_library.png) @@ -266,4 +266,4 @@ Sometimes errors occur, if the code is not working there are some common issues ## Conclusion -In this simple tutorial, we learned what an IMU sensor module is, how to use the **BMI270_BMM150** library, and how to use an Arduino Nano 33 BLE Rev2 to get data. Furthermore, we utilized the 3-axis accelerometer sensor, in order to measure and print out the degrees and relative position of the board. \ No newline at end of file +In this simple tutorial, we learned what an IMU sensor module is, how to use the **BMI270_BMM150** library, and how to use an Arduino Nano 33 BLE Rev2 to get data. Furthermore, we utilized the 3-axis accelerometer sensor, in order to measure and print out the degrees and relative position of the board. From c8e0d2f6390bb0a7561669e0393de7f671c0dd35 Mon Sep 17 00:00:00 2001 From: pedromsousalima <32345730+pedromsousalima@users.noreply.github.com> Date: Thu, 26 Sep 2024 16:30:22 +0200 Subject: [PATCH 9/9] Fix RUN check (part2) --- .../boards/nano-33-ble/tutorials/imu-accelerometer/content.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md b/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md index 4e2eef0a73..75fe6af594 100644 --- a/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md +++ b/content/hardware/03.nano/boards/nano-33-ble/tutorials/imu-accelerometer/content.md @@ -73,7 +73,7 @@ In this example, we will use the accelerometer as a "level" that will provide in **1. Setting up** -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**. +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**. ![Finding the library in the Cloud Editor.](./assets/nano33BLE_01_include_library.png)