1
/************************************************************************
2
* This program is free software; you can redistribute it and/or modify *
3
* it under the terms of the GNU License V2. *
4
* This program is distributed in the hope that it will be useful, *
5
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
6
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
7
* GNU General Public License, version 2 for more details *
9
* Bare bones ADXL345 i2c example for Arduino 1.0 *
10
* by Jens C Brynildsen <http://www.flashgamer.com> *
11
* This version is not reliant of any external lib *
12
* (Adapted for Arduino 1.0 from http://code.google.com/p/adxl345driver)*
14
* Demonstrates use of ADXL345 (using the Sparkfun ADXL345 breakout) *
15
* with i2c communication. Datasheet: *
16
* http://www.sparkfun.com/datasheets/Sensors/Accelerometer/ADXL345.pdf *
17
* If you need more advanced features such as freefall and tap *
18
* detection, check out: *
19
* https://github.com/jenschr/Arduino-libraries *
20
***********************************************************************/
22
// Cabling for i2c using Sparkfun breakout with an Arduino Uno / Duemilanove:
23
// Arduino <-> Breakout board
30
// Cabling for i2c using Sparkfun breakout with an Arduino Mega / Mega ADK:
31
// Arduino <-> Breakout board
40
#define DEVICE (0x53) // Device address as specified in data sheet
44
char POWER_CTL = 0x2D; //Power Control Register
45
char DATA_FORMAT = 0x31;
46
char DATAX0 = 0x32; //X-Axis Data 0
47
char DATAX1 = 0x33; //X-Axis Data 1
48
char DATAY0 = 0x34; //Y-Axis Data 0
49
char DATAY1 = 0x35; //Y-Axis Data 1
50
char DATAZ0 = 0x36; //Z-Axis Data 0
51
char DATAZ1 = 0x37; //Z-Axis Data 1
54
void writeTo(byte address, byte val) {
55
Wire.beginTransmission(DEVICE); // start transmission to device
56
Wire.write(address); // send register address
57
Wire.write(val); // send value to write
58
Wire.endTransmission(); // end transmission
61
// Reads num bytes starting from address register on device in to _buff array
62
void readFrom(byte address, int num, byte _buff[])
64
Wire.beginTransmission(DEVICE); // start transmission to device
65
Wire.write(address); // sends address to read from
66
Wire.endTransmission(); // end transmission
68
Wire.beginTransmission(DEVICE); // start transmission to device
69
Wire.requestFrom(DEVICE, num); // request 6 bytes from device
71
// device may send less than requested (abnormal)
73
while(Wire.available())
75
_buff[i] = Wire.read(); // receive a byte
78
Wire.endTransmission(); // end transmission
84
// join i2c bus (address optional for master)
86
// start serial for output. Make sure you set your Serial Monitor to the
89
Serial.print("init\n");
91
//Put the ADXL345 into +/- 4G range by writing the value 0x01 to the
92
//DATA_FORMAT register.
93
writeTo(DATA_FORMAT, 0x01);
94
//Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL
96
writeTo(POWER_CTL, 0x08);
100
//read the acceleration data from the ADXL345
101
uint8_t howManyBytesToRead = 6;
102
readFrom( DATAX0, howManyBytesToRead, _buff);
104
// each axis reading comes in 10 bit resolution, ie 2 bytes. Least
105
// Significat Byte first!! thus we are converting both bytes in to one int
106
int x = (((int)_buff[1]) << 8) | _buff[0];
107
int y = (((int)_buff[3]) << 8) | _buff[2];
108
int z = (((int)_buff[5]) << 8) | _buff[4];
111
Serial.print(" y: ");
113
Serial.print(" z: ");
121
readAccel(); // read the x/y/z tilt
122
delay(500); // only read every 0,5 seconds