Ligne 241 : |
Ligne 241 : |
| | | |
| You may want to try different pulldown resistors depending on the light level range you want to detect! | | You may want to try different pulldown resistors depending on the light level range you want to detect! |
| + | |
| + | == Simple code for analog light measurements == |
| + | |
| + | This code doesn't do any calculations, it just prints out what it interprets as the amount of light in a qualitative manner. For most projects, this is pretty much all thats needed! |
| + | |
| + | [[Fichier:cds-mesure1.jpg]] |
| + | |
| + | [[Fichier:cds-mesure2.jpg]] |
| + | |
| + | <nowiki>/* Photocell simple testing sketch. |
| + | |
| + | Connect one end of the photocell to 5V, the other end to Analog 0. |
| + | Then connect one end of a 10K resistor from Analog 0 to ground |
| + | |
| + | For more information see www.ladyada.net/learn/sensors/cds.html */ |
| + | |
| + | int photocellPin = 0; // the cell and 10K pulldown are connected to a0 |
| + | int photocellReading; // the analog reading from the analog resistor divider |
| + | |
| + | void setup(void) { |
| + | // We'll send debugging information via the Serial monitor |
| + | Serial.begin(9600); |
| + | } |
| + | |
| + | void loop(void) { |
| + | photocellReading = analogRead(photocellPin); |
| + | |
| + | Serial.print("Analog reading = "); |
| + | Serial.print(photocellReading); // the raw analog reading |
| + | |
| + | // We'll have a few threshholds, qualitatively determined |
| + | if (photocellReading < 10) { |
| + | Serial.println(" - Dark"); |
| + | } else if (photocellReading < 200) { |
| + | Serial.println(" - Dim"); |
| + | } else if (photocellReading < 500) { |
| + | Serial.println(" - Light"); |
| + | } else if (photocellReading < 800) { |
| + | Serial.println(" - Bright"); |
| + | } else { |
| + | Serial.println(" - Very bright"); |
| + | } |
| + | delay(1000); |
| + | } |
| + | </nowiki> |
| + | |
| + | To test it, I started in a sunlit (but shaded) room and covered the sensor with my hand, then covered it with a piece of blackout fabric. |
| + | |
| + | [[Fichier:cds-mesure3.jpg]] |
| | | |
| | | |