Datalogger - Huzzah32 + Adalogger feather + 2 LIS3DH accels
I've been working on a datalogger using the Adafruit Feather Huzzah ESP32 + Adafruit Adalogger feather (RTC + SD) + 2 Adafruit LIS3dh accelerometers using I2C and SPI (SD card). I'm successfully getting data and writing it to the card but i can't seem to get past about 333 Hz. I'd like to be somewhere between 3000-5000 Hz. I believe this is possible with this setup? Do i have too many logfiles, saving in float, any ideas why I can't achieve higher hertzs? Thanks!
// Bike Data logger for acceleration at hubs and touch points
// Hardware: Adafruit ESP32, Adalogger feather+RTC, 2x LIS3DH accels, LED, push button
// Created: Sep 7 2019
// Updated: Oct 1 2019
// Uses I2C
#include <SPI.h>
#include <SD.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
#include "RTClib.h"
#include <Wire.h>
#include <time.h>
// SD Card uses SPI, Set the selct pin
#define cardSelect 33
// I2C
Adafruit_LIS3DH lis = Adafruit_LIS3DH(); //Sensor 1, at hub 0x18
Adafruit_LIS3DH lis2 = Adafruit_LIS3DH(); //Sensor 2, at post 0x19 (on 3.3 at SDO)
// RTC
RTC_PCF8523 rtc;
//SD writing
File logfile;
//Counter
unsigned long Millisec = 0;
unsigned long Micro = 0;
//button and LED setup
const int button = 4; //pushbutton pin GPIO #4, A5
const int ledPin = 27; //led pin GPIO #27
int stateLED = HIGH; //LED is high to start
int logData = 1; //how to run logging loop
int reading; //button reading
int previous = HIGH; //button previous reading
long debounce = 200; //mS for debounce
int x;
//=============================================================================================
void setup() {
//Serial.begin(1000000); //230400 works
//Wire.begin();
Wire.setClock(400000); //set I2C speed
pinMode(13, OUTPUT); //set LED to show writing on SD Card
pinMode(ledPin, OUTPUT); //set LED to show when switched off
pinMode(button, INPUT); //button to turn recording on/off
if (! lis.begin(0x18)) { // Sensor 1, change this to 0x19 for alternative i2c address
Serial.println("Couldnt start Sensor 1");
while (1);
}
Serial.println("LIS3DH Sensor 1 found!");
if (! lis2.begin(0x19)) { // Sensor 2, SDO is 3V
Serial.println("Couldnt start Sensor 2");
while (1);
}
Serial.println("LIS3DH Sensor 2 found!");
lis.setRange(LIS3DH_RANGE_16_G); // 2, 4, 8 or 16 G!
lis2.setRange(LIS3DH_RANGE_16_G);
Serial.print("Range 1/2 = ");
Serial.print(2 << lis.getRange());
Serial.print("/");
Serial.print(2 << lis2.getRange());
Serial.println("G");
// see if the card is present and can be initialized:
if (!SD.begin(cardSelect)) {
Serial.println("Card init. failed!");
//error(2);
}
// Create filename scheme ====================================================================
char filename[15];
strcpy(filename, "/BDLDATA00.TXT");
for (uint8_t i = 0; i < 100; i++) {
filename[8] = '0' + i/10;
filename[9] = '0' + i%10;
// create if does not exist, do not open existing, write, sync after write
if (! SD.exists(filename)) {
break;
}
}
// Create file and prepare it ============================================================
logfile = SD.open(filename, FILE_WRITE);
if( ! logfile ) {
Serial.print("Couldnt create ");
Serial.println(filename);
//error(3);
}
Serial.print("Writing to ");
Serial.println(filename);
pinMode(13, OUTPUT);
Serial.println("Ready!");
// Use RTC - Year/month/day for reference later ======================================
DateTime now = rtc.now();
logfile.print(now.year(), DEC);
logfile.print("/");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print("\t");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.println();
logfile.println();
//Column labels
logfile.print("Time");
logfile.print("\t");
logfile.print("Sensor 1 X");
logfile.print("\t");
logfile.print("Sensor 1 Y");
logfile.print("\t");
logfile.print("Sensor 1 Z");
logfile.print("\t");
logfile.print("Sensor 2 X");
logfile.print("\t");
logfile.print("Sensor 2 Y");
logfile.print("\t");
logfile.print("Sensor 2 Z");
logfile.println();
}
uint8_t i=0;
//==========================================================================================
void loop() {
reading = digitalRead(button);
if (reading == HIGH && previous == LOW && millis() - Millisec > debounce){ //= button state when releasing
if (stateLED == HIGH){
stateLED = LOW;
logData = 0;
//digitalWrite(13, HIGH);
}
else {
stateLED = HIGH;
logData = 1;
//logfile.flush();
logfile.close(); //must save and close file to flush out RAM to card
}
Millisec = millis(); //for debounce
}
//digitalWrite(ledPin, stateLED); //led on/off
previous = reading; //reset previous
// Turn datalogging on/off
if (logData == 0){
DataLogger();
}
else{
//digitalWrite(13, LOW);
}
}
//==================datalogging loop=====================================
void DataLogger(){
//for (int x = 0; x < 20000; x++){
//Counter
Micro = micros();
logfile.print(Micro);
logfile.print("\t");
lis.read(); // get X Y and Z data at once (raw data) sensor 1
lis2.read(); // get X Y and Z data at once (raw data) sensor 2
// log raw data from Sensor 1 and 2
// Note: must normalize raw data which means measured values are between -32768 and 32767. -Range G/+Range G = -32768/32767
logfile.print(lis.x);
logfile.print("\t");
logfile.print(lis.y);
logfile.print("\t");
logfile.print(lis.z);
logfile.print("\t");
logfile.print(lis2.x);
logfile.print("\t");
logfile.print(lis2.y);
logfile.print("\t");
logfile.print(lis2.z);
logfile.println();
/* Or....get a new sensor event, normalized */
/*sensors_event_t event;
lis.getEvent(&event);
sensors_event_t event2;
lis2.getEvent(&event2); */
//Display the results (acceleration is measured in m/s^2)
/*Serial.print(" \tX: "); Serial.print(event.acceleration.x,4);
Serial.print(" \tY: "); Serial.print(event.acceleration.y,4);
Serial.print(" \tZ: "); Serial.print(event.acceleration.z,4);
Serial.println(" m/s^2 "); */
// log from Sensor 1 and 2
/*logfile.print(event.acceleration.x,3);
logfile.print("\t");
logfile.print(event.acceleration.y,3);
logfile.print("\t");
logfile.print(event.acceleration.z,3);
logfile.print("\t");
logfile.print(event2.acceleration.x,3);
logfile.print("\t");
logfile.print(event2.acceleration.y,3);
logfile.print("\t");
logfile.print(event2.acceleration.z,3);
logfile.println(); */
delayMicroseconds(200);
}
//logfile.flush();
//x = 0;
//}