Rev 2381 Rev 2394
1 /* IMU Fusion Board - ADXL345 & IMU3000 1 /* IMU Fusion Board - ADXL345 & IMU3000
2 Example Arduino Sketch to read the Gyro and Accelerometer Data 2 Example Arduino Sketch to read the Gyro and Accelerometer Data
3 3
4 Written by www.hobbytronics.co.uk 4 Written by www.hobbytronics.co.uk
5 See the latest version at www.hobbytronics.co.uk/arduino-adxl345-imu3000 5 See the latest version at www.hobbytronics.co.uk/arduino-adxl345-imu3000
6 08-Apr-2011 6 08-Apr-2011
7 */ 7 */
8   8  
9 #define GYRO 0x68 // gyro I2C address 9 #define GYRO 0x68 // gyro I2C address
10 #define REG_GYRO_X 0x1D // IMU-3000 Register address for GYRO_XOUT_H 10 #define REG_GYRO_X 0x1D // IMU-3000 Register address for GYRO_XOUT_H
11 #define ACCEL 0x53 // Accel I2c Address 11 #define ACCEL 0x53 // Accel I2c Address
12 #define ADXL345_POWER_CTL 0x2D 12 #define ADXL345_POWER_CTL 0x2D
13   13  
14 byte buffer[12]; // Array to store ADC values 14 byte buffer[12]; // Array to store ADC values
15 int gyro_x; 15 int gyro_x;
16 int gyro_y; 16 int gyro_y;
17 int gyro_z; 17 int gyro_z;
18 int accel_x; 18 int accel_x;
19 int accel_y; 19 int accel_y;
20 int accel_z; 20 int accel_z;
21 int i; 21 int i;
22 #include <Wire.h> 22 #include <Wire.h>
23   23  
24 void setup() 24 void setup()
25 { 25 {
26 Serial.begin(9600); 26 Serial.begin(9600);
27 27
28 Wire.begin(); 28 Wire.begin();
29 // Set Gyro settings 29 // Set Gyro settings
30 // Sample Rate 1kHz, Filter Bandwidth 42Hz, Gyro Range 500 d/s 30 // Sample Rate 1kHz, Filter Bandwidth 42Hz, Gyro Range 500 d/s
31 writeTo(GYRO, 0x16, 0x0B); 31 writeTo(GYRO, 0x16, 0x0B);
32 //set accel register data address 32 //set accel register data address
33 writeTo(GYRO, 0x18, 0x32); 33 writeTo(GYRO, 0x18, 0x32);
34 // set accel i2c slave address 34 // set accel i2c slave address
35 writeTo(GYRO, 0x14, ACCEL); 35 writeTo(GYRO, 0x14, ACCEL);
36 36
37 // Set passthrough mode to Accel so we can turn it on 37 // Set passthrough mode to Accel so we can turn it on
38 writeTo(GYRO, 0x3D, 0x08); 38 writeTo(GYRO, 0x3D, 0x08);
39 // set accel power control to 'measure' 39 // set accel power control to 'measure'
40 writeTo(ACCEL, ADXL345_POWER_CTL, 8); 40 writeTo(ACCEL, ADXL345_POWER_CTL, 8);
41 //cancel pass through to accel, gyro will now read accel for us 41 //cancel pass through to accel, gyro will now read accel for us
42 writeTo(GYRO, 0x3D, 0x28); 42 writeTo(GYRO, 0x3D, 0x28);
43   43  
44 } 44 }
45   45  
46 // Write a value to address register on device 46 // Write a value to address register on device
47 void writeTo(int device, byte address, byte val) { 47 void writeTo(int device, byte address, byte val) {
48 Wire.beginTransmission(device); // start transmission to device 48 Wire.beginTransmission(device); // start transmission to device
49 Wire.send(address); // send register address 49 Wire.send(address); // send register address
50 Wire.send(val); // send value to write 50 Wire.send(val); // send value to write
51 Wire.endTransmission(); // end transmission 51 Wire.endTransmission(); // end transmission
52 } 52 }
53   53  
54 void loop() 54 void loop()
55 { 55 {
56 // Read the Gyro X, Y and Z and Accel X, Y and Z all through the gyro 56 // Read the Gyro X, Y and Z and Accel X, Y and Z all through the gyro
57 57
58 // First set the register start address for X on Gyro 58 // First set the register start address for X on Gyro
59 Wire.beginTransmission(GYRO); 59 Wire.beginTransmission(GYRO);
60 Wire.send(REG_GYRO_X); //Register Address GYRO_XOUT_H 60 Wire.send(REG_GYRO_X); //Register Address GYRO_XOUT_H
61 Wire.endTransmission(); 61 Wire.endTransmission();
62   62  
63 // New read the 12 data bytes 63 // New read the 12 data bytes
64 Wire.beginTransmission(GYRO); 64 Wire.beginTransmission(GYRO);
65 Wire.requestFrom(GYRO,12); // Read 12 bytes 65 Wire.requestFrom(GYRO,12); // Read 12 bytes
66 i = 0; 66 i = 0;
67 while(Wire.available()) 67 while(Wire.available())
68 { 68 {
69 buffer[i] = Wire.receive(); 69 buffer[i] = Wire.receive();
70 i++; 70 i++;
71 } 71 }
72 Wire.endTransmission(); 72 Wire.endTransmission();
73   73  
74 //Combine bytes into integers 74 //Combine bytes into integers
75 // Gyro format is MSB first 75 // Gyro format is MSB first
76 gyro_x = buffer[0] << 8 | buffer[1]; 76 gyro_x = buffer[0] << 8 | buffer[1];
77 gyro_y = buffer[2] << 8 | buffer[3]; 77 gyro_y = buffer[2] << 8 | buffer[3];
78 gyro_z = buffer[4] << 8 | buffer[5]; 78 gyro_z = buffer[4] << 8 | buffer[5];
79 // Accel is LSB first. Also because of orientation of chips 79 // Accel is LSB first. Also because of orientation of chips
80 // accel y output is in same orientation as gyro x 80 // accel y output is in same orientation as gyro x
81 // and accel x is gyro -y 81 // and accel x is gyro -y
82 accel_y = buffer[7] << 8 | buffer[6]; 82 accel_y = buffer[7] << 8 | buffer[6];
83 accel_x = buffer[9] << 8 | buffer[8]; 83 accel_x = buffer[9] << 8 | buffer[8];
84 accel_z = buffer[11] << 8 | buffer[10]; 84 accel_z = buffer[11] << 8 | buffer[10];
85   85  
86 // Print out what we have 86 // Print out what we have
87 Serial.print(gyro_x); // echo the number received to screen 87 Serial.print(gyro_x); // echo the number received to screen
88 Serial.print(","); 88 Serial.print(" ");
89 Serial.print(gyro_y); // echo the number received to screen 89 Serial.print(gyro_y); // echo the number received to screen
90 Serial.print(","); 90 Serial.print(" ");
91 Serial.print(gyro_z); // echo the number received to screen 91 Serial.print(gyro_z); // echo the number received to screen
92 Serial.print(","); 92 Serial.print(" ");
93 Serial.print(accel_x); // echo the number received to screen 93 Serial.print(accel_x); // echo the number received to screen
94 Serial.print(","); 94 Serial.print(" ");
95 Serial.print(accel_y); // echo the number received to screen 95 Serial.print(accel_y); // echo the number received to screen
96 Serial.print(","); 96 Serial.print(" ");
97 Serial.print(accel_z); // echo the number received to screen -  
98   -  
99 Serial.println(""); // prints carriage return 97 Serial.println(accel_z); // prints the number and carriage return
100 delay(400); // wait for a second 98 delay(100); // some wait before new reading
101 } 99 }