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