

From eLinux.org
Jump to: navigation, search
Line 104: Line 104:
This code can be compiled with the command ''gcc /path/to/code/adxl335.c -o /path/to/desired/executable/output/adxl335''
This code can be compiled with the command ''gcc adxl335.c -o adxl335''

Revision as of 03:59, 23 September 2012




Inputs and Outputs



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.


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.

#* adxl335.c
#* This file demonstrates the usage of the ADXL335 accelerometer.
#* With the proper accelerometer axis inputs defined below, 
#* this application will output and constantly update the readings
#* from the accelerometer

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/time.h>

#define ANINX 7
#define ANINY 3
#define ANINZ 1

int keepGoing = 1;

void signal_handler(int sig)
	keepGoing = 0;

*	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.
int read_anin(char * fileName){
	FILE *fp;
	char readValue[5];

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

	//Set pointer to begining of the file
	//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

	return atoi(readValue);

	int i;

	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);

		printf("X: %d | ", read_anin(fileNameX));
		printf("Y: %d | ", read_anin(fileNameY));
		printf("Z: %d\r", read_anin(fileNameZ));
	signal(SIGINT, signal_handler);

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