# Memsic 2125 Accelerometer to SD Module

Hey everyone,

I'm trying to come up with a schematic and code to log data from a Memsic 2125 accelerometer to an SD module. For the schematic, should I connect the accelerometer's X and Y outputs to analog pins or digital? As for the code itself, I know how to set it up for the accelerometer alone and the SD module alone but I haven't been able to figure out how to combine the two as I'm still fairly new to Arduino and programming in general. The ultimate purpose of this is to get data from a model rocket flight.

Any help would be greatly appreciated :)

Sincerely, Sara :)

..any input! Use the "pulseIn" command to measure the length of the pulse. refer the datasheet: http://www.robotshop.com/media/files/pdf/memsic-2125-datasheet-28017.pdf

Thank you, knut_ny, for clearing up the input question. So here are the 2 codes for the accelerometer and the SD module separately that I’ve previously used but how do I combine the two? Also, are there any condensed versions of these codes to just stick to the basics?

Accelerometer

// these constants won’t change:
const int xPin = 2; // X output of the accelerometer
const int yPin = 3; // Y output of the accelerometer

void setup() {
// initialize serial communications:
Serial.begin(9600);
// initialize the pins connected to the accelerometer
// as inputs:
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
}

void loop() {
// variables to read the pulse widths:
int pulseX, pulseY;
// variables to contain the resulting accelerations
int accelerationX, accelerationY;

// read pulse from x- and y-axes:
pulseX = pulseIn(xPin,HIGH);
pulseY = pulseIn(yPin,HIGH);

// convert the pulse width into acceleration
// accelerationX and accelerationY are in milli-g’s:
// earth’s gravity is 1000 milli-g’s, or 1g.
accelerationX = ((pulseX / 10) - 500) * 8;
accelerationY = ((pulseY / 10) - 500) * 8;

// print the acceleration
Serial.print(accelerationX);
// print a tab character:
Serial.print("\t");
Serial.print(accelerationY);
Serial.println();

delay(100);

}

SD Write

#include <FileIO.h>

void setup() {
// Initialize the Bridge and the Serial
Bridge.begin();
Serial.begin(9600);
FileSystem.begin();

while (!Serial); // wait for Serial port to connect.
Serial.println(“Filesystem datalogger\n”);
}

void loop() {
// make a string that start with a timestamp for assembling the data to log:
String dataString;
dataString += getTimeStamp();
dataString += " = ";

// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
dataString += String(sensor);
if (analogPin < 2) {
dataString += “,”; // separate the values with a comma
}
}

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
// The FileSystem card is mounted at the following “/mnt/FileSystema1”
File dataFile = FileSystem.open("/mnt/sd/datalog.txt", FILE_APPEND);

// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn’t open, pop up an error:
else {
Serial.println(“error opening datalog.txt”);
}

delay(15000);

}

// This function return a string with the time stamp
String getTimeStamp() {
String result;
Process time;
// date is a command line utility to get the date and the time
// in different formats depending on the additional parameter
time.begin(“date”);
time.addParameter("+%D-%T"); // parameters: D for the complete date mm/dd/yy
// T for the time hh:mm:ss
time.run(); // run the command

// read the output of the command
while (time.available() > 0) {