Introduction: Johnny Five, an Automated Turret

I want to start by saying that this is not going to be an instructable that you can follow step by step, without thought, to create exactly what I am showing you. This is because the actual output was tuned to a specif set of sensors, servos, and field of vision.

The intent of this instructable is to set forth a clear step by step process that can be followed, including debugging, that will allow an experienced hobbyist to replicate, and hopefully improve upon, the final product that will be shown in this instructable.

The instructable will walk you through the process of using a teensy 3.0, servos, and an ultrasonic sensor to look at a field of vision and target a stationary target, as opposed to using camera and vision processing system such as OCV. The advantage to this is that it runs quicker, costs less, and is portable without having to have a laptop.

All text in italics will be used to denote cautions or suggestions. Pay close attention to them as they are things that tripped me up going through the build the first time around.

Difficulty: Hard

It is assumed that the reader has basic soldering skills, debugging skills, knows how to read a sketch, and knows how to read a sketch/schematic and assemble a circuit.

Time: 12 - 24 hours of work

Step 1: Concept
Step 2: Bill of Materials
Step 3: Physical Construction
Step 4: Analysis: Physical Construction
Step 5: The Teensy
Step 6: Initial Program
Step 7: A Note on Servos
Step 8: Expanding the program
Step 9: Targeting System Adjustment
Step 10: Testing, Testing, and more Testing
Step 11: A Working Product
Step 12: Improvements
Step 13: Additional Resources

Step 1: Concept

The picture for this step is of the playing field that the turret, nicknamed Johnny Five, functions on. As such, there were these limitations and bounds on my design. These designs were chosen as such to attempt to maximize our chances with the attached rules for a competition.

1. The turret will sit in the middle of the south side of the cube. It will need to filter out things that are not inside the box. You will need to set a minimum distance that is greater than 3 cm.

2. There were, as shown, two targets on the right and left for calibration. These need to not be targeted and shot at, however they can be used to calibrate targets.

3. (Not shown) The target in the field can be 1.5 feet above or below the table top. The turret needs to be able to target in this range.

4. I could either go fast, or I could go slow and get lots of good data. I choose to go slow and target more reliably as opposed to fast.

The first step in your design needs to be to produce such a list. Consider the following:

1.What shape do you want to target? For your design, you could designate a square that is smaller, wider, or a semi circle. You can leave patches out of the scan or set a minimum bound. (see image)

2. How far up do you want to aim? Do you want to shoot to the ceiling?

4. How far away do you want to target?

5. What radial degree do you want to target. 0 to 180? 45 to 110? You can decide.

6. What kind of speed do you want to traverse at? What resolution do you want the scans to have?

Step 2: Bill of Materials


1. Firing mechanism $3.50

2. Ultrasonic Sensor $2.00 (on ebay)

The link links to an amazon product, because the links on ebay are so transitory.

3. Pan-Tilt servos and mounts (also here) $14.98 (on ebay)

4. Teensy 3.0 $19.00

5. Header pins $1.72 (there will be extras!)

6. A protoboard or breadboard $0.80 to $4.00 (depending on your choice)

7. Solid core wire

8. Tape

9. Solder

10. Hot glue

11. (suggested, but not implemented) Shrink tubing

12. (used in testing but ultimately removed) Laser diode $1.00

13. SPST Momentary button

TOTAL: $43.00


1. Wire strippers

2. Soldering iron

4. Glue gun

6. Hot air gun (or blow dryer)

7. Drill, and bits

8. Hobby knife (like this)

9. Screw driver and bits

Of course you will need a computer, USB cable, and paper and pencil to take notes with and sketch ideas.

*Many of these items you may have lying around. If not, then I have found that ebay is a cheap source for most of these parts.

Step 3: Physical Construction

The physical construction of the turret remained fairly constant throughout the project. I used a plastic sheet to mount the ultrasonic sensor and firing mechanism to the pan-tilt. If I had access to a 3D printer, I would have printed a better mount. I would suggest that if at all possible a part be printed. It will prevent several other errors I had to adjust for. That being said, doing as I did will allow for acceptable operation.

The first thing that I noticed was that the base of the pan-tilt was so small and so light that I highly doubted that they could stand on their own without some sort of base. Because I don't like exposed wires, I decided to put the entire thing into a box. At first it was a cardboard box, but later I found a plastic case (as pictured).

In your design you could attach the turret to a breadboard, a protoboard, a piece of wood or plate, or even put it in an enclosure. What ever you do, make sure that the reset button can be easily reached, as it is used often when programming.

With the pan-tilt unit used, it is very important that if you screw it down (as opposed to glue or adhesive) GENTLY. Do not over tighten as I did. I broke the plastic connector around one screw and therefore lost some stability in the mount.

Because servos are not perfect, no matter what was done, the pan-tilt never reached exactly 0 to 180. It was more like 5 to 185. The black guideline across the case was the reference point for where baseline is.

To create such a reference, see Step 7.

Step 4: Analysis: Physical Construction


  • The design was small and light weight.
  • The self contained system made it hard to pull a wire or disconnect things by accident.
  • The plastic mount for the ultrasonic and firing mechanism was easy to modify, great for prototyping the system


  • The plastic mount limited our range of view, and because of its high profile, wiggled quite a bit. Also it didn't fit the firing mechanism perfectly, as as a result was not level.
  • Lightweight means I had to tape the turret down when in use.
  • The plastic was brittle because it was thin and not built for the purpose it was used for. (That said, it worked okay)
  • The quick release for the firing turned out to be more a hassle than help, as it came unplugged often. I ended up hot gluing it together. In the future I would drop it altogether.


  • A different base, such as a laser cut wood base with a heavier and larger footprint would improve the function of the system.
  • A custom made, 3D printed mount to hold the firing and ultrasonic (and later the laser) would allow for finer adjustments and better range.
  • A thicker base would have allowed for a more secure mounting of the protoboard, which would be fixed by the first bullet point.

Step 5: Using the Teensy

The Brains

The brains of the operation was the Teensy 3.0. It was chosen because of its realatvily big memory, many I/O ports, and its ability to do real-time calculations. It is important to note at this point that an Arduino would also function in place of the Teensy. However, doing so would limit future expansion of the project.

Preparing the Teensy

The Teensy can be purchased with or without headers. Mine did not have headers, so I took the time to solder them on at this point. It should be noted that when we do this, and plug it into a breadboard, we loose access to the underside of the board which does have some I/O pins. In my case, they were unneeded and so this was not a problem.

Operation of the Teensy

The beauty of the Teensy is that it can run using the Arduino's IDE without major modification of the program. Teensy has this tutorial to illustrate how to load Arduino programs onto the Teensy brand micro controllers. Note that it was necessary for me to use the beta of their loader program at the time of publication of this instructable because the official version had not been released for the Teensy 3.0.

After loading their program onto my computer, I was able to load the basic blinking light example and communicate with the board via the serial monitor to check that the operation was successful.

Step 6: Initial Program


The first step in our programming journey is to load a servo sketch onto our Teensy, wire it up, and watch the servos run through loops to make sure they work. A great place to start is the default library with Arduino. It can be downloaded here, but it should be installed by default with the Arduino software. Dual_Sweep.ino (at the end of this step) should get you started in that department.

Once your servos are working, I would comment out the up-down servo and play with the right to left. See what kinds of delays you like, what increments of degrees you want to move at. Make yourself familiar with the program. This will be the bread and butter of the system, so if it doesn't make sense, it will only get worse from here out. Once you are comfortable, move on.

Ultrasonic Sensor

The next step is to play with the ultrasonic sensor. I loaded a sketch I found online here. There are various other places to get sketches, and multiple other instructables on the matter.

I decided on doing all calculations from this point on in inches, and trimmed down the program to only calculate inches.

At this point, you should start documenting the actual operation of the sensor. Measure distances and see how accurate your sensor is. In my case I had two sensors that I interchanged. I noticed that on one sensor the default value when an object was out of range ( <12 feet) was 67 inches while the other would report numbers in the 1200 - 2000 range. I decided to use the latter. What ever you choose make sure that you document the accuracy of the sensor. If like mine you get a range that is similar to the first sensor, make a note, so that in the future you can account for it.

It took a while before I realized that sensor read out in a cone shape. Now would be a good time to see what kind of angles to the right and left the sensor reads. The datasheet has some data on it, however it is an approximation. Once again, this information will make life easier in the future.

Do not move on until you understand how your sensor reacts.

Step 7: A Note on Servos

There are several ways that the servos can be controlled. A couple are evaluated here.


Pulse With Modulation is a common way to control servos. A detailed explanation on operation can be found here. Due to time constraints I was unable to attempt to use this. However, it the future it is a real possibility as it allows for further customization of the servos.

Arduino Library

Ultimately what I used, this library does everything for you. Like the example in the previous step, it was as simple as making a deceleration. The limits are that you move only in one degree increments and that you can only load 8 servos. Once again, because of time constraints, I never moved far enough past this to implement the next step.

Writing Your Own Library

This might sound scary, but it thus far hasn't been bad. A great link can be accessed here. The great part of this was that I could control to a much finer degree where the servo was. This turned out to be necessary because the project was limited more by the sensor than the servos.

A note on servo operation

When the servos run, if they run off the same supply as the Teensy, may run rough. This is something that can be seen on the videos at the end of this instructable. To fix that, two power supplies can be used or a decoupling cap can be used on both servos. I have not yet gotten that working on this project, but have on previous projects, so it is possible.

I found it handy to have several spare SG90 servos on hand if a problem arises and I burn out a servo.

Step 8: Expanding the Program

By now you have two sketches. One that controls the servos and the other that utilizes the ultrasonic sensor. Here there are several ways you can proceed:

Path One

  1. Take the servo sketch and make it control two servos.
  2. Merge the ultrasonic sensor sketch with the two servo sketch.
    1. Have the new sketch move the servos by a step, read the ultrasonic sensor. Print the values to the serial monitor.
  3. Instead of printing to the screen, put the values in an array.
  4. Adjust the sensor to read twice before moving, and insert the average into the array.

Path Two

  1. Take the ultrasonic sketch and have it read two values.
  2. Average the two values, and print the result to the screen.
  3. Create an array and add the average value into the array.
  4. Merge the servo sketch with the new ultrasonic sketch.
    1. Adjust the sketch to read, move, read, until it reaches the end of the array.

Path Three (suggested for quick implementation)

  1. Take the code at the end of this instructable, and implement it.
  2. Adjust the parameters to fit your physical requirements and limitations.

This step is really the guts of the project. I spent hours on following these few steps. Therefore, I give you my working code.

Step 9: Targeting System Adjustment

Now that the sensor is reading the inputs and averaging them, you should be able to print the array and see the values read. If you put a target in the range, you will see it on your readings.

Now that you can see array, you can set the limits of the software to filter out things that are too far, or too close. You can also see that when the sensor is not pointing directly at the target, that for some time it still shows up. This is because the sensor's readings radiate in a cone shape (like shown in the figure). The black shows where is read, the red is where we would like to read, and the two green circles in a box represent the sensor.

I never fully figured out out to make the sensor read only in the red square. Instead, I adjusted my targeting algorithm to take account for this. This caused some error because of the targeting adjustment targets at 0 degrees and 180 degrees. The software compensates for them based on the angle it knows it is looking at.

Step 10: Testing, Testing, and More Testing

Now, that is the basic setup. What took me so many hours was making adjustments to the code. If the first time doesn't work, don't worry. Take things a step at a time, look at the output, and make decisions on adjustments based on that.

Keep a backup copy of your code each time you make a major change. This way, if there is a problem or bug that you can't seem to get rid of, you can always ditch the changes and move on to an old, but semi-functional build. Doing this would have saved me hours of time in recreating what I had messed up by accident.

Step 11: A Working Product

Now that you have read through the entire instructable, we can see that a copy paste method will not work for having the turret work. That said, instead of having to re-invent the wheel, I've attached a complete copy of the code to run the turret. It will give you a starting point from which you will be easily able to make adjustments based on your own hardware.

I've also included a link to a video that shows one of the rounds that the turret ran. It will show you what you can expect as normal operational parameters.

Step 12: Improvements

The list of possible improvement is quite long, and I daresay that as I do other projects and surf the web, more and more ideas come to mind. Here is a sampling of the possible improvements to this project.

  • 3D print a mount. This would make the system more stable and more accurate,and allow it to target down further.
  • Laser cut a case for the electronics. This would allow for easy access to the electronics without compromising the turret hardware.
  • Replace the springs in the firing mechanism with stronger ones. Instead of lobbing the darts a direct shot could be taken. It will also increase range.
  • Implement the feedback wires on the firing mechanism. Right now, it is time based. It fires only one dart about 95% of the time. With feedback, it would work 100%.
  • Rebuild the ultrasonic sensor or get a better one. This could fix the problem of range and the cone shaped readings.
  • Led indicators. As this is an embedded system, it would tell where in the program I am, and what is going right/wrong for easy diagnosis.
  • Permanent laser sights. Knowing where the system targets before it shoots would save time in collecting darts.
  • Wii camera addition for added functionality. Two different sensors would allow for more precision.
  • Battery as opposed to outlet power. This would add portability.
  • Reverse the direction of the turret, allowing for better up down targeting. This turret could not point down far. A 3D printed mount also fixes this problem.
  • Print a custom circuit board. Protoboard looks bad. A custom board would look nice and professional.
  • Have the sensor trigger on edges of objects so it can follow moving targets. If it knows where the edge is, as the target is moved, it could follow it.
  • Once battery powered, this would be fun to put on a car. Have it target cats who jump on, or upgrade the firing mechanism to shoot paintballs and scare away vandals.

If I had better tools, and resources I would be able to use them to improve this design and do some of the other projects that I have in mind. If you liked this instructable, comment. I have a couple other instrutables and other projects that you can follow. Check them out at my site:

If you liked this, please vote for it!

Step 13: Additional Resources

I know that I am not perfect, and that this instructable is not without omissions and errors. If you need help or have questions, please leave a comment.

For more videos, and a for commentary on the actual competition feel free to visit which has not only the success but also the failure point of the turret. As I said, it is not without it's errors.

Lastly, during the time constructing this turret, the following links and resources were helpful and insightful.

Tech Contest

Participated in the
Tech Contest

Microcontroller Contest

Participated in the
Microcontroller Contest