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
2234 kaklik 58 }
59  
60 int get_light_measurement()
61 {
62 int lux=0;
63  
64 // Connect to device and set register address
65 Wire.beginTransmission(address);
66 Wire.send(0x01); // sends address of LSB reagister
67 Wire.endTransmission(); // stop transmitting
68  
69 // Connect to device and request one byte
70 Wire.beginTransmission(address);
71 Wire.requestFrom(address, 1);
72 data = Wire.receive();
73 Wire.endTransmission(); // stop transmitting
74 lux=data;
75  
76 // Connect to device and set register address
77 Wire.beginTransmission(address);
78 Wire.send(0x02); // sends address of MSB register
79 Wire.endTransmission(); // stop transmitting
80  
81 // Connect to device and request one byte
82 Wire.beginTransmission(address);
83 Wire.requestFrom(address, 1);
84 data = Wire.receive();
85 Wire.endTransmission(); // stop transmitting
86  
87 lux+=data*256;
88 return lux;
89 }
90  
2202 kaklik 91 void loop()
92 {
93 int lux=0;
94  
2235 kaklik 95 set_light_sensor(SENSE_VIS);
96  
2202 kaklik 97 // Delay for measurement
2235 kaklik 98 led_blink();
2202 kaklik 99  
100 // Connect to device and set register address
101 Wire.beginTransmission(address);
2235 kaklik 102 Wire.send(0x00); // sends address (command register)
2202 kaklik 103 Wire.endTransmission(); // stop transmitting
2235 kaklik 104  
105 // Connect to device and request command register settings
2202 kaklik 106 Wire.beginTransmission(address);
107 Wire.requestFrom(address, 1);
108 data = Wire.receive();
109 Wire.endTransmission(); // stop transmitting
2235 kaklik 110 Serial.print(data, BIN);
2202 kaklik 111  
2235 kaklik 112 Serial.print("lux=");
113 Serial.println((unsigned)get_light_measurement(), DEC);
2202 kaklik 114  
2234 kaklik 115  
2235 kaklik 116 set_light_sensor(SENSE_IR);
2202 kaklik 117  
2234 kaklik 118 // Delay for measurement
119 led_blink();
2202 kaklik 120  
2234 kaklik 121 // data print
2235 kaklik 122 Serial.print("luxIR=");
2234 kaklik 123 Serial.println((unsigned)get_light_measurement(), DEC);
2202 kaklik 124 }
125