help with programing PIR and LDR night time security light

So I am making a security light with the Arduino nano and a PIR motion sensor going in to pin D2 and a Photo sensor using voltage divider rule going in to pin A0. I then have A MOSFET connected to output pin D3 to control an LED light . What I need help with is making a program that will allow me to turn on the LED from pin D3 when the motion sensor is activated but only at night. During the day the LED needs to be off. I would appreciate any help.

It is easier to help if you post the code you have tried.

Have a look at Planning and Implementing a Program

It seems to me the general logic is something like this

if LDR says it is dark
   if PIR detects motion
      turn on the light

If you have not already done so start with simple one-purpose programs to learn how to get data from the LDR and from the PIR and how to make the Mosfet control the light.

When all the parts work separately start thinking about joining them together.


If you just want to get started with Arduino, this is a great little project, and I’d recommend you check out the examples that come with the Arduino IDE, take a look at Robin2’s posts, or take a look at my “Beginners guide to Arduino”, a very in-depth explanation of the basic concepts of electronics and programming needed to get started with Arduino.

If your goal is however, to just make a night light, an Arduino might be a bit of an overkill, as this can easily be achieved with some good ol’ analog circuitry. I drew a quick schematic that will do exactly what you are looking for: it consists of a comparator that compares the voltage of the potentiometer to the voltage of the LDR’s voltage divider, and outputs 5v if it is dark. Then it goes into an AND circuit: if both transistors conduct (when it is dark and the PIR is activated), the gate of the MOSFET is pulled high, so it will conduct, and turn on the lights.