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