forked from sableangle/Unity2Arduino-Android-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGY_271.ino
More file actions
135 lines (105 loc) · 3.33 KB
/
GY_271.ino
File metadata and controls
135 lines (105 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* FILE: ARD_GY291_ADXL345_Example
DATE: 02/04/14
VERSION: 0.1
REVISIONS:
02/04/14 Created version 0.1
This is an example of how to use the Hobby Components GY-291 accelerometer module
(HCMODU0060). This module is based on the Analog Devices ADXL345 triple axis
accelerometer device.
This example sketch will demonstrate how perform a basic initialisation and then
will continually read each of the 3 axis registers and output them to the serial port.
PINOUT:
MODULE` Arduino
GND GND
VCC +3.3V
CS +3.3V*
INT1 N/A
INT2 N/A
SD0 N/A
SDA A4*
SCL A5*
*Please note that the ADXL345 opperates at 3.3V (via a 3.3V regulator) and these
pins should not be driven above 3.6V therefore you may require level shifters to
ensure safe opperation.
You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used for the purpose of promoting or selling products
that directly compete with Hobby Components Ltd's own range of products.
THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/
/* Include the standard wire library */
#include <Wire.h>
/* Alternate I2C address of the module */
#define I2C_Add 0x53
/* ADXL345 register addresses */
#define POWER_CTL 0x2D
#define DATA_FORMAT 0x31
#define X_Axis 0x32
#define Y_Axis 0x34
#define Z_Axis 0x36
/* Accelerometer range modes */
#define RANGE_2g 0
#define RANGE_4g 1
#define RANGE_8g 2
#define RANGE_16g 3
void setup()
{
/* Initialise the I2C bus */
Wire.begin();
/* Initialise the serial interface */
Serial.begin(9600);
/* Initialise the ADXL345 */
Init_ADXL345(RANGE_2g);
}
/* Main program */
void loop()
{
/* Continually read and output all 3 axis to the serial port */
Serial.print("X: ");
Serial.print(Read_Axis(X_Axis));
Serial.print(" Y: ");
Serial.print(Read_Axis(Y_Axis));
Serial.print(" Z: ");
Serial.println(Read_Axis(Z_Axis));
delay(1000);
}
/* Read one of the 3 axis via the I2C interface */
int Read_Axis(byte axis)
{
int Data;
Wire.beginTransmission(I2C_Add);
Wire.write(axis);
Wire.endTransmission();
Wire.beginTransmission(I2C_Add);
Wire.requestFrom(I2C_Add, 2);
/* If data is available then read it (2 bytes) */
if(Wire.available())
{
Data = (int)Wire.read();
Data = Data | (Wire.read() << 8);
}else
{
Data = 0;
}
Wire.endTransmission();
return Data;
}
/* Initialise the ADXL345 */
void Init_ADXL345(byte range)
{
Wire.beginTransmission(I2C_Add);
/* Set the sensitivity of the module */
Wire.write(DATA_FORMAT);
Wire.write(range);
Wire.endTransmission();
/* Put the module into measurement mode to start taking measurements */
Wire.beginTransmission(I2C_Add);
Wire.write(POWER_CTL);
Wire.write(0x08);
Wire.endTransmission();
}