loading
Picture of Motion Activated Servo
This is a basic motion activated servo that can be used in a wide variety of projects, and it is very simple and easy to make. If you use it creatively, you can do a lot of things with it. For example, I used mine to create a motion activated nerf gun.

Step 1: Materials

Picture of Materials
In order to make this, you will need:

An Arduino.
A PIR motion sensor.
A servo.
Wire.
 
Nguyen-erT made it!2 months ago
I did everything dicribed but it seems to be not working?
image.jpgimage.jpgimage.jpg
Please help
Justith9911 months ago

Hi!

What would you change in the code to make the servo only go 180 degrees and back once, when triggered by the sensor? I noticed it goes to 180 and back three times when triggered. Thanks!

Did you ever figure out why the servo loops three times? I'm having the same issue.

I'm experiencing this as well! If anyone could comment on this, it would be really helpful!

Found it. Edit delay in "//waits for the servo to reach the position" or use this simplified code :

#include <Servo.h>

Servo myservo; //creates a servo object

//a maximum of eight servo objects can be created

int pos = 0; //variable to store servo position

//amount of time we give the sensor to calibrate(10-60 secs according to the datasheet)

int calibrationTime = 30;

int pirPin = 12; //digital pin connected to the PIR's output

int pirPos = 13; //connects to the PIR's 5V pin

void setup(){

myservo.attach(4); //attaches servo to pin 4

Serial.begin(9600); //begins serial communication

pinMode(pirPin, INPUT);

pinMode(pirPos, OUTPUT);

digitalWrite(pirPos, HIGH);

//give the sensor time to calibrate

Serial.println("calibrating sensor ");

for(int i = 0; i < calibrationTime; i++){

Serial.print(calibrationTime - i);

Serial.print("-");

delay(1000);

}

Serial.println();

Serial.println("done");

while (digitalRead(pirPin) == HIGH) {

delay(500);

Serial.print(".");

}

Serial.print("SENSOR ACTIVE");

}

void loop(){

if(digitalRead(pirPin) == HIGH){ //if the PIR output is HIGH, turn servo

for(pos = 0; pos < 170; pos += 1) //goes from 0 to 180 degrees

{ //in steps of one degree

myservo.write(pos); //tells servo to go to position in variable "pos"

delay(50); //waits for the servo to reach the position

}

}

if(digitalRead(pirPin) == LOW){

}

}

nojo1272 months ago

Very helpful.

JulioM52 months ago

HI Seraine,

I've follow your instructions and seem to be getting close to my desired intent. The problem I am running into is like in your video, the servo motor is constantly rotating and does not come to a complete stop before being triggered again by the motion sensor. Perhaps you can help me with this?

I am attempting to implement a kinetic skin system to my final architecture thesis project and would appreciate any help. Thanks!

Brezorojo3 months ago

Thanks so much for this, I couldn't work out how to combine the Servo code with the PIR code. All is now clear! I have made a useless object which moves kind of flower things when someone approaches.

MatthewS411 months ago

Thank you for posting this! I am having one problem with it and I hope you can help me understand what I'm doing wrong. Everything starts fine, the sensor triggers the servo as it's supposed to, but the servo never stops. I have isolated the sensor so the motion of the servo isn't setting it off. Any ideas? Thanks again!

swordst1 year ago

Made this for my Halloween owl amazing, I am trying to add led blinking program to it but keep running into problems says voids are wrong and loop is wrong. You are an amazing programmer can not wait to see what the future holds for you

seraine (author)  swordst1 year ago
How do you want the led to blink? Do you want it to blink when the servo is moving, or something else? Could I see your code?
Seraine
After playing with your program I added the lights
Thank you for the reply tho
Joekevdv1 year ago

Really nice tutorial. I have a problem because the PIR is too sensitive, do you have the same problem?

Oliverhall1 year ago

Nice tutorial!

You can get the hc-sr501 PIR motion sensor for cheap here: http://voltatek.com/en/sensors/69-motion-sensor-m...

Abhiman1 year ago

How to connect it with servo ?

seraine (author)  Abhiman1 year ago
Copy and pasted from step two:
The servo's red wire should be plugged into the Arduino's 5 volt pin, the black wire should be plugged into the Arduino's Ground pin, and the signal wire (it was white on my servo) should be plugged into the Arduino's digital pin 4.
us2410982 years ago
cool bro !!! could you provide me a little i wanna light two bulbs on detecting motion could u plzz give me a code , i am a newbie
EET19822 years ago
Pretty cool man. Thanks for the 'ible.
cridds2 years ago
Looking for some help with a project I am thinking of undertaking. I want to attempt this with a couple of mods - http://www.instructables.com/id/Squirt/step2/The-circuit/ (small pic only) but this one uses a 12v supply for the servo. Most servos I have seen are 5v. Am I correct in thinking I could move the servo power wire from the 12v as shown to the 5v on the board to power it and still maintain the functionality?

Any help would be appreciated.
seraine (author)  cridds2 years ago
Most servos should be able to be powered by the 5v pin on the Arduino, so you should be fine. That's how mine was powered. However, if you connect multiple devices that draw as much current as a servo to your Arduino or if your servo uses an unusually high amount of current, you could potentially fry the Arduino, so beware of that. Also, you can't power a servo through a digital pin on the Arduino by turning the pin HIGH, as the pin can't give enough current. Other than that, I can't think of any problems.
kambob132 years ago
Hi, how do I connect multiple servos, each triggered by individual sensors
seraine (author)  kambob132 years ago
I am assuming you want to have multiple servos, and each one controlled by its own individual sensor. I will address the electrical changes you need first, and then move onto programming.

The positive pins of all of the PIR sensors will need to be connected together, and the negative pins of all of the PIR sensors will need to be connected together. Then connect the positive pins to the Arduino's 5V pin, and the negative pins to the Arduino's GND pin. Finally, connect the signal pin from each individual sensor to its own Arduino pin. For example, the pin from your first sensor will be connected to Arduino pin 11, the pin from the second to pin 12, etc.

You will need to do something similar with the servos. However, you cannot connect the positive and negative pins to the Arduino's 5V and GND pins, because you will probably draw too much current and potentially fry the Arduino. Instead, you will need to connect the positive and negative pins to the positive and negative wires of a 5 volt power source (three AA batteries connected in series would probably work). Then connect the negative wire of the power source to the Arduino's GND pin. This is necessary because the electrical signal from the Arduino has to be able to return to the Arduino, and if they weren't connected, the signal wouldn't do anything.

As far as programming goes, you will need to start by attaching more pins for the new sensors and servo. For example, this

int pirPin = 12
Servo myServo;

void setup() {

myServo.attach(4);
pinMode(pirPin, INPUT);

}


would be become this

int pir1 = 11;
int pir2 = 12;

Servo servo1;
Servo servo2;

void setup() {

servo1.attach(4);
servo2.attach(5);

pinMode(pir1, INPUT);
pinMode(pir2, INPUT);

}

You will also need to change the if loops. For example, this

if(digitalRead(pirPin) == HIGH){
for(pos = 0; pos < 180; pos += 1)
{
myservo.write(pos);
delay(5);
}
for(pos = 180; pos>=1; pos-=1)
{
myservo.write(pos);
delay(5);
}
if(lockLow){

lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}


Would have to become something like this

if(digitalRead(pir1) == HIGH){

boolean pirState1 = true;

if(lockLow){

lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime1 = true;
}

if(digitalRead(pir2) == HIGH){

boolean pirState2 = true;

if(lockLow){

lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime2 = true;
}

if (pirState1 == true) {
for(pos = 0; pos < 180; pos += 1) {
servo1.write(pos);
delay(5);
}
for(pos = 180; pos>=1; pos-=1) {
servo1.write(pos);
delay(5);
}
}
if (pirState2 == true) {
for(pos = 0; pos < 180; pos += 1)
{
servo2.write(pos);
delay(5);
}
for(pos = 180; pos>=1; pos-=1)
{
servo2.write(pos);
delay(5);
}
}

I hope this helps you with your design, but beware, you will probably need to modify the code to account for whatever I missed. If you need further help, feel free to ask.
seraine (author)  seraine2 years ago
I just realized that the loop code I wrote won't work very well. The problem with it is that it would sweep one servo, then sweep the next, etc. In order to have them all sweeping at the same time, it would have to look something like this.

boolean pirState1 = false;
boolean pirState2 = false;

if(digitalRead(pir1) == HIGH){

pirState1 = true;

if(lockLow){

lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime1 = true;
}

if(digitalRead(pir2) == HIGH){

pirState2 = true;

if(lockLow){

lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime2 = true;
}

if (pirState1 == true && pirState2 == true) {
for(pos = 0; pos < 180; pos += 1) {
servo1.write(pos);
servo2.write(pos);
delay(5);
}
for(pos = 180; pos>=1; pos-=1) {
servo1.write(pos);
servo2.write(pos);
delay(5);
} else if (pirState1 == true) {
for(pos = 0; pos < 180; pos += 1) {
servo1.write(pos);
delay(5);
}
for(pos = 180; pos>=1; pos-=1) {
servo1.write(pos);
delay(5);
} else if (pirState2 == true) {

for(pos = 0; pos < 180; pos += 1) {
servo2.write(pos);
delay(5);
}
for(pos = 180; pos>=1; pos-=1) {

servo2.write(pos);
delay(5);

} else {
}


This code is probably very inefficient, but it gives the gist of what I am thinking of.
kambob132 years ago
thanks alot. I want to build an art installation that reacts when someone walks by, i'm thinking of using about 10 servos
danny592 years ago
This is nice and simple, yet effective. Your a very smart young man, and I wish I would have started my electronics hobby when I was your age. I didn't start until I turned 50, (now 53) and it amazes me at how smart some young inventors can be. Great job and keep up the good work, or should I say play :-)
seraine (author)  danny592 years ago
Thanks!
danny59 seraine2 years ago
You are welcome.