HC-SR501 with LDR attached to it.

I attached LDR to the pins as shown in the attachment.
This LDR is now inside the PIR.

My question is How to access the LDR values.
I am also attaching the schematic.

And here is the code I picked to modify:

#define LAMP  8  // choose the pin for the RELAY
#define PIR 13   // choose the input pin (for PIR sensor)                 
void setup()
  pinMode(LAMP, OUTPUT); // declare lamp as output
  pinMode(PIR,INPUT); // declare sensor as input                                                                                                                            
void loop() 
  int value_ldr = analogRead(A4); // read LDR value
  int value_pir = digitalRead(PIR); // read input value

 if((300>value_ldr) && ( value_pir==HIGH) ){
       digitalWrite(LAMP,1);  // Turn ON the light
else {
       digitalWrite(LAMP,0); // Turn OFF the light

The code is using A4; and I have no way to connect to pin A4 since the LDR is inside the PIR as I mentioned earlier.

If someone used this sensor, he/she will be able to understand my confusion.

Here what I found so far:

  1. Very informative youtube
    Adding a light sensor to a PIR module. - YouTube
  2. I soldered the LDR and took a wire between R3 and the LDR ( see schematic ).
  3. used the above sketch as is,just adding println to display ldr value and taking a wire to A4.

I am getting very low LDR value and the module is NOT responsive as it should be..

Any Ideas ???.

It doesn't sound like you're using the module the way you should.

Just get another LDR, and wire it up the proper way for your Arduino to read, and let the module take care of its own LDR.

You are right....The way the module works internally is to measure the voltage across the added LDR.Then the mcu takes care of enable/disable based on that voltage measure.

And I think your solution will make me in full control of the LDR readings.

Thank you for your input

If you connect the LDR like you did, then the PIR chip will ofcourse disable it's output when it's light outside.
So you can't control anything with the Arduino anymore unless ist's dark outside.

Yes. The added feature ( installing LDR inside the module) is doing exactly that.
What triggered this post was: How dark and how light the mcu will perform the disabling of the PIR function.
Also can I hack it to specific range of Darkness.

If anyone of you care to join me in this little hack, I can give you my thoughts on it.

The question is; is it worth it ?? For me it is the challenge.....there is no material benefits out of it.

Remove the LDR from the PIR sensor.
Use the Arduino to read darkness levels (using LDR or other light sensor).
When dark enough have it switch on the sensor: high side PNP transistor or logic level P-MOSFET in the Vcc line to the sensor (LOW signal will switch ON the sensor, HIGH signal switches it off).

Or if you mean have the Arduino respond to the PIR signal:
Remove the LDR from the PIR sensor.
Use the Arduino to read darkness levels (using LDR or other light sensor).
When dark enough start acting on the signal coming from the sensor, otherwise simply ignore it.

I will go with your first approach. Can you please look at the script that I got from Internet (see above)
I need to latch the relay when I get the signal from PIR ( yes after removing the LDR and installing it externally)

Is it possible to help in modifying the script to work with your suggested PNP solution.

The way I am visualizing your approach is; The signal from PIR will go to the base of the PNP with say 1 K ohm.
The collector will control the Relay, And the relay will LATCH the lamp ON.
If I want to switch the lamp OFF I will use the physical switch .

Of course life in NOT that easy; because of this dumm switch status. ( is it ON...Is it OFF ???? )

So you want to have the PIR work in the dark only, and switch on a latching relay, that stays on until you press a button (momentary switch).

So the only thing the Arduino has to do, is detect dark/light and switch on the PIR sensor accordingly, right? Sounds a very overengineered solution to just adding the LDR to the PIR sensor directly. If you want better control for the darkness level at which it switches - which is based on the resistance of the LDR, low resistance is light, high resistance is dark - it may work to add a 100k pot.

Then all that's left is the latching relay, that's also a routine circuit. Easiest is if you have a double relay, where one set of poles switches the light, the other set latches the relay itself.

Like this:
Note: the NPN transistor is probably built into your relay module already, together with the base resistor.

Thank you for taking the time to help me out with this project.

By "overengineering" you mean there is no need for arduino. Just a combination of PIR, LDR and relay.
( I misguided you when I said Latched Relay ) I do not have a lathed relay I have normal 5v relay module.

I learned a lot from you comments and schematics:

  1. The physical switch must be momentary switch to toggle the status of the lamp.
  2. Using pot will make it easy to adjust the darkness level, which was a major request from my side.
  3. I wonder then if arduino is not used I should rely on my effort to do it and NOT bothering you ( Arduino community) with a problem that is pure electronics.

If my comments make sense to you, then please advice I am at a loss here

A latched relay is just a way of having a relay keeping itself on. Nothing special, really. I drawn it using a double relay; you can also use a second relay to switch the lamp, which in turn is kept on by the latching one. A momentary push button can break this circuit and switch off the light after the PIR signal has gone low.

A PIR will normally keep the line high for a certain amount of time (a few seconds to about 10 minutes), then turn it low again. You can not interrupt this, you can however adjust it through a pot present on the unit.

Make sure your button out of view of the PIR sensor or just the motion of pushing it will trigger the sensor again.

This is great....Really....

Now...what I have so far is adjustable Dark range and the Lamp is on ONLY in the dark that I specified though the pot as you suggested.

And yes the lamp goes off after few second. This is great so far.

Now latching the relay and the momentary switch. I like the idea of second relay.But I have no idea how to connect it to latch the lamp on after these few seconds!!!

Also how the momentary switch does the flipping of on/off of the lamp. HOW ?? your explanation will be greatly appreciated.

I will make sure that the PIR is hidden to avoid the retriggering and messing things up.

Latching relay works as I have shown above - the moment it's activated the NO contacts close, keeping the relay activated. The momentary button breaks that circuit, releasing the relay. Just use the output of that one to not only switch itself on, but also another relay. That's quite straightforward.

I will follow the wiring that you posted above. The only difference using TWO relays instead of the

(DPDT) RELAY you used, is that I have to trigger BOTH relays with PIR signal ( using NPN Transistor) and short circuiting pins following your schematic, will provide the latching. ( is that true ????)

I assume that I need only ONE momentary switch to do the unlatching which will provide the lamp on/off function.

I will do it as I explained here and hope for the best.

P.S. May be..Just may be ...people prefer using microprocessor to do what I am trying to do. Just a thought !!!

Well, not stopping you to use a MCU for this project. It's just that not every project that can be done with an MCU should be done with an MCU.

MY problem is my exposure to this particular issue is very limited AND over my head. Let me share with you my frustration.

I found a two relay module in my collection.Perfect I say, let me try to latch one according to your ...schematic.

Both relays have one 5 volt source and ground. Did that

The module has a "weared" jumper ( 3 pins labeled JD-VCC, VCC, and Ground) Have no clue for their purpose.
The default came with pin 1 and 2 has a jumper again no clue)

The triggers labelled IN2 and IN2.

Tried to apply 5 volt to IN1 or IN2 nothing.....Applied zero volt the Relay 1 and Relay 2 worked.

So, this module activate on LOW.

So far, so good.

According to your schematic you connected 5 V to the NO pin of Relay one. My understanding is you did it, to latch relay 2. I hope I am correct !!!

Then the frustration starts: I want to latch relay 2. so applied 0 volt to IN1 and IN2
My expectation is that both relays will activate AND RELAY 2 remain LATCHED after the trigger..... no way

I am definitely doing something wrong but WHAT ???.... I hope the "quality of my questions" matches your expectation.

Here is a picture of it


You do your homework, no complaints there.

That jumper is probably to completely separate the coil from the control signal through an optocoupler. It looks like there are two optocouplers.

My schematic is based on active high relays, some are indeed active low. So need some modifications. The trick for latching a relay is that the relay, by being activated, makes the circuit that activates the relay.
The blue dotted part is inside your relay module itself - schematic is not the same but same functionality: active low signal.

Key is to link connect the signal via the first relay to GND, so the relay keeps itself on. Then the same signal is used to switch on the second relay. The switch breaks the circuit in K1, and that switches off both relays.

You may need to add an extra transistor to invert the signal from your PIR (which is probably active high).

As a backup I ordered the AZ850 that you used in your schematic. In the meantime I will construct the circuit you kindly sent to me. I need to do the latching first before I incorporate the two relay to the PIR circuit that is working now on a timer.