Rev Author Line No. Line
2202 kaklik 1 // I2C Light Sensor
2  
3 #include <Wire.h>
4  
5 #define address 0x44 // A0 = L
2235 kaklik 6 #define SENSE_VIS 0
7 #define SENSE_IR 1
2202 kaklik 8  
9  
10 void setup()
11 {
12 Wire.begin(); // join i2c bus (address optional for master)
13 // I2C pins PD4, PD5
14 //
15 pinMode(3, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela
16 pinMode(5, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela
17 Serial.begin(9600); // Zmerena intenzita osvetleni se bude vypisovat na seriovou linku
18 }
19  
20 int data = 0;
21  
2234 kaklik 22 void led_blink()
23 {
24 digitalWrite(3, HIGH); // set the LED on
25 delay(500);
26 digitalWrite(3, LOW); // set the LED off
27 delay(500);
28 }
29  
30  
2235 kaklik 31 int set_light_sensor(int mode)
2234 kaklik 32 {
2235 kaklik 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 }
2234 kaklik 48  
2235 kaklik 49 default:
50 return 3;
51 }
52  
53 // Setup device
54 Wire.beginTransmission(address);
55 Wire.send(0x00); // sends address
56 Wire.send(command); // setup (eye light sensing; one time measurement; measurement range 1)
57 Wire.endTransmission(); // stop transmitting
2236 kaklik 58  
59  
60 // Connect to device and set register address
61 Wire.beginTransmission(address);
62 Wire.send(0x00); // sends address (command register)
63 Wire.endTransmission(); // stop transmitting
64  
65 // verify written command byte
66 Wire.beginTransmission(address);
67 Wire.requestFrom(address, 1);
68 if (command != Wire.receive())
69 {
70 return 4;
71 Serial.print(data, BIN);
72 }
73 Wire.endTransmission(); // stop transmitting
74  
2234 kaklik 75 }
76  
77 int get_light_measurement()
78 {
79 int lux=0;
80  
81 // Connect to device and set register address
82 Wire.beginTransmission(address);
83 Wire.send(0x01); // sends address of LSB reagister
84 Wire.endTransmission(); // stop transmitting
85  
86 // Connect to device and request one byte
87 Wire.beginTransmission(address);
88 Wire.requestFrom(address, 1);
89 data = Wire.receive();
90 Wire.endTransmission(); // stop transmitting
91 lux=data;
92  
93 // Connect to device and set register address
94 Wire.beginTransmission(address);
95 Wire.send(0x02); // sends address of MSB register
96 Wire.endTransmission(); // stop transmitting
97  
98 // Connect to device and request one byte
99 Wire.beginTransmission(address);
100 Wire.requestFrom(address, 1);
101 data = Wire.receive();
102 Wire.endTransmission(); // stop transmitting
103  
104 lux+=data*256;
105 return lux;
106 }
107  
2202 kaklik 108 void loop()
109 {
2236 kaklik 110 int lux=0;
2202 kaklik 111  
2236 kaklik 112 set_light_sensor(SENSE_VIS); //setup sensor for visible measuring
113 led_blink(); // Delay for measurement
114 Serial.print("lux=");
2235 kaklik 115 Serial.println((unsigned)get_light_measurement(), DEC);
2202 kaklik 116  
2236 kaklik 117 set_light_sensor(SENSE_IR); // setup sensor for infrared measuring
118 led_blink(); // Delay for measurement
119 Serial.print("luxIR=");
120 Serial.println((unsigned)get_light_measurement(), DEC); // data print
2202 kaklik 121 }
122