Line 1... |
Line 1... |
1 |
// I2C Light Sensor |
1 |
// I2C Light Sensor |
2 |
|
2 |
|
3 |
#include <Wire.h> |
3 |
#include <Wire.h> |
4 |
|
4 |
|
5 |
#define address 0x44 // A0 = L |
5 |
#define address 0x44 // A0 = L |
- |
|
6 |
#define SENSE_VIS 0 |
- |
|
7 |
#define SENSE_IR 1 |
6 |
|
8 |
|
7 |
|
9 |
|
8 |
void setup() |
10 |
void setup() |
9 |
{ |
11 |
{ |
10 |
Wire.begin(); // join i2c bus (address optional for master) |
12 |
Wire.begin(); // join i2c bus (address optional for master) |
Line 24... |
Line 26... |
24 |
digitalWrite(3, LOW); // set the LED off |
26 |
digitalWrite(3, LOW); // set the LED off |
25 |
delay(500); |
27 |
delay(500); |
26 |
} |
28 |
} |
27 |
|
29 |
|
28 |
|
30 |
|
29 |
int set_light_sensor() |
31 |
int set_light_sensor(int mode) |
30 |
{ |
32 |
{ |
- |
|
33 |
int command; |
- |
|
34 |
|
- |
|
35 |
switch (mode) |
- |
|
36 |
{ |
- |
|
37 |
case SENSE_VIS: |
- |
|
38 |
{ |
- |
|
39 |
command=0b11000000; // setup (eye light sensing; one time measurement; measurement range 1) |
- |
|
40 |
break; |
- |
|
41 |
} |
- |
|
42 |
|
- |
|
43 |
case SENSE_IR: |
- |
|
44 |
{ |
- |
|
45 |
command=0b11100000; // setup (eye light sensing; measurement range 2 [4000 lx]) |
- |
|
46 |
break; |
- |
|
47 |
} |
- |
|
48 |
|
- |
|
49 |
default: |
- |
|
50 |
return 3; |
- |
|
51 |
} |
- |
|
52 |
|
31 |
// Setup device |
53 |
// Setup device |
32 |
Wire.beginTransmission(address); |
54 |
Wire.beginTransmission(address); |
33 |
Wire.send(0x00); // sends address |
55 |
Wire.send(0x00); // sends address |
34 |
Wire.send(0b11100000); // setup (eye light sensing; measurement range 2 [4000 lx]) |
56 |
Wire.send(command); // setup (eye light sensing; one time measurement; measurement range 1) |
35 |
Wire.endTransmission(); // stop transmitting |
57 |
Wire.endTransmission(); // stop transmitting |
36 |
|
- |
|
37 |
} |
58 |
} |
38 |
|
59 |
|
39 |
int get_light_measurement() |
60 |
int get_light_measurement() |
40 |
{ |
61 |
{ |
41 |
int lux=0; |
62 |
int lux=0; |
Line 69... |
Line 90... |
69 |
|
90 |
|
70 |
void loop() |
91 |
void loop() |
71 |
{ |
92 |
{ |
72 |
int lux=0; |
93 |
int lux=0; |
73 |
|
94 |
|
74 |
/* Serial.print("lux="); |
- |
|
75 |
|
- |
|
76 |
// Setup device |
- |
|
77 |
digitalWrite(5, HIGH); |
95 |
set_light_sensor(SENSE_VIS); |
78 |
|
- |
|
79 |
Wire.beginTransmission(address); |
- |
|
80 |
Wire.send(0x00); // sends address |
- |
|
81 |
Wire.send(0b11000000); // setup (eye light sensing; one time measurement; measurement range 1) |
- |
|
82 |
Wire.endTransmission(); // stop transmitting |
- |
|
83 |
|
96 |
|
84 |
// Delay for measurement |
97 |
// Delay for measurement |
- |
|
98 |
led_blink(); |
85 |
|
99 |
|
86 |
digitalWrite(5, LOW); // set the LED off |
- |
|
87 |
delay(500); |
- |
|
88 |
digitalWrite(5, HIGH); // set the LED on |
- |
|
89 |
delay(500); |
- |
|
90 |
|
- |
|
91 |
// Connect to device and set register address |
- |
|
92 |
Wire.beginTransmission(address); |
- |
|
93 |
Wire.send(0x00); // sends address |
- |
|
94 |
Wire.endTransmission(); // stop transmitting |
- |
|
95 |
// Connect to device and request one byte |
- |
|
96 |
Wire.beginTransmission(address); |
- |
|
97 |
Wire.requestFrom(address, 1); |
- |
|
98 |
data = Wire.receive(); |
- |
|
99 |
Wire.endTransmission(); // stop transmitting |
- |
|
100 |
// Serial.print(data, HEX); |
- |
|
101 |
|
- |
|
102 |
// Serial.print(" LSB="); |
- |
|
103 |
// Connect to device and set register address |
100 |
// Connect to device and set register address |
104 |
Wire.beginTransmission(address); |
101 |
Wire.beginTransmission(address); |
105 |
Wire.send(0x01); // sends address |
102 |
Wire.send(0x00); // sends address (command register) |
106 |
Wire.endTransmission(); // stop transmitting |
103 |
Wire.endTransmission(); // stop transmitting |
- |
|
104 |
|
107 |
// Connect to device and request one byte |
105 |
// Connect to device and request command register settings |
108 |
Wire.beginTransmission(address); |
106 |
Wire.beginTransmission(address); |
109 |
Wire.requestFrom(address, 1); |
107 |
Wire.requestFrom(address, 1); |
110 |
data = Wire.receive(); |
108 |
data = Wire.receive(); |
111 |
Wire.endTransmission(); // stop transmitting |
109 |
Wire.endTransmission(); // stop transmitting |
112 |
// Serial.print(data, HEX); |
110 |
Serial.print(data, BIN); |
113 |
lux=data; |
- |
|
114 |
|
111 |
|
115 |
// Serial.print(" MSB="); |
112 |
Serial.print("lux="); |
116 |
// Connect to device and set register address |
- |
|
117 |
Wire.beginTransmission(address); |
- |
|
118 |
Wire.send(0x02); // sends address |
- |
|
119 |
Wire.endTransmission(); // stop transmitting |
- |
|
120 |
// Connect to device and request one byte |
- |
|
121 |
Wire.beginTransmission(address); |
- |
|
122 |
Wire.requestFrom(address, 1); |
- |
|
123 |
data = Wire.receive(); |
- |
|
124 |
Wire.endTransmission(); // stop transmitting |
- |
|
125 |
// Serial.print(data, HEX); |
113 |
Serial.println((unsigned)get_light_measurement(), DEC); |
126 |
lux+=data*256; |
- |
|
127 |
|
114 |
|
128 |
Serial.print((unsigned)lux, DEC); |
- |
|
129 |
*/ |
- |
|
130 |
|
115 |
|
131 |
set_light_sensor(); |
116 |
set_light_sensor(SENSE_IR); |
132 |
|
117 |
|
133 |
// Delay for measurement |
118 |
// Delay for measurement |
134 |
led_blink(); |
119 |
led_blink(); |
135 |
|
120 |
|
136 |
// Connect to device and set register address |
- |
|
137 |
Wire.beginTransmission(address); |
- |
|
138 |
Wire.send(0x00); // sends address |
- |
|
139 |
Wire.endTransmission(); // stop transmitting |
- |
|
140 |
// Connect to device and request one byte |
- |
|
141 |
Wire.beginTransmission(address); |
- |
|
142 |
Wire.requestFrom(address, 1); |
- |
|
143 |
data = Wire.receive(); |
- |
|
144 |
Wire.endTransmission(); // stop transmitting |
- |
|
145 |
|
- |
|
146 |
// data print |
121 |
// data print |
147 |
Serial.print(" luxIR="); |
122 |
Serial.print("luxIR="); |
148 |
Serial.println((unsigned)get_light_measurement(), DEC); |
123 |
Serial.println((unsigned)get_light_measurement(), DEC); |
149 |
} |
124 |
} |
150 |
|
125 |
|