PiTank - a Web Controlled Tank With Cannon and Live Video Stream

Introduction: PiTank - a Web Controlled Tank With Cannon and Live Video Stream

The PiTank is a web controller tank built for a competition at my school's robotics club. The project took about two weeks to complete with all 5 team members participating in various aspects. Its main projectile are ping pong balls (up to 3).The firing mechanism is based on a spud gun. The PiTank is controlled using a web browser and is capable of real-time video streaming. It is powered by a Raspberry Pi and an Arduino, along with a few other components. Here is a picture of the completed project and a demonstration video.

The instructable itself will touch on every aspect of the build but will not go into the small details. However, links to other useful tutorials and reference guides will be provided.

Step 1: Materials

The list of materials is mostly guideline. I won't go into every little piece that you'll need for the project. You can substitute a lot of the material with what's available in your area.

Raspberry Pi Components

Arduino Components

As for the cannon you'll need

  • Water bottle
  • ABS/PVC pipe (slighter bigger than your projectile's diameter)
  • PVC T adapter
  • BBQ Igniter
  • Some wooden sticks

You'll need a chassis to put everything in. I used this one . Make sure you get the 4WD as it has 2 more motors which gives it extra power. Unfortunately I got the 2WD version so my robot is a little slow.

You'll also need some basic tools such as

  • Screw drivers
  • Pliers
  • Hot Glue
  • Super Glue
  • Soldering Iron and acc.
  • Wires for soldering
  • Drill

Step 2: System Overview

There are 2 main parts to the robot :

Raspberry pi :

  • Main controller
  • Hosts the web page
  • Controls the camera
  • Controls the servos
  • Communicates with the Arduino via serial ports


  • Controls the motor
  • Controls the relay and BBQ sparker (used to fire the canon)

Step 3: Building - Part 1 (cannon)

The cannon is the coolest part of the project.It is made out of a hard transparent plastic project with some pvc pipes and attachments and some wooden sticks. It uses the same principle as a spud gun. The barrel is hot glued to the bottle. The wooden sticks serves as a mounting point for the fuel spray servo.

You'll need to make 2 holes to insert the spark wires (image 2) and a bigger hole to spray the the propellant ( image 4). You might need to experiment a bit to find the correct size of the hole,since the air/fuel ratio is important to achieve ignition. For the propellant , I used Bullet Axe mounted on a servo ( see image 4). The top of the can should point towards the right side of the image. When the servo is activated, it will press down on the Axe can and spray into the ignition chamber.

We used a T attachement to build a simple magazine chamber ( see image 5), It can hold up to 3 ping pong balls.

We also put a screw in the PVC pipe to prevent the ping-pong ball from rolling into the chamber. You can see it in image 2.

Make sure you wear appropriate protecting when testing/building the cannon.

Step 4: Building - Part 2 (Raspberry Pi Hardware)

As mentioned previously, The RPi communicates to the servo board and to the Arduino. It also capture live video using the RPi Camera

  • Servo Board
    • Follow these instructions for set-up
    • Make sure you have an external power supply for the servo
    • I2C should only take 4 wires total.
    • The I2C port is dependent on which version of the RPi you use, make sure you know which port you're using
  • Arduino
    • I used the TX/RX port on the RPi to connect to the TX/RX port on the arduino
    • You need a Voltage translator becasue the Pi is rated at 3.3V while the Arduino is rated at 5V
    • You can either buy one and use this guide to hook up , or build one yourself!
  • Camera

Step 5: Building - Part 2.5 (Raspberry Pi Software)

The software setup is a bit more complicated than than the hardware. But I'll go over it step by step.

  • First you should get my codes here. It's a GitHub repo.
  • Basics
    • Raspbian OS
      • Get it here
      • Flash to a SD/Micro SD card using WinDiskImager32. Or use other methods if running on Linux/OSX
    • SSH
      • Enabling SSH on the Pi is pretty much a necessity for file transfer and remote login
      • Tutorial
    • Putty for Windows PC
      • PuTTY is a great client for SSH on windows. Get it here
    • FileZilla Client
      • Very useful for file transfer between PC and Pi
      • Download
    • Static IP Address
      • You'll want a static IP for the Pi so you don't have to find it everytime you want to use SSH. By using SSH and Static IP, we won't been needing a dedicated monitor/keyboard to program the Pi.
      • Tutorial
    • Camera
      • Make sure you enable the camera in sudo raspi-config
    • RAM Split
      • The camera needs at least 128 MB to work, so dedicated at least 128 MB to video card memory in sudo raspi-config.
    • Overclocking
      • I recommend using at least the Medium overclocking if you don't have a Pi 2. The live stream will be much smoother.
    • Enable I2C
      • Enabled I2C in sudo raspi-config
  • Servo Board
    • Complete the tutorial in the previous page and make sure everything works.
    • Under the servo folder in the github repo, there are 2 files : piservo.py, pimotor.py
    • Piservo.py contains the code for the pan/tilt servo, while pimotor.py contains the code for the drivetrain motors and reload servo. You should take a look at these files and change some servo settings according to your need.
    • The folder also contains the necessary library for the servo board
    • Depending on I2C port, you'll need to make some adjustment in Adafruit_PWM_Servo_Driver.py
	self.i2c = Adafruit_I2C(address,0)  // if you use port 0 (256MB)
	<p>self.i2c = Adafruit_I2C(address,1) // port 1 (512MB, PI2)</p>
    • Copy the folder to your Pi home folder. You can move them to another location.
    • You can automate the execution of the scripts by adding them to /etc/rc.local using VI or Nano. They will be called every time the Pi boots.
	sudo python /your/file/location/piservo.py &
	sudo python /your/file/location/pimotor.py &

Don't execute the scripts yet. We have a few more things to setup .

  • Arduino
    • In pimotor.py, we open a serial port and talk to the Arduino by sending byte messages. Different messages tells the Arduino to do different things such as moving foward/backward or sparking the igniter.
  • Control and Live Stream
    • Probably the most complicated thing to set-up
    • We're basing our live-stream and control software on the excellent http://elinux.org/RPi-Cam-Web-Interface. Installing it is pretty straightforward.
    • This program allows you to stream the camera feed directly to a web page via a web server running on the Pi. It also allows the user to have full control of the camera directly from the webpage. We will be extending the software so it can provide a control interface to our robot directly from the webpage.
    • In order to do so we will use a simple html -> js -> php control scheme
    • We need to edit some files in /var/www. You should use FileZilla and SSH for this step
      • Copy jquery-2.1.1.min.js, pimotor.js and piservo.js to /var/www/js
      • Make a backup copy of index.php in /var/www and overwrite it with the one provided in my code
      • Copy pimotor.php and piservo.php to /var/www/
      • Copy layout.css to /var/ww/css
      • Note that you can't copy the files directly to /var/www using FileZilla you need to copy them to your home folder and manually copy them to /var/www using sudo and command line.

Step 6: Building - Part 3 (Arduino Hardware)

The Arduino controls the motors and the relay for the firing mechanism and communicates with the Pi via the voltage translator

  • Motors
    • Follow the tutorial on how to setup the motor shield with Arduino
    • Make sure you use a separate power supply for the motors
  • BBQ Igniter and Relay
    • Look at the diagram.
    • The BBQ igniter works by pushing on the tail cap. Inside the tail there are 2 AA batteries. When we push the cap, the batteries makes contact and ignites the sparker. In order to attach it to the relay. I soldered the battery contacts to 2 wires (orange and black). The orange wire represents the red wire on the diagram and is connected to the + contact on the external battery pack.

    • I de-soldered the connector on the board and soldered the pins directly on to the board.
    • The Sig wire is connected to pin 2 on the Arduino
  • RPi and Voltage Translator
    • The voltage translator needs to be powered by 5V on one side and 3.3V on the other side. I used the 5V and 3V output on the Arduino to do so.
    • Pin 0 and 1 are connected to the voltage translator like so
      • Arduino TX <---Voltage Translator----> RPi RX
      • Arduino RX <----Voltage Translator----> RPi TX

Step 7: Building - Part 3.5(Arduino Software)

Just upload the code called serialwithpi.ino to the Arduino

Step 8: Building - Part 4 (Putting It All Together)

This is a more of a trial and error step. Depending on what components you use, the procedure is different. I will provide some photos and general guideline

  • The cannon is mounted to the servo using a wooden stick and a pipe clamp
  • The Rpi is mounted on the cannon using a wooden stick glues to the cannon barrel
  • We've decide to not use the pan servo for the cannon because it was too unstable

Step 9: Testing

  • If everything is programmed and connected correctly. You should be able to type in your Pi's IP address into the browser and see the main interface. From there you can directly control the robot. Of course you need to run the 2 python scripts pimotor.py and piservo.py if they are not in rc.local
  • If you want you can make the robot accessible from anywhere in the world by using port fowarding on your router. I'm not going to go into details on how to do it, but just make sure you install appropriate software suchs as fail2ban to protect yourself on the Web. Also it's is a good idea to make the webpage password protected ( it is an option when you install RPI-Cam-Web-Interface)

Step 10: Improvements and Final Thoughts

I would like to improve the robot by reducing as much weight as possible. The Arduino is probably not necessary as motor shield for the Pi probably exists.

The canon has a rather long cool down period between shots. Adding a fan would probably help the airflow and shorten the cool down period.

The webpage needs some formatting fixes

All in all we had a lot of fun doing this project and certainly learned a ton!

Raspberry Pi Contest

First Prize in the
Raspberry Pi Contest

Be the First to Share


    • Woodworking Contest

      Woodworking Contest
    • Stick It Challenge

      Stick It Challenge
    • Trash to Treasure Contest

      Trash to Treasure Contest



    6 years ago

    could you potentially run all of this through a just the rpi with fans and overclocking

    The Screwed Scientists
    The Screwed Scientists

    Reply 6 years ago

    You will need some kind of motor driver board because the pi's gpio pins cant deliver enough voltage and current


    Reply 4 years ago

    Check out for H-bridges. Of course, I have separate power source for the RasPi (a powerbank that gives steady 2.1A) and a battery/pack (6V or 9V) for the motors (optionaly with a step-down regulator so as not to burn the motors). Eventually, since the powerbank has 2 outputs, it could feed the motors too.


    Reply 6 years ago

    if I had that would the pi alone have enough power to handle all the tasks?


    4 years ago

    Very cool!

    Why not add a laser diod / pointer for a better aim? These some under a dollar. ;o)


    6 years ago

    I'm a little confused on the cannon. can you upload a video on the cannon only?

    maybe a little more detail :D

    Thankss ;)


    6 years ago on Introduction


    I think that the file config.php is missing.

    really awesome guys, I really appreciate your effort and idea. Can you send me a detailed document on the project. I need the parts required in detailed and the concerned code for each component. Please guys , your idea rocks and is awesome. Please send the document to pkompally@gmail.com or post it on my timeline in Fb i.e Pranav Kompally. My profile pic is a Porsche cayman

    The Screwed Scientists
    The Screwed Scientists

    Reply 6 years ago

    Unfortunately there aren't any detailed documents since most of it was improvised. Th


    6 years ago

    Nice project.but instead of arduino a motor driver could be used like L298N to lower project cost. Please have a fire extinguisher available when building cannon part.

    The Screwed Scientists
    The Screwed Scientists

    Reply 6 years ago

    I had the Arduino and the motor shield at hand, so it was easier than making a board, but you're definitely right.


    6 years ago on Introduction

    In my opinion this motor driver has to low driving current for engines in Rover 5 chassis.


    6 years ago

    would you please upload a video content


    6 years ago on Introduction

    Very cool project and well documented, good job!

    On step 10 you say that you want to find a way to reduce the weight of the tank, please let me give you some suggestions. One thing you could do is to get rid of all those AA batteries. The capacity they give you, is very small compared to their weight, you should find an alternative. A small lead acid battery perhaps?

    With a lead acid battery you can not only replace the AA batteries, but the power bank too. That way the weight can be reduced a lot more. Of course you will need battery charging circuitry and a buck converter for the Pi, but a small PCB weighs much less than all those batteries.

    Furthermore, this way your whole tank will be rechargeable, now when the AA batteries are dead you will have to replace them.