Using Arduino > Project Guidance

Motion Detector activating motor: What's involved? (Beginner)

<< < (2/3) > >>

Here in the FRC (FIRST Robotics Competition), we use Talons to control our DC motors. The Talons take a joystick max, min and idle current to calibrate through a PWM (pulse width modulation, basically just a fake analog signal). They then map  the max and min to -12 and 12 and this ends up being the voltage sent through the talons, which comes from a PD board (power distribution board) in one side and out the other to the DC motor. I'm guessing you aren't going to be using a 12 volt motor, and if you really want to do an AC motor, (which I wouldn't recommend as a first project) I would find something like a Talon for them. If you don't have an AC motor, I would just go with DC.


--- Quote from: PeterH on Sep 14, 2013, 12:08 am ---
In that case I suggest you start by using an ordinary domestic PIR as used to control security lamps. You would power it from the mains and it would provide a switched mains voltage output.
--- End quote ---

I second that.  Performs the task perfectly.

--- Quote from: PeterH on Sep 14, 2013, 12:08 am ---
I don't know whether this output would be OK to drive an inductive load but if the motor is within the rated power for the lamp this device is designed to control then I think it's worth a try. These things already come with a potentiometer you can use to adjust the activation time.

--- End quote ---

Most have a relay as the control element; will manage a small motor (less than 300W) quite well.

Okay, I've got it almost there!

I ended up getting a Power Switch Tail II to control the AC motor, and a Parallax PIR sensor.
I found some code that was originally written for a PIR to switch an LED, and I substituted the Power Switch in its place (same pin).

All is well, except: it's backwards of what I want!  When the PIR is activated, the Switch shuts off.  I want the activated PIR to turn the switch ON.

I'm a bit confused as to whether I should do this with the wiring or the code.

The PST: +in: 5v,  -in: pin 8, Ground: ground on Arduino
The PIR: GND to Arduino GND, VCC to 5v, OUT to pin 7


Here's the code:

--- Code: ---/*
* //////////////////////////////////////////////////
* //making sense of the Parallax PIR sensor's output
* //////////////////////////////////////////////////
* Switches a LED according to the state of the sensors output pin.
* Determines the beginning and end of continuous motion sequences.
* @author: Kristian Gohlke / krigoo (_) gmail (_) com /
* @date:   3. September 2006
* kr1 (cleft) 2006
* released under a creative commons "Attribution-NonCommercial-ShareAlike 2.0" license
* The Parallax PIR Sensor is an easy to use digital infrared motion sensor module.
* (
* The sensor's output pin goes to HIGH if motion is present.
* However, even if motion is present it goes to LOW from time to time,
* which might give the impression no motion is present.
* This program deals with this issue by ignoring LOW-phases shorter than a given time,
* assuming continuous motion is present during these phases.


//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;       

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000; 

boolean lockLow = true;
boolean takeLowTime; 

int pirPin = 7;    //the digital pin connected to the PIR sensor's output
int ledPin = 8;

void setup(){
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(pirPin, LOW);

  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");

void loop(){

     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;           
         Serial.print("motion detected at ");
         Serial.println(" sec");
         takeLowTime = true;

     if(digitalRead(pirPin) == LOW){       
       digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state

        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
       //if the sensor is low for more than the given pause,
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){ 
           //makes sure this block of code is only executed again after
           //a new motion sequence has been detected
           lockLow = true;                       
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
--- End code ---

In my view it's easier to press a few keys to change the software than to change the hardware to invert the signal.


--- Code: ---     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state

--- End code ---

Change the first HIGH to LOW to invert the behavior of the PIR sensor.


[0] Message Index

[#] Next page

[*] Previous page

Go to full version