Hi all,
I am trying to write the IMU (Accel, Gyro, Magneto) data to the SD card in the loop(). The sampling frequency of these sensors is fixed at 104Hz. That is not a problem. However, I cannot get all the data points when I write to the SD card - I am writing to a .txt. The data points are about 100ms apart, eyeballing the timestamps. How do I ensure writing in all the data points? Any special data structures to use? Please help
It is difficult to say without seeing your code. Please post your code in a reply and use code tags.
Hi blh64. Here is the code. It is modified from the DataLogger example. The resulting file only gets 1/10th the sampled values.
#include "Arduino_BMI270_BMM150.h"
#include <SD.h>
#include <SPI.h>
File dataFile;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Started");
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
Serial.print("Accelerometer sample rate = ");
Serial.print(IMU.accelerationSampleRate());
Serial.println(" Hz");
Serial.println();
Serial.println("Acceleration in G's");
Serial.println("X\tY\tZ");
if (!SD.begin(8)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
if (SD.exists("sensor_data.txt")) {
dataFile = SD.open("sensor.txt", FILE_WRITE);
Serial.println(F("Sensor Data file exists and opened"));
} else {
dataFile = SD.open("sensor.txt", FILE_WRITE);
dataFile.println("timestamp,x,y,z");
Serial.println(F("Sensor Data file created and headers wrote in"));
}
dataFile.close();
}
void loop() {
unsigned long timestamp = millis();
float x, y, z;
dataFile = SD.open("sensor.txt", FILE_WRITE);
if (dataFile) {
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);
dataFile.print(timestamp);
dataFile.print('\t');
dataFile.print(x);
dataFile.print('\t');
dataFile.print(y);
dataFile.print('\t');
dataFile.println(z);
//Serial.println(F(timestamp));
dataFile.close();
}
} else {
Serial.println(F("Can't find target file"));
}
}
Writing to the SD card is not a fast process. A couple of things you could do
- no need to open your file inside loop() if acceleartionAvailable() returns false.
- You could just print the data to Serial and capture it in the Serial Monitor. That would be much faster, expecially if you change the baud rate to something more reasonable, like 115200. 9600 baud was the standard in the 1980s...
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.