What We're DoingWhilst 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.

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

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 DarkThis 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).