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