# How to switch PIR sensor on/off based on photocell result

I've built the following circuit based on an ATtiny85 for a night lamp. The circuit is powered by a 3.7v LiPo battery. I'd like to turn off the PIR sensors when the photocell detects light in order to save battery.

I tried putting a 2N2222 transistor between the power and the PIRs VCC pins but that makes the sensors unstable as not enough current was going through. I suspect some resistance by the 2N2222 but I might be wrong. I'm successfully using a 2N2222 to switch the photocell off when I don't need it. The sensors draw 100 uAh when standing by.

Can someone tell me what would be the best and most efficient way to switch (on/off) the current of the PIR sensors?

Thanks.

• One immediate thing sticks out as a bit funky in your diagram. There is a (+) sign on your power connector, pin 1? But.... that's connected to your MCU's GND pin, for example. Is that just a diagram mistake? Or? – jonk Nov 8 '18 at 3:07
• Besides that, are you using a photocell to tell you when to apply power to two other PIR devices? What is their part number, specifically? (And datasheet?) All these IR light sensors are confusing me about exactly what you are trying to achieve. Kind of like you want to turn off devices designed to detect light by using another sensor to detect light..... Not to mention a kind of weird way of arranging an LED and BJT, too... Maybe a better description of everything you are struggling to do would help. – jonk Nov 8 '18 at 3:09
• @jonk The plus sign is the object's "anchor" in Eagle used to select the object not a positive sign. As for my goal (not the present circuit), I'd like to wake up the Attiny every couple of seconds to check if photocell detects any lignt. If not, I will fire up the PIR sensors, wait for movement and light up the LED when motion is detected. It is used to light up the staircase during the night. Spec sheet of the PIR is here: abra-electronics.com/sensors/sensors-proximity-en/… – Pascal longpre Nov 8 '18 at 4:38
• So, put in my own words, you are willing to squander energy keeping the PIR sensors running, but only when it is dark. If it is light, there's no need to operate them. And you want to check the light/dark status once every few seconds. All this must be battery powered or else you wouldn't worry about all that. Are your lights mains-powered? Or running a 12 V multi-meter long LED strip? (I use LED strips on my stairs... works great... so I could just use the 12 V supply, which is built into a mains powered light-well in the ceiling I don't bother activating.) – jonk Nov 8 '18 at 4:43

There are multiple faults with your circuit:

1. There is insufficient voltage to drive your PIR motion detectors. Reference to the link you provided, the HC-SR505 require from 4.5-20V. You LiPo battery will only provide 4.2 when fully charged, and for most of the discharge cycle will be below 4V. No matter what you use to switch the sensors on/off with, they are unlikely to provide consistent results.
2. Using a BJT to try and cycle the VCC for the HC-SR505 will reduce the voltage to an even more critical level.
3. The HC-SR505 much like its bigger brother the HC-SR501 takes a long time to initialize and is VERY sensitive to VCC fluctuations. During initialization time you may see an active output. I've used both and typically wait at about 4-5 seconds after applying power to allow the output to settle. At the VCC voltage you are operating the sensor you are likely to see multiple resets.
4. The best advice seems to be that the HC-SR505 uses an EG4001 chip while the HC-SR501 uses a BIS0001, both chips used in the sensors draw low current. You have already found this to be around 100uA. You can switch the VCC to the sensor using any GPIO port on the ATTiny85 as they will handle many mA (though you need the VCC to be high enough for the regulator to work). I've used both the HC-SR505 and 501 on a 5V Arduino driven directly from the GPIO pins and they work fine. You just have to allow initialization before believing the output signal.

A good review of the HC-505 along with a schematic is here.

You will notice that the input power connection has a series diode, which accounts for the minimum supply voltage of 4.5V.

Suggestions:

1. You could short out the protection diode and the regulator (HT7133) would then work down to about 3.45V which would then work for your LiPo battery.
2. Connect your sensors directly to the ATTiny85 GPIO pin (both sensors could go to one pin), but ensure you give sufficient time for the sensors to settle when you power them up.
3. Connect your ambient light detector (CDS cell) to an analog input with the pullup resistor driven by another GPIO pin. Then when you put the ATTiny to sleep you reduce (switch off) that current.
4. Switch off your peripherals and put the MCU to sleep for the longest time you can count. When the MCU wakes up test the ambient light detector. If it's dark you can power up your PIR detectors.
5. When your PIR detectors are on, minimize the MCU current by turning off as much as you can (like the A/D and BOD) and use IDLE state with wake on PIR detection.
• Thanks so much for this detailed answer! I'm mad at myself for not seing the 4.5v minimum requirement but since I've been successfully running a similar circuit (with only one PIR) for many months now, I got to assume it was a 3v device. I'll do some more testing today based on your comments! – Pascal longpre Nov 8 '18 at 13:52
• As a follow-up, I built the project using @Jack 's recommendations and built an instructables ou of it! You an see the project and schema used here: instructables.com/id/… – Pascal longpre Nov 13 '18 at 19:36
• @Pascallongpre Well thought out implementation. A couple of comments though... 1) You appear to have no current limiting resistor for your LED, how do you set the max current for it. 2) You could add a potentiometer in series with the 1k to set the sensitivity of the LDR detection. – Jack Creasey Nov 13 '18 at 22:33
• 1) I want to have the maximum brightness and, from what I see, my LED has no issue dealing with what the processor outputs, event with the LiPo fully charged. Do I expose myself to problems if I don't put one? 2) Good idea for the pot on the LDR, maybe in the next release! :) – Pascal longpre Nov 14 '18 at 0:32
• @Pascallongpre Read the datasheet: ww1.microchip.com/downloads/en/DeviceDoc/… Look at Fig 22-21 and see the current source capability of a GPIO pin. You don't specify your LED so I don't know what the Vf is. Since in the picture it's white, I assume Vf is probably around 3.3-3.5V. Here's a 3mm T1: bivar.com/portals/0/products/3UWCX.XXXC-X.pdf ...Look at Fig 1 to see the Vf/Current graph. You can easily see that you will exceed the current rating of the GPIO pins on the ATTiny85 – Jack Creasey Nov 14 '18 at 17:39

The way you're connecting your NPN transistors is called "common collector" or "emitter follower", and it leaves you with one diode drop between the base voltage and the emitter voltage -- so your PIR was getting Vbatt - 0.65V, rather than Vbatt.

You want to use a P-channel MOSFET, connected as shown, to switch power to your PIR sensors. Note that when the gate is HIGH it will be OFF -- that's opposite of what you're getting with your NPN emitter follower.

Your photosensor should be drawing little enough current that you can just power it straight from your microprocessor pin. Check the data sheet, but with that 1k$$\\Omega\$$ resistor in there, it should only be pulling about 4mA, which your processor can almost certainly source. If it can't, use another P-channel FET.

The part number for the FET is almost certainly not correct -- you want to find a "logic-level" P-channel FET with an $$\RDS_{ON}\$$ small enough so that the PIR sensors won't notice the voltage drop.

simulate this circuit – Schematic created using CircuitLab

• Thanks a lot Tim! Do you think this would work? pdf.datasheetcatalog.com/datasheet/irf/irf9z24.pdf – Pascal longpre Nov 8 '18 at 2:04
• @Pascallongpre: probably not. The key phrase I think you missed is "logic level". MOSFETs want a LOT of gate drive; if you want one that'll reliably work off of a 3.7V supply, you need to get one that's made for it -- those ones are called "logic level". That part is designed for a 10V gate drive. – TimWescott Nov 8 '18 at 2:24
• Do you know how to calculate the voltage drop across a resistor at a given current? If you do, you want a FET that'll drop no more than about 50mV at the PIR's rated current draw (you could get away with 100mV -- 50 is better). That means looking at the rated $RDS_{ON}$ for the gate voltage you can supply and doing some math to choose the part. – TimWescott Nov 8 '18 at 2:26
• @TimWescott. This light sensor needs a comparator function so that light above a certain level cuts OFF the power to the PIR sensors. Maybe the TINY85 has a comparator function. Transistors alone are not good enough. – Sparky256 Nov 8 '18 at 2:50
• @Sparky256 I'm not sure what your point is. The subject here is how to power the sensor, not how to read it. Based on the signal names on the schematic, the OP is going into an ADC line on the processor, so if they know enough they'll be reading it with an ADC and doing the "comparator" function in software. – TimWescott Nov 8 '18 at 15:41

PIRs use so little current that you can power them directly from the AVR GPIO pins

the photoresistor can be powered this way too saving even more energy, you only need to check for light once every second or so...