Arduino – logging accelerometer

This logging accelerometer is set up using

The library for using the SD-card comes with the arduino IDE v 1.0.5. To use the accelerometer, I downloaded a library from Seedstudio on github

The connections are as follows:

  • A4 -> SDA on accelerometer
  • A5 -> SCL on accelerometer
  • D4 -> CS on SD-module
  • D11 -> MOSI on SD-module
  • D12 -> MISO on SD-module
  • D13 -> SCK on SD-module

Then the following sketch will read the accelerometer a bit less than once pr 100 ms and store the raw data and a time stamp to the SD-card. The read delay is configurable. If the delay is set to 0, my arduino UNO manages to read about once pr 15 ms. Default, the accelerometer is set up to 32 readings per second, i.e.one reading each 31.25th ms. The sample rate may be set using accmeter.setSampleRate() – with the constants AUTO_SLEEP_1, AUTO_SLEEP_2, AUTO_SLEEP_4 … AUTO_SLEEP_64 or AUTO_SLEEP_120 for sampling 1,2,4 … 128 times per second. Using an ardoino UNO, sampling more frequent than 64 times/second will probably not work. It is default set to 32 readings per second. The sketch writes to a file datalog.txt in the root of the SD-card. It will always be appending to the file, so if a new file is needed, the old one has to be moved or deleted outside the sketch.

Planned future development:

  1. Real time clock
  2. GPS-logging

The sketch can be downloaded from acclogger.zip.

/* 
 
 This sketch logs data from an accelerometer to an SD card
 	
 The circuit:
 * Accelerometer attached to wire bus as follows:
 ** SDA - pin A4
 ** SCL - pin A5
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
Based on demos from SD-card library and accelerometer library.

Written by Morten Sickel February 2014 

//  This sketch is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
	 
 */

#include <sd.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;
#include <wire.h>
#include "MMA7660.h"
MMA7660 accelemeter;

// sets the pause between individual reads. if set to 0, a UNO reads about each 15th ms.
const int readdelay=100; 

void setup()
{
  Serial.begin(9600);
  Serial.print("Initializing ...");
  accelemeter.init();  
  Serial.print("Accelerometer OK");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed");
    // don't do anything more:
    return;
  }
  Serial.println("SDcard OK");
}

void loop()
{
  String dataString = "";
  int8_t x;
  int8_t y;
  int8_t z;
  float ax,ay,az;
  accelemeter.getXYZ(&x,&y,&z);
  dataString+=String(x); 
  dataString+=",";
  dataString+=String(y); 
  dataString+=",";
  dataString+=String(z);
  dataString+=",";
  dataString+=String(millis());
  
  Serial.println(dataString);
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
    delay(10000);
  } 	
  delay(readdelay);
}
This entry was posted in arduino, Data, Diverse. Bookmark the permalink.