Hi,
I've been trying to squeeze as much out of my SD datalogger as possible but I just don't seem to be able to get above a 80Hz sample rate using a Nano. I thought maybe if I tried using the esp32 (wroom 32) I'd smash 80Hz, but in fact I'm getting less than 20! I'm clealy missing something or not fully understanding the hardware. Can any help me understand whats going on please?
On paper, at least to my eye, the esp32 is just faster, so why is the write speed so slow?
This is the Nano code I'm using (sdfat)
// Basic demo for accelerometer readings from Adafruit LIS3DH with RTC
#include <Wire.h>
#include<SPI.h>
//#include "FS.h"
#include "SdFat.h"
#include "RTClib.h"
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
//----------------------
// I2C
Adafruit_LIS3DH lis = Adafruit_LIS3DH();
//SD Card
SdFat SD;
File dataFile;
const uint8_t SD_CHIP_SELECT = 10;
//SD Filename
char fileName[25];
//RTC2321
RTC_DS3231 rtc;
char DateAndTime[20];
//----------------------
//Millis Setup
const unsigned long currentMilis = millis;
const unsigned long newMillis = 0;
//=====================================================================
void setup(void) {
Serial.begin(9600);
//LIS3DH Accelerometer
lis.begin(0x18);
lis.setRange(LIS3DH_RANGE_4_G); // 2, 4, 8 or 16 G!
//RTC
rtc.begin(); //Start RCT
//SD Card
SD.begin();
SdFile::dateTimeCallback(dateTime); //timestamp file
}
//=====================================================================
void loop() {
Accel();
}
//=====================================================================
void Accel() {
DateTime now = rtc.now();
sprintf(DateAndTime, "%02d/%02d/%02d,%02d:%02d:%02d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
getFileName();
sensors_event_t event;
lis.getEvent(&event);
dataFile = SD.open(fileName, FILE_WRITE);
if (dataFile) {
dataFile.print(DateAndTime);
dataFile.print(",");
dataFile.print(event.acceleration.x);
dataFile.print(",");
dataFile.print(event.acceleration.y);
dataFile.print(",");
dataFile.println(event.acceleration.z);
dataFile.close();
}
}
//=======================================================================
void getFileName() {
DateTime now = rtc.now();
sprintf(fileName, "Accel_%02d%02d%02d_%02d%02d.csv", now.year(), now.month(), now.day(), now.hour(), now.minute());
}
//=======================================================================
void dateTime(uint16_t* date, uint16_t* time) {
//used for callback function for file timestamp
DateTime now = rtc.now();
// return date using FAT_DATE macro to format fields
*date = FAT_DATE(now.year(), now.month(), now.day());
// return time using FAT_TIME macro to format fields
*time = FAT_TIME(now.hour(), now.minute(), now.second());
}
This is the esp32 code I'm using (standard SD library as sdfat wouldn't work)
#include <Wire.h>
#include<SPI.h>
#include "FS.h"
#include "SD.h"
#include "RTClib.h"
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
//----------------------
// I2C
Adafruit_LIS3DH lis = Adafruit_LIS3DH();
//SD Card
#define SD_CS = 5;
File file;
//RTC2321
RTC_DS3231 rtc;
char DateAndTime[20];
//----------------------
String timeStamp;
String accelData;
float accl_X;
float accl_Y;
float accl_Z;
//=====================================================================
void setup() {
Serial.begin(9600);
Wire.begin();
//LIS3DH Accelerometer
lis.begin(0x18);
lis.setRange(LIS3DH_RANGE_4_G); // 2, 4, 8 or 16 G!
//RTC
rtc.begin(); //Start RCT
//SD Card
SD.begin();
}
//=====================================================================
void loop() {
readAccelData();
logSDCard();
}
//=====================================================================
void readAccelData() {
//First read time
DateTime now = rtc.now();
sprintf(DateAndTime, "%02d/%02d/%02d,%02d:%02d:%02d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
//Now read accel data
sensors_event_t event;
lis.getEvent(&event);
//reformat infomation
timeStamp = DateAndTime;
accl_X = event.acceleration.x;
accl_Y = event.acceleration.y;
accl_Z = event.acceleration.z;
}
//=======================================================================
// Write the sensor readings on the SD card
void logSDCard() {
accelData = String(timeStamp) + "," + String(accl_X) + "," + String(accl_Y) + "," +
String(accl_Z) + "\r\n";
appendFile(SD, "/datafile.csv", accelData.c_str());
}
//=======================================================================
void appendFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if (!file) {
Serial.println("Failed to open file for appending");
return;
}
if (file.print(message)) {
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
file.close();
}
Thank you ![]()