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 10 //PB2 |
20 |
#define chipSelect 10 //PB2 |
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 |
while(true); |
68 |
while(true); |
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, LOW); // set the LED off |
85 |
digitalWrite(3, LOW); // set the LED off |
86 |
delay(100); |
86 |
delay(20); |
87 |
digitalWrite(3, HIGH); // set the LED on |
87 |
digitalWrite(3, HIGH); // set the LED on |
88 |
} |
88 |
} |
89 |
|
89 |
|
90 |
/*int light_sensor_write( unsigned int data, unsigned int address) |
90 |
/*int light_sensor_write( unsigned int data, unsigned int address) |
91 |
{ |
91 |
{ |
92 |
} |
92 |
} |
93 |
*/ |
93 |
*/ |
94 |
|
94 |
|
95 |
|
95 |
|
96 |
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) |
97 |
{ |
97 |
{ |
98 |
int command=0; |
98 |
int command=0; |
99 |
|
99 |
|
100 |
command = (command & 0b01111111) | (en << 7); |
100 |
command = (command & 0b01111111) | (en << 7); |
101 |
command = (command & 0b10111111) | (mode << 6); |
101 |
command = (command & 0b10111111) | (mode << 6); |
102 |
command = (command & 0b11011111) | (light << 5); |
102 |
command = (command & 0b11011111) | (light << 5); |
103 |
command = (command & 0b11100011) | (res << 2); |
103 |
command = (command & 0b11100011) | (res << 2); |
104 |
command = (command & 0b11111100) | (range); |
104 |
command = (command & 0b11111100) | (range); |
105 |
|
105 |
|
106 |
// Setup device |
106 |
// Setup device |
107 |
Wire.beginTransmission(address); |
107 |
Wire.beginTransmission(address); |
108 |
Wire.send(0x00); // sends address |
108 |
Wire.send(0x00); // sends address |
109 |
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) |
110 |
Wire.endTransmission(); // stop transmitting |
110 |
Wire.endTransmission(); // stop transmitting |
111 |
|
111 |
|
112 |
// Connect to device and set register address |
112 |
// Connect to device and set register address |
113 |
Wire.beginTransmission(address); |
113 |
Wire.beginTransmission(address); |
114 |
Wire.send(0x00); // sends address (command register) |
114 |
Wire.send(0x00); // sends address (command register) |
115 |
Wire.endTransmission(); // stop transmitting |
115 |
Wire.endTransmission(); // stop transmitting |
116 |
// verify written command byte |
116 |
// verify written command byte |
117 |
Wire.beginTransmission(address); |
117 |
Wire.beginTransmission(address); |
118 |
Wire.requestFrom(address, 1); |
118 |
Wire.requestFrom(address, 1); |
119 |
if (command != Wire.receive()) |
119 |
if (command != Wire.receive()) |
120 |
{ |
120 |
{ |
121 |
Serial.println("Error in sensor setting"); |
121 |
Serial.println("Error in sensor setting"); |
122 |
return 4; |
122 |
return 4; |
123 |
} |
123 |
} |
124 |
Wire.endTransmission(); // stop transmitting |
124 |
Wire.endTransmission(); // stop transmitting |
125 |
return 0; |
125 |
return 0; |
126 |
} |
126 |
} |
127 |
|
127 |
|
128 |
float get_light_measurement() |
128 |
float get_light_measurement() |
129 |
{ |
129 |
{ |
130 |
unsigned int ret=0; |
130 |
unsigned int ret=0; |
131 |
unsigned int setting=0; // variable to storage readed settings |
131 |
unsigned int setting=0; // variable to storage readed settings |
132 |
|
132 |
|
133 |
unsigned long int resolution; // parsed ADC bit resolution |
133 |
unsigned long int resolution; // parsed ADC bit resolution |
134 |
unsigned int range; // parsed measurement range |
134 |
unsigned int range; // parsed measurement range |
135 |
|
135 |
|
136 |
// Connect to device and set register address |
136 |
// Connect to device and set register address |
137 |
Wire.beginTransmission(address); |
137 |
Wire.beginTransmission(address); |
138 |
Wire.send(0x01); // sends address of LSB reagister |
138 |
Wire.send(0x01); // sends address of LSB reagister |
139 |
Wire.endTransmission(); // stop transmitting |
139 |
Wire.endTransmission(); // stop transmitting |
140 |
|
140 |
|
141 |
// Connect to device and request first byte |
141 |
// Connect to device and request first byte |
142 |
Wire.beginTransmission(address); |
142 |
Wire.beginTransmission(address); |
143 |
Wire.requestFrom(address, 1); |
143 |
Wire.requestFrom(address, 1); |
144 |
ret = Wire.receive(); |
144 |
ret = Wire.receive(); |
145 |
Wire.endTransmission(); // stop transmitting |
145 |
Wire.endTransmission(); // stop transmitting |
146 |
|
146 |
|
147 |
// Connect to device and set register address |
147 |
// Connect to device and set register address |
148 |
Wire.beginTransmission(address); |
148 |
Wire.beginTransmission(address); |
149 |
Wire.send(0x02); // sends address of MSB register |
149 |
Wire.send(0x02); // sends address of MSB register |
150 |
Wire.endTransmission(); // stop transmitting |
150 |
Wire.endTransmission(); // stop transmitting |
151 |
|
151 |
|
152 |
// Connect to device and request second byte |
152 |
// Connect to device and request second byte |
153 |
Wire.beginTransmission(address); |
153 |
Wire.beginTransmission(address); |
154 |
Wire.requestFrom(address, 1); |
154 |
Wire.requestFrom(address, 1); |
155 |
ret +=256 * Wire.receive(); |
155 |
ret +=256 * Wire.receive(); |
156 |
Wire.endTransmission(); // stop transmitting |
156 |
Wire.endTransmission(); // stop transmitting |
157 |
|
157 |
|
158 |
|
158 |
|
159 |
// Connect to device and set register address |
159 |
// Connect to device and set register address |
160 |
Wire.beginTransmission(address); |
160 |
Wire.beginTransmission(address); |
161 |
Wire.send(0x00); // sends address (command register) |
161 |
Wire.send(0x00); // sends address (command register) |
162 |
Wire.endTransmission(); // stop transmitting |
162 |
Wire.endTransmission(); // stop transmitting |
163 |
// get sensor setting |
163 |
// get sensor setting |
164 |
Wire.beginTransmission(address); |
164 |
Wire.beginTransmission(address); |
165 |
Wire.requestFrom(address, 1); |
165 |
Wire.requestFrom(address, 1); |
166 |
setting = Wire.receive(); |
166 |
setting = Wire.receive(); |
167 |
Wire.endTransmission(); // stop transmitting |
167 |
Wire.endTransmission(); // stop transmitting |
168 |
|
168 |
|
169 |
switch (setting & 0b00011100) // determine ADC resolution |
169 |
switch (setting & 0b00011100) // determine ADC resolution |
170 |
{ |
170 |
{ |
171 |
case LIGHT_INTERNAL_16: |
171 |
case LIGHT_INTERNAL_16: |
172 |
resolution=65536; |
172 |
resolution=65536; |
173 |
break; |
173 |
break; |
174 |
|
174 |
|
175 |
case LIGHT_INTERNAL_12: |
175 |
case LIGHT_INTERNAL_12: |
176 |
resolution=4096; |
176 |
resolution=4096; |
177 |
break; |
177 |
break; |
178 |
|
178 |
|
179 |
case LIGHT_INTERNAL_8: |
179 |
case LIGHT_INTERNAL_8: |
180 |
resolution=256; |
180 |
resolution=256; |
181 |
break; |
181 |
break; |
182 |
|
182 |
|
183 |
case LIGHT_INTERNAL_4: |
183 |
case LIGHT_INTERNAL_4: |
184 |
resolution=16; |
184 |
resolution=16; |
185 |
break; |
185 |
break; |
186 |
} |
186 |
} |
187 |
|
187 |
|
188 |
switch (setting & 0b00000011) // determine measurement range |
188 |
switch (setting & 0b00000011) // determine measurement range |
189 |
{ |
189 |
{ |
190 |
case LIGHT_RANGE1: |
190 |
case LIGHT_RANGE1: |
191 |
range=1000; |
191 |
range=1000; |
192 |
break; |
192 |
break; |
193 |
|
193 |
|
194 |
case LIGHT_RANGE2: |
194 |
case LIGHT_RANGE2: |
195 |
range=4000; |
195 |
range=4000; |
196 |
break; |
196 |
break; |
197 |
|
197 |
|
198 |
case LIGHT_RANGE3: |
198 |
case LIGHT_RANGE3: |
199 |
range=16000; |
199 |
range=16000; |
200 |
break; |
200 |
break; |
201 |
|
201 |
|
202 |
case LIGHT_RANGE4: |
202 |
case LIGHT_RANGE4: |
203 |
range=64000; |
203 |
range=64000; |
204 |
break; |
204 |
break; |
205 |
} |
205 |
} |
206 |
return ((500000.0*range)/(510000.0*resolution)*ret); // calculate output value |
206 |
return ((500000.0*range)/(510000.0*resolution)*ret); // calculate output value |
207 |
} |
207 |
} |
208 |
|
208 |
|
209 |
void loop() |
209 |
void loop() |
210 |
{ |
210 |
{ |
211 |
float luxVIS=0; |
211 |
float luxVIS=0; |
212 |
float luxIR=0; |
212 |
float luxIR=0; |
213 |
float time; |
213 |
float time; |
214 |
|
214 |
|
215 |
set_light_sensor(LIGHT_ENABLE, LIGHT_ONETIME, LIGHT_SENSE_VIS, LIGHT_INTERNAL_16, LIGHT_RANGE3); //setup sensor for visible measuring |
215 |
set_light_sensor(LIGHT_ENABLE, LIGHT_ONETIME, LIGHT_SENSE_VIS, LIGHT_INTERNAL_16, LIGHT_RANGE3); //setup sensor for visible measuring |
216 |
led_blink(); // Delay for measurement |
216 |
led_blink(); // Delay for measurement |
217 |
delay(500); |
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_RANGE3); // setup sensor for infrared measuring |
222 |
set_light_sensor(LIGHT_ENABLE, LIGHT_ONETIME, LIGHT_SENSE_IR, LIGHT_INTERNAL_16, LIGHT_RANGE3); // setup sensor for infrared measuring |
223 |
led_blink(); // Delay for measurement |
223 |
led_blink(); // Delay for measurement |
224 |
delay(500); |
224 |
delay(500); |
225 |
luxIR=get_light_measurement(); |
225 |
luxIR=get_light_measurement(); |
226 |
|
226 |
|
227 |
// 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, |
228 |
// so you have to close this one before opening another. |
228 |
// so you have to close this one before opening another. |
229 |
File dataFile = SD.open(filename, FILE_WRITE); |
229 |
File dataFile = SD.open(filename, FILE_WRITE); |
230 |
|
230 |
|
231 |
// if the file is available, write to it: |
231 |
// if the file is available, write to it: |
232 |
if (dataFile) { |
232 |
if (dataFile) { |
233 |
dataFile.print("$LUX0.1 "); |
233 |
dataFile.print("$LUX0.1 "); |
234 |
dataFile.print(time,3); |
234 |
dataFile.print(time,3); |
235 |
dataFile.print(" "); |
235 |
dataFile.print(" "); |
236 |
dataFile.print(luxVIS,3); |
236 |
dataFile.print(luxVIS,3); |
237 |
dataFile.print(" "); |
237 |
dataFile.print(" "); |
238 |
dataFile.println(luxIR,3); |
238 |
dataFile.println(luxIR,3); |
239 |
dataFile.close(); |
239 |
dataFile.close(); |
240 |
|
240 |
|
241 |
// print to the serial port too: |
241 |
// print to the serial port too: |
242 |
Serial.print("$LUX0.1 "); |
242 |
Serial.print("$LUX0.1 "); |
243 |
Serial.print(time,3); |
243 |
Serial.print(time,3); |
244 |
Serial.print(" "); |
244 |
Serial.print(" "); |
245 |
Serial.print(luxVIS,3); |
245 |
Serial.print(luxVIS,3); |
246 |
Serial.print(" "); |
246 |
Serial.print(" "); |
247 |
Serial.println(luxIR,3); |
247 |
Serial.println(luxIR,3); |
248 |
} |
248 |
} |
249 |
// if the file isn't open, pop up an error: |
249 |
// if the file isn't open, pop up an error: |
250 |
else { |
250 |
else { |
251 |
Serial.print("error opening "); |
251 |
Serial.print("error opening "); |
252 |
Serial.println(filename); |
252 |
Serial.println(filename); |
253 |
while(true); |
- |
|
254 |
} |
253 |
} |
255 |
delay(2000); |
254 |
delay(2000); |
256 |
} |
255 |
} |
257 |
|
256 |
|