Instructables
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.
 
Remove these adsRemove these ads by Signing Up

Step 2: Wiring

Picture of Wiring
IMG_1751.JPG
IMG_1749.JPG
The wiring in this Instructable is very simple. The ground pin (GND) on the PIR sensor should be plugged into the Arduino's ground pin, the 5 volt pin (VCC) should be plugged into the Arduino's digital pin 13, and the data pin (OUT) should be plugged into the Arduino's digital pin 12.

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.

The wiring is complete! If you are confused, look at the pictures or ask in the comments.
Justith9912 days 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!

MatthewS41 month 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 month 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 month 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?
swordst seraine1 month ago
Seraine
After playing with your program I added the lights
Thank you for the reply tho
Joekevdv4 months ago

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

Oliverhall6 months ago

Nice tutorial!

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

Abhiman7 months ago

How to connect it with servo ?

seraine (author)  Abhiman7 months 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.
us2410981 year 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
EET19821 year ago
Pretty cool man. Thanks for the 'ible.
cridds1 year 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)  cridds1 year 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.
kambob131 year ago
Hi, how do I connect multiple servos, each triggered by individual sensors
seraine (author)  kambob131 year 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)  seraine1 year 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.
kambob131 year ago
thanks alot. I want to build an art installation that reacts when someone walks by, i'm thinking of using about 10 servos
danny591 year 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)  danny591 year ago
Thanks!
You are welcome.