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