Photocell Tutorial with Arduino
Tutorials Section
Photocell Tutorial with Arduino
Overview
Photocells are one of the coolest components for detecting light and darkness. These components are small, cost effective and so easy to use! Photocells are used in the general market everyday ranging from toys to indoor and outdoor light sensors.
In this tutorial, we will provide a short summary of the photocell and then we will put it into action with the Arduino Duemilanove.
Tutorial
Tools Required
- Arduino Development Board.
- 1 Photocell
- 1 Breadboard
- 1 LED (Light Emitting Diode)
- Hookup Wire
- 10K Ohm Resistor
- 1K Ohm Resistor
- A Power Source for the Arduino Development Board. (USB or Power Supply)
- Multimeter
- Thinking cap.
*Please note that these items are available separately or you can purchase the Photocell Kit in our store.
Learn
Photocells are designed to detect light and darkness through resistance in the cell. When light strikes the photocell, the internal resistance decreases.When there is no light striking on the photocell, then resistance increases. If we were to connect the photocell directly into a circuit, then current would be limited in darkness and current would move more freely during light. So in a nutshell, the photocells acts as a variable (changing) resistor. Here’s a visual explanation of how a photocell can be used in a circuit.
Note* Photocell is R2 aligned with a 10K Resistor R1.
Here are some pictures testing the theory of the resistance in the photocell.
As you see with the first picture (left), the photocell alone has a lower resistance. In the second picture (right), the covered photocell has a higher resistance.
Enjoy
Now that you have learned a little bit about photocells, let’s get the photocell working! First, we need to setup a circuit. We are going to have a 10K or a 1K Ohm resistor in series to the photocell. The analog output will be measured between the photocell and the resistor. See the picture below.

Now let’s program the Arduino and make it happen! Here’s the code:
int sensorPin = 0; // select the input pin for the photocell
int sensorValue = 0; // variable to store the value coming from the photocell
void setup() {
Serial.begin(9600); //Set baud rate to 9600 on the Arduino
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin); //get the voltage value from input pin
Serial.println(sensorValue); //print the value to Serial monitor
delay(2000); //delay for 2 seconds
}
The program code above reads the analog input of the photocell which is then converted into a digital value. The digital signal at it’s highest potential can be high 10 bits and the lowest potential can be zero bits. When the Arduino provides the value to the human viewer it will be viewed as the highest value of 1023 (10 bits) or 0 (0 bits). Thus, there is 1024 possibilities.
So if we applied 5 volts to the circuit and the resistance of the photocell is 5K Ohm with a 10K Ohm resistor in series, then using a voltage divider formula it would be: (10K/15K) * 5V = 3.33V. So the digital equivalance for 3.33V is (3.33V/ 5V) *1024 = 682.
Now let’s try using an LED (Light Emitting Diode) for feedback. We are going to hook up the LED to a digital port with a 1K Ohm Resistor in series with the LED. See the picture below.

Now let’s use a program that emits an LED when darkness appears. Please note to change the sensor threshold value to a desired level.
int sensorPin = 0; // select the input pin for the photocell
int sensorValue = 0; // variable to store the value coming from the photocell
int LEDpin = 8; //LED Pin is on the Digital i/o pin number 8
void setup() {
Serial.begin(9600); //Set baud rate to 9600 on the Arduino
pinMode(LEDpin, OUTPUT); //set the LED pin as an output on digital i/o pin 8
}
void loop() {
sensorValue = analogRead(sensorPin); //get the value from input pin
Serial.println(sensorValue); //print the value to Serial monitor
delay(2000);
if (sensorValue < 300) //if there is darkness then turn led on
{
digitalwrite(LEDpin,HIGH);
}
else
{
digitalwrite(LEDpin, LOW); //else, keep the led off
}
}
Imagine
Now that you have learned, programmed and hopefully enjoyed this tutorial, here are some ideas that you could use to spark the imagination:
Photocell and Serial LCD, read a value from the photocell and display on the LCD.
Photocell and a robot, get the sensor value of the photocell and move a robot into the light or away from the light.

Login Status
Love your site man keep up the good work
some truly excellent articles on this internet site , thanks for contribution.
Thanks for the comment. We try our best to make sure you learn, enjoy and imagine electronics.
We love what we do! If you have questions or comments, feel free to drop us a line.
Thanks for the comment. We try our best to make sure you learn, enjoy and imagine electronics.
We love what we do! If you have questions or comments, requests or even suggestions, let us know because we care how you think.
hi, excellent web blog, and a very good understand! one for my bookmarks.
You can definitely see your enthusiasm in the work you write. The world hopes for more passionate writers like you who aren?¯t afraid to say how they believe. Always go after your heart.
Thanks for the comment. We try our best to make sure you learn, enjoy and imagine electronics.
We love what we do! If you have questions, comments , requests or even suggestions, let us know because we care how you think.
Thanks for the comment. We try our best to make sure you learn, enjoy and imagine electronics.
We love what we do! If you have questions, comments, or even suggestions, let us know because we care how you think.
Incredible, that is definitely what I was shooting for! This post just saved me alot of work
I’ll make certain to put this in good use!
Great article. I found this searching for the title of your article. Thanks.
WONDERFUL Post.thanks for share..extra wait .. …
Great stuff! However, I would like to point out that your code does not work in my Arduino directly, because apparently the code is case sensitive, so
digitalwrite(ledpin,high);
gave errors, but
digitalWrite(LEDpin, HIGH); works great!
Probably just a newbie thing, but it took me 2 hrs to figure out what was wrong
Thanks for the code, though, and all your work. It’s really awesome!
Hey thanks for the check up on the code. We apologize for the slight mishap on the code. We have corrected the changes and we are glad that it worked for you despite the small minor problem.
Thanks for the notice and please refer your friends to our site.
…Additional Information ca be found here…
[...]you make blogging glance[...]…
Thank you! I’m just learning about Arduino and your site is very helpful. Is there a way to use the circuit above and then turn a pin “high” after 1 hour of time? I need a photocell control where the control circuit activates one hour after dark.
Hi,
You will need the Arduino to detect that the photocell has changed from light to dark. In response to that change, then you will set a counter in your Arduino program to count up to 60 minutes and change whatever conditions you desire.