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