Hi community!
I have analog accelerometer that I connect to arduino and can read the data and print in Serial. I connect to arduino from PC with USB and can save the outputs from Serial. The frequency of my accelerometer can be 500 Hz and 5 kHz that is important to note. I want to have opportunity to save the data as at max frequency as it possible.
There is the sketch:
// Make sure these two variables are correct for your setup
int scale = 200; // 3 (3g) for ADXL337, 200 (200g) for ADXL377
boolean micro_is_5V = true; // Set to true if using a 5V microcontroller such as the Arduino Uno, false if using a 3.3V microcontroller, this affects the interpretation of the sensor data
void setup()
{
// Initialize serial communication at 115200 baud
Serial.begin(115200);
}
// Read, scale, and print accelerometer data
void loop()
{
// Get raw accelerometer data for each axis
int rawX = analogRead(A0);
int rawY = analogRead(A1);
int rawZ = analogRead(A2);
// Scale accelerometer ADC readings into common units
// Scale map depends on if using a 5V or 3.3V microcontroller
float scaledX, scaledY, scaledZ; // Scaled values for each axis
if (micro_is_5V) // Microcontroller runs off 5V
{
scaledX = mapf(rawX, 0, 1023, -scale, scale); // 3.3/5 * 1023 =~ 675
scaledY = mapf(rawY, 0, 1023, -scale, scale);
scaledZ = mapf(rawZ, 0, 1023, -scale, scale);
}
else // Microcontroller runs off 3.3V
{
scaledX = mapf(rawX, 0, 675, -scale, scale);
scaledY = mapf(rawY, 0, 675, -scale, scale);
scaledZ = mapf(rawZ, 0, 675, -scale, scale);
}
Serial.print(scaledX); Serial.print(";");
Serial.print(scaledY); Serial.print(";");
Serial.print(scaledZ); Serial.print(";");
}
// Same functionality as Arduino's standard map function, except using floats
float mapf(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
As output I have the next (acceleration in g in x, y and z direction respectively) :
3.52;0.54;-1.25
9.15;-0.53;-2.89
9.20;-0.54;-2.91
9.23;-0.53;-2.88
The problem is that accelerometer with arduino should be located far from PC or notebook approx 30-35 meters. So I decided to write the data on SD Card. I found the example from source code of SD library (SD/Datalogger.ino at master ยท arduino-libraries/SD ยท GitHub) and measured the time of opening, writing and closing the file like that:
void saveSD()
{
// create file for saving data
start_open_t=micros(); // time in milli seconds
File dataFile = SD.open("datalog.txt", FILE_WRITE);
delta_open_t=micros() - start_open_t ;
// if file is available for writing
if (dataFile) {
// save data
start_write_t=micros(); // time in milli seconds
dataFile.println(dataString);
delta_write_t=micros() - start_write_t ;
// close the file
start_close_t=micros(); // time in milli seconds
dataFile.close();
delta_close_t=micros() - start_close_t ;
// print the messages
Serial.println("Save OK");
Serial.println("Open time:");
Serial.println(delta_open_t);
Serial.println("Write time:");
Serial.println(delta_write_t);
Serial.println("Close time:");
Serial.println(delta_close_t);
} else {
// if file is not available
Serial.println("Error opening datalog.txt");
}
}
Measured time:
Open time:
7656
Write time:
2176
Close time:
8076
The open time is increasing there is no surprise (with increasing the file size).
Write to file and close time are almost constant. I thought to open file in setup() and initialize timer. Write to file in loop() and after some time interval to close the file, after that open new file and so on. But as can be seen the print operation dataFile.println(dataString);
takes approximately 2 ms that is 500 Hz, and it is too slow.
I'm trying to find way to save data fast.
I'm new to arduino and don't know all capabilities and I ask for help.
I have some thoughts how to be:
- Sounds crazy to connect to PC with 30 m usb cable. There is potential risk of weak signal.
- May be use wifi or bluetooth to transfer data? I did not do it and afraid there is some limitation as with SD card
- Connect the arduino to another and save Serial. Is it legal?))
- Find way to efficient save file on SD card. May be save in buffer binary data and to save binary once a some period.
Any suggestions are appreciated.
UPD
- For 500 Hz acceleremeter I use Arduino Uno
For 5 ะบะz I use Arduino Due - I want to store approx 30 min of reading. I can save data in different files let's say with 2-10 sec of recording
I checked that if I write with 5 kHz 1 s of recording will weight 110 kB if it writes floats