User:Sarah Labianca/Notebook/Smyth Lab/2012/04/05

From OpenWetWare

(Difference between revisions)
Jump to: navigation, search
(Downloads)
(Links)
Line 11: Line 11:
==Links==
==Links==
-
[http://bildr.org/2011/03/adxl345-arduino/ Bildr Tutorial (with circuit and library)]
+
*[http://bildr.org/2011/03/adxl345-arduino/ Bildr Tutorial (with circuit and library)]
-
[http://arduino.cc/hu/Main/Software Arduino IDE]
+
*[http://arduino.cc/hu/Main/Software Arduino IDE]
-
[http://www.sparkfun.com/datasheets/Sensors/Accelerometer/ADXL345.pdf Data Sheet]
+
*[http://www.sparkfun.com/datasheets/Sensors/Accelerometer/ADXL345.pdf Data Sheet]
==Building a force gauge==
==Building a force gauge==

Revision as of 01:58, 11 April 2012

Project name Main project page
Previous entry      Next entry

Downloads

Links

Building a force gauge

About the ADXL345 and Arduino

Background reading on accelerometers. Our accelerometer of choice was the ADXL 345 from SparkFun Electronics. There are a bunch of code examples for the ADXL345, and the datasheet and quickstart guide were very helpful. The board we used to control and process data from the ADXL345 was an Arduino Uno R3.

Circuit

I connected the ADXL to the Arduino using a relatively simple circuit. I used the circuit setup from a bildr tutorial. The accelerometer came mounted on a breakout board, with holes where pins could go. I had to solder pins onto the breakout board

  • Arduino 3.3 v is connected to ADXL VCC and CS
  • Arduino ground is connected to ADXL ground
  • Arduino Analog In 4 (A4) is connected to ADXL SDA
  • Arduino Analog In 5 (A5) is connected to ADXL SCL


The Arduino takes its power from (and transmits data through) a USB port.

Development Environment

I wrote and uploaded my code to the Arduino and accelerometer using the Arduino 1.0 IDE on Ubuntu 11.04. Help for getting the integrated development environment running on Ubuntu can be found here. The language is very similar to C.

To write code for the ADXL in Arduino 1.0, one needs to have a library containing a .cpp and a .h file. The library is inserted into the libraries folder in the Arduino 1.0 directory. Essentially, a library contains subroutines one may find useful in writing their code and helps a programmer interface with a device, without having to worry about all the details.

I used an ADXL 345 library that I found on a bildr tutorial.

Code

This is the code I used to take in data from the ADXL345. Parts of the code from a bildr tutorial were reused.

#include <Wire.h>
#include <ADXL345.h>
 
ADXL345 adxl; //variable adxl is an instance of the ADXL345 library
int x,y,z; 
float Gx, Gy, Gz;
int count=0;
 
void setup(){
  Serial.begin(9600);
  adxl.powerOn();
  adxl.setRangeSetting(2);  //G sensing range from -2g to +2g
 
  //set activity/ inactivity thresholds (0-255)
  adxl.setActivityThreshold(75); //62.5mg per increment
  adxl.setInactivityThreshold(75); //62.5mg per increment
  adxl.setTimeInactivity(6); // how many seconds of no activity is inactive?
 
  //look of activity movement on this axes - 1 == on; 0 == off 
  adxl.setActivityX(1);
  adxl.setActivityY(1);
  adxl.setActivityZ(1);
 
  //look of inactivity movement on this axes - 1 == on; 0 == off
  adxl.setInactivityX(1);
  adxl.setInactivityY(1);
  adxl.setInactivityZ(1);
 
  //setting all interupts to take place on int pin 1
  //I had issues with int pin 2, was unable to reset it
  adxl.setInterruptMapping( ADXL345_INT_ACTIVITY_BIT,     ADXL345_INT1_PIN );
  adxl.setInterruptMapping( ADXL345_INT_INACTIVITY_BIT,   ADXL345_INT1_PIN );
 
  //register interupt actions - 1 == on; 0 == off  
  adxl.setInterrupt( ADXL345_INT_ACTIVITY_BIT,   1);
  adxl.setInterrupt( ADXL345_INT_INACTIVITY_BIT, 1);
}
 
 
void loop(){
  //this loop will continuously output data from the accelerometer
   Scale();
   PrintData();
 
  //getInterruptSource clears all triggered actions after returning value
  //so do not call again until you need to recheck for triggered actions
  byte interrupts = adxl.getInterruptSource();
 
  //inactivity
  if(adxl.triggered(interrupts, ADXL345_INACTIVITY)){
   Serial.println("inactivity");
   PrintHeadings();
   }
 
 //DetermineOffsets();        
 
}
 
 
//------------Scale----------
//This function scales the raw values taken
//  from the accelerometer, and converts it to g's.
 void Scale (void){
 adxl.readAccel(&x, &y, &z);
 //correcting offset of accelerometer due to position
   //axis = axis - reported value determined by offset function
   x=x-12;
   y=y-18;
   z=z-245;
 //scaling in g's
   //scale = total g range / 1024
   //-2g to +2g = 4/1024 = 0.00390625, -4g to +4g = 8/1024 = 0.0078125
   //-8g to +8g = 16/1024 = 0.015625, -16g to +16g = 32/1024 = 0.03125
   Gx = x * 0.0039;
   Gy = y * 0.0039;
   Gz = z * 0.0039;
  } 
 
 
//----------PrintHeadings----------
//This function labels the columns of data (for
//  convenience). It is not necessary and can be
//  removed if one does not want labels.
 void PrintHeadings(void){
    Serial.print("X Raw");
   Serial.print("\t");
 
   Serial.print("Y Raw");
   Serial.print("\t");
 
   Serial.print("Z Raw");
   Serial.print("\t");
 
   Serial.print("X Scaled");
   Serial.print("\t");
 
   Serial.print("Y Scaled");
   Serial.print("\t");
 
   Serial.println("Z Scaled"); 
   Serial.println(""); 
 
 }
 
//----------PrintData----------
//This function prints out all the raw and scaled
//  accelerometer values.
 
 void PrintData (void){
   Serial.print(x);      
   Serial.print("\t");
 
   Serial.print(y);    
   Serial.print("\t");
 
   Serial.print(z);    
   Serial.print("\t");
 
   Serial.print(Gx,3);     
   Serial.print("\t");
 
   Serial.print(Gy,3);
   Serial.print("\t");
 
   Serial.print(Gz,3);
   Serial.println("");
 }
 
 
 
//----------DetermineOffsets----------
//This function must be used when accelerometer
//  is placed in its initial position so that
//  offsets can be determined, and corrected for
//  in the Scale function. 
// This function should be run initially, and 
//  the rest of the main loop should be ignored,
//  and the line calling DetermineOffsets should be 
//  uncommented.
//  Take the output values from this function and 
//  plug them in to the offset correcting part of 
//  the scale function. Once this has been done,
//  uncomment the rest of the loop, and comment out
//  the line calling DetermineOffsets.
  void DetermineOffsets(void){
    int x,y,z;  
    adxl.readAccel(&x, &y, &z); 
    Serial.println(x);
    Serial.println(y);
    Serial.println(z);
    delay(3600);
  }



Personal tools