1 |
// I2C Light Sensor |
1 |
// I2C Light Sensor |
2 |
|
2 |
|
3 |
#include <Wire.h> |
3 |
#include <Wire.h> |
- |
|
4 |
#include <math.h> |
4 |
|
5 |
|
5 |
#define address 0x44 // A0 = L |
6 |
#define address 0x44 // A0 = L |
- |
|
7 |
#define SENSE_VIS 0 |
- |
|
8 |
#define SENSE_IR 1 |
- |
|
9 |
|
- |
|
10 |
#define LIGHT_AUTORANGE 0 |
- |
|
11 |
#define LIGHT_RANGE1 1 |
- |
|
12 |
#define LIGHT_RANGE2 2 |
- |
|
13 |
#define LIGHT_RANGE3 3 |
- |
|
14 |
#define LIGHT_RANGE4 4 |
- |
|
15 |
|
6 |
|
16 |
|
7 |
void setup() |
17 |
void setup() |
8 |
{ |
18 |
{ |
9 |
Wire.begin(); // join i2c bus (address optional for master) |
19 |
Wire.begin(); // join i2c bus (address optional for master) |
- |
|
20 |
// I2C pins PD4, PD5 |
- |
|
21 |
// |
10 |
pinMode(3, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela |
22 |
pinMode(3, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela |
11 |
pinMode(5, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela |
23 |
pinMode(5, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela |
12 |
Serial.begin(9600); // Zmerena intenzita osvetleni se bude vypisovat na seriovou linku |
24 |
Serial.begin(9600); // Zmerena intenzita osvetleni se bude vypisovat na seriovou linku |
13 |
} |
25 |
} |
14 |
|
26 |
|
15 |
int data = 0; |
27 |
int data = 0; |
- |
|
28 |
int light_sensor_setup; |
16 |
|
29 |
|
17 |
void loop() |
30 |
void led_blink() |
18 |
{ |
31 |
{ |
- |
|
32 |
digitalWrite(3, HIGH); // set the LED on |
19 |
int lux=0; |
33 |
delay(500); |
20 |
|
- |
|
- |
|
34 |
digitalWrite(3, LOW); // set the LED off |
21 |
Serial.print("lux="); |
35 |
delay(500); |
22 |
|
36 |
} |
23 |
// Setup device |
- |
|
24 |
digitalWrite(5, HIGH); |
- |
|
25 |
|
37 |
|
26 |
Wire.beginTransmission(address); |
- |
|
27 |
Wire.send(0x00); // sends address |
- |
|
28 |
Wire.send(0b11000000); // setup (eye light sensing; one time measurement; measurement range 1) |
- |
|
29 |
Wire.endTransmission(); // stop transmitting |
- |
|
30 |
|
38 |
|
31 |
// Delay for measurement |
39 |
int set_light_sensor(int mode) |
- |
|
40 |
{ |
- |
|
41 |
int command; |
- |
|
42 |
|
- |
|
43 |
switch (mode) |
32 |
{ |
44 |
{ |
33 |
long n; |
45 |
case SENSE_VIS: |
34 |
for(n=0;n<66000;n++) |
- |
|
35 |
{ |
46 |
{ |
36 |
digitalWrite(5, LOW); // set the LED off |
47 |
command=0b11000000; // setup (eye light sensing; one time measurement; measurement range 1) |
37 |
// delay(500); |
- |
|
38 |
digitalWrite(5, HIGH); // set the LED on |
- |
|
39 |
delayMicroseconds(100); |
- |
|
40 |
// delay(500); |
48 |
break; |
41 |
} |
49 |
} |
42 |
} |
- |
|
43 |
|
50 |
|
- |
|
51 |
case SENSE_IR: |
- |
|
52 |
{ |
- |
|
53 |
command=0b11100000; // setup (eye light sensing; measurement range 2 [4000 lx]) |
- |
|
54 |
break; |
- |
|
55 |
} |
- |
|
56 |
|
- |
|
57 |
default: |
- |
|
58 |
return 3; |
- |
|
59 |
} |
44 |
|
60 |
|
45 |
// Connect to device and set register address |
61 |
// Setup device |
46 |
Wire.beginTransmission(address); |
62 |
Wire.beginTransmission(address); |
47 |
Wire.send(0x00); // sends address |
63 |
Wire.write(0x00); // sends address |
48 |
Wire.endTransmission(); // stop transmitting |
64 |
Wire.write(command); // setup (eye light sensing; one time measurement; measurement range 1) |
49 |
// Connect to device and request one byte |
- |
|
50 |
Wire.beginTransmission(address); |
- |
|
51 |
Wire.requestFrom(address, 1); |
- |
|
52 |
data = Wire.receive(); |
- |
|
53 |
Wire.endTransmission(); // stop transmitting |
65 |
Wire.endTransmission(); // stop transmitting |
54 |
// Serial.print(data, HEX); |
- |
|
55 |
|
66 |
|
56 |
// Serial.print(" LSB="); |
67 |
|
57 |
// Connect to device and set register address |
68 |
// Connect to device and set register address |
58 |
Wire.beginTransmission(address); |
69 |
Wire.beginTransmission(address); |
59 |
Wire.send(0x01); // sends address |
70 |
Wire.write(0x00); // sends address (command register) |
60 |
Wire.endTransmission(); // stop transmitting |
71 |
Wire.endTransmission(); // stop transmitting |
- |
|
72 |
|
61 |
// Connect to device and request one byte |
73 |
// verify written command byte |
62 |
Wire.beginTransmission(address); |
74 |
Wire.beginTransmission(address); |
63 |
Wire.requestFrom(address, 1); |
75 |
Wire.requestFrom(address, 1); |
64 |
data = Wire.receive(); |
76 |
if (command != Wire.read()) |
- |
|
77 |
{ |
- |
|
78 |
return 4; |
- |
|
79 |
Serial.print(data, BIN); |
- |
|
80 |
} |
65 |
Wire.endTransmission(); // stop transmitting |
81 |
Wire.endTransmission(); // stop transmitting |
66 |
// Serial.print(data, HEX); |
82 |
light_sensor_setup=command; |
67 |
lux=data; |
83 |
} |
68 |
|
84 |
|
69 |
// Serial.print(" MSB="); |
- |
|
70 |
// Connect to device and set register address |
- |
|
71 |
Wire.beginTransmission(address); |
- |
|
72 |
Wire.send(0x02); // sends address |
- |
|
73 |
Wire.endTransmission(); // stop transmitting |
- |
|
74 |
// Connect to device and request one byte |
- |
|
75 |
Wire.beginTransmission(address); |
- |
|
76 |
Wire.requestFrom(address, 1); |
- |
|
77 |
data = Wire.receive(); |
85 |
float get_light_measurement() |
78 |
Wire.endTransmission(); // stop transmitting |
- |
|
79 |
// Serial.print(data, HEX); |
- |
|
- |
|
86 |
{ |
80 |
lux+=data*256; |
87 |
int ret=0; |
81 |
|
88 |
|
82 |
/* |
- |
|
83 |
Serial.print((unsigned)lux, DEC); |
- |
|
84 |
|
- |
|
85 |
Serial.print(" luxIR="); |
- |
|
86 |
|
- |
|
87 |
// Setup device |
- |
|
88 |
Wire.beginTransmission(address); |
- |
|
89 |
Wire.send(0x00); // sends address |
- |
|
90 |
Wire.send(0b11100000); // setup (eye light sensing; measurement range 2 [4000 lx]) |
- |
|
91 |
Wire.endTransmission(); // stop transmitting |
- |
|
92 |
|
- |
|
93 |
// Delay for measurement |
- |
|
94 |
digitalWrite(3, HIGH); // set the LED on |
- |
|
95 |
delay(500); |
- |
|
96 |
digitalWrite(3, LOW); // set the LED off |
- |
|
97 |
delay(500); |
- |
|
98 |
|
- |
|
99 |
|
- |
|
100 |
// Connect to device and set register address |
89 |
// Connect to device and set register address |
101 |
Wire.beginTransmission(address); |
90 |
Wire.beginTransmission(address); |
102 |
Wire.send(0x00); // sends address |
91 |
Wire.write(0x01); // sends address of LSB reagister |
103 |
Wire.endTransmission(); // stop transmitting |
92 |
Wire.endTransmission(); // stop transmitting |
104 |
// Connect to device and request one byte |
- |
|
105 |
Wire.beginTransmission(address); |
- |
|
106 |
Wire.requestFrom(address, 1); |
- |
|
107 |
data = Wire.receive(); |
- |
|
108 |
Wire.endTransmission(); // stop transmitting |
- |
|
109 |
// Serial.print(data, HEX); |
- |
|
110 |
|
93 |
|
111 |
// Serial.print(" LSB="); |
- |
|
112 |
// Connect to device and set register address |
- |
|
113 |
Wire.beginTransmission(address); |
- |
|
114 |
Wire.send(0x01); // sends address |
- |
|
115 |
Wire.endTransmission(); // stop transmitting |
- |
|
116 |
// Connect to device and request one byte |
94 |
// Connect to device and request one byte |
117 |
Wire.beginTransmission(address); |
95 |
Wire.beginTransmission(address); |
118 |
Wire.requestFrom(address, 1); |
96 |
Wire.requestFrom(address, 1); |
119 |
data = Wire.receive(); |
97 |
ret = Wire.read(); |
120 |
Wire.endTransmission(); // stop transmitting |
98 |
Wire.endTransmission(); // stop transmitting |
121 |
// Serial.print(data, HEX); |
- |
|
122 |
lux=data; |
- |
|
123 |
|
99 |
|
124 |
// Serial.print(" MSB="); |
- |
|
125 |
// Connect to device and set register address |
100 |
// Connect to device and set register address |
126 |
Wire.beginTransmission(address); |
101 |
Wire.beginTransmission(address); |
127 |
Wire.send(0x02); // sends address |
102 |
Wire.write(0x02); // sends address of MSB register |
128 |
Wire.endTransmission(); // stop transmitting |
103 |
Wire.endTransmission(); // stop transmitting |
- |
|
104 |
|
129 |
// Connect to device and request one byte |
105 |
// Connect to device and request one byte |
130 |
Wire.beginTransmission(address); |
106 |
Wire.beginTransmission(address); |
131 |
Wire.requestFrom(address, 1); |
107 |
Wire.requestFrom(address, 1); |
132 |
data = Wire.receive(); |
108 |
ret +=256 * Wire.read(); |
133 |
Wire.endTransmission(); // stop transmitting |
109 |
Wire.endTransmission(); // stop transmitting |
- |
|
110 |
|
134 |
// Serial.print(data, HEX); |
111 |
return (1000.0/pow(2.0,16)*ret); |
- |
|
112 |
} |
- |
|
113 |
|
- |
|
114 |
void loop() |
- |
|
115 |
{ |
135 |
lux+=data*256; |
116 |
int lux=0; |
136 |
*/ |
117 |
|
- |
|
118 |
set_light_sensor(SENSE_VIS); //setup sensor for visible measuring |
- |
|
119 |
led_blink(); // Delay for measurement |
- |
|
120 |
Serial.print("lux="); |
137 |
Serial.println((unsigned)lux, DEC); |
121 |
Serial.println(get_light_measurement(),2); |
- |
|
122 |
|
- |
|
123 |
set_light_sensor(SENSE_IR); // setup sensor for infrared measuring |
- |
|
124 |
led_blink(); // Delay for measurement |
- |
|
125 |
Serial.print("luxIR="); |
- |
|
126 |
Serial.println(get_light_measurement(), 2); // data print |
138 |
} |
127 |
} |
139 |
|
128 |
|