Learning to Program a Arduino - PhotoCell




Introduction: Learning to Program a Arduino - PhotoCell

Hi there! Today I will be talking about using a photocell with programming, But first we have to Bread board all the components into the right places.  do so like the photo below, or buy the Photocell kit from Aarons Robotics and read the manual in the package.


Here is what

How do Photocells Work?!

That is a very good question, Photocell consists of two special pieces of silicon joined together.
Pure silicon is naturally charged but if small amount of an impurity such as phosphorus is added, there are more free electron which make this a better conductor than pure silicon.
This is known as n-type(negative) silicon.
If a different impurity, such as boron, is added there is an absence of free electron. The absence of an electron is known as a hole and the silicon is known as p-type silicon.
When these two types of silicon are placed together it is called a p-n junction. An electric field is created between the two layers.
Sunlight is made up of a tiny packets of energy called photons. When photons are absorbed the energy causes electron to become free. They move to the negative layer and leave the junction to flow in an external circuit.

Step 1: Programing

We can now start taking about how we program this unique tool to sense light!  Be sure you have the Arduino software downloaded on your computer! This can be found at :  http://arduino.cc/en/Main/Software

Next Upload this code given below

int photocellPin = 0; // the cell and 10K pulldown are connected to a0
    int photocellReading; // the analog reading from the sensor divider
    int LEDpin = 11; // connect Red LED to pin 11 (PWM pin) (this is not needed to use the sensor but if you have one laying around it could add a cool affect! This can be bought at your local Radio Shack.)
    int LEDbrightness; //
    void setup(void) {
    Serial.begin(9600);    // We'll send debugging information via the Serial monitor so if you can read it   without using a led

    void loop(void) {
    photocellReading = analogRead(photocellPin);
    Serial.print("Analog reading = ");
    Serial.println(photocellReading); // the raw analog reading
    // LED gets brighter the darker it is at the sensor
    // that means we have to -invert- the reading from 0-1023 back to 1023-0
    photocellReading = 1023 - photocellReading;    //now we have to map 0-1023 to 0-255 since thats the          range analogWrite uses
    LEDbrightness = map(photocellReading, 0, 1023, 0, 255);
    analogWrite(LEDpin, LEDbrightness);

Step 2: Hacking!

Test it out! The Reading will show up under the serial monitor under “Tools” at the took on the arduino editor screen .  You can Hack this code a Little and have it buzz with a piezo speaker! (you can put this up also at Radio Shack!)
Plug the piezo into pin 4 with the black wire or change the pin it at int piezoPin = 4;
Plug in the Photocell Circuit in at Pin 0 or change it at int photosensorPin = 0;

int photosensorPin = 0;
int piezoPin = 4;
int val = 0;
void setup() {
  pinMode(piezoPin, OUTPUT);
void loop() {
  digitalWrite(piezoPin, LOW);
  val = analogRead(photosensorPin);
  val = val/2;
for( int i=0; i<500; i++ ) {  // play it for 50 cycles
    digitalWrite(piezoPin, HIGH);
    digitalWrite(piezoPin, LOW);

here is my Video of using a little speaker to sense light
Flash Animation

Thanks! Please contact me if you have any questions in using the Photo Cell and programming with it.

Be the First to Share


    • Make It Bridge

      Make It Bridge
    • Big and Small Contest

      Big and Small Contest
    • Game Design: Student Design Challenge

      Game Design: Student Design Challenge



    6 years ago

    OK so this needs a lot more detail. What is the resistance for example? THis is just a load of links not an instructable at all.


    6 years ago

    Hello. I need to use arduino and photocell for coin sorter and counting project. But i dont know how to program it. Can you help me?


    thanks for the arduino tutorial, I think i'd be helpful if you talked a little more about what the code in step one does, maybe upload a video


    Reply 10 years ago on Introduction

    Ok, well I can edit the code a little cuz after any // means I'm talking about what that line in the code does. hope this help i could also post this up there to tell them I'm trying to tell what part does what. I do not have a desk top camcorder.