Photocell Hookup Guide

Pages
Contributors: jimblom
Favorited Favorite 14

Introduction

Photocells are light-sensitive, variable resistors. As more light shines of the sensor’s head, the resistance between its two terminals decreases. They're easy-to-use, and an essential component in projects that require ambient-light sensing.

Mini Photocell

Mini Photocell

SEN-09088
$1.60
7

In pitch-black conditions, the photocell’s resistance will be in the megaohm’s (1.0MΩ+) range. Shining an LED on the sensor can drop the resistance to near-zero, but usually the resistance of the photocell falls between 8-20kΩ in normal lighting conditions.

By combining the photocell with a static resistor to create a voltage divider, you can produce a variable voltage that can be read by a microcontroller's analog-to-digital converter.

Suggested Materials

This tutorial serves as a quick primer on resistive photocells', and demonstrates how to hook them up and use them. Beyond the light sensor, the following materials are recommended:

Arduino Uno -- We'll be using the Arduino's analog-to-digital converter to read in the variable resistance of the photocell. Any Arduino-compatible development platform -- be it a RedBoard, Pro or Pro Mini -- can substitute.

Resistor Kit -- To turn the photocell's variable resistance into a readable voltage, we'll combine it with a static resistor to create a voltage divider. This resistor kit is handy for some trial-and-error testing to hone in on the most sensitive circuit possible.

Breadboard and Jumper Wires -- The photocell's legs, like any through-hole resistor, can be bent and shaped to fit. We'll stick them and the resistor into a breadboard, then use the jumper wires to connect from breadboard to Arduino.

Resistor Kit - 1/4W (500 total)

Resistor Kit - 1/4W (500 total)

COM-10969
$8.95
189
Breadboard - Self-Adhesive (White)

Breadboard - Self-Adhesive (White)

PRT-12002
$5.50
48
SparkFun RedBoard - Programmed with Arduino

SparkFun RedBoard - Programmed with Arduino

DEV-13975
$21.50
50
Jumper Wires Standard 7" M/M - 30 AWG (30 Pack)

Jumper Wires Standard 7" M/M - 30 AWG (30 Pack)

PRT-11026
$2.45
20

Suggested Reading

Photocells are a great entry-level component for beginners, but there are still a few basic electronics concepts you should be familiar with. If any of these tutorial titles sound foreign to you, consider skimming through that content first.

Light

Light is a useful tool for the electrical engineer. Understanding how light relates to electronics is a fundamental skill for many projects.

Analog to Digital Conversion

The world is analog. Use analog to digital conversion to help digital devices interpret the world.

Voltage Dividers

Turn a large voltage into a smaller one with voltage dividers. This tutorial covers: what a voltage divider circuit looks like and how it is used in the real world.

Analog vs. Digital

This tutorial covers the concept of analog and digital signals, as they relate to electronics.

Photocell Overview

The photocell, sometimes referred to as a photoresistor or light-dependent resistor (LDR), is a two-terminal, resistive component that increases or decreases its resistance depending on the light it senses. They're available in a variety of shapes, sizes, and form factors; the mini photocell in our catalog features a 5x4.3mm head, and through-hole legs that can be soldered into a PCB or inserted into a breadboard.

Mini photocell dimensions

In pitch black conditions, the resistance of most photocells will measure in the megaohms range. The typical light resistance of photocells varies by component. The mini photocell, for example, usually produces a resistance between 8-20kΩ in normal lighting conditions.

The graph below demonstrates the mini photocell's illumination and resistance relationship:

Mini photocell illuminance vs resistance

Light vs. Resistance graph from the mini photocell datasheet.

As you can tell from the graph above, these sensor's aren't designed for absolute lux-measurement accuracy -- they leave a lot of room for interpretation. But, by measuring the photocell's resistance, they can provide a relative idea of a room's lighting conditions, or tell us if the sun has risen or set.

Example Circuit

To measure the photocell's resistance with a microcontroller's ADC, we actually have to use it to generate a variable voltage. By combining the photocell with a static resistor, we can create a voltage divider that produces a voltage dependent on the photocell's resistance.

A static resistor value between 1kΩ and 10kΩ should pair well with the photocell. If you have a resistor kit, you may want to introduce some trial-and-error to hone in on that perfect static resistance.

In this example, we'll use a 4.7kΩ resistor to divide voltage with the photocell. Here's the example circuit:

Example circuit fritzing diagram

And a schematic:

Example circuit schematic

The 4.7kΩ resistor on the ground side, and the photocell on the 5V side, means as the cell's resistance increases (meaning the sensor's surroundings are getting darker) the voltage on A0 will decrease.

Example Program

Here is a simple Arduino example based on the circuit above. Copy and paste this into your Arduino IDE, then upload!

Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE.

If you have not previously installed an Arduino library, please check out our installation guide.

language:c
/******************************************************************************
Photocell_Example.ino
Example sketch for SparkFun's photocell - light-variable resistor
  (https://www.sparkfun.com/products/9088)
Jim Lindblom @ SparkFun Electronics
April 28, 2016

Create a voltage divider circuit combining a photocell with a 4.7k resistor.
- The resistor should connect from A0 to GND.
- The photocell should connect from A0 to 3.3V
- Connect an LED to pin 13 (if there's not one built into your Arduino)
As the resistance of the photocell increases (surroundings get darker), the
voltage at A0 should decrease.

Development environment specifics:
Arduino 1.6.7
******************************************************************************/
const int LIGHT_PIN = A0; // Pin connected to voltage divider output
const int LED_PIN = 13; // Use built-in LED as dark indicator

// Measure the voltage at 5V and the actual resistance of your
// 47k resistor, and enter them below:
const float VCC = 4.98; // Measured voltage of Ardunio 5V line
const float R_DIV = 4660.0; // Measured resistance of 3.3k resistor

// Set this to the minimum resistance require to turn an LED on:
const float DARK_THRESHOLD = 10000.0;

void setup() 
{
  Serial.begin(9600);
  pinMode(LIGHT_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
}

void loop() 
{
  // Read the ADC, and calculate voltage and resistance from it
  int lightADC = analogRead(LIGHT_PIN);
  if (lightADC > 0)
  {
    // Use the ADC reading to calculate voltage and resistance
    float lightV = lightADC * VCC / 1023.0;
    float lightR = R_DIV * (VCC / lightV - 1.0);
    Serial.println("Voltage: " + String(lightV) + " V");
    Serial.println("Resistance: " + String(lightR) + " ohms");

    // If resistance of photocell is greater than the dark
    // threshold setting, turn the LED on.
    if (lightR >= DARK_THRESHOLD)
      digitalWrite(LED_PIN, HIGH);
    else
      digitalWrite(LED_PIN, LOW);

    Serial.println();
    delay(500);
  }
}

After uploading, open your serial monitor, and set the baud rate to 9600 bps.

Then trigger some changes in light; cover the photocell with your hand, turn your lights off, or shine a flashlight on the cell. You should see the voltage and resistance calculations vary with the light. If it gets darker, the resistance should go up. If it gets lighter, the resistance should go down.

FSR readings to serial monitor

If the values don't look correct to you, make sure the R_DIV variable at the top of the sketch is set to your static resistor's value.

When the sensed light gets too dark, the Arduino should turn on the pin 13 LED to try to brighten things up. By adjusting the DARK_THRESHOLD variable, you can change what lighting conditions trigger this LED illumination.

Resources and Going Further

Now that you've got your Arduino reacting to ambient lighting conditions, what light-dependent project are you going to create? Need some inspiration? Check out some of these SparkFun tutorials:

SparkPunk Hookup Guide

How to assemble and modify the SparkPunk Sound Generator kit.

SparkFun Inventor's Kit for MicroView

The SparkFun Inventor's Kit for MicroView follows our tried and true inventor's kit model, bringing you 11 simple yet fun experiments to introduce you to the SparkFun MicroView.

LilyPad Light Sensor Hookup Guide

How to hook up the LilyPad Light Sensor as well as some project ideas and example code.

Blynk Board Project Guide

A series of Blynk projects you can set up on the Blynk Board without ever re-programming it.

Light-Seeking Robot

We use parts from the SparkFun Inventor's Kit v4.0 to create a light-seeking robot that mimics the behavior of single-celled organisms.

SparkFun Inventor's Kit Experiment Guide - v4.0

The SparkFun Inventor's Kit (SIK) Experiment Guide contains all of the information needed to build all five projects, encompassing 16 circuits, in the latest version of the kit, v4.0a.

Python Programming Tutorial: Getting Started with the Raspberry Pi

This guide will show you how to write programs on your Raspberry Pi using Python to control hardware.

Or check out the old example that was used in our SIK guide that sets the LED's brightness based on the input of the photocell.

SIK Experiment Guide for Arduino V3.3: Experiment 6: Reading a Photoresistor

Maybe check out the Toy Car Speed Trap project example listed in Workbench Education or embed the light sensor into a project.