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_MEASUREMENT_ONETIME 0 |
27 |
#define LIGHT_ONETIME 0 |
28 |
#define LIGHT_MEASUREMENT_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_TIMING_INTERNAL_16 0b000 |
34 |
#define LIGHT_TIMING_INTERNAL_12 0b001 |
34 |
#define LIGHT_TIMING_INTERNAL_12 0b001 |
35 |
#define LIGHT_TIMING_INTERNAL_8 0b010 |
35 |
#define LIGHT_TIMING_INTERNAL_8 0b010 |
36 |
#define LIGHT_TIMING_INTERNAL_4 0b011 |
36 |
#define LIGHT_TIMING_INTERNAL_4 0b011 |
37 |
#define LIGHT_TIMING_EXTERNAL_ADC 0b100 |
37 |
#define LIGHT_TIMING_EXTERNAL_ADC 0b100 |
38 |
#define LIGHT_TIMING_EXTERNAL_TIMER 0b101 |
38 |
#define LIGHT_TIMING_EXTERNAL_TIMER 0b101 |
39 |
|
39 |
|
40 |
#define LIGHT_AUTORANGE 0 |
40 |
//#define LIGHT_AUTORANGE 0 |
41 |
|
41 |
|
42 |
#define LIGHT_RANGE1 1 |
42 |
#define LIGHT_RANGE1 0b00 |
43 |
#define LIGHT_RANGE2 2 |
43 |
#define LIGHT_RANGE2 0b01 |
44 |
#define LIGHT_RANGE3 3 |
44 |
#define LIGHT_RANGE3 0b10 |
45 |
#define LIGHT_RANGE4 4 |
45 |
#define LIGHT_RANGE4 0b11 |
46 |
|
46 |
|
47 |
char filename[13]; |
47 |
char filename[13]; |
48 |
int data = 0; |
48 |
int data = 0; |
49 |
int light_sensor_setup; |
49 |
int light_sensor_setup; |
50 |
|
50 |
|
51 |
void setup() |
51 |
void setup() |
52 |
{ |
52 |
{ |
53 |
int count=0; |
53 |
int count=0; |
54 |
|
54 |
|
55 |
Wire.begin(); // join i2c bus (address optional for master) |
55 |
Wire.begin(); // join i2c bus (address optional for master) |
56 |
|
56 |
|
57 |
pinMode(3, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela |
57 |
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 |
58 |
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 |
59 |
Serial.begin(9600); // Zmerena intenzita osvetleni se bude vypisovat na seriovou linku |
60 |
|
60 |
|
61 |
Serial.print("Initializing SD card..."); |
61 |
Serial.print("Initializing SD card..."); |
62 |
// make sure that the default chip select pin is set to |
62 |
// make sure that the default chip select pin is set to |
63 |
// output, even if you don't use it: |
63 |
// output, even if you don't use it: |
64 |
pinMode(10, OUTPUT); |
64 |
pinMode(10, OUTPUT); |
65 |
|
65 |
|
66 |
// see if the card is present and can be initialized: |
66 |
// see if the card is present and can be initialized: |
67 |
if (!SD.begin(chipSelect)) { |
67 |
if (!SD.begin(chipSelect)) { |
68 |
Serial.println("Card failed, or not present"); |
68 |
Serial.println("Card failed, or not present"); |
69 |
// don't do anything more: |
69 |
// don't do anything more: |
70 |
return; |
70 |
return; |
71 |
} |
71 |
} |
72 |
Serial.println("card initialized."); |
72 |
Serial.println("card initialized."); |
73 |
Serial.print("searching for files..."); |
73 |
Serial.print("searching for files..."); |
74 |
|
74 |
|
75 |
do |
75 |
do |
76 |
{ |
76 |
{ |
77 |
sprintf(filename,"Lux%d.txt",count); |
77 |
sprintf(filename,"Lux%d.txt",count); |
78 |
count++; |
78 |
count++; |
79 |
} |
79 |
} |
80 |
while(SD.exists(filename)); |
80 |
while(SD.exists(filename)); |
81 |
Serial.print("Using "); |
81 |
Serial.print("Using "); |
82 |
Serial.println(filename); |
82 |
Serial.println(filename); |
83 |
} |
83 |
} |
84 |
|
84 |
|
85 |
void led_blink() |
85 |
void led_blink() |
86 |
{ |
86 |
{ |
87 |
digitalWrite(3, HIGH); // set the LED on |
87 |
digitalWrite(3, HIGH); // set the LED on |
88 |
delay(500); |
88 |
delay(500); |
89 |
digitalWrite(3, LOW); // set the LED off |
89 |
digitalWrite(3, LOW); // set the LED off |
90 |
delay(500); |
90 |
delay(500); |
91 |
} |
91 |
} |
92 |
|
92 |
|
93 |
int set_light_sensor(int en, int mode, int light, int res, int range) |
93 |
int set_light_sensor(int en, int mode, int light, int res, int range) |
94 |
{ |
94 |
{ |
95 |
int command=0; |
95 |
int command=0; |
96 |
|
96 |
|
97 |
commad= (en << 7 & 0b10000000); |
97 |
command = (command & 0b01111111) | (en << 7); |
98 |
|
- |
|
99 |
switch (light) |
- |
|
100 |
{ |
- |
|
101 |
case LIGHT_SENSE_VIS: |
98 |
command = (command & 0b10111111) | (mode << 6); |
102 |
{ |
- |
|
103 |
command=0b11000000; // setup (eye light sensing; one time measurement; measurement range 1) |
99 |
command = (command & 0b11011111) | (light << 5); |
104 |
break; |
- |
|
105 |
} |
- |
|
106 |
|
- |
|
107 |
case LIGHT_SENSE_IR: |
100 |
command = (command & 0b11100011) | (res << 2); |
108 |
{ |
- |
|
109 |
command=0b11100000; // setup (eye light sensing; measurement range 2 [4000 lx]) |
101 |
command = (command & 0b11111100) | (range); |
110 |
break; |
- |
|
111 |
} |
- |
|
112 |
|
- |
|
113 |
default: |
- |
|
114 |
return 3; |
- |
|
115 |
} |
- |
|
116 |
|
102 |
|
117 |
// Setup device |
103 |
// Setup device |
118 |
Wire.beginTransmission(address); |
104 |
Wire.beginTransmission(address); |
119 |
Wire.send(0x00); // sends address |
105 |
Wire.send(0x00); // sends address |
120 |
Wire.send(command); // setup (eye light sensing; one time measurement; measurement range 1) |
106 |
Wire.send(command); // setup (eye light sensing; one time measurement; measurement range 1) |
121 |
Wire.endTransmission(); // stop transmitting |
107 |
Wire.endTransmission(); // stop transmitting |
122 |
|
- |
|
123 |
|
108 |
|
124 |
// Connect to device and set register address |
109 |
// Connect to device and set register address |
125 |
Wire.beginTransmission(address); |
110 |
Wire.beginTransmission(address); |
126 |
Wire.send(0x00); // sends address (command register) |
111 |
Wire.send(0x00); // sends address (command register) |
127 |
Wire.endTransmission(); // stop transmitting |
112 |
Wire.endTransmission(); // stop transmitting |
128 |
|
113 |
|
129 |
// verify written command byte |
114 |
// verify written command byte |
130 |
Wire.beginTransmission(address); |
115 |
Wire.beginTransmission(address); |
131 |
Wire.requestFrom(address, 1); |
116 |
Wire.requestFrom(address, 1); |
132 |
if (command != Wire.receive()) |
117 |
if (command != Wire.receive()) |
133 |
{ |
118 |
{ |
134 |
Serial.println(data, BIN); |
119 |
Serial.println("Error in sensor setting"); |
135 |
return 4; |
120 |
return 4; |
136 |
} |
121 |
} |
137 |
Wire.endTransmission(); // stop transmitting |
122 |
Wire.endTransmission(); // stop transmitting |
138 |
light_sensor_setup=command; |
123 |
light_sensor_setup=command; |
- |
|
124 |
Serial.println(command, BIN); |
- |
|
125 |
|
139 |
return 0; |
126 |
return 0; |
140 |
} |
127 |
} |
141 |
|
128 |
|
142 |
float get_light_measurement() |
129 |
float get_light_measurement() |
143 |
{ |
130 |
{ |
144 |
int ret=0; |
131 |
int ret=0; |
145 |
|
132 |
|
146 |
// Connect to device and set register address |
133 |
// Connect to device and set register address |
147 |
Wire.beginTransmission(address); |
134 |
Wire.beginTransmission(address); |
148 |
Wire.send(0x01); // sends address of LSB reagister |
135 |
Wire.send(0x01); // sends address of LSB reagister |
149 |
Wire.endTransmission(); // stop transmitting |
136 |
Wire.endTransmission(); // stop transmitting |
150 |
|
137 |
|
151 |
// Connect to device and request one byte |
138 |
// Connect to device and request first byte |
152 |
Wire.beginTransmission(address); |
139 |
Wire.beginTransmission(address); |
153 |
Wire.requestFrom(address, 1); |
140 |
Wire.requestFrom(address, 1); |
154 |
ret = Wire.receive(); |
141 |
ret = Wire.receive(); |
155 |
Wire.endTransmission(); // stop transmitting |
142 |
Wire.endTransmission(); // stop transmitting |
156 |
|
143 |
|
157 |
// Connect to device and set register address |
144 |
// Connect to device and set register address |
158 |
Wire.beginTransmission(address); |
145 |
Wire.beginTransmission(address); |
159 |
Wire.send(0x02); // sends address of MSB register |
146 |
Wire.send(0x02); // sends address of MSB register |
160 |
Wire.endTransmission(); // stop transmitting |
147 |
Wire.endTransmission(); // stop transmitting |
161 |
|
148 |
|
162 |
// Connect to device and request one byte |
149 |
// Connect to device and request second byte |
163 |
Wire.beginTransmission(address); |
150 |
Wire.beginTransmission(address); |
164 |
Wire.requestFrom(address, 1); |
151 |
Wire.requestFrom(address, 1); |
165 |
ret +=256 * Wire.receive(); |
152 |
ret +=256 * Wire.receive(); |
166 |
Wire.endTransmission(); // stop transmitting |
153 |
Wire.endTransmission(); // stop transmitting |
167 |
|
154 |
|
168 |
return (1000.0/pow(2.0,16)*ret); |
155 |
return (64000.0/pow(2.0,16)*ret); |
169 |
} |
156 |
} |
170 |
|
157 |
|
171 |
void loop() |
158 |
void loop() |
172 |
{ |
159 |
{ |
173 |
float luxVIS=0; |
160 |
float luxVIS=0; |
174 |
float luxIR=0; |
161 |
float luxIR=0; |
175 |
float time; |
162 |
float time; |
176 |
|
163 |
|
177 |
set_light_sensor(LIGHT_SENSE_VIS); //setup sensor for visible measuring |
164 |
set_light_sensor(LIGHT_ENABLE, LIGHT_ONETIME, LIGHT_SENSE_VIS, LIGHT_TIMING_INTERNAL_16, LIGHT_RANGE4); //setup sensor for visible measuring |
178 |
led_blink(); // Delay for measurement |
165 |
led_blink(); // Delay for measurement |
179 |
luxVIS=get_light_measurement(); |
166 |
luxVIS=get_light_measurement(); |
180 |
|
167 |
|
181 |
time=millis()/1000.0; |
168 |
time=millis()/1000.0; |
182 |
|
169 |
|
183 |
set_light_sensor(LIGHT_SENSE_IR); // setup sensor for infrared measuring |
170 |
set_light_sensor(LIGHT_ENABLE, LIGHT_ONETIME, LIGHT_SENSE_IR, LIGHT_TIMING_INTERNAL_16, LIGHT_RANGE4); // setup sensor for infrared measuring |
184 |
led_blink(); // Delay for measurement |
171 |
led_blink(); // Delay for measurement |
185 |
luxIR=get_light_measurement(); |
172 |
luxIR=get_light_measurement(); |
186 |
|
173 |
|
187 |
// open the file. note that only one file can be open at a time, |
174 |
// open the file. note that only one file can be open at a time, |
188 |
// so you have to close this one before opening another. |
175 |
// so you have to close this one before opening another. |
189 |
File dataFile = SD.open(filename, FILE_WRITE); |
176 |
File dataFile = SD.open(filename, FILE_WRITE); |
190 |
|
177 |
|
191 |
// if the file is available, write to it: |
178 |
// if the file is available, write to it: |
192 |
if (dataFile) { |
179 |
if (dataFile) { |
193 |
dataFile.print("$LUX0.1 "); |
180 |
dataFile.print("$LUX0.1 "); |
194 |
dataFile.print(time,3); |
181 |
dataFile.print(time,3); |
195 |
dataFile.print(" "); |
182 |
dataFile.print(" "); |
196 |
dataFile.print(luxVIS,3); |
183 |
dataFile.print(luxVIS,3); |
197 |
dataFile.print(" "); |
184 |
dataFile.print(" "); |
198 |
dataFile.println(luxIR,3); |
185 |
dataFile.println(luxIR,3); |
199 |
dataFile.close(); |
186 |
dataFile.close(); |
200 |
|
187 |
|
201 |
// print to the serial port too: |
188 |
// print to the serial port too: |
202 |
Serial.print("$LUX0.1 "); |
189 |
Serial.print("$LUX0.1 "); |
203 |
Serial.print(time,3); |
190 |
Serial.print(time,3); |
204 |
Serial.print(" "); |
191 |
Serial.print(" "); |
205 |
Serial.print(luxVIS,3); |
192 |
Serial.print(luxVIS,3); |
206 |
Serial.print(" "); |
193 |
Serial.print(" "); |
207 |
Serial.println(luxIR,3); |
194 |
Serial.println(luxIR,3); |
208 |
} |
195 |
} |
209 |
// if the file isn't open, pop up an error: |
196 |
// if the file isn't open, pop up an error: |
210 |
else { |
197 |
else { |
211 |
Serial.print("error opening "); |
198 |
Serial.print("error opening "); |
212 |
Serial.println(filename); |
199 |
Serial.println(filename); |
213 |
} |
200 |
} |
214 |
} |
201 |
} |
215 |
|
202 |
|