Make your own, custom AUTONOMOUS SENTRY GUN!

A Microcontroller Contest Finalist and Runner Up Winner.

This instructable is out-of-date. For the most recent version of the tutorial, see the website . Cheers!

This sentry gun autonomously tracks, aims, and shoots at targets, using:
-An airsoft or paintball gun
-A webcam to find targets
-A computer to process the video feed and aim the gun
-Servo motors to physically aim the gun and squeeze the trigger
-A sturdy tripod base
-A microcontroller to interface between the computer and the servo motors
-Lots of camo paint
The end result is a paintball/airsoft spewing robot, that can turn the tides of any match.

Video if it in action here .

Want to build your own?
You're in luck! All the help and information you will need is here, and the software is free and open-source. Lots of people have already used this system in their own sentry guns, with successful results.
So go ahead, make your own, and soon your own backyard will be no-man's land, too!

P.S. take pictures during the project, and when you are finished, send me a picture, and a description of your project, and I will feature it on the Successful Projects page of my website. Thanks!

Step 1: Parts List

          Airsoft / paintball gun, or other 'projectile dispenser'
$30   Webcam
$25   Arduino (or equivalent)
$27   3 Servo motors (pan, tilt, trigger) $9 ea.
$10   Assorted screws, nuts and bolts
$9     Wood/metal for construction
$5     USB A to B cable
$2     Some 22 gauge solid wire
$2     Servo battery box
$110 TOTAL

Also, you will need a laptop or computer to run the sentry gun program. $250 for a cheap netbook, if you don't have anything else...

Step 2: Arduino

What's an Arduino? ...and how do you pronounce that, anyway?

An Arduino is a microcontroller. It plugs into a computer with a USB cord. On the Arduino board, shown below, there is an ATMega328 microchip, as well as 14 digital input/outputs, 6 analog inputs, and a USB port.
That's not all, but it's everything that you will need to know about, to build a sentry gun. For complete details on the Arduino board, go to http://arduino.cc/en/Main/ArduinoBoardUno.

You will use the Arduino board to take commands from the computer, and drive the servo motors as the commands dictate. Also, it will provide you with hookups for expanding your own ciruit easily afterwards.

Where to buy your own Arduino board


Step 3: Servo Motors

To control the movement of the gun, you will use 2 servo motors.

The "pan" servo controls the side-to-side movement of the gun.
The "tilt" servo controls the up-down movement of the gun.

Your remaining servo motor will be used to squeeze the trigger of your gun.

For heavy guns, such as the paintball gun shown below, you might want to get some stronger servo's.

Step 4: Constructing the Base

Now is time to put everything together. How you do this is largely up to you.

Some design considerations:

The gun should be balanced on the tilt servo. Find the gun's center of gravity, and mount it at that point. This is so the servo dosn't have to waste energy trying to keep the gun level. IMPORTANT: when the servo has no power supplied to it, the gun should tilt freely, and when you move it with your hand it should not return to level - rather it should stay right where you let go. Spend a lot of time getting this balance just right, it is a major factor in the effectiveness of your finished sentry.

Stability is key. You want a strong, solid sentry. Vibration will cause a lot of issues with the camera later, if you don't suppress it now. I'm not going to teach you how to build well, but keep in mind that screws and bolts are better than nails, nails are better than zip ties, zip ties are better than staples, and even staples are better than duct tape.

The webcam must be solidly mounted to the base. You want it close to the gun, but the gun barrel can't ever move into the camera's view - it will see that as a target. Also, the webcam should be as solid as possible - vibration from the servo's and gun firing will mess it up.

Depending on the expected use for the sentry (airsoft? paintball? water jet? laser?), you may need to add a durable compartment to hold the electronics.

Finally: tripods are cool, but they can be prone to a lot of shaking if built badly, or if you use a flimsy camera tripod. Surveyor's tripods are great for sentry guns - they are heavy, stable, and often adjustable too.

Step 5: Wiring

Arduino :
The Arduino connects to the computer through the USB A to B cord. This also powers the Arduino. Various other things wire to the Arduino:

Here's a wiring schematic , and you can get a sentry shield if you want an easy solution.

Servos :
You will need a seperate power source for your servo's. I reccomend 4 C-cell or D-cell batteries, wired in series to make 6V. Check the data sheet to find the power consumption for your servo's!
Servo motors have three wires: power, ground, and signal. Each servo's power wire is typically red, and should be connected to the (+) wire from your servo power source. The ground wire is typically black or brown and should be connected to a ground pin on the Arduino board, and to the (-) wire of your power source. The signal wire is typically yellow, orange or white and should be connected as follows: The x-axis servo's signal wire goes to the Arduino's digital I/O pin 3, the y-axis servo to pin 4, and the trigger servo to pin 6.

You can also add some optional switches and LED's , but they are not necessary:
-USB status indicator LED to digital I/O pin 5
-firing indicator LED to digital I/O pin 2
-a "power switch" between the (+) from the servo power source and the servo's, to turn off the servo's and save battery power
-a "safety switch" between the (+) from the servo power source, and the trigger servo, (but after the 'power switch'), to turn off the firing functionality easily and quickly (in an emergency)
-a 'reload' switch, wired between +5V from the Arduino and digital I/O pin 7, to set the gun in a programmable position convenient for reloading

Solder everything if you can. If you want, make your own arduino 'shield' with all the conections. This allows you to "plug in" the Arduino, and "unplug" it when it is needed elsewhere, without losing track of which wires go to which pins.

Step 6: Software

Now you have your sentry gun built, the wiring finished, and no doubt you want it to do something!

Before you plug your Arduino into your computer, go download the Arduino IDE .

If you have a Diecimila or older board, set the Power Jumber to USB.
Now, plug your Arduino in -  the green Power LED on the Arduino should light up, and the orange LED might flash a few times. Good job!

The Arduino IDE is what you will use to load the Arduino program onto your board. However, you will need the seperate, Processing IDE to run the computer side of the code. This does the camera tracking, and sends commands to the Arduino.
Go download the Processing IDE .

Open both Processing IDE and Arduino IDE. They should look very similar.

With a little bit of trial-and-error on the code, you should have a working sentry in no time! So get started typing!

Just kidding, of course.

The next thing you need to do is download the JMyron, blobDetection, and controlP5 libraries  from processing.org. Find the link to each library on that page. The instructions for downloading each one can be found by clicking on it's link.

You are ready to go get the code!  Download the latest version it from my website . After you have saved it to your computer, unzip the folder, and follow the directions in the strategically placed README.txt files.

Open the Arduino code in the Arduino IDE. Make sure you have selected the proper board and serial port from the Tools menu. Next, click 'Verify', wait for it to say "done compiling", then click 'Upload'. The RX and TX LED's on the Arduino board should pulse a little, then stop.

Congrats! You have finished programming the Arduino!

Plug in your webcam. Install the drivers, if you haven't already.

Now, open the Processing code in the Processing IDE. Click 'Run', and watch the magic happen!

Of course, for many of you this will not work on the first try. Don't get upset, it's normal. Pop me an email with any problems, or leave a comment below.

A tip for Windows 7 users, from ljfa321:

If you are getting this error when you try to run: processing.app.debug.RunnerException: UnsatisfiedLinkError: D:\Software\processing-1.2.1\libraries\JMyron\library\JMyron.dll: Can't find dependent libraries:

"Reason : Windows 7 doesn't come with MicroSoft C libraries, which were normally include in the other (older) Windoes system.
This means there are two files missing in the Windows 7 system: MSVCP71.DLL and Msvcr71.dll
Solution : Download these two files from here:
For Windows 7 32-bit OS: put both dll files inside Windows/System32 folder
For Windows 7 64-bit OS: put both dll files inside Windows/SysWOW64 folder."

Step 7: Using the Software

When you run the code, two windows should pop up. One is the Webcam View and the other is the Control Panel.

In Manual Mode, you can aim and fire by pointing and clicking on the webcam view. In Autonomous Mode, the software takes over, and your gun gains a life of it's own! Give it a little time to adjust to the background, and then it will shoot at anything that enters its view.

Play around with the other functions as well - Auto/Semi-auto, hide camera view, etc.

Calibrating - your servo and camera arrangement is probably unique, so you will need to calibrate it the first time.
You can do this by adjusting the values of xMin, xMax, yMin, and yMax, at the beginning of the code. Play around with them untill you have the gun aiming where you point the mouse on the webcam view. A useful tool to do this is to watch the numbers scrolling by at the bottom of the Processing IDE while you run the code.

You may also need to calibrate the servo positions set in the Arduino code:

To set the reloading position, change these lines:
    if(digitalRead(7) == HIGH) {
        xPosition = 110;
        yPosition = 135;
        fire = 0;

To set the home position (no USB communication), change these lines:
    if(idle) {
        if(idleCounter > 10000) {
            analogWrite(5, 50);
            digitalWrite(5, LOW);
            idleCounter = 0;
             digitalWrite(5, LOW);
        xPosition = 110;
        yPosition = 100;
        fire = 0;

To set the 'squeezed' and 'not squeezed' positions for the trigger servo, change these lines:
    void Fire(int selector) {
        if(selector == 1) {
            if(fireTimer >=0 && fireTimer <= triggerTravelMillis) {
                digitalWrite(2, HIGH);
            if(fireTimer > triggerTravelMillis && fireTimer < 1.5*triggerTravelMillis) {
                digitalWrite(2, LOW);
            if(fireTimer >= 1.5*triggerTravelMillis) {
                fireTimer = 0;
        if(selector == 3) {
            digitalWrite(2, HIGH);
    void ceaseFire(int selector) {
        if(selector == 1) {
            fireTimer = 0;
            digitalWrite(2, LOW);
        if(selector == 3) {
            digitalWrite(2, LOW);

unable to get the jmyron library please help<br>
<p>Hi there,</p><p>I'm currently working to build a manually controlled robot with a mounted autonomous NERF weapon system mounted on top. The aim is to have this system autonomously detect and fire at human figures, faces, and other robots. I understand your system is not designed to work while its moving however, do you have any advice or leads as to how I can implement this? </p><p>For additional info: my current parts include a NERF Rival Khaos, Intel Compute Stick (for processing), and an Arduino Uno (for servo control etc) to name a few. For detection I'm looking into various OpenCV algorithms.</p><p>Thank you for your time!</p>
<p>please send code my mail?</p>
The updated link at the top is no longer valid. It looks like it should be http://psg.rudolphlabs.com based on the GitHub code I found.
Sir I am facing an Error Message saying &quot;Error inside Serial.ports&quot; and the compiler shows <br> <br>java.lang.UnsatisfiedLinkError: D:\Workshops\processing-1.5.1\rxtxSerial.dll: Can't load AMD 64-bit .dll on a IA 32-bit platform thrown while loading gnu.io.RXTXCommDriver <br>java.lang.UnsatisfiedLinkError: D:\Workshops\processing-1.5.1\rxtxSerial.dll: Can't load AMD 64-bit .dll on a IA 32-bit platform <br> at java.lang.ClassLoader$NativeLibrary.load(Native Method) <br> at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1803) <br> at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1728) <br> at java.lang.Runtime.loadLibrary0(Runtime.java:823) <br> at java.lang.System.loadLibrary(System.java:1028) <br> at gnu.io.CommPortIdentifier.(CommPortIdentifier.java:123) <br> at processing.serial.Serial.list(Unknown Source) <br> at Turret_06_04.retryArduinoConnect(Turret_06_04.java:1917) <br> at Turret_06_04.setup(Turret_06_04.java:188) <br> at processing.core.PApplet.handleDraw(Unknown Source) <br> at processing.core.PApplet.run(Unknown Source) <br> at java.lang.Thread.run(Thread.java:662) <br>Exception in thread &quot;Animation Thread&quot; java.lang.RuntimeException: Error inside Serial.ports() <br> at processing.serial.Serial.errorMessage(Unknown Source) <br> at processing.serial.Serial.list(Unknown Source) <br> at Turret_06_04.retryArduinoConnect(Turret_06_04.java:1917) <br> at Turret_06_04.setup(Turret_06_04.java:188) <br> at processing.core.PApplet.handleDraw(Unknown Source) <br> at processing.core.PApplet.run(Unknown Source) <br> at java.lang.Thread.run(Thread.java:662) <br> <br>PLEASE HELP ME OUT
I think you need to reconfigure the port you are using. Look for port settings and make sure it is the one you are hooked up to.
I have not built it,but I am looking at building it,but I was wondering if you could wire it wirelessly some how,so you wouldn't have to have a laptop bolted to the platform
try bluetooth
<p>Replace with a real weapon and this would be one dangerous machine.</p>
<p>I believe this is the new link. They are VERY similar at the very least</p><p>http://projectsentrygun.rudolphlabs.com/home</p>
<p>link is broke. </p><p><strong>This instructable is out-of-date. For the most recent version of the tutorial, see the <a href="https://sites.google.com/site/projectsentrygun/" rel="nofollow">website</a> . Cheers!</strong></p><p><a href="https://sites.google.com/site/sites/system/errors" rel="nofollow">https://sites.google.com/site/sites/system/errors</a></p><p>Site not found</p> <br> <br><div><div>We're sorry, we were unable to locate the site /projectsentrygun/.<p>You can return to <a href="https://sites.google.com/site/sites/system/app/pages/meta/dashboard" rel="nofollow">your sites</a>.</p></div></div><p>/WebspaceNotFound?path=%2Fprojectsentrygun%2F </p><p></p>
<p>Could you post another link for the code?</p>
<p>Could you provide a new link for the code? The link provided is no longer available</p>
<p>Hi im using windows 7 and i just keep getting &quot; a library relies on a native code thats not available&quot; any help would be much appreciated!</p>
<p>Can't download the Code. Showing invalid link.</p><p>Please, provide the valid link to downlaod the code</p>
<p>my processing code keep saying &quot;A library used by sketch is not installed properly&quot; probably not going to get replay ?? </p>
<p>Здравствуйте,</p><p>мне очень нравится Arduino но я в нём абсолютно не понимаю.</p><p>Помогите пожалуйста, хочу самостоятельно изготовить дистанционно управляемую подставку для видеокамеры и закрепить на штатив. Стобы эта подставка поворачивалась горизонтально 120 градусв и вертикально 60 градусов. И чтобы данное устройство управлялось с андроида.</p><p>Подскажите мне новичку какие устройства мне понадобятся, желательно с ссылками где можно приобрести.</p>
<p>is it possible to use a raspberry pi as the processor and hard drive for the software? Without using a note book?</p>
does this work using a raspberry pi instead of an arduino?
Hi , <br>I'll build a turret using your tutorial, but from where i can buy the Controller and motors and all other stuff ?!
I'm a highschool student looking for a senior project and I think this is what I want to do! Our goal is to find something we are interested in but know little about and become experts. My questions are would someone with little experience with computer programming do alright pulling this off? It seems easy enough but then again what do I know? :P Also approximately how much time does this project take? If you could get back to me soon that would be great! Thanks!
How much does this project cost, total?
im geting an error running procesing <br>&quot;unsatisfiedlinkerror: C:\users\owener\documents\processing\libraries\jmyron\library\jmyron.dll: cant find dependent libraries &quot; <br>why is this? <br> <br>
how would I go about changing the servo pins? the code calls for pins 8,9,10 for pan tilt and trigger servos, but me servo shield only has pins 10,11,12,13.
Hello... i am a beginner too. Im having problems with the mechanical design of .the robotic sentry im making... any advice?? Please... thanks... how can i make it more stable?
when trying to run i keep getting this error,<br><br>processing.app.debug.RunnerException: UnsatisfiedLinkError: D:\Software\processing-1.2.1\libraries\JMyron\library\JMyron.dll: Can't find dependent libraries<br> at processing.app.Sketch.placeException(Sketch.java:1543)<br> at processing.app.debug.Runner.findException(Runner.java:582)<br> at processing.app.debug.Runner.reportException(Runner.java:558)<br> at processing.app.debug.Runner.exception(Runner.java:498)<br> at processing.app.debug.EventThread.exceptionEvent(EventThread.java:367)<br> at processing.app.debug.EventThread.handleEvent(EventThread.java:255)<br> at processing.app.debug.EventThread.run(EventThread.java:89)<br>Exception in thread &quot;Animation Thread&quot; java.lang.UnsatisfiedLinkError: D:\Software\processing-1.2.1\libraries\JMyron\library\JMyron.dll: Can't find dependent libraries<br> at java.lang.ClassLoader$NativeLibrary.load(Native Method)<br> at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1803)<br> at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1728)<br> at java.lang.Runtime.loadLibrary0(Runtime.java:823)<br> at java.lang.System.loadLibrary(System.java:1028)<br> at JMyron.JMyron.(JMyron.java:14)<br> at Turret_05_03.setup(Turret_05_03.java:142)<br> at processing.core.PApplet.handleDraw(Unknown Source)<br> at processing.core.PApplet.run(Unknown Source)<br> at java.lang.Thread.run(Thread.java:619)<br><br>I have installed jmyron as instructed in the file but i still get this error :(<br>Your help would be greatly appreciated :)
Hey there, The .txt file in theJMyron download has an inaccuracy:<br> <br> Copy the &quot;JMyron&quot; folder found in this directory into the Processing/libraries directory.<br> <em><strong>NO! Put that folder instead at C:\Users\</strong></em><strong>USERNAME</strong><em><strong>\Documents\processing-1.1\processing-1.1\libraries </strong></em><br> <br> Copy the &quot;JMyron Examples&quot; folder into your Processing Examples folder.<br> <br> IMPORTANT FOR WINDOWS USERS: there is a folder called &quot;Extra DLLs&quot; - those are DLLs that JMyron depends on so make sure they're copied into your system's DLL search path - such as the Processing root directory, or even C:\windows\System32.<br> <br> If Processing is already running, then restart it.
I just run into that problem yesterday, seems like they doulbe do the packaging work
Well, the library was originally made for older versions of Processing. Newer versions store the libraries in a different palce. The instructions and file packaging were not updated on the JMyron site, and many other libraries as well.
ok, that make sence XD<br><br>BTW, do you know what kind of souce I will be able to find a air solenoid?<br><br>I want to use it to control the release of compressed air.<br><br>Ask every shops I can think of in my town and still no luck.<br><br>
sorry for the late reply, but couldn't you just use a water solenoid(for irrigation) and put air through it instead?
sorry for the late reply, but couldn't you just use a water solenoid(for irrigation) and put air through it instead?
FESTO, AndyMark, SMC, Automation Direct
thx alot
Something funny happen, I have no problem with this setting in my Windows VISTA machine, but fail to work when I try to install it to my Windows 7 machine.<br><br>The error message is exactly the same<br>(processing.app.debug.RunnerException: UnsatisfiedLinkError: D:\Software\processing-1.2.1\libraries\JMyron\library\JMyron.dll: Can't find dependent libraries)<br><br>I event try to copy the entire working folder from VISTA to 7, but still no luck, and I do put the DLLs in to both process root and C:\Windows\System32.<br><br>Any idea how this happened?<br><br>BTW, my windows 7 machine is completly clean, I only install the new version of JAVA since the one come with the installing CD is old I think.
Also, my Windows7 machine don't have a webcam installed, perhaps thats the reason of the error message?
well, it could very posssibly be the lack of a webcam. Try running the code on the vista machine, without the webcam plugged in, and see if you get the same error.
I actually bought a webcam to connect to the windows7 machine this morning. The webcam work like a charm, but the process still show the same error massage.<br><br>Also, I try to put the two dll files all over the place, but still got no luck so far.<br><br>I will keep trying tonight~
OK, problem solved, you might want to add this to your tutorial, since all windows 7 machine should inconter this problem.<br><br><br>Reason: Windows 7 doesn't come with MicroSoft C libraries, which were normally include in the other (older) Windoes system.<br>This means there are two files missing in the Windows 7 system: MSVCP71.DLL and Msvcr71.dll<br><br>Solution: Download these two files from here:<br>http://www.addictivetips.com/?attachment_id=38105<br><br>AND!<br><br>For Windows 7 32-bit OS: put both dll files inside Windows/System32 folder<br><br>For Windows 7 64-bit OS: put both dll files inside Windows/SysWOW64 folder<br><br>Than the program should run with no problem.<br><br>==========================================<br><br>New problem occurs now, but not a very big deal to me though.<br><br>Still, in Windows 7 machine, the behavior of the portal sound track is weired.<br><br>When start the program, the first sound track (deploying~) plays no problem, but whatever the sound track comes after that, tent to lag forever at the end of it.<br><br>Example: press RUN &gt; First sound: &quot;Deplaying&quot; &gt; Second sound: &quot;Will you come over herererererererererererererererererererer................&quot;<br><br><br>The tracking still works fine and smooth while the sound is lagging, it just really creeped me out when I first heard this at 1:30am by myself XD&quot;
Ok, thanks for the tip, I'll put that on the software step. Nice work researching the problem!
Can't wait to do your build. <br>However, I did the same thing and no results<br>can you explain a little more on your instructions:<br><br>&quot;IMPORTANT FOR WINDOWS USERS: there is a folder called &quot;Extra DLLs&quot; - those are DLLs that JMyron depends on so make sure they're copied into your system's DLL search path - such as the Processing root directory, or even C:\windows\System32.&quot;<br><br>Place &quot;Extra DLLs&quot; to system's DLL search path&quot; I don't understand this.<br><br>a video tutorial would be great for your instrucable
Put them inside system32, that should be inside the search path. Actually, a lot of the time it will work fine without doing this anyways. <br>Cheers. Any more questions, let me know!
So i've done that and various ways to get this program off the ground. Unfortunately, no avail can you make a screen cast of exactly how to successfully acomplish this project. There nothing but endless failures on my attempts on this project. I'm really looking forward to this.
what would be really great is if you could make an app to wirelessly control the sentry and see the camera veiw from any mobile device that you sync with <br>
What would be cool is if you then got an IR camera, so it can track body heat, and then some sort of marker (UV led vest? Tracking dots?) you can use to distinguish between your own team and others.
I would think a special hat or helmet with an IR bulb on top would do just fine.
Hello, <br> I am a beginner in these things, I want to know that how u have managed to pull the trigger of the gun as soon as it sense something ? is it something to change with the mechanical design or what, How have u taken that as input to the board? I will be obliged to you for the answer <br> <br>Thanks
hello, <br> I am a beginner in making these things, one thing that i want to know is how u have manged to pull the trigger automatically as soon as the target is detected ? is there any mechanical alteration that is actually doing it or what ? i will obliged if you can answer to clear me <br> <br>Thanks

About This Instructable




More by sentryGun53:LED "Neon" Glow Lights for Automotives Autonomous Paintball Sentry Gun 
Add instructable to: