22 March 2010

Task 27 - ladyada software improved

/*
* Project: ladyada_Photocell_task_27
* Author: Jane-Maree Howard
* Date: Monday 22/03/2010
* Platform: Arduino 18
* Purpose: To improve on the ladyada Photocell operation.
* Operation: The previous arrangement showed good sensitivity on the
* Serial monitor, but very little on the LED.
* NOTE: I took out the line
* photocellReading = 1023 - photocellReading;
* and it started to behave as I thought it should i.e.
* getting darker as the photocell was darkened & v.v.
* 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
For more information see www.ladyada.net/learn/sensors/cds.html
*/
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 LEDbrightness; //
void setup(void)
{
// We'll send debugging information via the Serial monitor
Serial.begin(9600); // baud rate 9600 should be set on the SM
}//end setup()

void loop(void)
{
photocellReading = analogRead(photocellPin); //i.e. analog 0

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

//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);

delay(100);
}//end loop()

/* The device now behaves as it should- getting brighter or darker with the cds Photocell's light level.
Definitely an improvement!!*/

2 comments:

  1. A 1kOhm resistor as a LED buffer gives a nice range of brightness (measured on a multimeter - i can't read those tiny-tiny resistor rings.

    The measured resistance of the photocell is around 7-8kOhm, so a 10kOhm resistor is about right for the divider network.

    I'm not sure why the 'ladyada' program had that bit about inverting the Analog input - it's doing fine without it!!

    ReplyDelete
  2. I shone a small but rather bright LED torch on the photocell & the reading on the SM shot up to 900+

    I then covered the photocell with a small socket from my little toolkit, & the reading on the SM dropped to single figures & sometimes to zero.

    ReplyDelete