Difference between revisions of "LilyPad Accelerometer ADXL335"

From Geeetech Wiki
Jump to: navigation, search
(Example code)
(Example code)
Line 12: Line 12:
 
==Example code==
 
==Example code==
  
// these constants describe the pins. They won't change:
+
// these constants describe the pins. They won't change:
const int xpin = A1;                  // x-axis of the accelerometer
+
const int xpin = A1;                  // x-axis of the accelerometer
const int ypin = A2;                  // y-axis
+
const int ypin = A2;                  // y-axis
const int zpin = A3;                  // z-axis (only on 3-axis models)
+
const int zpin = A3;                  // z-axis (only on 3-axis models)
//
 
int sampleDelay = 500;  //number of milliseconds between readings
 
void setup()
 
{
 
// initialize the serial communications:
 
Serial.begin(9600);
 
 
  //
 
  //
  //Make sure the analog-to-digital converter takes its reference voltage from
+
  int sampleDelay = 500;  //number of milliseconds between readings
// the AREF pin
+
void setup()
pinMode(xpin, INPUT);
+
{
pinMode(ypin, INPUT);
+
  // initialize the serial communications:
pinMode(zpin, INPUT);
+
  Serial.begin(9600);
}
+
  //
void loop()
+
  //Make sure the analog-to-digital converter takes its reference voltage from
{
+
  // the AREF pin
int x = analogRead(xpin);
+
  pinMode(xpin, INPUT);
//
+
  pinMode(ypin, INPUT);
//add a small delay between pin readings.  I read that you should
+
  pinMode(zpin, INPUT);
//do this but haven't tested the importance
+
}
  delay(1);  
+
void loop()
//
+
{
int y = analogRead(ypin);
+
  int x = analogRead(xpin);
//
+
  //
//add a small delay between pin readings.  I read that you should
+
  //add a small delay between pin readings.  I read that you should
//do this but haven't tested the importance
+
  //do this but haven't tested the importance
  delay(1);  
+
    delay(1);  
//
+
  //
int z = analogRead(zpin);
+
  int y = analogRead(ypin);
//
+
  //
//zero_G is the reading we expect from the sensor when it detects
+
  //add a small delay between pin readings.  I read that you should
//no acceleration.  Subtract this value from the sensor reading to
+
  //do this but haven't tested the importance
//get a shifted sensor reading.
+
    delay(1);  
float zero_G =512;  
+
  //
//
+
  int z = analogRead(zpin);
//scale is the number of units we expect the sensor reading to
+
  //
//change when the acceleration along an axis changes by 1G.
+
  //zero_G is the reading we expect from the sensor when it detects
//Divide the shifted sensor reading by scale to get acceleration in Gs.
+
  //no acceleration.  Subtract this value from the sensor reading to
float scale =102.3;
+
  //get a shifted sensor reading.
//
+
  float zero_G =512;  
Serial.print(((float)x - zero_G)/scale);
+
  //
Serial.print("\t");
+
  //scale is the number of units we expect the sensor reading to
//
+
  //change when the acceleration along an axis changes by 1G.
Serial.print(((float)y - zero_G)/scale);
+
  //Divide the shifted sensor reading by scale to get acceleration in Gs.
Serial.print("\t");
+
  float scale =102.3;
//
+
  //
Serial.print(((float)z - zero_G)/scale);
+
  Serial.print(((float)x - zero_G)/scale);
Serial.print("\n");
+
  Serial.print("\t");
//
+
  //
// delay before next reading:
+
  Serial.print(((float)y - zero_G)/scale);
delay(sampleDelay);
+
  Serial.print("\t");
}
+
  //
 +
  Serial.print(((float)z - zero_G)/scale);
 +
  Serial.print("\n");
 +
  //
 +
  // delay before next reading:
 +
  delay(sampleDelay);
 +
}
  
 
==How to buy==
 
==How to buy==
 
Click here to buy [http://www.geeetech.com/adxl335-triple-axis-accelerometer-breakout-p-380.html ADXL335 3 axis accelormeter module]
 
Click here to buy [http://www.geeetech.com/adxl335-triple-axis-accelerometer-breakout-p-380.html ADXL335 3 axis accelormeter module]

Revision as of 02:56, 23 August 2013

Introduction

IMG 9931.jpg

This is a three axis accelerometer for the LilyPad system. Based on the ADXL335 MEMS accelerometer from Analog Devices, the LilyPad Accelerometer can detect joint movement as well as inclination and vibration.The ADXL335 outputs a 0V to 3V analog signal on each of the X, Y, and Z axis. You will need to convert this analog voltage to a gravity amount and use trigonometry to calculate a true 'angle'. If you are not looking for tilt, it is very easy to use this sensor for basic motion sensing.


Features

Usage

Example code

// these constants describe the pins. They won't change:
const int xpin = A1;                  // x-axis of the accelerometer
const int ypin = A2;                  // y-axis
const int zpin = A3;                  // z-axis (only on 3-axis models)
//
int sampleDelay = 500;   //number of milliseconds between readings
void setup()
{
 // initialize the serial communications:
 Serial.begin(9600);
 //
 //Make sure the analog-to-digital converter takes its reference voltage from
 // the AREF pin
 pinMode(xpin, INPUT);
 pinMode(ypin, INPUT);
 pinMode(zpin, INPUT);
}
void loop()
{
 int x = analogRead(xpin);
 //
 //add a small delay between pin readings.  I read that you should
 //do this but haven't tested the importance
   delay(1); 
 //
 int y = analogRead(ypin);
 //
 //add a small delay between pin readings.  I read that you should
 //do this but haven't tested the importance
   delay(1); 
 //
 int z = analogRead(zpin);
 //
 //zero_G is the reading we expect from the sensor when it detects
 //no acceleration.  Subtract this value from the sensor reading to
 //get a shifted sensor reading.
 float zero_G =512; 
 //
 //scale is the number of units we expect the sensor reading to
 //change when the acceleration along an axis changes by 1G.
 //Divide the shifted sensor reading by scale to get acceleration in Gs.
 float scale =102.3;
 //
 Serial.print(((float)x - zero_G)/scale);
 Serial.print("\t");
 //
 Serial.print(((float)y - zero_G)/scale);
 Serial.print("\t");
 //
 Serial.print(((float)z - zero_G)/scale);
 Serial.print("\n");
 //
 // delay before next reading:
 delay(sampleDelay);
}

How to buy

Click here to buy ADXL335 3 axis accelormeter module