Photocell Switch

Hello,

I am trying to create a photocell switch for an LED. when the light levels decrease to a specified level, I want the LED to turn on. Unfortunately it is not working as I want it to.

I used a photocell setup like the one in Ladyada’s Photocell Tutorial. The 5v is running to the photocell which is running through a 10k pull down resistor, to Analog input 0 and to Ground. I attached my LED to pin 11 and ground.

This is my code:
/* Photocell simple testing sketch.

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 11 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 = 11; // connect Red LED to pin 11 (PWM pin)
int LEDbrightness; //
void setup(void) {
// We’ll send debugging information via the Serial monitor
Serial.begin(9600);
pinMode(LEDpin, OUTPUT);
}

void loop(void) {
photocellReading = analogRead(photocellPin);

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

// LED gets brighter the darker it is at the sensor
// that means we have to -invert- the reading from 0-1023 back to 1023-0
photocellReading = 1023 - photocellReading;
//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);
if (photocellReading < 200){
digitalWrite(LEDpin, HIGH);
}else{
digitalWrite(LEDpin, LOW);
}
}

My problem is the LED blinks and also changes brightness in response to the photocell reading. What I want is an independent LED that turns on and off in response to the photocell reading but does not change its brightness. I am a bit confused why it does change brightness in this setup because.

Anyone have any advice. Sorry if I did a poor job explaining the setup, Im new to Arduino.

Thanks

You've got both analogWrites and digitalWrites to the LED pin. Which did you intend?

To be honest I'm not sure. I dont really know the difference. Whichever makes more sense for the circuit.

Digital if you want on/off, analogue if you want dimming

Just realized I didn't complete a thought in my original post.

"I am a bit confused why it does change brightness in this setup because."

Because I thought I had put the LED in a separate circuit, isolated from the photocell resistor. I want the Arduino to read the photocell resistor and then tell the LED to turn on or off in response to that reading. I thought I did that by putting the LED in a circuit with pin 11 and ground but it is still changing brightness in response to the photocell.

First try to understand the photocell completely. Try this code and you will see what values the analog port returns under which conditions. If this is clear, add the logic for the LED (on/off)
Every yourney begins with small steps (chinese proverb)

int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the sensor divider

void setup(void) 
{
 Serial.begin(9600);  
 pinMode(LEDpin, OUTPUT);
}

void loop(void) 
{
 photocellReading = analogRead(photocellPin);  

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

 delay(100);
}

PS you can use the - code tag # - in the editor to make code look better

Thank you Groove. I switched the code to digitalRead the photocell. Now in the serial monitor it reads "1" every time it Reads the photocell. Why does this occur? How is digitalRead different than analogRead.

robtillaart, I have already succesfully gotten that code to work. I am now trying to take the second step and get the photocell to trigger an LED.

Thanks for the clarification.

I got the code to work. Not sure exactly what I did wrong before, but it works! I started with the analogRead code in the Arduino example database.
Thanks everyone for your help. Here’s the working code

/*
  
 
 
int sensorPin = 0;    // select the input pin for the photocell
int ledPin = 11;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  
  Serial.begin(9600);   
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);    
  Serial.print("Digital reading = ");
  Serial.println(sensorValue);     // the raw analog reading
  // turn the ledPin on
  if (sensorValue < 700){
    digitalWrite(ledPin, HIGH);
  }else{
  digitalWrite(ledPin,LOW);
  }  
  // stop the program for <sensorValue> milliseconds:
  delay(10);          
              
}