@@ -875,46 +875,12 @@ The onboard magnetometer of the Nicla Voice can be used to determine the board's
875
875
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).
876
876
877
877
` ` ` 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
-
887
878
# include "NDP.h"
888
879
889
880
// 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
918
884
919
885
/**
920
886
Blinks onboard red LED periodically every 200 ms.
@@ -931,19 +897,21 @@ void ledRedBlink() {
931
897
}
932
898
933
899
// 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)
941
909
942
910
void setup () {
943
911
int status;
944
912
uint8_t __attribute__(( aligned(4 )) ) sensor_data[SENSOR_DATA_LENGTH];
945
913
946
- // Initiate Serial communication for debugging and monitoring.
914
+ // Initiate Serial communication for debugging and monitoring.
947
915
Serial.begin(115200);
948
916
949
917
// Initialize Nicla Voice board' s system functions.
@@ -955,25 +923,21 @@ void setup() {
955
923
956
924
// Set up error and event handlers:
957
925
// - 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
+
960
927
NDP.onError(ledRedBlink);
961
- NDP.onMatch(ledBlueOn);
962
- NDP.onEvent(ledGreenOn);
963
928
964
929
// NDP processor initialization with firmwares and models.
965
930
Serial.println("- NDP processor initialization...");
966
931
NDP.begin("mcu_fw_120_v91.synpkg");
967
932
NDP.load("dsp_firmware_v91.synpkg");
968
- NDP.load("ei_model.synpkg");
969
933
Serial.println("- NDP processor initialization done!");
970
934
971
935
// Enable power control bit
972
936
status = NDP.sensorBMM150Write(0x4B, 0x01);
973
937
CHECK_STATUS(status);
974
938
delay(20);
975
939
976
- // Read power control byte
940
+ // Read power control byte
977
941
status = NDP.sensorBMM150Read(0x4B, 1, sensor_data);
978
942
CHECK_STATUS(status);
979
943
@@ -988,10 +952,10 @@ void setup() {
988
952
989
953
void loop() {
990
954
// 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];
992
956
993
957
// 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;
995
959
float x_mag, y_mag, z_mag;
996
960
997
961
// Read operation status variable.
@@ -1008,6 +972,7 @@ void loop() {
1008
972
// The sensor data is read into an array of bytes (8-bit values). Each measurement from the magnetometer consists
1009
973
// of two bytes, hence the bit shifting and bitwise OR operations to combine these two bytes into one 16-bit value.
1010
974
// Data for each axis (X, Y, Z) of the magnetometer is extracted from the array.
975
+
1011
976
x_mag_raw = (0x0000 | sensor_data[0] >> 3 | sensor_data[1] << 5) ;
1012
977
y_mag_raw = (0x0000 | sensor_data[2] >> 3 | sensor_data[3] << 5);
1013
978
z_mag_raw = (0x0000 | sensor_data[4] >> 1 | sensor_data[5] << 7);
@@ -1026,7 +991,7 @@ void loop() {
1026
991
Serial.print("hall_raw:");
1027
992
Serial.println(hall_raw);
1028
993
1029
- delay(1000 );
994
+ delay(100 );
1030
995
}
1031
996
` ` `
1032
997
0 commit comments