[아두이노] 자이로센서 신호 받기
Hardware/Arduino2013. 6. 17. 10:58
이번에도 역시 참조 URL 만 넣고 공유합니다.
저번 LCD 화면 뿌리기에서 추가된 버전입니다.
제가 구매한 자이로센서는 GY-50 L3G4200 gyroscope 이구요.
아래 사이트에서 구매 하였습니다.
http://www.devicemart.co.kr/goods/view.php?seq=38316
배선은 이렇게 하였는데…
나중에 실수로 SU0이 뽑혔는데… 작동하는데 큰 문제는 없더군요.
소스는 아래와 같으며….
1:
2: // include the library code:
3: #include <LiquidCrystal.h>
4: #include <Wire.h>
5:
6: #define CTRL_REG1 0x20
7: #define CTRL_REG2 0x21
8: #define CTRL_REG3 0x22
9: #define CTRL_REG4 0x23
10: #define CTRL_REG5 0x24
11:
12: int L3G4200D_Address = 105; //I2C address of the L3G4200D
13:
14: int x;
15: int y;
16: int z;
17:
18: static float adc_data_float_value = 0.0f;
19:
20: // initialize the library with the numbers of the interface pins
21: LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
22:
23:
24: int backLight = 13; // pin 13 will control the backlight
25:
26: void setup() {
27:
28: Wire.begin();
29: setupL3G4200D(2000); // Configure L3G4200 - 250, 500 or 2000 deg/sec
30:
31: delay(1500); //wait for the sensor to be ready
32:
33: pinMode(backLight, OUTPUT);
34: digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
35:
36: // set up the LCD's number of columns and rows:
37: lcd.begin(16, 2);
38: // Print a message to the LCD.
39: lcd.print("Current illumination:");
40: }
41:
42: void loop() {
43:
44: lcd.clear();
45:
46: getGyroValues(); // This will update x, y, and z with new values
47:
48: lcd.print("X:");
49: lcd.print(x);
50:
51: lcd.print(" Y:");
52: lcd.print(y);
53:
54: lcd.print(" Z:");
55: lcd.println(z);
56:
57: delay(1000); //Just here to slow down the serial to make it more readable
58: }
59:
60: void getGyroValues(){
61:
62: byte xMSB = readRegister(L3G4200D_Address, 0x29);
63: byte xLSB = readRegister(L3G4200D_Address, 0x28);
64: x = ((xMSB << 8) | xLSB);
65:
66: byte yMSB = readRegister(L3G4200D_Address, 0x2B);
67: byte yLSB = readRegister(L3G4200D_Address, 0x2A);
68: y = ((yMSB << 8) | yLSB);
69:
70: byte zMSB = readRegister(L3G4200D_Address, 0x2D);
71: byte zLSB = readRegister(L3G4200D_Address, 0x2C);
72: z = ((zMSB << 8) | zLSB);
73: }
74:
75: int setupL3G4200D(int scale){
76: //From Jim Lindblom of Sparkfun's code
77:
78: // Enable x, y, z and turn off power down:
79: writeRegister(L3G4200D_Address, CTRL_REG1, 0b00001111);
80:
81: // If you'd like to adjust/use the HPF, you can edit the line below to configure CTRL_REG2:
82: writeRegister(L3G4200D_Address, CTRL_REG2, 0b00000000);
83:
84: // Configure CTRL_REG3 to generate data ready interrupt on INT2
85: // No interrupts used on INT1, if you'd like to configure INT1
86: // or INT2 otherwise, consult the datasheet:
87: writeRegister(L3G4200D_Address, CTRL_REG3, 0b00001000);
88:
89: // CTRL_REG4 controls the full-scale range, among other things:
90:
91: if(scale == 250){
92: writeRegister(L3G4200D_Address, CTRL_REG4, 0b00000000);
93: }else if(scale == 500){
94: writeRegister(L3G4200D_Address, CTRL_REG4, 0b00010000);
95: }else{
96: writeRegister(L3G4200D_Address, CTRL_REG4, 0b00110000);
97: }
98:
99: // CTRL_REG5 controls high-pass filtering of outputs, use it
100: // if you'd like:
101: writeRegister(L3G4200D_Address, CTRL_REG5, 0b00000000);
102: }
103:
104: void writeRegister(int deviceAddress, byte address, byte val) {
105: Wire.beginTransmission(deviceAddress); // start transmission to device
106: Wire.write(address); // send register address
107: Wire.write(val); // send value to write
108: Wire.endTransmission(); // end transmission
109: }
110:
111: int readRegister(int deviceAddress, byte address){
112:
113: int v;
114: Wire.beginTransmission(deviceAddress);
115: Wire.write(address); // register to read
116: Wire.endTransmission();
117:
118: Wire.requestFrom(deviceAddress, 1); // read a byte
119:
120: while(!Wire.available()) {
121: // waiting
122: }
123:
124: v = Wire.read();
125: return v;
126: }
'Hardware > Arduino' 카테고리의 다른 글
[Arudino] 아두이노 LCD 연결해서 화면에 글자 쓰기 (0) | 2013.06.14 |
---|