Author Options:

PIR DC Motor? Answered

Hi all,

I am trying to set up a dc motor controlled by a PIR sensor using an arduino.  When the PIR sensor is triggered, I would like the motor to turn on for half a second and then off.  However, no matter what "delay" I write into the sketch, the motor always stays on for around four seconds.  Could someone please help me?

The sketch I have written is below and have provided a picture of my set-up:

const int pirPower = 12;
const int pirIn = 13;
int motorPin1 = 3;

void setup(){
pinMode(pirPower, OUTPUT);
pinMode(pirIn, INPUT);
pinMode(motorPin1, OUTPUT);
digitalWrite(motorPin1, LOW);
digitalWrite(pirPower, HIGH);

void loop(){
  int value= digitalRead(pirIn);
    if (value == HIGH){
      digitalWrite(motorPin1, HIGH);
      delay(2000); //will not start if = 0-20.  The signal is stronger the higher the delay goes.  Time active remains the same.
      digitalWrite(motorPin1, LOW);



Best Answer 3 years ago

Yes, during start up the motor draws ten times the normal current and with the brush noise it disrupts the Arduino so that you cannot expect the code to work properly.

I believe the Arduino restarts more then once. try using a separate battery for the motor and add an LED to light to go on when the motor should then compare the action WITH and WITHOUT the motor.

I'm new to circuits. I have a battery pack that I can connect the motor to but I do not know how to given the circuit. Could you describe how to do this?


3 years ago

Here is how you add a separate battery for your motor.

Use one of the LEDs already on the Arduino PCB.


someone help me to control servo motor using PIR MOTION AND RASPBERRY LI 3

Thank you iceng! I connected the battery and the system works. When the PIR detects motion the LED on the Arduino PCB lights up and the motor starts. After 4 seconds both go out. However, I would like the motor to turn off after 1 second. I still cannot get the motor to turn off before the PIR sensor returns to low which takes about four seconds. Any thoughts?

Thanks again for all your help!

When you detect a PRI High you set a bit variable // name it Flag

Then start your motor, LED and a delay after which stop motor and LED.

Then test PIR until it goes low when you clear the Flag bit.

Now resume the test PIR High wait Loop.


And please mark my answer as the [ Best Answer ]

I do not see how that solves the problem. The problem is that as long as the PIR is high I cannot turn off the motor. No matter the value I put in the parentheticals for the delay function, it always takes 4.3 seconds for the motor to cut off. It is as if the delay function is meaningless.

What you are saying is?:

if (value == HIGH){

bitSet (?, ?) // flag
digitalWrite(motorPin1, HIGH);

digitalWrite(motorPin1, LOW);

bitClear (?, ?) // Flag

That flag bit is a simple device to prevent restarting a motor on the same PRI triggered high by making sure the PRI returns to zero before allowing the code to respond again.

Does The LED Turn-off Before The Motor ??

IF it Does Not then your problem is only in your code.

IF it Does then your problem could be hardware related.

Two strategies to try ;

1] In Code, after starting the motor before the delay turn off the PRI power.

2] REPLACE the PRI with a push switch [ see pic ]


Thanks again for all your help! How would you write the flag bit into the program?

Most uPs allow you to set and clear an output line and read the state of that line. That line or pin works as a single bit flag.

Hi, nice project did you get it to function? Can you please post the end results plus the the codes. I Try to make something similar but I also have a difficult time to get it to work.

thanks in advance.

icing and robodude95 advice is good I would add a wait in the code so the PRI sensor is not retriggering the motor.



3 years ago

Don't power the PIR sensor through a digital pin...power it through the 5v pin on the Arduino.Here's an instructable on using the PIR sensor.

Also try using a separate supply for the motor...since more often than not the Arduino isn't able to source the required current for the motor.