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:
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:
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.
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:
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)
}
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.
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.
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.
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
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.
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.