Arduino Night Light using a Photocell

Hooray It’s Saturday, instead of making music, painting or coding Actionscript I decided to tinker with my Arduino. I started off my day by heading down to the coffee shop and picking up a cup of Joe. As I was waiting in line, I decided I would take a drive up to my local Radio Shack and peruse the component bins for some ideas. Now, this isn’t an ideal situation, since Radio Shacks components are a bit pricey, but since I had already perused my component bin at home and wanted something new to work on, I thought what the hell a few more dollars wouldn’t break my wallet.

Anyhow, at Radio Shack I bought a pack of Photocells, which contained 5 cells of different sizes. I have used Photocells in the past when I built some DIY analog synths, so, I was familiar with them, but I have never integrated them with my Arduino. So, I set out on an adventure to make a simple electronic gadget. The first thing that came to mind was to control a basic LED. I figured it out rather quickly, and realized that I could make an interesting night light with it, if I swapped the basic LED with a ShiftBrite LED, but that’s a recipe for another blog post.

So what the hell is a Photocell?

A Photocell acts as a light sensor and is usually used when you just want to detect light. Photocells are used in toys, appliances and in street lamps that turn themselves on at night. Sometimes known as photo resistors, they look like a small 0.5 to 2 inch disk with two leads out the back. They are inexpensive, low-power, and very easy to use. Photocells are available from a number of online sources and as mentioned at your local Radio Shack (catalog number 276-1657 ).
Technically Photocells are basically a resistor that changes its resistive value (in ohms Ω) depending on how much light the face is exposed to. Photocell properties vary widely from model to model, they tend to be very inaccurate and they shouldn’t be used to try to determine precise light levels you should only use them to determine basic light changes. Photocells are non-polarized and are pretty hardy as long as you avoid bending the leads right at the epoxied sensor.

Enough technical talk lets get crackin’.
Make sure you have all the components needed to make this simple circuit:

  • Arduino Duemilanove
  • Breadboard
  • 220-Ohm resistor
  • LED
  • Photocell
  • 10k resistor
  • Breadboard wires
  • The first thing I always do when prototyping, is connect the Arduino to a power source either via the USB or external. I then proceed to power up the breadboard.

    I simply take a Red wire from the Arduino 5v(+) and connect it to the red rail on the breadboard. Then I connect a Red wire from the red rail on the breadboard to the other red rail on the opposite side of the breadboard. I then take a Black wire and connect it to the GND of the Arduino and connect it to the blue rail on the Breadboard. I then follow suit by connecting a Black wire from the blue rail on the breadboard to the other blue rail on the opposite side of the breadboard. We now have power!

    The next step is to connect the Photocell to the breadboard and wire it up to the Arduino.
    Add the Photocell to the board. Connect a Red wire from the red rail on the board to one of the leads of the Photocell, remember Photocells are non-polarized, which means you can connect them up ‘either way’ and they’ll work just fine.

    Now, lets add the 10k resistor. Connect one end of the resistor to the other lead of the Photocell.

    Connect a wire from the same Photocell lead that the resistor resides on to Analog 0 on the Arduino. Then ground the resistor by adding a black wire from the blue rail on the breadboard to the other end of the resistor, like so.

    You have now created a basic Photocell circuit.
    The code below is all you would need to get values from the Photocell. You could easily copy the code below, create a new photocellTest.pde file, compile, upload to your Arduino and view the output of the Photocell in the Serial monitor.
    /*
    This example shows the output of an analogRead() of a Photocell.
    By M.Gonzalez
    www.codingcolor.com
    The example code is in the public domain
    */

    int photocellPin = 0;// Photocell connected to analog pin 0
    int photocellVal = 0; // define photocell variable


    void setup() {
      Serial.begin(9600);
      pinMode(photocellPin, INPUT);
    }
    void loop() {
      photocellVal = analogRead(photocellPin);// read the analog from photocell
      Serial.println( photocellVal);    // print to screen
      delay(30);

     }

    Due to the inaccuracies of the readings, I would recommend adding a conditional statement as a threshold like so.

    /*
    This example shows the output of an analogRead() of a Photocell.
    By M.Gonzalez
    www.codingcolor.com
    The example code is in the public domain
    */

    int photocellPin = 0;// Photocell connected to analog pin 0
    int photocellVal = 0; // define photocell variable


    void setup() {
      Serial.begin(9600);
      pinMode(photocellPin, INPUT);
    }
    void loop() {
      photocellVal = analogRead(photocellPin);// read the analog from photocell
      Serial.println( photocellVal); // output to screen

      if (photocellVal < 100){
        Serial.println("lights on");// output to screen
       }                      
        else if (photocellVal > 100){
        Serial.println("light out");// output to screen
       }
      delay(30);

     }

    Moving forward we will add the LED which will be the Night Light.
    Grab your LED. You will notice that one lead is longer than the other. The longer lead is the Anode (+) and shorter lead is the Cathode(-), its important to now this, since you need to make sure you are connecting the circuit correctly. Plug the LED into your breadboard taking note of which lead is the Anode (positive) and which is the Cathode (ground). Now run a black wire from the blue rail on the breadboard to the Cathode lead(-) of the LED. Connect one end of the 220 Ohm resistor to the Anode(+) lead of the LED. Connect a wire from the PMW 9 pin on the Arduino to the other end of the 220 Ohm resistor. Your board should now resemble the following:

    We have finally completed the simple circuit. Lets now update or existing code. We will add variables for the LED, create a new function which fades the LED in and out, and revise the conditional threshold statement.

    /*
    This example shows the output of an analogRead() of a Photocell.
    By M.Gonzalez
    www.codingcolor.com
    The example code is in the public domain
    */


    int photocellPin = 0;// Photocell connected to analog pin 0
    int photocellVal = 0; // define photocell variable
    int ledPin = 9;// LED connected to digital pin 9
    int ledState = 0;//state of the led
    int fadeDown = 30;//delay per fade
    int fadeUp = 20;//delay per fade
    int minLight = 100;//min light threshold
    int maxLight = 100;//max light threshold


    void setup() {
     //Serial.begin(9600);
      pinMode(photocellPin, INPUT);
      pinMode(ledPin, OUTPUT);
    }
    void loop() {
      photocellVal = analogRead(photocellPin);

      if (photocellVal < minLight and ledState == 0){
        fadeLed(1);
        //Serial.println("fade up");
      }                      
        else if (photocellVal > maxLight and ledState == 1){
        fadeLed(0);
       // Serial.println("fade down");
      }
       

     
    }

    void fadeLed(int num){
      if (num == 1){
         for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
         analogWrite(ledPin, fadeValue);            
         delay(fadeUp);                            
        }
         ledState = 1;
       
     }
      else{  
         for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
         analogWrite(ledPin, fadeValue);            
         delay(fadeDown);                            
      }
       ledState = 0;
     }

     
    }

    That pretty much concludes my Night Light prototype. Like I mentioned at the beginning of this post, I plan to tinker around with this base code and circuit and add a brighter LED. I’m awaiting on both a ShiftBrite LED and a BlinkM to arrive in my mail box. Till then happy tinkering.

    Be Sociable, Share!
    Coding Color | 2009 All Rights Reserved.