Acceleration recorder


You can get simple and cheap acceleration modules (Pollin 810143) equipped with the ADXL330KCPZ (Analog Devices) giving you three analog voltages according to the three dimensions.

As you only need a simple way to store your data (the size of the EEPROM is really too small) you can take any micro SD card which comes usually shipped with an adaptor for standard SD cards and do the little soldering as described by Nathan Chantrell, Yersinia and others.

Add the ability to select different sample rates as described here and auto-increment the filename as described here.

And this is what you get:

/* analog sensors
don't overwrite old files, generate new file names!
 ** SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK  - pin 13
 ** CS   - pin 10 */
#include <SD.h>
const int chipSelect = 10;
boolean cardOK = true;
long fileNum = 10000;  // maximum 99999
String fileName;
char name[13];
File dataFile;
// -----------------
const byte Gnd = A5;
const byte Vcc = A1;
const byte z = A2;
const byte y = A3;
const byte x = A4;
long sampleInterval;
long t1;
long sampleIntervals[] = {    10,       20,        50,  //  10 - 50 msec
                             100,      200,       500,  // 0.1 - 0.5 sec
                            1000,     2000,      5000,  // 1 - 5 sec
                           10000,    15000,     30000,  // 10 - 30 sec
                           60000,   120000,    300000,  // 1 - 5 min
                          600000,   900000,   1200000,  // 10 - 30 min
                         3600000,  7200000,  18000000,  // 1 - 5 hours
                        43200000, 86400000, 172800000   // 12 - 46 hours
                        };
/* jumper setting
 d7   d6   d5   d4   d3  interval
  -    -    -    -    -     10 ms
  -    -    -    -    x     20 ms
  -    -    -    x    -     50 ms
  -    -    -    x    x    100 ms
  -    -    x    -    -    200 ms
  -    -    x    -    x    500 ms
  -    -    x    x    -      1 s
  -    -    x    x    x      2 s
  -    x    -    -    -      5 s
  -    x    -    -    x     10 s
  -    x    -    x    -     15 s
  -    x    -    x    x     30 s
  -    x    x    -    -      1 min
  -    x    x    -    x      2 min
  -    x    x    x    -      5 min
  -    x    x    x    x     10 min
  x    -    -    -    -     15 min
  x    -    -    -    x     30 min
  x    -    -    x    -      1 h
  x    -    -    x    x      2 h
  x    -    x    -    -      5 h
  x    -    x    -    x     12 h
  x    -    x    x    -     24 h
  x    -    x    x    x     48 h  
*/  

int getConfig() {
  pinMode(2,OUTPUT);
  for (byte i = 3; i <= 7; i++) digitalWrite(i,HIGH); // internal pull-up resistor
  byte b = 0;
  for (byte i = 7; i >= 3; i--) b = 2 * b + digitalRead(i); 
  return b;
}

void incFileNum() { // generate next file name:
  String s = "dat" + String(++fileNum) + ".txt";
  s.toCharArray(name,13);
}

void save(String s) {
  dataFile = SD.open(name, FILE_WRITE);
  if (dataFile) {
    dataFile.println(s);
    dataFile.close();
    Serial.println(s);
  }  
  else Serial.println("error opening " + String(name));
}

void setup() {
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  pinMode(chipSelect, OUTPUT);
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    cardOK = false; // don't perform the loop()
    return;
  }
  Serial.println("card initialized.");
  incFileNum(); // set it to dat10000.txt
  while (SD.exists(name)) incFileNum();
  Serial.println("new file name: " + String(name));
  // activate the acceleration sensor:
  pinMode(Gnd,OUTPUT);
  pinMode(Vcc,OUTPUT);
  digitalWrite(Vcc,HIGH);
  sampleInterval = sampleIntervals[getConfig()];
  Serial.print("sample interval (milliseconds): ");
  Serial.println(sampleInterval);
  if (sampleInterval >= 3600000) {
    Serial.print(sampleInterval / 3600000);
    Serial.println(" hour(s)");
  } else
  if (sampleInterval >= 60000) {
    Serial.print(sampleInterval / 60000);
    Serial.println(" minute(s)");
  } else
  if (sampleInterval >= 1000) {
    Serial.print(sampleInterval / 1000);
    Serial.println(" second(s)");
  } 
  t1 = sampleInterval; 
  save("t,x,y,z");
}

void loop() {
  while (millis() < t1);
  t1 += sampleInterval;
  if (!cardOK) return;
  String dataString = String(millis());
  int sensor;
  sensor = analogRead(x) - 200;
  dataString += ","; 
  dataString += String(sensor);
  sensor = analogRead(y);
  dataString += ","; 
  dataString += String(sensor);
  sensor = analogRead(z) + 200;
  dataString += ","; 
  dataString += String(sensor);
  save(dataString);
}

On the top right you see the jumper selector, on the left you see a DIY shield holding the SD card, and on the bottom right you see the acceleration sensor.





contact: nji(at)gmx.de