Difference between revisions of "SparkFun: ADXL335, three-axis accelerometer"

From eLinux.org
Jump to: navigation, search
(changed a part of the sample code that was unnecessary)
Line 87: Line 87:
  
 
int main(int argc, char **argv, char **envp){
 
int main(int argc, char **argv, char **envp){
int i;
 
 
 
char fileNameX[50], fileNameY[50], fileNameZ[50];
 
char fileNameX[50], fileNameY[50], fileNameZ[50];
 
sprintf(fileNameX, "/sys/devices/platform/omap/tsc/ain%d", ANINX);
 
sprintf(fileNameX, "/sys/devices/platform/omap/tsc/ain%d", ANINX);

Revision as of 20:37, 25 September 2012


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.

Wiring example for ADXL335 on a Bone

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 10kOhm 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.

/***************************************
#* 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)
{
	printf( "Ctrl-C pressed, cleaning up and exiting..\n" );
	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);
		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