Posted By

kbpsark on 07/22/14


Tagged

Processing arduino photocell


Versions (?)

Using a Photocell


 / Published in: Processing
 

URL: https://learn.adafruit.com/photocells/using-a-photocell

Code from Adafruit site about using a photocell.

  1. /* Photocell simple testing sketch.
  2.  
  3. Connect one end of the photocell to 5V, the other end to Analog 0.
  4. Then connect one end of a 10K resistor from Analog 0 to ground
  5. Connect LED from pin 11 through a resistor to ground
  6. For more information see http://learn.adafruit.com/photocells */
  7.  
  8. int photocellPin = 0; // the cell and 10K pulldown are connected to a0
  9. int photocellReading; // the analog reading from the sensor divider
  10. int LEDpin = 11; // connect Red LED to pin 11 (PWM pin)
  11. int LEDbrightness; //
  12. void setup(void) {
  13. // We'll send debugging information via the Serial monitor
  14. Serial.begin(9600);
  15. }
  16.  
  17. void loop(void) {
  18. photocellReading = analogRead(photocellPin);
  19.  
  20. Serial.print("Analog reading = ");
  21. Serial.println(photocellReading); // the raw analog reading
  22.  
  23. // LED gets brighter the darker it is at the sensor
  24. // that means we have to -invert- the reading from 0-1023 back to 1023-0
  25. photocellReading = 1023 - photocellReading;
  26. //now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
  27. LEDbrightness = map(photocellReading, 0, 1023, 0, 255);
  28. analogWrite(LEDpin, LEDbrightness);
  29.  
  30. delay(100);
  31. }

Report this snippet  

You need to login to post a comment.