NCIR
09
.:Light:.
.:Photo Resistors:.
(NEDX) .:Netduino Experimentation Kit:. (NEDX)

What We're Doing
Whilst getting input from a potentiometer can be useful for human controlled experiments, what do we use when we want an environmentally controlled experiment? We use exactly the same principles but instead of a potentiometer (twist based resistance) we use a photo resistor (light based resistance). The Netduino cannot directly sense resistance (it senses voltage) so we set up a voltage divider (http://nedx.org/VODI). The exact voltage at the sensing pin is calculable, but for our purposes (just sensing relative light) we can experiment with the values and see what works for us. A low value will occur when the sensor is well lit while a high value will occur when it is in darkness.


The Circuit
The Parts
NCIR-09
Breadboard Sheet
x1
2 Pin Header
x4
Photo-Resistor
x1
Wire
10k Ohm Resistor
Brown-Black-Orange
x1
560 Ohm Resistor
Green-Blue-Brown
x1
Green LED
x1


Schematic

 

Resources
.:download:.
breadboard layout sheet
http://nedx.org/NBLS09

 

Code (no need to type everything in just)
Download the Code from ( http://nedx.org/CODE09 )
(copy the text and paste it into an empty Netduino Sketch)

//Libraries that our program uses
using System;                               //use base classes                            
using System.Threading;                     //use threading classes        
using Microsoft.SPOT;                       //use smart personal objects technology classes
using Microsoft.SPOT.Hardware;              //use hardware related SPOT classes
using SecretLabs.NETMF.Hardware;            //use Secret Labs hardware framework
using SecretLabs.NETMF.Hardware.Netduino;   //use the Netduino specific classes

namespace NCIR09                    /// Define the namespace we are in ///
{
    public class Program            /// Define any variables used in the program and subrout
                    //ines after Main()///
    {
        static AnalogInput lightSensor = new AnalogInput(Pins.GPIO_PIN_A0);  // Define the p
                    //hoto resistor as connected to analog pin 0
        static PWM led = new PWM(Pins.GPIO_PIN_D9);     // Define the LED as connected to pi
                    //n 9

        public static void Main()   /// The Main loop (run at power up) ///
        {
            int value = 0;          // declare a variable for holding the light sensor readi
                    //ng

            while (true)            /// Do Forever ///
            {
                value = lightSensor.Read();             // read the photo resistor value
                value = (int)((double)value * 0.1111);  // adjust to the seen value ~0-900 a
                    //nd scale to 0-100
                led.SetDutyCycle((uint)value);          // set the brightness of the LED bas
                    //ed on the sensor reading
            }                       /// Close Forever Loop ///  
        }                           /// Close the Main() Loop ///
    }                               /// Close the Program Loop ///
}                                   /// Close the Namespace Loop ///

 

Not Working? (3 things to try)
 

 
LED Remains Dark
This is a mistake we continue to make time and time again, if only they could make an LED that worked both ways. Pull it up and give it a twist.
 
It Isn't Responding to Changes in Light.
Given that the spacing of the wires on the photo-resistor is not standard, it is easy to misplace it. Double check its in the right place.
 
Still not quite working?
You may be in a room which is either too bright or dark. Try turning the lights on or off to see if this helps. Or if you have a flashlight near by give that a try.
 
 

Making it Better?
 
Reverse the response:
Perhaps you would like the opposite response. Don't worry we can easily reverse this just change:

led.SetDutyCycle((uint)value);
|
led.SetDutyCycle(100 - (uint)value);

Upload and watch the change:

Night light:
Rather than controlling the brightness of the LED in response to light, let's instead turn it on or off based on a threshold value. Replace the code after
while (true) with.
int threshold = 300;
value = lightSensor.Read();
if (value > threshold)
{
led.SetDutyCycle(100);
}
else
{
led.SetDutyCycle(0);
}


To change the point where the LED turns on or off change the threshold value.

For this example we use the PWM function on pin 9. As we are just turning the LED on or off, you could switch to using an OutputPort instead (this would allow you to use any pin rather than just those compatible with PWM).