Author Options:

Trying to get help with arduino photocell sketch... Answered

Not too familiarl with writing arduino sketches but I'd like to connect a photocell to arduino so that when  a shadow is casted upon a photocell nothing happens but when exposed to light again it triggers an action. Any help would be much appreciated. I've looked for any project like this but have been unable to find anything. 

5 Replies

Avadhut.DeshmukhBest Answer (author)2011-05-04

Just a quick post about hooking up a photocell to your arduino. This setup should work for other variable resistors (flex, force, etc).

Materials you’ll need:
Arduino Duemilanove
10K ohm resistor (brown, black, orange)

Arduino Sketch

01 /*
02 * Photocell Reading
03 * -----------------
04 *
05 * Read an analog value from a photocell (variable resistor)
06 * connected to analog pin 0.
07 *
08 * Created January 2010
09 * Matt Wiechec
10 *
11 */
13 int photocellPin = 0; // select the input pin for the photocell
14 int photocellVal; // variable to store the analog valie from the cell
16 void setup(void) {
17 Serial.begin(9600); // for debugging via the Serial Monitor
18 }
20 void loop(void) {
21 photocellVal = analogRead(photocellPin);
23 Serial.print("Photocell Val: ");
24 Serial.println(photocellVal); // the raw analog reading
26 delay(100);
27 }

Select as Best AnswerUndo Best Answer

purpulhaze (author)2011-05-23

Thanks alot for the help but I decided to use picaxe logicator as it is pretty easier to use without knowing much about coding.

Select as Best AnswerUndo Best Answer

steveastrouk (author)2011-05-15

Avad has once again stolen someone else's work and presented it as his own.
The original, and helpful link is here

The link doesn't help with the code you need though to do what you want.

Select as Best AnswerUndo Best Answer

steveastrouk (author)2011-05-04

Connect the cell to one of the analogue inputs.

Set a variable "arm= false"

Read the input
If Input < dark level, then Arm=true;
Until (arm=true) ;

Read input
Until input > trigger.

{do your event}

Select as Best AnswerUndo Best Answer

frollard (author)steveastrouk2011-05-04


to add to the pseudocode:

the first repeat loop waits until its gone dark, and only once it's dark can the second loop begin waiting for a light transition.

Select as Best AnswerUndo Best Answer