Turning ON/OFF a LED with light sensor

Hi,

I'm a newbie in electronics.

I'm following an example in the book "Getting Started with Arduino" byMassimo Banzi.

At page 60 bellow the "Use a Light Sensor Instead of the Pushbutton" heading it is advised that tha one build the simple circuit that came with Example 02 ( "Using a Pushbutton to Control the LED" ). See attachment: Abra_4-6_pici_300.png.
That circuit can be used with the code:

// Example 02 : Turn on LED while the button is pressed

const int LED = 13;      // the pin for the LED
const int BUTTON = 7; // the input pin where the pushbutton is connected
int val = 0;               // val will be used to store the state of the input pin

void setup()
{
  pinMode(LED, OUTPUT);    // tell Arduino LED is an output
  pinMode(BUTTON, INPUT); // and BUTTON is an output
}

void loop()
{
  vred = digitalRead(BUTTON); // read input value and store it
  
  // check whether the input is HIGH ( button pressed )
  if (vred == HIGH)   {
     digitalWrite(LED, HIGH);   // turn LED on
  }  else  {
  digitalWrite(LED, LOW);   // turn LED off
  }
}

Then it is advised to plug the LDR onto the breadboard instead of the pushbutton. See the second attachment: Abra_4-6_FotoCellaval_400.png.

Now when one covers the LDR with it's hands, the LED should turn off, and when one uncovers the LDR the light should goes on.

But here this is not happen!?
Why?

Abra_4-6_pici_300.png

Abra_4-6_FotoCellaval_400.png

when using the LDR you made a voltage divider. You should connect it not to digital pin but to an analogpin and use analogRead()

const int LED = 13;      // the pin for the LED

void setup()
{
  pinMode(LED, OUTPUT); 
}

void loop()
{
  int vred = analogRead(A0);    //vred = 0..1023
  
  if (vred > 500)  {
     digitalWrite(LED, HIGH);   // turn LED on
  }  else  {
  digitalWrite(LED, LOW);   // turn LED off
  }
}

The reason it is probably not working is the LDR/Resistor voltage divider is not raising/dropping the voltage beyond the on/off threshold of the digital input pin.
You could connect the wire going to pin 7 to A0 instead and use Serial to print the analogue read on that pin to check the circuit is working okay. The code you posted has errors and I'm surprised it even compiled and uploaded.

Another issue I have with the circuit is it shows the LED directly connected between a pin and GND. Some LED's have resistors built in and/or maybe this is fine for pin 13 as it already has an LED attached but I don't think it's a good demonstration of how LED's are connected to arduino pins.

Well, as I sed I'm a newbie in electronics.

The circuit and the code is working yet but I was who doesn't know how to cover the LDR properly.

I tried to cover LDR just by putting my one finger at the top of the LDR but this wasn't enough.
One must to get LDR around with tree fingers or to cover it with palm so form with her/his palm a shape like a hemisphere.

And yes, LDR is for connecting to an analog pin of Arduino so one can uses analogRead() function.
And yes, a LED must be used with a resistor of 220 or 270 Ohm connected serial with the LED.

Thank you for your help!

[SOLVED] changed the value in the if-statement to 50 [/SOLVED]

Hello

I'm following the same book to get introduced into Arduino.
I have entered the code that Rob provided, but it doesn't seem to do the trick.

Attached you'll find my scheme (or an attempt of a scheme). Am I doing something wrong? I already replaced the LDR en LED but the result stays the same.

Kind Regards
David