Yea, So just to put a BOW on this here is the code for the Grab me Demo… This does NOT use interrupts. Due to the lack of “I/O pins” the IMU does NOT support it. (sucks)
so I’m sticking with the Nrf52840 Sense… How would you do Fall or AWT without interrupts
polling is way too inefficient in my world. this is a DOWNgrade IMO. The logic behind the designers choice is for power efficiency they choose to use the pins instead to power On/Off the device…LOL (so chop off the leg to stop the bleeding?)
Not my first choice… I would rather put it to sleep to save power and use the INT to wake as I have on the other. You can’t do that with this !
anyway, Soapbox = OFF
So here is the code & compiler output, serial output too and the Demo Video ENjoy…
The dev board has that sweet SEEED GREEN SOCKET (printed in PLA+ & up on Thingiverse)
GL PJ
Ps. Filmed on a potato
Code:
/*
uf2 flash Basic IMU test. Based on the Basic IMU in the Examples
Will test if IMU is preasent and detects movement.
sennds Axcellerometer readings out to Serial port
the Sensativity can be adjusted via "accelerationThreshold " value.
1.25 is defualt "nAt" super sensative. Beeps the buzzer when it's over the limit.
Press the User Button on the Expansion board to lower the Sensativity to movement.
prints the new value 3.25 way less sensative.
- Xiao Nrf52840 Sense board.
- Xiao expansion baord.
-Add Movement Accel code.
Prepared by PjGlasso
built with BSP 2.9.2
3/25/24
added test for MG24 IMU (power pins)
*/
#include <LSM6DS3.h>
#include <Wire.h>
#include <U8x8lib.h>
//Create a instance of class LSM6DS3
LSM6DS3 myIMU(I2C_MODE, 0x6A); //I2C device address 0x6A // IMU
U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* clock=*/ PIN_WIRE_SCL, /* data=*/ PIN_WIRE_SDA, /* reset=*/ U8X8_PIN_NONE);
// OLEDs without Reset of the Display
float aX, aY, aZ, gX, gY, gZ;
const float accelerationThreshold = 1.25; // threshold of significant in G's
float nAt = accelerationThreshold;
const int numSamples = 59;
int samplesRead = numSamples;
const int buttonPin = D1; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
int BuzzerPin = PC3; //A3; //A3 , P0.29 PIN 4
uint8_t interruptCount = 0; // Amount of received interrupts
uint8_t prevInterruptCount = 0; // Interrupt Counter from last loop
int buzzer = BuzzerPin;
int Onthemove = 0; // Not Moving Status
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
delay(2000); //relax...
Serial.println();
Serial.println("Xiao Expansion board and MCU check & Test compiled on " __DATE__ " at " __TIME__);
Serial.println("Processor came out of reset.\n");
pinMode(PD5,OUTPUT); // Turn ON IMU
digitalWrite(PD5,HIGH); //Turn ON IMU
u8x8.begin();
u8x8.setFlipMode(3); // set number from 1 to 3, the screen word will rotary 180
pinMode(LED_BUILTIN, OUTPUT);// initialize the LED pin as an output:
pinMode(buttonPin, INPUT_PULLUP);// initialize the pushbutton pin as an input:
pinMode(BuzzerPin, OUTPUT);
//pinMode(LEDR, OUTPUT);
// pinMode(LEDG, OUTPUT);
//pinMode(LEDB, OUTPUT);
//setLedRGB(false, false, true); // set blue led
//while (!Serial);
//Call .begin() to configure the IMUs
if (myIMU.begin() != 0) {
Serial.println("Device error");
//setLedRGB(false, true, false); // Red
} else {
Serial.println("IMU GOOD");
//getOrientation();
Serial.print("Temperature: ");
Serial.print((String) + myIMU.readTempF() + "\u00b0");
Serial.println();
}
u8x8.setFont(u8x8_font_8x13B_1x2_r);
u8x8.clearDisplay();
u8x8.setCursor(4, 0);
u8x8.print("Grab Me ");
u8x8.setCursor(5, 3);
u8x8.print("&");
u8x8.setCursor(1, 6);
u8x8.print("Activity-");
u8x8.print("Demo");
}
void loop() {
static uint8_t lastPinState = 1;
uint8_t pinState = digitalRead(1);
delay(250);
if(!pinState && lastPinState){
onButton();
}
lastPinState = pinState;
//while(Serial.available()) Serial.write(Serial.read());
// wait for significant motion
while (samplesRead == numSamples) {
// read the acceleration data
aX = myIMU.readFloatAccelX();
aY = myIMU.readFloatAccelY();
aZ = myIMU.readFloatAccelZ();
// sum up the absolutes
float aSum = fabs(aX) + fabs(aY) + fabs(aZ);
// check if it's above the threshold
if (aSum >= nAt) { // checks threshold value.
// reset the sample read count
samplesRead = 0;
//setLedRGB(true,false, false); // set RED only
Onthemove = 1;
tone (buzzer, 800);
delay (800);
noTone(buzzer);
delay (200);
u8x8.clearDisplay();
u8x8.setCursor(5, 3);
u8x8.print("ON The Move");
break;
}
//setLedRGB(false, true, false); // set green only
noTone(buzzer);
}
// check if the all the required samples have been read since
// the last time the significant motion was detected
while (samplesRead < numSamples) {
// check if both new acceleration and gyroscope data is
// available
// read the acceleration and gyroscope data
samplesRead++;
// print the data in CSV format
Serial.print(myIMU.readFloatAccelX(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatAccelY(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatAccelZ(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatGyroX(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatGyroY(), 3);
Serial.print(',');
Serial.print(myIMU.readFloatGyroZ(), 3);
Serial.println();
if (samplesRead == numSamples) {
// add an empty line if it's the last sample
Serial.println();
if (Onthemove != 0 ){
u8x8.clearDisplay();
u8x8.setCursor(4, 0);
u8x8.print("Grab Me ");
u8x8.setCursor(5, 3);
u8x8.print("AGAIN");
}
}
}
}
// -------------------- Utilities ------------------------- //
void onButton(){
String out = "Button pushed ";
nAt = 3.25; // increase threshold of significant in G's needed to alarm
out += String(accelerationThreshold);
Serial.println(out);
Serial.println(nAt);
//ble.begin(out);
}
/*/void setLedRGB(bool red, bool green, bool blue) {
if (!blue) { digitalWrite(LEDB, HIGH); } else { digitalWrite(LEDB, LOW); }
if (!green) { digitalWrite(LEDG, HIGH); } else { digitalWrite(LEDG, LOW); }
if (!red) { digitalWrite(LEDR, HIGH); } else { digitalWrite(LEDR, LOW); }
} */
Compiler:
FQBN: SiliconLabs:silabs:xiao_mg24
Using board 'xiao_mg24' from platform in folder: C:\Users\Dude\AppData\Local\Arduino15\packages\SiliconLabs\hardware\silabs\2.2.0
Using core 'silabs' from platform in folder: C:\Users\Dude\AppData\Local\Arduino15\packages\SiliconLabs\hardware\silabs\2.2.0
Detecting libraries used...
edit for brevity...
...
Using library Seeed Arduino LSM6DS3 at version 2.0.3 in folder: D:\Arduino_projects\libraries\Seeed_Arduino_LSM6DS3
Using library Wire at version 2.2.0 in folder: C:\Users\Dude\AppData\Local\Arduino15\packages\SiliconLabs\hardware\silabs\2.2.0\libraries\Wire
Using library U8g2 at version 2.35.30 in folder: D:\Arduino_projects\libraries\U8g2
Using library SPI at version 2.2.0 in folder: C:\Users\Dude\AppData\Local\Arduino15\packages\SiliconLabs\hardware\silabs\2.2.0\libraries\SPI
"C:\\Users\\Dude\\AppData\\Local\\Arduino15\\packages\\SiliconLabs\\tools\\gcc-arm-none-eabi\\12.2.rel1/bin/arm-none-eabi-size" -A "C:\\Users\\Dude\\AppData\\Local\\arduino\\sketches\\7F646C991D82DAAF25E67AD2873F4347/IMU_CAP1.ino.elf"
Sketch uses 862900 bytes (54%) of program storage space. Maximum is 1572864 bytes.
Global variables use 178180 bytes (67%) of dynamic memory, leaving 83964 bytes for local variables. Maximum is 262144 bytes.
"C:\Users\Dude\AppData\Local\Arduino15\packages\SiliconLabs\tools\openocd\0.12.0-arduino1-static/bin/openocd" -d2 -s "C:\Users\Dude\AppData\Local\Arduino15\packages\SiliconLabs\tools\openocd\0.12.0-arduino1-static/share/openocd/scripts/" -f interface/cmsis-dap.cfg -f target/efm32s2_g23.cfg -c "init; reset_config srst_nogate; reset halt; program {C:\Users\Dude\AppData\Local\arduino\sketches\7F646C991D82DAAF25E67AD2873F4347/IMU_CAP1.ino.hex}; reset; exit"
Open On-Chip Debugger 0.12.0+dev-01514-g21fa2de70 (2024-02-07-19:18)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
debug_level: 2
Info : auto-selecting first available session transport "swd". To override use 'transport select <transport>'.
efm32s2_dci_read_se_status
Info : Using CMSIS-DAPv2 interface with VID:PID=0x2886:0x0062, serial=7074E152
Info : CMSIS-DAP: SWD supported
Info : CMSIS-DAP: FW Version = 2.0.0
Info : CMSIS-DAP: Serial# = 7074E152
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 0 TDO = 0 nTRST = 0 nRESET = 0
Info : CMSIS-DAP: Interface ready
Info : clock speed 1000 kHz
Info : SWD DPIDR 0x6ba02477
Info : [efm32s2.cpu] Cortex-M33 r0p4 processor detected
Info : [efm32s2.cpu] target has 8 breakpoints, 4 watchpoints
Info : [efm32s2.cpu] Examination succeed
Info : starting gdb server for efm32s2.cpu on 3333
Info : Listening on port 3333 for gdb connections
[efm32s2.cpu] halted due to debug-request, current mode: Thread
xPSR: 0xf9000000 pc: 0x08000170 msp: 0x20001008
[efm32s2.cpu] halted due to debug-request, current mode: Thread
xPSR: 0xf9000000 pc: 0x08000170 msp: 0x20001008
** Programming Started **
Info : detected part: MG24B220, rev 16
Info : flash size = 1536 KiB
Info : flash page size = 8192 B
** Programming Finished **
Serial port OUTPUT :
Xiao Expansion board and MCU check & Test compiled on Dec 10 2024 at 23:11:16
Processor came out of reset.
IMU GOOD
Temperature: 87.27°
then if you move it, you get the Data samples printed out
-0.050,-0.016,1.019,-0.280,-3.570,0.560
-0.052,-0.019,1.017,-0.420,-3.850,0.490
-0.051,-0.015,1.021,0.210,-3.640,0.700
-0.047,-0.015,1.019,-0.630,-3.570,0.630
-0.049,-0.019,1.020,-0.980,-3.360,0.420
-0.049,-0.019,1.020,-0.840,-3.500,0.700
-0.051,-0.019,1.019,-0.630,-3.640,0.630
-0.049,-0.016,1.019,-0.770,-3.500,0.420
-0.050,-0.018,1.020,-0.140,-3.080, LIKE this.
Demo Video