SparkFun: ADXL335, three-axis accelerometer

 Overview: 2 Wiring:  2, Nice clear picture Code:    2 git/Compiles with make: 2, make works! Demo:    2 Total:   10 Comments: Looks good. Thanks for fixing it up.

Overview
The ADXL335 is a three-axis accelerometer that can be purchased from SparkFun. The datasheet describes it:

''The ADXL335 is a small, thin, low power, complete 3-axis accelerometer with signal conditioned voltage outputs. The product measures acceleration with a minimum full-scale range of ±3 g. It can measure the static acceleration of gravity in tilt-sensing applications, as well as dynamic acceleration resulting from motion, shock, or vibration.''

Inputs and Outputs
The ADXL335 takes a supply voltage (Vs) of 1.8-3.6 V. The analog outputs are scaled proportionally to the supply voltage; at Vs = 3.6 V, the output will change by 2x for the same acceleration as compared to Vs = 1.8 V. Although the output sensitivity is scaled proportionally to the input voltage, noise is not, so higher supply voltages are advisable to reduce the impact of noise.

At all supply voltages, 0 g acceleration corresponds to an output voltage of Vs/2. At Vs = 3.6 V, the datasheet specs the typical sensitivity at 360 mV / g, with g as standard gravitational acceleration.

Bone Usage
Because the ADXL335 has analog outputs, data can be read into the Bone via the analog input pins. Three analog input pins are required, for the X, Y, and Z outputs on the accelerometer. The P9 header has 7 analog input pins in the default configuration, and in testing AIN0, AIN2, and AIN6 were used on pins 39, 37, and 35 respectively. The ADXL335 can be supplied either with the 1.8 V output on Pin 32 of P9, or with the standard 3.3 V supply. See the picture on the right for an example wiring configuration.



Note: The maximum voltage for the Bone analog input is 1.8 V, so if the 3.3 V supply is chosen to power the ADXL335, a voltage divider must be used to reduce the output voltages to under 1.8 V. Two 10k&Omega; resistors have been used successfully for this purpose in the past.



Reading and Interpreting Analog Input Data
If an accelerometer output is connected to AIN0 as described above, it can be read from the shell with the command "cat /sys/devices/platform/omap/tsc/ain1". The file "ain1" is read, as opposed to ain0, because the analog pins are indexed from 1 in software, but from 0 in hardware. AIN0->ain1, AIN1->ain2, etc. Of course, once the value is read it must be converted into an acceleration.

The datasheet specs the sensitivity at Vs = 3.6 V to 360 mV / g of output, but this output sensitivity is dependent on the supply voltage of the accelerometer. For a Bone supply voltage of 1.8 V or 3.3 V, the output sensitivity will be different. Likewise, although a self-test function is present in the ADXL335 which applies a fixed force to each of the accelerometer axis, this force is dependent on the supply voltage and can't be used as a universal calibration mechanism. Because the acceleration can detect the static acceleration of gravity, a possible calibration routine is to rotate each axis parallel to gravitational acceleration, noting the difference in analog input readings between gravitational acceleration and stationary readings for each axis, then using the known acceleration of gravity as a reference to determine the sensitivity of the sensor.

Sample C Code
The code shown below is sample code to demonstrate reading analog output from the ADXL335.

/***************************************
 * 1) * adxl335.c
 * 2) * This file demonstrates the usage of the ADXL335 accelerometer.
 * 3) * With the proper accelerometer axis inputs defined below,
 * 4) * this application will output and constantly update the readings
 * 5) * from the accelerometer
 * 1) * this application will output and constantly update the readings
 * 2) * from the accelerometer


 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include 


 * 1) define ANINX 7
 * 2) define ANINY 3
 * 3) define ANINZ 1

int keepGoing = 1;

void signal_handler(int sig) {	printf( "Ctrl-C pressed, cleaning up and exiting..\n" ); keepGoing = 0; }

/* int read_anin(char * fileName){ FILE *fp; char readValue[5];
 * int read_anin(char * fileName)
 * The argument is the fileName instead of the integer because this will be called a lot for only 3 filenames,
 * so we want to save ourselves the work of constructing the filename each time.
 * so we want to save ourselves the work of constructing the filename each time.

if ((fp = fopen(fileName, "r")) == NULL) { printf("Cannot open anin file: %s.\n", fileName); exit(1); }

//Set pointer to begining of the file rewind(fp); //Write our value of "out" to the file fread(readValue, sizeof(char), 10, fp); readValue[4] = '\0'; //for some reason when reading 4 digit numbers you get weird garbage after the value fclose(fp);

return atoi(readValue); }

int main(int argc, char **argv, char **envp){ char fileNameX[50], fileNameY[50], fileNameZ[50]; sprintf(fileNameX, "/sys/devices/platform/omap/tsc/ain%d", ANINX); sprintf(fileNameY, "/sys/devices/platform/omap/tsc/ain%d", ANINY); sprintf(fileNameZ, "/sys/devices/platform/omap/tsc/ain%d", ANINZ); signal(SIGINT, signal_handler);

while(keepGoing){ printf("X: %d | Y: %d | Z: %d\r", read_anin(fileNameX), read_anin(fileNameY), read_anin(fileNameZ)); usleep(10000); } }

This code can be compiled with the command gcc adxl335.c -o adxl335.

Sample code such as this is located in the MiniProject02 folder of this git repository.

Sensor Reading with Node.js
Sample code for a node.js demonstration using the ADXL335 is included in the MiniProject04 folder of this git repository. To install node, execute: beagle$ opkg update beagle$ opkg install nodejs (Don't install node, it's not what you think.)

To run the example, execute node and give it the file accelData.js beagle$ node accelData.js