26 March 2010

Task 28 - photocell-triggered LED switch: software

/*
* Project: Photocell_switch_task_28
* Author: Jane-Maree Howard
* Date: Friday 26/03/2010
* Platform: Arduino 18
* Purpose: To make a Photocell Switch for a LED.
* Operation: The circuit arrangement is the same as the ladyada
* LED dimmer, but this time the software has a trigger threshold
* for turning the LED on.
* The output of Analog 0 is measured as before, but instead
* of analogWrite() to the LED pin, digitalWrite is used to
* switch the LED on or off.
* We don't need to map the a0 output, since it's merely a trigger.
* Original description:
* Connect one end of the photocell to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground
Connect LED from pin 9 through a resistor to ground

*/
int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the sensor divider
int LEDpin = 9; // connect Red LED to pin 09(PWM pin)
int darkLevel = 100; // threshold for LED trigger action
void setup(void)
{
// We'll send debugging information via the Serial monitor
Serial.begin(9600); // baud rate 9600 should be set on the SM
digitalWrite(LEDpin, LOW);// turn LED off
}//end setup()

void loop(void)
{
digitalWrite(LEDpin, LOW);// turn LED off
photocellReading = analogRead(photocellPin); //i.e. analog 0

Serial.print("Analog reading = ");
Serial.print(photocellReading); // the raw analog reading

//we now use our 'darkLevel' variable to trigger the LED ON.
if (photocellReading <>

1 comment:

  1. I COULD swap the 150 Ohm LED buffer resistor for 100 Ohm to make the LED a bit brighter, but the principle has been demonstrated.. it works.

    ReplyDelete