Ball Balancing PID System




About: Hi, I'm Johan Link, a 18 years old student living in Switzerland. I love robotics, computers, 3D printing, photography and skate.

This system holds a ball in balance on a plate. A webcam films the system and a python program analyzes the images to find the position of the ball. The python program calculates the tilting of the tray to prevent the ball from falling.

A proportional-integral-derivative (PID) regulator is used to compensate the movements of the ball. The position and speed of the ball are measured by the camera and these measurements are used by the PID regulator in the python program.

Sorry about the English faults, it’s not my mother tongue.

Step 1: PCB

PCB manufacturing

I drew the pcb with eagle then manufactured it.

PCB assembly :

I soldered all smd components without paste flux. I don't recommend you to do like me. Instead, follow this tutorial to solder smd components:

The PCB is powered with a 5-6v 2A power supply.
PCB components :

Role of the PCB

The PCB communicates with the computer continuously. This PCB serves only to control the servomotors.

Step 2: 3D Printing

Warning : inferieur.stl, superieur.stl and plateau.stl files are not 3D printable. inferieur.stl and superieur.stl are made of acrylic. You can use a CNC to make these parts, but you can also cut them yourself in an acrylic sheet because these parts don't have a very complicated design. plateau.stl can be cut in cardboard.

Step 3: Parts Assembly

Step 4: PCB Programming

The PCB contains the same microcontroller as the arduino leonardo. Therefore it can be programmed with Arduino software. The code can be downloaded here.

Before programming the PCB, you have to burn the bootloader: Arduino as ISP and Arduino Bootloaders

Step 5: Python Program

The most important code is in the computer that controls the system. You can find the code here.

How does it work. The first step is to find the position of the ball. The camera sends the computer live video. The python program receives the video and has to process it. I use the OpenCV library to do the image processing. The program detects the ball thanks to its color. Here the ball is orange, the program will then average the position of all the orange pixels of the image to find the position of the ball. With OpenCV all this is quite simple. I recommend this tutorial for color recognition with OpenCV:

Now that we have the ball position we can calculate the inclination of the board with PID (proportional, integral, and derivative) control. The regulation is done in three stages. The first step is the simplest. Imagine that we want to stabilize the ball in the center of the board. The further away the ball is from the center, the more it will be necessary to tilt the board. Then you have to measure the speed of the ball: the faster the ball moves away from the center, the more you have to tilt the board.

Step 6: Conclusion

Thanks for reading! If you have any questions, do not hesitate to write a comment.

You can also vote for me in the PCB contest.

PCB Contest

First Prize in the
PCB Contest



    • Organization Contest

      Organization Contest
    • Pocket Sized Contest

      Pocket Sized Contest
    • Weaving Challenge

      Weaving Challenge

    133 Discussions


    Question 1 day ago

    Why don't the arms collapse? It seems as if there's only one servo per arm. But the arm has multiple joints.

    I feel that the whole thing would collapse.


    Tip 3 days ago on Introduction

    Congratulations. I love this project. Thank you very much.


    8 days ago

    Hi Johan,
    Thanks for taking the time to share you project with everyone. I notice your plans contain a joystick connection. Is this to manually to control the balance table? Did you manage to complete the design of the Joystick connection?


    10 days ago

    Un énorme bravo !
    Your project is simply great.
    JP (France)


    19 days ago

    Hi Johan, what is the "layer" or "Calque" function used for? I assume it is to line up the servo arms with the camera. Is that correct?

    1 reply

    Question 21 days ago

    Hi Johan,
    Congratulations on a very impressive and inspiring project. Well done!
    Could you share more details on how you adapted/fastened the lower arms to the servo motors shafts?

    2 answers
    Johan Linkbtshil2000

    Answer 19 days ago

    Here is an explanation drawing
    (it misses the screw on the drawing)

    Capture d’écran 2019-02-28 à 10.50.48.png

    23 days ago

    Bonjour John,
    Ton projet est vraiment fascinant. J'ai tout imprimé les pièces déjà. Je veut maintenant m'attaquer à la partie logiciel. Python c'est assez nouveau pour moi et j'aimerais savoir quel version de python tu utilise ainsi que les version des diférents modules comme Numpy, OpenCV, Tkinter, PIL(Pillow) etc. Merci d'avance, car cela m'aiderais beaucoup. Mon objectif est de le rendre tout a fait autonome en implantant le tout sur un RPI,

    2 replies
    Johan LinkAreJayJay

    Reply 22 days ago


    Merci pour le commentaire.
    Voila les versions des librairies que j'utilise:
    Python : 3.6.4
    Numpy : 1.14.5
    OpenCV : 3.3.0
    Tkinter : 8.5
    PIL : 5.1.0
    imutils : 0.4.6
    serial : 3.4

    N'hésite pas à m'envoyer une photo du projet, ça m'intéresse :)
    AreJayJayJohan Link

    Reply 22 days ago

    Bonjour Johan et non John, désolé.
    Merci pour les versions, je viens tous juste de réussir a faire tourner
    Je suis en attente de quelques pièces et je vais définitivement te tenir au courant de me développements. J'ai modifier, avec Fusion360, les bras pour ajouter des roulements a billes au joint.Pas vraiment nécessaire mais bon.


    22 days ago on Step 6

    I loved it I Loved It I loved it
    I play table tennis at local level and also software engineer with python developing experience. I want this in my bedroom, will make this


    25 days ago

    Hi, here is a picture of my implementation of the design. I was able to print all the parts on my CR10. I used an Arduino Nano for the servo control and had to provide a separate power supply for the servos to keep them from resetting the Arduino. I’ve been trying to get the PID working off and on for a couple of weeks, but haven’t been able to make it work. I assume the problem is the mapping of the camera to the angles controlled by the servos.

    2 replies
    Johan LinkJerryolsen

    Reply 24 days ago

    Thank you very much for the picture.
    I may know where your problems come from.
    - Is there 18cm between the end of each arm. (see picture)
    - If you press the "Ranger les bras" button, all arms should be lowered as much as possible. (see picture)

    Capture d’écran 2019-02-23 à 12.31.13.pngF5YQOLPJQ2O7076.LARGE.jpg
    JerryolsenJohan Link

    Reply 23 days ago

    Thanks Johan, the ends of my arms are 18 cm apart from center to center. Should they be 18cm between the outsides of the magnet holders? I found I have a loose servo arm and and need to disassemble the project to tighten it.


    25 days ago

    I am kind of speechless at the beauty and ingenuity of this project. Congratulations on winning the competition and I look forward to your next instructable.


    4 weeks ago

    I wonder if a RPi w/ camera would be able to handle the speed for balancing the ball
    Such a great deserved to win first prize :)


    4 weeks ago

    Hy John.
    We have almost done your project but we have a problem.
    The program doesn't recognize the ball.
    We have tried with diferent colours but we haven't been able to.
    Could you help us?