Go Down

Topic: Motion Detector activating motor: What's involved? (Beginner) (Read 8986 times) previous topic - next topic

burntcloth

Hi,

Just started with the Arduino (Uno SMD edition), and decided to jump into a motion-detector project for an art installation. 
Basically, I am looking to have a sensor activate a motor only when a person is standing directly in front of the piece, run as long as it is activated, and stops when they walk away. 
The sensor will be about 8' away from the Arduino. I have AC power.

I'm assuming I'm getting a PIR sensor for this.  I don't know about the 8' wire extension, what kind of wire to use, etc. 

Any tips, leads, advice, encouragement is greatly appreciated. 
Thanks!

Marshall

aphelps231

Would you like guidance writing code for this project as well? Or just the hardware? I am willing to help. I'm assuming you already know a bit of C or C++.

For now, the first step (of course) is buying your sensors, assuming you already own and arduino. You're going to need a range finder, one such as the SHARP GP2Y0A21YK0F is perfect. I currently have two of them, one sitting next to my keyboard doing nothing and another hooked up to an arduino. Second, you will need your DC motors. If you have a battery pack full of 6 AA batteries, hook the positive lead up to your Vin (volts in) port and your ground (negative, black) up to your GND ports. You can easily solder a switch to break the connection between either the positive or ground leads, doesn't matter, or you can simply unplug and re-plug one of them. Next, rig your DC motor(s). Red to one of the analog ports (A0-A5) and black to GND. OR, if you can get your hands on an arduino motor shield, even better and easier. For your wires, I would use 18 or 20 gauge wire. Any color works, but having black, red and white will keep it neat and looking good. You will hook your white (signal) wire of your range finder to one of the analog ports, and the red and black to the 5v and the GND. Now, if you don't have any 5v or GND ports left, you can buy a breadboard and hook multiple wires to the same 5v, 3.5v, or GND outputs. Finally, hook your arduino up to your computer and load on a program that uses Serial.println(Serial.read(YOURANALOGPORT)); to test what kind of feedback your range finder is giving you. Just play around with this and if you need further help with coding, etc. just let me know.

PeterH

A typical PIR sensor will detect movement rather than presence, so detecting when somebody is stationary in the sensing area might be problematic. You could certainly detect them walking into or out of the area, although you wouldn't know which it was.

If your sensor is sensitive enough, you would be able to detect very small movements which might be sufficient. In that case you're probably looking for the sort of sensor used in a typical intruder alarm system. I don't know what sort of interface these typically provide, but I wouldn't be surprised if they were available with a simple output which you could use to trigger your motor directly i.e. without requiring a separate microcontroller. The cheap and cheerful PIR motion sensors used to control security lights are certainly available with a switched output which could be used to power your motor directly - although in my experience these are not very sensitive and may not be sensitive enough for your needs.

burntcloth

Thanks for the responses!
I'm thinking that I'll probably have an AC motor, since that's the power source and I need a good amount of torque.  Which brings up the question of how to control an AC motor with an arduino?  I'm assuming I need some kind of switch connected to the arduino that it operates.

The point about a PIR only detecting movement is a good one.  But I think that it will be okay, particularly if I can program the motor to run for a certain amount of time before it needs to be "restimulated" by the PIR to operate again. 


PeterH

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. 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.

aphelps231

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 http://arduino.cc/en/Reference/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. http://www.crosstheroadelectronics.com/Talon.html If you don't have an AC motor, I would just go with DC.

Paul__B


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.

I second that.  Performs the task perfectly.


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.

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

burntcloth

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.

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

Thanks!
Marshall


Here's the code:
Code: [Select]
/*
* //////////////////////////////////////////////////
* //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 / http://krx.at
* @date:   3. September 2006
*
* kr1 (cleft) 2006
* released under a creative commons "Attribution-NonCommercial-ShareAlike 2.0" license
* http://creativecommons.org/licenses/by-nc-sa/2.0/de/
*
*
* The Parallax PIR Sensor is an easy to use digital infrared motion sensor module.
* (http://www.parallax.com/detail.asp?product_id=555-28027)
*
* 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.

*/

/////////////////////////////
//VARS
//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;


/////////////////////////////
//SETUP
void setup(){
  Serial.begin(9600);
  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.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
  }

////////////////////////////
//LOOP
void loop(){

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

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

       if(takeLowTime){
        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");
           delay(50);
           }
       }
  }

PeterH

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

PaulS

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

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

burntcloth

Thanks!  That did the trick (I also had to invert the following command's LOW to HIGH).

All I need to do now is cut down the sensitivity on the PIR and get those red lights off.  I've read elsewhere I can do this with layers of poly sheeting and the red lights by cutting the only visible trace on the face of the PIR. 



Go Up