Nightlight

Hello all, I am working on a project that I am calling "nightlight" and basically when the room is dark a photoresistor turns an LED on, and when it is light it turns it off. I am not sure if my issue is wiring or programming, so I'm posting this in both categories . As soon as I turn the arduino on with this setup, the LED just turns on and stays on no matter what I do with the photoresistor. Here is the wiring rendering and the code:

Wiring: NightLight | isaac kubas | Flickr

void setup()
{
  pinMode(2, INPUT);
  pinMode(8, OUTPUT);
}

void loop()
{
  if (digitalRead(2) == HIGH) digitalWrite(8, HIGH);
  else digitalWrite(8, LOW);
}

First, don’t post in 2 places.

Next,
if (digitalRead(2) == HIGH)

probably want to use an analog pin for this so you can get back more than a 0/1 result.

sigilwig444:
Hello all, I am working on a project that I am calling "nightlight" and basically when the room is dark a photoresistor turns an LED on, and when it is light it turns it off. I am not sure if my issue is wiring or programming, so I'm posting this in both categories . As soon as I turn the arduino on with this setup, the LED just turns on and stays on no matter what I do with the photoresistor. Here is the wiring rendering and the code:

Wiring: NightLight | isaac kubas | Flickr

void setup()

{
  pinMode(2, INPUT);
  pinMode(8, OUTPUT);
}

void loop()
{
  if (digitalRead(2) == HIGH) digitalWrite(8, HIGH);
  else digitalWrite(8, LOW);
}

As CrossRoads has said - the vast majority of LDRs are indeed analogue devices - therefore it is indeterminate what will happen when you connect them to a digital pin.

I would suggest you put some serial code into your example so you can get some basic debugging out to the serial console so you have an idea of what is happening.

It is also very helpful when doing an LDR to have a Potentiometer inline with it to enable you to control the values the LDR is returning and thus fine tune the process.

Craig

craigcurtin:

sigilwig444:
Hello all, I am working on a project that I am calling “nightlight” and basically when the room is dark a photoresistor turns an LED on, and when it is light it turns it off. I am not sure if my issue is wiring or programming, so I’m posting this in both categories . As soon as I turn the arduino on with this setup, the LED just turns on and stays on no matter what I do with the photoresistor. Here is the wiring rendering and the code:

Wiring: http://flic.kr/p/feu5Ao

void setup()

{
  pinMode(2, INPUT);
  pinMode(8, OUTPUT);
}

void loop()
{
  if (digitalRead(2) == HIGH) digitalWrite(8, HIGH);
  else digitalWrite(8, LOW);
}

As CrossRoads has said - the vast majority of LDRs are indeed analogue devices - therefore it is indeterminate what will happen when you connect them to a digital pin.

I would suggest you put some serial code into your example so you can get some basic debugging out to the serial console so you have an idea of what is happening.

It is also very helpful when doing an LDR to have a Potentiometer inline with it to enable you to control the values the LDR is returning and thus fine tune the process.

Craig

How do you program an analog pin???

if (digitalRead(A0) < 7)
{
  digitalWrite(8, HIGH)
}

To read an analogue value from an analogue pin and compare it with a value, which can be a variable if you want

if (analogRead(A0) < threshold)
{
  digitalWrite(outputPin, HIGH)
}

UKHeliBob:
To read an analogue value from an analogue pin and compare it with a value, which can be a variable if you want

if (analogRead(A0) < threshold)

{
  digitalWrite(outputPin, HIGH)
}

What would the threshold for a dark room be with a photo resistor???

It is also very helpful when doing an LDR to have a Potentiometer inline with it to enable you to control the values the LDR is returning and thus fine tune the process.

Do some experimenting.
Measure the voltage drop across the LDR and say a 10K POT (the divider would be powered by +5V)
+5---POT(10K)----analog_i/p_VoltMeter_LDR---GND_VoltMeter-
5V=1023, 0V=0

The meter may give you for example, 1volt = 204 for threshold when dark.

You need to print the reading from the sensor and decide the threshold for your setup

sigilwig444:

UKHeliBob:
To read an analogue value from an analogue pin and compare it with a value, which can be a variable if you want

if (analogRead(A0) < threshold)

{
  digitalWrite(outputPin, HIGH)
}

What would the threshold for a dark room be with a photo resistor???

You can only lead a horse to water - you can not make him drink it !!!

Craig

Hello!!! Once again I’m having trouble with this project. I don’t know what’s going on. The LED turns on and off depending upon what I set my threshold at. If anyone can tell me what’s going on that’d be great. When I set my threshold to 14 the LED is off and to 13 it is on.

Once again here is my wiring drawing: http://flic.kr/p/feu5Ao

Here is my code:

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

void loop()
{
  if (analogRead(A0) < 14); digitalWrite(8, HIGH)
  else digitalWrite(8, LOW)
}

BTW I switched my LDR from pin 2 to analog pin 0.

As wired, the LDR is just acting as a variable pullup resistor to 5V.
You need to wire it with a 2nd resistor as a voltage divider so that when the LDR changes values, the voltage at the junction of the resistors will change.
Vout = 5V*LDR/(LDR+R1)
where R1 connects to 5V, LDR connects to Gnd, their junction Vout connects to A0.

sigilwig444:
Hello!!! Once again I’m having trouble with this project. I don’t know what’s going on. The LED turns on and off depending upon what I set my threshold at. If anyone can tell me what’s going on that’d be great. When I set my threshold to 14 the LED is off and to 13 it is on.

Once again here is my wiring drawing: http://flic.kr/p/feu5Ao

Here is my code:

void setup()

{
pinMode(8, OUTPUT);
}

void loop()
{
  if (analogRead(A0) < 14); digitalWrite(8, HIGH)
  else digitalWrite(8, LOW)
}




BTW I switched my LDR from pin 2 to analog pin 0.

Have a look here - all the basic circuits are explained and documented - remember the code is not applicable to us - but will give you a basic understand of the flow of using some of these circuits

Craig

 if (analogRead(A0) < 14);

{ cough! }

CrossRoads:
As wired, the LDR is just acting as a variable pullup resistor to 5V.
You need to wire it with a 2nd resistor as a voltage divider so that when the LDR changes values, the voltage at the junction of the resistors will change.
Vout = 5V*LDR/(LDR+R1)
where R1 connects to 5V, LDR connects to Gnd, their junction Vout connects to A0.

You mean like this??? http://instagram.com/p/cMihqxqMXR/

Yes. Wired that way the changing light on the LDR will cause a variable voltage to be presented to the pin. You will need to write a small program to establish the threshold value. You would also be wise to have the light turn on and off at different light levels to avoid it turning on and off as the light varies around the threshold, such as when a cloud passes over.

It's finally finished. I redid some of my code and wired it properly. Here it is!!! http://instagram.com/p/cPu0yNqMZI/

sigilwig444:
It's finally finished. I redid some of my code and wired it properly. Here it is!!! http://instagram.com/p/cPu0yNqMZI/

Well done - finally got there - a good feeling isn't it ?

You should post your code up for someone else to use as a basis (even if you think it is ugly) in the completed projects

Craig