Skip to content

Commit 41bd61e

Browse files
committed
added hardware offsets
1 parent 7588077 commit 41bd61e

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

Using_ADXL345/Using_ADXL345.ino

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
11
/*
2-
I2C communication protocol
3-
ADXL345 Arduino Uno/Nano
2+
I2C communication protocol
3+
ADXL345 Arduino Uno/Nano
44
GND GND
55
Vin 3.3V
66
SDA A4
77
SDO A5
8-
Reference:
8+
Reference:
99
http://codeyoung.blogspot.in/2009/11/adxl345-accelerometer-breakout-board.html
1010
http://morf.lv/modules.php?name=tutorials&lasit=31
1111
http://www.analog.com/media/en/technical-documentation/data-sheets/ADXL345.pdf
12-
12+
1313
*/
1414
#include<Wire.h>
1515

1616
#define DEVICE (0x53) //Device Address
1717
const int numBytes = 6; //Number of bytes to receive(two bytes for each axis)
1818

1919
byte values[6]; //To store the data received
20-
int rawx,rawy,rawz;
20+
int rawx, rawy, rawz;
2121

22-
typedef struct{
22+
typedef struct {
2323
double x;
2424
double y;
2525
double z;
26-
}accel;
27-
26+
} accel;
2827
accel a;
2928

30-
void setup(){
29+
void setup() {
3130
Wire.begin();
3231
Serial.begin(9600);
3332

3433
writeTo(DEVICE, 0x2D, 0);
3534
writeTo(DEVICE, 0x2D, 16);
3635
writeTo(DEVICE, 0x2D, 8);
3736
writeTo(DEVICE, 0x31, 11); // 11 for 13 bit resolution and ±16 g
37+
init(60, 45, 127);
3838
}
3939

40-
void loop(){
40+
void loop() {
4141
readFrom(DEVICE, 0x32, numBytes, values);
4242
rawx = (((int)values[1]) << 8) | values[0];
4343
rawy = (((int)values[3]) << 8) | values[2];
4444
rawz = (((int)values[5]) << 8) | values[4];
4545

46-
a.x = rawx*0.00390625;
47-
a.y = rawy*0.00390625;
48-
a.z = rawz*0.00390625;
46+
a.x = rawx * 0.00390625;
47+
a.y = rawy * 0.00390625;
48+
a.z = rawz * 0.00390625;
4949

50-
Serial.print(a.x, 4); Serial.print(" ");
50+
Serial.print(a.x, 4); Serial.print(" ");
5151
Serial.print(a.y, 4); Serial.print(" "); Serial.println(a.z, 4);
5252
delay(30);
5353
}

Using_ADXL345/offsets.ino

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*The offset registers are each 8 bit. I used a 13 bit resoulution with 3.90625 mg/LSB, but for offset adjustment
2+
scale factor is 15.6 mg/LSB.
3+
Also z axis acceleration was around 5m/s^2 and could not be adjusted by hardware offset because z_offset was 340 and
4+
is out of range for a byte.
5+
*/
6+
void init(byte x_offset, byte y_offset, byte z_offset)
7+
{
8+
writeTo(DEVICE, 0x1E, x_offset);
9+
writeTo(DEVICE, 0x1F, y_offset);
10+
writeTo(DEVICE, 0x20, z_offset);
11+
}
12+

0 commit comments

Comments
 (0)