Rev Author Line No. Line
2241 kaklik 1 /* I2C Light Sensor
2  
3 The circuit:
4 * analog sensors on analog ins PC0, PC1, and PC2
5 * SD card attached to SPI bus as follows:
6 ** MOSI/CMD - PB3
7 ** MISO/DAT0 - PB4
8 ** CLK - PB5
2242 kaklik 9 ** CS/CD/DAT3 - PD4 (4)
10  
11 I2C pins PC4, PC5
2241 kaklik 12  
13 */
14  
2242 kaklik 15 #include <Wire.h>
2241 kaklik 16 #include <math.h>
2242 kaklik 17 #include <stdlib.h>
18 #include <SD.h>
2241 kaklik 19  
2242 kaklik 20 #define chipSelect 4
2241 kaklik 21  
22 #define address 0x44 // A0 = L
23  
2246 kaklik 24 #define LIGHT_ENABLE 1
25 #define LIGHT_DISABLE 0
26  
2247 kaklik 27 #define LIGHT_ONETIME 0
28 #define LIGHT_CONTINUOUS 1
2246 kaklik 29  
30 #define LIGHT_SENSE_VIS 0
31 #define LIGHT_SENSE_IR 1
32  
2248 kaklik 33 #define LIGHT_INTERNAL_16 0b00000000
34 #define LIGHT_INTERNAL_12 0b00000100
35 #define LIGHT_INTERNAL_8 0b00001000
36 #define LIGHT_INTERNAL_4 0b00001100
37 #define LIGHT_EXTERNAL_ADC 0b00010000
38 #define LIGHT_EXTERNAL_TIMER 0b00010100
2246 kaklik 39  
2247 kaklik 40 //#define LIGHT_AUTORANGE 0
2246 kaklik 41  
2248 kaklik 42 #define LIGHT_RANGE1 0b00000000
43 #define LIGHT_RANGE2 0b00000001
44 #define LIGHT_RANGE3 0b00000010
45 #define LIGHT_RANGE4 0b00000011
2241 kaklik 46  
2244 kaklik 47 char filename[13];
48  
2241 kaklik 49 void setup()
50 {
2244 kaklik 51 int count=0;
52  
2241 kaklik 53 Wire.begin(); // join i2c bus (address optional for master)
2242 kaklik 54  
2241 kaklik 55 pinMode(3, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela
56 pinMode(5, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela
57 Serial.begin(9600); // Zmerena intenzita osvetleni se bude vypisovat na seriovou linku
58  
59 Serial.print("Initializing SD card...");
60 // make sure that the default chip select pin is set to
61 // output, even if you don't use it:
62 pinMode(10, OUTPUT);
63  
64 // see if the card is present and can be initialized:
65 if (!SD.begin(chipSelect)) {
66 Serial.println("Card failed, or not present");
67 // don't do anything more:
68 return;
69 }
70 Serial.println("card initialized.");
2244 kaklik 71 Serial.print("searching for files...");
72  
73 do
74 {
75 sprintf(filename,"Lux%d.txt",count);
76 count++;
77 }
78 while(SD.exists(filename));
79 Serial.print("Using ");
80 Serial.println(filename);
2241 kaklik 81 }
82  
83 void led_blink()
84 {
2258 kaklik 85 digitalWrite(3, LOW); // set the LED off
86 delay(100);
2241 kaklik 87 digitalWrite(3, HIGH); // set the LED on
88 }
89  
2248 kaklik 90 /*int light_sensor_write( unsigned int data, unsigned int address)
91 {
92 }
93 */
94  
95  
2246 kaklik 96 int set_light_sensor(int en, int mode, int light, int res, int range)
2241 kaklik 97 {
2246 kaklik 98 int command=0;
99  
2247 kaklik 100 command = (command & 0b01111111) | (en << 7);
101 command = (command & 0b10111111) | (mode << 6);
102 command = (command & 0b11011111) | (light << 5);
103 command = (command & 0b11100011) | (res << 2);
104 command = (command & 0b11111100) | (range);
2241 kaklik 105  
106 // Setup device
107 Wire.beginTransmission(address);
108 Wire.send(0x00); // sends address
109 Wire.send(command); // setup (eye light sensing; one time measurement; measurement range 1)
110 Wire.endTransmission(); // stop transmitting
2247 kaklik 111  
112 // Connect to device and set register address
2241 kaklik 113 Wire.beginTransmission(address);
114 Wire.send(0x00); // sends address (command register)
115 Wire.endTransmission(); // stop transmitting
116 // verify written command byte
117 Wire.beginTransmission(address);
118 Wire.requestFrom(address, 1);
119 if (command != Wire.receive())
120 {
2247 kaklik 121 Serial.println("Error in sensor setting");
2241 kaklik 122 return 4;
123 }
2248 kaklik 124 Wire.endTransmission(); // stop transmitting
2246 kaklik 125 return 0;
2241 kaklik 126 }
127  
128 float get_light_measurement()
129 {
2248 kaklik 130 unsigned int ret=0;
131 unsigned int setting=0; // variable to storage readed settings
2241 kaklik 132  
2248 kaklik 133 unsigned long int resolution; // parsed ADC bit resolution
134 unsigned int range; // parsed measurement range
135  
2241 kaklik 136 // Connect to device and set register address
137 Wire.beginTransmission(address);
138 Wire.send(0x01); // sends address of LSB reagister
139 Wire.endTransmission(); // stop transmitting
140  
2247 kaklik 141 // Connect to device and request first byte
2241 kaklik 142 Wire.beginTransmission(address);
143 Wire.requestFrom(address, 1);
144 ret = Wire.receive();
145 Wire.endTransmission(); // stop transmitting
146  
147 // Connect to device and set register address
148 Wire.beginTransmission(address);
149 Wire.send(0x02); // sends address of MSB register
150 Wire.endTransmission(); // stop transmitting
151  
2247 kaklik 152 // Connect to device and request second byte
2241 kaklik 153 Wire.beginTransmission(address);
154 Wire.requestFrom(address, 1);
155 ret +=256 * Wire.receive();
156 Wire.endTransmission(); // stop transmitting
157  
2248 kaklik 158  
159 // Connect to device and set register address
160 Wire.beginTransmission(address);
161 Wire.send(0x00); // sends address (command register)
162 Wire.endTransmission(); // stop transmitting
163 // get sensor setting
164 Wire.beginTransmission(address);
165 Wire.requestFrom(address, 1);
166 setting = Wire.receive();
167 Wire.endTransmission(); // stop transmitting
168  
169 switch (setting & 0b00011100) // determine ADC resolution
170 {
171 case LIGHT_INTERNAL_16:
172 resolution=65536;
173 break;
174  
175 case LIGHT_INTERNAL_12:
176 resolution=4096;
177 break;
178  
179 case LIGHT_INTERNAL_8:
180 resolution=256;
181 break;
182  
183 case LIGHT_INTERNAL_4:
184 resolution=16;
185 break;
186 }
187  
188 switch (setting & 0b00000011) // determine measurement range
189 {
190 case LIGHT_RANGE1:
191 range=1000;
192 break;
193  
194 case LIGHT_RANGE2:
195 range=4000;
196 break;
197  
198 case LIGHT_RANGE3:
199 range=16000;
200 break;
201  
202 case LIGHT_RANGE4:
203 range=64000;
204 break;
205 }
2250 kaklik 206 return ((500000.0*range)/(180000.0*resolution)*ret); // calculate output value
2241 kaklik 207 }
208  
209 void loop()
210 {
211 float luxVIS=0;
212 float luxIR=0;
2243 kaklik 213 float time;
2241 kaklik 214  
2250 kaklik 215 set_light_sensor(LIGHT_ENABLE, LIGHT_ONETIME, LIGHT_SENSE_VIS, LIGHT_INTERNAL_16, LIGHT_RANGE3); //setup sensor for visible measuring
2241 kaklik 216 led_blink(); // Delay for measurement
2249 kaklik 217 delay(500);
2241 kaklik 218 luxVIS=get_light_measurement();
219  
2244 kaklik 220 time=millis()/1000.0;
2243 kaklik 221  
2250 kaklik 222 set_light_sensor(LIGHT_ENABLE, LIGHT_ONETIME, LIGHT_SENSE_IR, LIGHT_INTERNAL_16, LIGHT_RANGE3); // setup sensor for infrared measuring
2241 kaklik 223 led_blink(); // Delay for measurement
2249 kaklik 224 delay(500);
2241 kaklik 225 luxIR=get_light_measurement();
226  
227 // open the file. note that only one file can be open at a time,
228 // so you have to close this one before opening another.
2244 kaklik 229 File dataFile = SD.open(filename, FILE_WRITE);
2241 kaklik 230  
231 // if the file is available, write to it:
232 if (dataFile) {
2243 kaklik 233 dataFile.print("$LUX0.1 ");
234 dataFile.print(time,3);
235 dataFile.print(" ");
236 dataFile.print(luxVIS,3);
237 dataFile.print(" ");
238 dataFile.println(luxIR,3);
2241 kaklik 239 dataFile.close();
2243 kaklik 240  
2241 kaklik 241 // print to the serial port too:
2243 kaklik 242 Serial.print("$LUX0.1 ");
243 Serial.print(time,3);
244 Serial.print(" ");
2242 kaklik 245 Serial.print(luxVIS,3);
246 Serial.print(" ");
247 Serial.println(luxIR,3);
2241 kaklik 248 }
249 // if the file isn't open, pop up an error:
250 else {
2244 kaklik 251 Serial.print("error opening ");
252 Serial.println(filename);
2249 kaklik 253 }
254 delay(2000);
2241 kaklik 255 }
256