I'm using the
Arduino Portenta to read from a sensor, the
LIS3MDLmagnetometer. This is all through the Arduino software, and I'm using a QWIIC converter cable to connect from the Portenta to the sensor through I2C. I'm reading data, so there's nothing wrong with the wiring. The problems that I'm facing have to do with the data rate and the timestamp.
According to Adafruit's tutorial, you can set the LIS3MDL's data rate from 0.625 Hz to 1 kHz. This is supported with ST's
datasheet. However, changing the data rate doesn't seem to correspond with an increase in data points. For instance, when running my code, after 11 seconds, I only get 6461 data points, and that is when I set the LIS3MDL data rate to 1 kHz. By my calculations, assuming a data point every cycle, I should get ~11000 data points after 11 seconds. Likewise, setting it to lower frequencies results in the same behavior. Why am I getting less data than expected?
Regarding the timestamp, I'm using Arduino's millis() function to get a time stamp of sorts (based on how many milliseconds have elapsed since the program has started), but what is this function based on? Is it tied to the Arduino processor, or is it tied to something else? Could the LIS3MDL have some sort of time feature that is screwing up my time stamp? But that shouldn't be possible since my time stamp doesn't involve reading from the sensor...
If it helps, below is code that I'm using to diagnose coding with the sensor, though this isn't my final code. It's more or less no differerent than the code that Adafruit provides.
#include <Wire.h>
#include <Adafruit_LIS3MDL.h>
#include <Adafruit_Sensor.h>
Adafruit_LIS3MDL lis3mdl;
#define LIS3MDL_CLK 13
#define LIS3MDL_MISO 12
#define LIS3MDL_MOSI 11
#define LIS3MDL_CS 10
// Set up Function
void setup(void) {
Serial.begin(115200);
while (!Serial) delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("Adafruit LIS3MDL test!");
// Try to initialize!
if (! lis3mdl.begin_I2C()) { // hardware I2C mode, can pass in address & alt Wire
Serial.println("Failed to find LIS3MDL chip");
while (1) { delay(10); }
}
Serial.println("LIS3MDL Found!");
// Set LIS3MDL Performance mode to HIGH performance
lis3mdl.setPerformanceMode(LIS3MDL_HIGHMODE);
// Set LIS3MDL to operate continuously
lis3mdl.setOperationMode(LIS3MDL_CONTINUOUSMODE);
// Set LIS3MDL Data Rate to 1 kHz
lis3mdl.setDataRate(LIS3MDL_DATARATE_1000_HZ);
// Set LIS3MDL Gauss Range to 4 Gauss
lis3mdl.setRange(LIS3MDL_RANGE_4_GAUSS);
Serial.println("Set time to 0 microseconds");
Serial.println("Portenta H7");
lis3mdl.setIntThreshold(500);
lis3mdl.configInterrupt(false, false, true, // enable z axis
true, // polarity
false, // don't latch
true); // enabled!
}
// Global Variables
int ne = 0; // Number of events that have occurred
unsigned long timet; // Var. for the current timestamp
void loop() {
// Increase the number of events by 1
// and get the # of milliseconds since the program ran
ne = ne + 1;
timet = millis();
// Print the current event and timestamp
Serial.print(ne);
Serial.print(",");
Serial.print(timet);
Serial.print("~~");
lis3mdl.read();
/* Get a new sensor event, normalized to uTesla */
sensors_event_t event;
lis3mdl.getEvent(&event);
/* Display the results (magnetic field is measured in uTesla) */
Serial.print("\tX: ");
Serial.print(event.magnetic.x);
Serial.print(",");
Serial.print(" \tY: ");
Serial.print(event.magnetic.y);
Serial.print(",");
Serial.print(" \tZ: ");
Serial.print(event.magnetic.z);
delay(0);
Serial.println();
}