-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBH1745-Arduino.ino
59 lines (44 loc) · 1.28 KB
/
BH1745-Arduino.ino
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
#include <Wire.h>
#define BH1745_ADDR 0x38 // The I2C address of the sensor
#define SYSTEM_CONTROL 0x40
#define MODE_CONTROL1 0x41
#define MODE_CONTROL2 0x42
#define MODE_CONTROL3 0x44
void setup() {
Wire.begin();
Serial.begin(9600);
// Reset the sensor and enable it
writeRegister(SYSTEM_CONTROL, 0b10100000);
delay(10);
// Set measurement time to 320ms, and ADC gain to x1.
writeRegister(MODE_CONTROL1, 0b00000001);
// Enable RGB measurement.
writeRegister(MODE_CONTROL2, 0b00010000);
}
void loop() {
uint16_t red = readColorData(0x50);
uint16_t green = readColorData(0x52);
uint16_t blue = readColorData(0x54);
Serial.print("Red: ");
Serial.print(red);
Serial.print(", Green: ");
Serial.print(green);
Serial.print(", Blue: ");
Serial.println(blue);
delay(500);
}
void writeRegister(uint8_t reg, uint8_t value) {
Wire.beginTransmission(BH1745_ADDR);
Wire.write(reg);
Wire.write(value);
Wire.endTransmission();
}
uint16_t readColorData(uint8_t reg) {
Wire.beginTransmission(BH1745_ADDR);
Wire.write(reg);
Wire.endTransmission();
if (Wire.requestFrom((int)BH1745_ADDR, (int)2) == 2) {
return (Wire.read() | (Wire.read() << 8));
}
return -1;
}