Rev Author Line No. Line
2202 kaklik 1 // I2C Light Sensor
2  
3 #include <Wire.h>
4  
5 #define address 0x44 // A0 = L
6  
7  
8 void setup()
9 {
10 Wire.begin(); // join i2c bus (address optional for master)
11 // I2C pins PD4, PD5
12 //
13 pinMode(3, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela
14 pinMode(5, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela
15 Serial.begin(9600); // Zmerena intenzita osvetleni se bude vypisovat na seriovou linku
16 }
17  
18 int data = 0;
19  
2234 kaklik 20 void led_blink()
21 {
22 digitalWrite(3, HIGH); // set the LED on
23 delay(500);
24 digitalWrite(3, LOW); // set the LED off
25 delay(500);
26 }
27  
28  
29 int set_light_sensor()
30 {
31 // Setup device
32 Wire.beginTransmission(address);
33 Wire.send(0x00); // sends address
34 Wire.send(0b11100000); // setup (eye light sensing; measurement range 2 [4000 lx])
35 Wire.endTransmission(); // stop transmitting
36  
37 }
38  
39 int get_light_measurement()
40 {
41 int lux=0;
42  
43 // Connect to device and set register address
44 Wire.beginTransmission(address);
45 Wire.send(0x01); // sends address of LSB reagister
46 Wire.endTransmission(); // stop transmitting
47  
48 // Connect to device and request one byte
49 Wire.beginTransmission(address);
50 Wire.requestFrom(address, 1);
51 data = Wire.receive();
52 Wire.endTransmission(); // stop transmitting
53 lux=data;
54  
55 // Connect to device and set register address
56 Wire.beginTransmission(address);
57 Wire.send(0x02); // sends address of MSB register
58 Wire.endTransmission(); // stop transmitting
59  
60 // Connect to device and request one byte
61 Wire.beginTransmission(address);
62 Wire.requestFrom(address, 1);
63 data = Wire.receive();
64 Wire.endTransmission(); // stop transmitting
65  
66 lux+=data*256;
67 return lux;
68 }
69  
2202 kaklik 70 void loop()
71 {
72 int lux=0;
73  
74 /* Serial.print("lux=");
75  
76 // Setup device
77 digitalWrite(5, HIGH);
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  
84 // Delay for measurement
85  
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
104 Wire.beginTransmission(address);
105 Wire.send(0x01); // sends address
106 Wire.endTransmission(); // stop transmitting
107 // Connect to device and request one byte
108 Wire.beginTransmission(address);
109 Wire.requestFrom(address, 1);
110 data = Wire.receive();
111 Wire.endTransmission(); // stop transmitting
112 // Serial.print(data, HEX);
113 lux=data;
114  
115 // Serial.print(" MSB=");
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);
126 lux+=data*256;
2234 kaklik 127  
128 Serial.print((unsigned)lux, DEC);
2202 kaklik 129 */
130  
2234 kaklik 131 set_light_sensor();
2202 kaklik 132  
2234 kaklik 133 // Delay for measurement
134 led_blink();
2202 kaklik 135  
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
2234 kaklik 145  
146 // data print
147 Serial.print(" luxIR=");
148 Serial.println((unsigned)get_light_measurement(), DEC);
2202 kaklik 149 }
150