Skip to content

Commit 7f48445

Browse files
committed
magnetometer code ready
1 parent 491897f commit 7f48445

File tree

1 file changed

+19
-54
lines changed
  • content/hardware/06.nicla/boards/nicla-voice/tutorials/user-manual

1 file changed

+19
-54
lines changed

content/hardware/06.nicla/boards/nicla-voice/tutorials/user-manual/content.md

+19-54
Original file line numberDiff line numberDiff line change
@@ -875,46 +875,12 @@ The onboard magnetometer of the Nicla Voice can be used to determine the board's
875875
The example code below shows how to get raw magnetic field and Hall resistance data from the onboard magnetometer and stream it to the Arduino IDE Serial Monitor and Serial Plotter. You can download the example sketch [here](assets/nv_mag_test.rar).
876876
877877
```arduino
878-
/**
879-
Nicla Voice magnetometer test sketch
880-
Name: nv_mag_test.ino
881-
Purpose: Sketch tests onboard magnetometer (BMM150)
882-
883-
@author Arduino PRO Content Team
884-
@version 1.0 30/05/23
885-
*/
886-
887878
#include "NDP.h"
888879
889880
// Named constants
890-
#define READ_START_ADDRESS 0x42
891-
#define READ_BYTE_COUNT 8
892-
#define SENSOR_DATA_LENGTH 16
893-
894-
/**
895-
Turns on and off the onboard blue LED.
896-
897-
@param label to be printed on the Serial Monitor.
898-
*/
899-
void ledBlueOn(char* label) {
900-
nicla::leds.begin();
901-
nicla::leds.setColor(blue);
902-
delay(200);
903-
nicla::leds.setColor(off);
904-
Serial.println(label);
905-
nicla::leds.end();
906-
}
907-
908-
/**
909-
Turns on and off the onboard green LED.
910-
*/
911-
void ledGreenOn() {
912-
nicla::leds.begin();
913-
nicla::leds.setColor(green);
914-
delay(200);
915-
nicla::leds.setColor(off);
916-
nicla::leds.end();
917-
}
881+
#define READ_START_ADDRESS 0x42
882+
#define READ_BYTE_COUNT 8
883+
#define SENSOR_DATA_LENGTH 16
918884
919885
/**
920886
Blinks onboard red LED periodically every 200 ms.
@@ -931,19 +897,21 @@ void ledRedBlink() {
931897
}
932898
933899
// Macro definition used for checking the sensor status, print error if SPI access fails.
934-
#define CHECK_STATUS(s) do {\
935-
if (s) {\
936-
Serial.print("SPI access error in line ");\
937-
Serial.println(__LINE__);\
938-
for(;;);\
939-
}\
940-
} while (0)
900+
#define CHECK_STATUS(s) \
901+
do { \
902+
if (s) { \
903+
Serial.print("SPI access error in line "); \
904+
Serial.println(__LINE__); \
905+
for (;;) \
906+
; \
907+
} \
908+
} while (0)
941909
942910
void setup() {
943911
int status;
944912
uint8_t __attribute__((aligned(4))) sensor_data[SENSOR_DATA_LENGTH];
945913
946-
// Initiate Serial communication for debugging and monitoring.
914+
// Initiate Serial communication for debugging and monitoring.
947915
Serial.begin(115200);
948916
949917
// Initialize Nicla Voice board's system functions.
@@ -955,25 +923,21 @@ void setup() {
955923
956924
// Set up error and event handlers:
957925
// - In case of error, the red LED will blink.
958-
// - In case of match, the blue LED will turn on.
959-
// - In case of any event, the green LED will turn on.
926+
960927
NDP.onError(ledRedBlink);
961-
NDP.onMatch(ledBlueOn);
962-
NDP.onEvent(ledGreenOn);
963928
964929
// NDP processor initialization with firmwares and models.
965930
Serial.println("- NDP processor initialization...");
966931
NDP.begin("mcu_fw_120_v91.synpkg");
967932
NDP.load("dsp_firmware_v91.synpkg");
968-
NDP.load("ei_model.synpkg");
969933
Serial.println("- NDP processor initialization done!");
970934
971935
// Enable power control bit
972936
status = NDP.sensorBMM150Write(0x4B, 0x01);
973937
CHECK_STATUS(status);
974938
delay(20);
975939
976-
// Read power control byte
940+
// Read power control byte
977941
status = NDP.sensorBMM150Read(0x4B, 1, sensor_data);
978942
CHECK_STATUS(status);
979943
@@ -988,10 +952,10 @@ void setup() {
988952
989953
void loop() {
990954
// Allocate space for raw sensor data.
991-
uint8_t __attribute__((aligned(4))) sensor_data[SENSOR_DATA_LENGTH];
955+
uint8_t __attribute__((aligned(4))) sensor_data[SENSOR_DATA_LENGTH];
992956
993957
// Declare variables for magnetometer data.
994-
int16_t x_mag_raw, y_mag_raw, z_mag_raw, hall_raw ;
958+
int16_t x_mag_raw, y_mag_raw, z_mag_raw, hall_raw;
995959
float x_mag, y_mag, z_mag;
996960
997961
// Read operation status variable.
@@ -1008,6 +972,7 @@ void loop() {
1008972
// The sensor data is read into an array of bytes (8-bit values). Each measurement from the magnetometer consists
1009973
// of two bytes, hence the bit shifting and bitwise OR operations to combine these two bytes into one 16-bit value.
1010974
// Data for each axis (X, Y, Z) of the magnetometer is extracted from the array.
975+
1011976
x_mag_raw = (0x0000 | sensor_data[0] >> 3 | sensor_data[1] << 5);
1012977
y_mag_raw = (0x0000 | sensor_data[2] >> 3 | sensor_data[3] << 5);
1013978
z_mag_raw = (0x0000 | sensor_data[4] >> 1 | sensor_data[5] << 7);
@@ -1026,7 +991,7 @@ void loop() {
1026991
Serial.print("hall_raw:");
1027992
Serial.println(hall_raw);
1028993
1029-
delay(1000);
994+
delay(100);
1030995
}
1031996
```
1032997

0 commit comments

Comments
 (0)