How to connect an accelerometer to a microcontroller

Post Reply
User avatar
Shane
Captain
Captain
Posts: 226
Joined: Sun Jul 19, 2009 9:59 pm
Location: Jönköping, Sweden

How to connect an accelerometer to a microcontroller

Post by Shane » Thu Jun 23, 2011 11:29 pm

This tutorial demonstrates the use of the ADXL330 accelerometer. This is a three axis accelerometer that can detect joint movement as well as inclination and vibration. The ADXL330 outputs a 0V to 3V analogue signal on each of the X, Y, and Z axis. You will need to relate this analogue voltage to a gravity amount and use trigonometry to calculate an angle. The chip is mounted on a lilypad system that is designed for wearable electronics, you can actually sew it on your clothes using conductive thread.

Accelerometers measures the acceleration it experiences due to freefall. This means that an accelerometer will experience 1g (9.8m/s) of acceleration while it is at rest. The axis that experience this acceleration depends on the orientation and positioning of the accelerometer. Because of this 1g of acceleration we must subtract off a bias value that will give us a more useful value. The arduino will convert the analogue voltage to a value from 0-1023, where 512 corresponds to 1g of acceleration. Thus 512 is subtracted from the read in value to normalize the acceleration value.

In this circuit, we have connected each of three outputs of the accelerometer to the analogue inputs of the arudino. Each output corresponds to its respective axis and will be converted into a digital number (0-1023). Also, the accelerometer is wired up to a reference voltage (5V) and a common ground. In this code example, we read in the analogue values from each analogue axis-output and subtract a bias value that normalizes the acceleration data. We then perform a simple delta operation that calculates the total change in acceleration and print out all the data using the serial monitor.
1.jpg
1.jpg (91.25 KiB) Viewed 3109 times

Code: Select all

//use Ardunio to read in ADXL330 accelerometer data 
 //and use the serial monitor for real-time data analysis 

#define xPin 0
 #define yPin 1
 #define zPin 2

int delta; // for calculation 
 int x,y,z = 0;
 int bias = 512; // normalizing constant 

////////////////////////////////////////////////////
 // read in analog values for each axis and normalize 
 void accelRead()
 {
 x = analogRead(xPin) - bias;
 y = analogRead(yPin) - bias;
 z = analogRead(zPin) - bias;
 }

 ////////////////////////////////////////////////////
 // print out all the accelerometer data 
 void accelPrint()
 {
 Serial.print("x = "); Serial.print(x);
 Serial.print("\ty = "); Serial.print(y);
 Serial.print("\tz = "); Serial.print(z);
 Serial.print("\tdelta = "); Serial.print(delta); 
 Serial.println();
 }

void setup()
 {
 Serial.begin(9600); // set baud rate for serial monitor
 pinMode(xPin, INPUT);
 pinMode(yPin, INPUT);
 pinMode(zPin, INPUT);
 }

void loop()
 {
 delta = x + y + z; // add up all the accelerations 
 accelRead();
 delta -= x + y +z; // change in total acceleration
 accelPrint();
 delay(50);
 }
Accelerometer at Rest
2.png
2.png (21.06 KiB) Viewed 3109 times
Accelerometer Moving
3.png
3.png (50 KiB) Viewed 3109 times
Courtecy of UMASS AMHERST
Post Reply

Return to “Arduino”