PIR sensor only to be powered and it turns on another power supply and load

How do I get a PIR sensor (preferably HC-SR505 rather than HC-SR501) to be the only thing powered while waiting for prey and then when activated it turns on another power supply to turn a servo and turn on some normal LEDs?

I don’t mind if the second power source turns on an Arduino or some other device like a 555 timer. Would I require transistors, relays? Obviously I’m after low overall power consumption. The servo could be a cheapo and the LEDs are white and normal 3-5mm ones.

I’d also like the PIR to be on batteries and the other devices on both batteries and wall power (separately). I already know how to do the 2 diode thing to make the option of using either batts or wall power.

Or is it totally impossible?

:o

The output of the HC-SR505 is just like a normal microcontroller output with a (probably) 1K~1.5K resistor in series. If motion is detected, it is high (3.3V), otherwise, it's low (0V).
So you can drive a transistor, a MOSFET, a relay ... Which one of those three you pick, depends on the current draw of the other components.
Read more on how to use these components here. Just remember that there's already a resistor in series with the output.
If you use a transistor or a logic level MOSFET, you have to connect the ground of the battery to the ground of the power supply. If you use a relay or a MOSFET with an optocoupler, you don't have to connect them.

If by "turning on another power supply" you mean a wall powered power supply, you'll need a relay (not recommended). If you want to switch the output of the power supply, you can use any of the three options mentioned above. Also, depending on the power supply you're using, you may want to look for an 'enable' pin inside the power supply.

As for power consumption: an HC-SR505 draws very little current, but if you want to optimize it even more, you could take out the onboard 3.3V regulator. If you then match the voltage of your batteries with the voltage range of the chip (I couldn't find which one it is, but my HC-SR501 has a BISS0001 with a voltage range from 3V up to >5V), it'll draw even less current.

First your PIR must signal presence or movement. This signal triggers some circuit that turns on whatsoever, for at least a minimum time. A 555 can implement such a retriggerable monoflop.

No need for an extra 555, you can set the duration of the signal on the PIR board itself, up to 2 minutes if I'm not mistaken. The HC-SR501 has a trimpot to set it, on a HC-SR505 it is probably set by fixed resistors (8s +/-30%).

PieterP, I have been through that beginner's guide a few times, as well as multiple other websites trying to understand stuff and formulate my next questions.

The total amperage is up to about 800 mA, due to 50 mA Arduino, 600 mA servo (at full current spike to get it moving I believe) and two 20 mA LEDs.

Since the servo and LEDs are required only occasionally, I believe I can get away with 2 battery packs (rather than using the wall plug option for the Arduino) if the Arduino is in sleep mode with various tweaks, so that it uses only a few microamps prior to wake-up. The site http://electronics.stackexchange.com/questions/49182/how-can-i-get-my-atmega328-to-run-for-a-year-on-batteries/49233#49233, says that the Arduino should be powered via the 5V pin to avoid the regulator.

The same site also says that the servo will still draw power when the Arduino is asleep and that a logic level FET connecting the servo to the Arduino is a good method. You said that using a MOSFET with an optocoupler avoids connecting the 2 battery pack grounds together which may be better for me because the servo current spikes affect the PIR sensor.

My understanding is that logic level MOSFETs are voltage activated at 5V which the Arduino IO pins output and they will do not amplify current but will transfer enough current for my application, with only uA of wasted current compared to a transistor anyway.

So my guess is that I connect the signal pin of the PIR to an IO pin on the Arduino in sleep mode. Then connect the MOSFET with optocoupler to an IO pin from the Arduino and connect this to the signal pin of the servo. Then I connect the two LEDs to two IO pins on the Arduino. Not sure how to connect up the MOSFET really, even after trying to study diagrams. I haven't found very similar wiring diagrams yet. Most PIR applications are for buzzers or lights.

For all I know it could be a case of having a MOSFET coming off the PIR sensor as well as one going to the servo. MORE HELP WANTED!

Thanks

The way I see it, there are two main possibilities:

  • The PIR sensor turns on the power to the Arduino and the servo.
  • The Arduino remains in deep sleep until it is triggered by the PIR sensor, then the Arduino turns on the power to the servo.

Take a look at the schematics I’ve attached.

PIR_1.png
pir2.png

If you use a HC-SR501, or you are willing to mod your HC-SR505, you don’t need Q3 and R3.
Their purpose is to keep Q1 on as long as the Arduino needs to be on:
When the PIR sensor is triggered, it turns on Q2, which turns on Q1, so the Arduino starts up. Then the Arduino turns on Q3, in order to keep Q1 on, even if the PIR sensor turns off Q2.
When the Arduino is finished doing its job, it can turn off Q2 as well, to turn Q1 off, so it turns itself off.

Am I right in saying options 1 and 2 match with diagrams 1 and 2 respectively? Seems that way.

The choices you kindly delivered are back to 1 battery pack and no opto-isolation it seems. Just wondering if you considered voltage/current spikes/dips from the servo? I can’t test it until I get some MOSFETs sent :frowning: The option of 1 battery pack is quite nice.

How about the servo draining current even when the Arduino is in sleep mode? I read that an optocoupler can stop this. Maybe I have also learned (could be bulldust) that it needs to be a solid state relay in order for the PWM to get through to it? $2.40 on ebay entitled “5V DC 1-Channel Low Level Trigger Solid State Relay SSR Module Board for Arduino”. What do you think about this?

Regarding diagram 2, I thought N-ch mosfets were activated at low voltage and hence in diagram 2 the Arduino would always be on, until the PIR goes high and turns in off?

Would you recommend the FQP30N06 N-ch mosfets and the NDP6020P P-ch logic level mosfets?

Which option would be more efficient? At first glance it seems option 1 would be more efficient because the Arduino is off, but at second glance I see that the circuit has at least one transistor. My servo would be active, say 2% of the time.

Sorry for all the tard questions. I’m just having a hard time figuring this out.

bodkin77:
Am I right in saying options 1 and 2 match with diagrams 1 and 2 respectively? Seems that way.

Yes.

bodkin77:
The choices you kindly delivered are back to 1 battery pack and no opto-isolation it seems. Just wondering if you considered voltage/current spikes/dips from the servo?

This depends on the battery and on the servo: If the battery and servo are > 7V, you don't really have to worry, since you'll be using a voltage regulator for the Arduino.
If the servo runs @5V as well, you need to make sure that it doesn't cause voltage dips on the 5V supply, since this can cause problems with the Arduino.
If this can't be solved with decent bypass capacitors, you might indeed need a second battery/power supply for the servos.

bodkin77:
How about the servo draining current even when the Arduino is in sleep mode? I read that an optocoupler can stop this. Maybe I have also learned (could be bulldust) that it needs to be a solid state relay in order for the PWM to get through to it? $2.40 on ebay entitled "5V DC 1-Channel Low Level Trigger Solid State Relay SSR Module Board for Arduino". What do you think about this?

It is unclear to me why you would use a SSR to drive a servo ...
The N-channel MOSFET is there to turn off the power to the servo, to prevent it draining power.

bodkin77:
Regarding diagram 2, I thought N-ch mosfets were activated at low voltage and hence in diagram 2 the Arduino would always be on, until the PIR goes high and turns in off?

No, an N-channel MOSFET turns on at a positive gate voltage, a P-channel needs a negative voltage on the gate (relative to the source) in order to turn on.
So in the first example, the Arduino and the servo are off, until the PIR's output goes high.

bodkin77:
Would you recommend the FQP30N06 N-ch mosfets and the NDP6020P P-ch logic level mosfets?

FQP30N06 is not a logic level MOSFET, so no, I wouldn't recommend that. The NDP6020P is a logic level one, but that's not really necessary, it depends on the battery voltage: if your battery voltage is high enough, you don't really need a logic level MOSFET, but it should work anyway.

bodkin77:
Which option would be more efficient? At first glance it seems option 1 would be more efficient because the Arduino is off, but at second glance I see that the circuit has at least one transistor. My servo would be active, say 2% of the time.

#1 is definitely more efficient: only the PIR sensor is on (and you maybe get some leakage current through your transistors, but that's negligible).
In #2, you also have the power from the Arduino, and you have to take into account the voltage regulators, and the USB-to-Serial chip, they draw a significant amount of current as well. The servo will be off, because of the N-ch FET.

Thanks for those answers. Might go with diagram 1.

By modding the HC-SR505, do you mean to remove the regulator? Would it then output 5V instead of 3.3V to the 2n2222, which in turn will drop the voltage to 4.3V to be received at the gate of the MOSFET?

The IRF9540N p-Ch MOSFET (IRF9540 Datasheet) has a gate threshold voltage of minimum -2V and maximum -4V. I think it must mean that -4V is the threshold at high temperatures or something like that, because the graphs show that at this voltage almost no current can pass from source to drain. My 4.3V will only pass about 500 mA, which is not quite enough (800 mA required). Is this correct?

Would you know if the MOSFET is in fact going to get 4.3V or not when the sensor goes high before I go hunting for more MOSFET specs please?

After a little more consideration, diagram 1 would be no good because the sensor needs 10-60s warm up time.

I have to wait for the FQP30N06L mosfets to arrive before I can try Diagram 2. Will this circuit minimise quiescent leakage current from/to the servo? I have tested my servo to have a leakage current of 6 mA when my nano is sleeping.

@PieterP Could you explain how to modify the HC-SR505 to make it work in the same circuits that the HC-SR501 works in. Voltage mod would be just to remove the regulator right, but is there something else you are referring to? As I said in the previous post yesterday I have ordered a FQP30N06L so that I can build the 2nd circuit you posted. I know that with the mod of the sensor you were referring to the 1st circuit. As far as I can remember I have never had consistent stability with the HC-SR505, although I haven't tried separate power supplies yet.

bodkin77:
By modding the HC-SR505, do you mean to remove the regulator? Would it then output 5V instead of 3.3V to the 2n2222, which in turn will drop the voltage to 4.3V to be received at the gate of the MOSFET?

No, 5V will kill it, my point here was that a linear voltage regulator is less efficient than a buck converter, so you could bypass the on-board linear regulator, and add a 3V3 buck converter instead. But if your battery is large enough, don’t worry too much about it, I don’t think it will be worth the effort.

bodkin77:
The IRF9540N p-Ch MOSFET (http://www.futurlec.com/Transistors/IRF9540.shtml) has a gate threshold voltage of minimum -2V and maximum -4V. I think it must mean that -4V is the threshold at high temperatures or something like that, because the graphs show that at this voltage almost no current can pass from source to drain. My 4.3V will only pass about 500 mA, which is not quite enough (800 mA required). Is this correct?

Would you know if the MOSFET is in fact going to get 4.3V or not when the sensor goes high before I go hunting for more MOSFET specs please?

-4V is the threshold voltage, if you apply only -4.3V to the gate, the FET will barely conduct, you need much more than that (take a look at “Fig 3. Typical Transfer Characteristics” in the datasheet)
When Q2 and/or Q3 are open, the gate voltage of Q1 will be close to -Vbat, that should be enough when using a 7.4V LiPo or even a 5V power bank, for example.

bodkin77:
After a little more consideration, diagram 1 would be no good because the sensor needs 10-60s warm up time.

This is not an issue, since the PIR sensor is always on.

bodkin77:
I have to wait for the FQP30N06L mosfets to arrive before I can try Diagram 2. Will this circuit minimise quiescent leakage current from/to the servo? I have tested my servo to have a leakage current of 6 mA when my nano is sleeping.

The servo is switched by the MOSFET, so its current draw should be close to 0.

bodkin77:
@PieterP Could you explain how to modify the HC-SR505 to make it work in the same circuits that the HC-SR501 works in. Voltage mod would be just to remove the regulator right, but is there something else you are referring to? As I said in the previous post yesterday I have ordered a FQP30N06L so that I can build the 2nd circuit you posted. I know that with the mod of the sensor you were referring to the 1st circuit. As far as I can remember I have never had consistent stability with the HC-SR505, although I haven’t tried separate power supplies yet.

I don’t think modifying the sensor will be necessary: if it keeps its output high long enough for the Arduino to start up, you can then open Q3 (by writing its pin high in your Arduino setup). When you’re finished, write it low again, and it will turn off Q3 and Q1.

Pieter

@PeiterP

Sorry about all the detail below but I see so many people post 1 liners and get blazed for not providing detail. Who knows what info might be useful to prospective helpers.

I successfully built that 1st circuit you posted using two 2N2222 transistors and an NDP6020P P-ch logic level mosfet. I did, however, put the drain to the load and the source to the positive (for the mosfet). I think your circuit shows an enhancement mode p-ch mosfet symbol, but does the circuit show how a depletion mode mosfet would be wired up? Since my mosfet was enhancement mode, swapping gate and drain seemed to be required. I looked into it but mosfets are still quite confusing for me so I could have my facts wrong (and probably do).

I also connected the servo positive to the Arduino 5V line because this reduced the total quiescent current when the Arduino is off from 5 mA to 0.06 mA.

I also added red LEDs without resistors to the emitters of the transistors for debugging. I also added 2 white LEDs in series after the mosfet drain. They stay on after the sensor stops outputting, by outputting a digitalWrite HIGH to pin 5 connected to Q3 and R3 in your circuit.

During the time the Arduino is on, I have the servo moving back and forth for about 15 seconds. Meanwhile, the white LEDs and the second transistor red LED stay on. Then it either stays on with a new detection or turns off until the next detection.

A couple of things that I have failed at so far…

1/ Getting the servo to move to position 100 before turning the Arduino off with the command “digitalWrite(keeponPin, LOW);”.

I don’t know why the servo won’t make this move. I just thought of getting rid of the “void loop” altogether and make it a “void runonce” or including the code in setup. I can’t try it right now.

2/ Getting the Arduino to check whether the sensor is high or low. Maybe (a big maybe) I have just figured out the code for that now and I will include this updated code below. I will try it tomorrow. For this, I have allocated the variable “sensordetect” as an input on pin 10 with another 2n2222 transistor coming from the collector of Q2 to send the signal to pin 10. When this goes LOW I want to stop the servo (not the LEDs). There is nothing else connected to the Arduino, other than the Q3 and R3 which will still be outputting at this time. This is required in order to keep the white LEDs on, unless I somehow kept the LEDs on by say a 555 timer or something. However, I have never used one of them. I had trouble getting the right current from Q2 to pin 10, which I believe should be around 20 mA. For some reason, when I was playing around with base resistor values, reducing the current to anything below about 45 mA made the whole circuit inoperable! Probably a voltage thing, but I’m not so good at testing that because its voltage differences that we measure and sometimes I’m not sure which difference to measure! What voltage is required at an input pin on the Arduino? 3.3V?

3/ Getting the HC-SR505 sensor to work. I might try a separate power source.

Anyway, I’m pretty chuffed that I’ve gotten this far. By the way, I’m currently using an Uno, but I will transfer it to a 5V Pro Mini or Nano later.

Code unmodified (as described above)

int sensordetect = 10;
int keeponPin = 5;

#include <Servo.h>
Servo myservo;
int pos = 0;

void setup() {
myservo.attach(9);
pinMode(keeponPin, OUTPUT);
pinMode(sensordetect, INPUT);
}

void loop() {
  digitalWrite(keeponPin, HIGH);
  
  if (digitalRead(sensordetect == HIGH)) {
    for (int i = 0; i <= 10; i++){
      for (pos = 30; pos <= 50; pos += 1) { // goes from 0 degrees to 180 degrees
        // in steps of 1 degree
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(60);
      }
      for (pos = 50; pos >= 30; pos -= 1) { // goes from 0 degrees to 180 degrees
      // in steps of -1 degree
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(60);
      }
    }
  }

  if (digitalRead(sensordetect == LOW)) {
    myservo.write(100); delay(1000);
  }
digitalWrite(keeponPin, LOW);
delay(200);
}

Code to try tomorrow which has a digitalRead of pin 10 (to check if the PIR went LOW) embedded in the for loop that does 10 cycles of the left and right motion of the servo.

#include <Servo.h>
Servo myservo;
int pos = 0;

int sensordetect = 10;
int val = 0;
int keeponPin = 5;
pinMode(sensordetect, INPUT);
pinMode(keeponPin, OUTPUT);

void setup() {
myservo.attach(9);
digitalWrite(keeponPin, HIGH);
}

void loop() {
  if (digitalRead(sensordetect == HIGH)) {
    for (int i = 0; i <= 10; i++){
      val = digitalRead(sensordetect);
      if (val == LOW) {
        break;
      }
      for (pos = 30; pos <= 50; pos += 1) {
        myservo.write(pos);
      delay(60);
      }
      for (pos = 50; pos >= 30; pos -= 1) {
      myservo.write(pos); 
      delay(60);
      }
    }
  }

myservo.write(100);
digitalWrite(keeponPin, LOW);
delay(200);
}

bodkin77:
I successfully built that 1st circuit you posted using two 2N2222 transistors and an NDP6020P P-ch logic level mosfet. I did, however, put the drain to the load and the source to the positive (for the mosfet). I think your circuit shows an enhancement mode p-ch mosfet symbol, but does the circuit show how a depletion mode mosfet would be wired up? Since my mosfet was enhancement mode, swapping gate and drain seemed to be required. I looked into it but mosfets are still quite confusing for me so I could have my facts wrong (and probably do).

The schematic was indeed wrong, my bad, I changed it :wink:

bodkin77:
I also connected the servo positive to the Arduino 5V line because this reduced the total quiescent current when the Arduino is off from 5 mA to 0.06 mA.

I have no idea why that is … Maybe leakage from the FET?

bodkin77:
1/ Getting the servo to move to position 100 before turning the Arduino off with the command “digitalWrite(keeponPin, LOW);”.

I don’t know why the servo won’t make this move. I just thought of getting rid of the “void loop” altogether and make it a “void runonce” or including the code in setup. I can’t try it right now.

I don’t know either … Also, I’ve never heard of void runonce, but you could define it yourself:

void setup() {
  // ...
  runOnce();
}

void loop() { } // nothing

void runOnce() {
  // your code to run only once
}

bodkin77:
2/ Getting the Arduino to check whether the sensor is high or low. Maybe (a big maybe) I have just figured out the code for that now and I will include this updated code below. I will try it tomorrow. For this, I have allocated the variable “sensordetect” as an input on pin 10 with another 2n2222 transistor coming from the collector of Q2 to send the signal to pin 10. When this goes LOW I want to stop the servo (not the LEDs).

You can just read the sensor’s output directly:
PIR_1-2.png

Also, could you include a timing diagram that shows the state of the servo, LEDs and PIR reading?

bodkin77:
This is required in order to keep the white LEDs on, unless I somehow kept the LEDs on by say a 555 timer or something.

It’s pretty useless (at least in this case) to add a 555 timer if you’ve got a microcontroller.

bodkin77:
I had trouble getting the right current from Q2 to pin 10, which I believe should be around 20 mA. For some reason, when I was playing around with base resistor values, reducing the current to anything below about 45 mA made the whole circuit inoperable! Probably a voltage thing, but I’m not so good at testing that because its voltage differences that we measure and sometimes I’m not sure which difference to measure! What voltage is required at an input pin on the Arduino? 3.3V?

No current flows through an input pin, so I’m not sure what you are talking about. The voltage should be higher than 3V to trigger the Arduino’s HIGH state. Due to the resistance (~1.5K) at the PIR’s output, the voltage may be around 2.96V (It acts like a voltage divider: (3.3V-0.7V)*10K/(10K + 1.5K) + 0.7V = 2.96V < 3V (0.7V is the voltage drop across the base and emitter of Q2)) The easiest way to fix this is to increase the value of R1 (to 22K or even higher).

bodkin77:
3/ Getting the HC-SR505 sensor to work. I might try a separate power source.

What exactly isn’t working? Are you sure that the sensor itself is indeed working? It should be pretty straightforward: Movement = 3.3V out, no movement = 0V out.

Pieter

Hi PeiterP

Thankyou. I had already learned most of those things. I should have waited haha. Nice to confirm though. I actually changed the sensor input to an input_pullup and left the resistor at 10k. Not sure why it worked but it seemed I had to change it. So I solved my 2nd problem. I still have the 1st problem though. When I remove the command to turn off the Arduino the servo does move but when I have the command there, it seems the Arduino wants to run this code before running the code to move the servo! Could you peruse my totally rewritten (again) code below please? I need to get to the bottom of this.

Regarding the other sensor, I’ll try the large sensor without its focussing lens first.

Thanks again

include <Servo.h>
Servo myservo;
int pos = 0;

int sensordetect = 10;

int keeponPin = 5;
int count = 0;

void setup() {
Serial.begin(9600);
myservo.attach(9);
digitalWrite(keeponPin, HIGH);
pinMode(sensordetect, INPUT_PULLUP);
pinMode(keeponPin, OUTPUT);
}

void loop() {
  for (pos = 30; pos <= 50; pos += 1) {
    Serial.println(count);
    myservo.write(pos);
    delay(30);
    
    if (digitalRead(sensordetect) == HIGH) {
    count = 0;
    myservo.attach(9);
    }
      
    if (digitalRead(sensordetect) == LOW) {
      myservo.detach();
      count++;
    }
    
    if (digitalRead(sensordetect) == LOW && count == 300) {
      count = 0;
      myservo.attach(9);
      myservo.write(100);
      delay(1000);
      myservo.detach();
      digitalWrite(keeponPin, LOW);
    }
  }
}

I'm not sure why that doesn't work ... I'm not a servo expert, but I think that the servo.write() function is non-blocking: this means that it doesn't wait for the servo to reach the position before executing the next piece of code. But this should not be too much of an issue, because you have a delay(1000).
Maybe try connecting an LED (+ resistor) to the servo and keepOn pins, to see what they do.
Also, that servo.detach() before turning the Arduino off is not really necessary.

Pieter

I rearranged the code and all sorted. I removed detach and it's all good.