DIY Ball Balancer Robot!

by skulkarni3000 in Circuits > Robots

4575 Views, 44 Favorites, 0 Comments

DIY Ball Balancer Robot!

Ball Balancing Demo
IMG_1246.jpg
IMG_0017.JPG
IMG_0002.JPG

Bored this summer? Looking for a hands-on project that teaches core engineering and robotics skills? That’s what led me to build this ball-balancing robot, a 3-RRS parallel manipulator. When I started, I was new to robotics, and had little experience with sensors, control systems, or inverse kinematics. But through this project, I not only learned how to design, wire, code, and debug a complete system, but most importantly, I learned to problem-solve. This is the single most important thing that engineers look for in the job market.

This robot may look simple, but getting it to balance correctly is a real challenge. Building it forced me to learn skills I never thought I would be able to learn, but I enjoyed every moment of it. Whether you're a beginner or a student looking to apply theory in a real system, this project is a great place to start. Let's build it!


You'll find a Github link down below with all resources you'll need for this project, as well as several links containing inspiration that helped me make this project. I encourage everyone interested in making this project to try their own hand at designing, wiring, and coding this project before using these resources for a real challenge!

Github


Inspiration

More inspiration

Even more inspiration

Supplies

IMG_0987.JPEG
IMG_0986.JPEG
617QPT7+sIL.jpg
51isTJy+bkL._UF894,1000_QL80_.jpg
61hkuaVxWFL._UF894,1000_QL80_.jpg

Tools:

  1. Wire cutters
  2. Wire strippers
  3. Multimeter
  4. Pliers/needle-nose pliers
  5. M3, M4 allen keys
  6. Soldering iron, lead-free solder, solder flux, heat-resistant mat
  7. Power Supply with at least 24V and 5A
  8. Personal protection equipment (Be safe!)

Supplies

  1. Teensy 4.0 (with headers)
  2. Three (3) Nema 17 Stepper Motors 2A Bipolar
  3. 8.4x4in Resistive Touchscreen Panel
  4. TMC2208 Stepper Motor Drivers
  5. Protoboard Pack
  6. Header Pins
  7. 5V Voltage Regulators
  8. Wires (I used solid core wires but you can use others if you prefer)
  9. 1 in Steel Ball Bearing (any small ball can work though)
  10. M3x5mm Spacers
  11. Metric Screws (I'm using M3 and M4 for this project)
  12. M3 Tie Rods
  13. A basic electronics kit (I'm using this for the breadboard for quick prototyping and also need some 100 microfarad capacitors)

A full BOM for everything I purchased for this project, plus links for where I purchased it, can be found here

Designing the Robot

ezgif-83b15887bf295b.gif
Screenshot 2025-07-29 131906.png
Screenshot 2025-07-29 134045.png
ezgif-282d23431401e6.gif
Screenshot 2025-07-29 134547.png
Screenshot 2025-07-29 134603.png
Screenshot 2025-07-29 134613.png
Screenshot 2025-07-29 134629.png
Screenshot 2025-07-29 134648.png

This robot is a 3-RRS parallel manipulator, meaning each of its three legs has:

  1. A revolute joint that's motorized
  2. Another revolute joint connecting the lower arm to the upper arm
  3. A spherical joint connecting the upper arm to the platform

The basic idea is to place three of these arms in an equilateral triangle so the platform gets 3 degrees of freedom:

  1. Rotation along the X axis
  2. Rotation along the Y axis
  3. Translation along the Z axis


Design Considerations:

  1. Stepper motor placement must form an equilateral triangle
  2. Must leave space to house electronics
  3. A top platform to mount the arms and hold the touchscreen
  4. The entire structure needs to be stable

I used Solidworks to model all parts and 3D printed them using PLA. My files are attached, but I highly recommend you try designing your own parts if you're up for a challenge, as it's a great opportunity to improve your CAD and problem-solving skills! (Note, the full assembly file cannot be uploaded on this site, but is available on my Github page)

Once your parts are designed, go ahead and 3D print them. Now it's time to start building!

Design and Build the Circuit

circuit_image.png
teensy40_pinout1.png
IMG_0005.JPG
IMG_0018.JPG
IMG_1008.JPEG
IMG_0009.JPG
IMG_0004.JPG
IMG_1023.JPEG
IMG_0011.JPG
IMG_1048.jpg

I used a perfboard to solder my final circuit so the wires would stay put, but if you’re new to soldering, use a solderless breadboard to test things out.

Power Overview

  1. Stepper Motors run at 24V
  2. The Teensy 4.0 microcontroller can only handle up to 5V

Because of this, we need a voltage regulator to step down 24V to 5V.

Very important:

Adjust your voltage regulator before connecting the Teensy. Use a multimeter on the regulator's output and set it to exactly 5V by turning the tiny screw (potentiometer) on top.

Microcontroller: Teensy 4.0

The brains of this entire robot.

  1. Much faster and smaller than most Arduinos
  2. Works with the Arduino IDE (just install the Teensyduino add-on)
  3. Only accepts up to 5V, make sure the voltage regulator is set up properly or risk frying it!

I used header pins to make it removable for convenience.

TMC2208 Stepper Motor Drivers

These tiny drivers pack a punch and power the Nema 17 stepper motors.

  1. VM → 24V
  2. VIO → 3.3V from Teensy
  3. Add a 100μF capacitor between VM and GND to prevent voltage spikes
  4. Connect MS1 and MS2 to VIO for 1/16 microstepping (smoother movement)
  5. Connect EN, STEP, DIR to digital pins on Teensy (I used pins 0–6)
  6. All EN pins should go to the same Teensy pin so motors turn on/off together
  7. Also mounted these drivers on headers for easy removal

Nema 17 Stepper Motors

Connect these to M1A, M1B, M2A, M2B on the motor drivers. Make sure all pins face the same way across motors.

Before integrating into your final circuit, test one motor with a driver and Teensy to make sure microstepping is working.

Touchscreen Panel

The final component! This connects to analog pins 14–17 on the Teensy.

Tip: Test and calibrate the touchscreen before attaching it to the final circuit to calibrate the touchscreen values through the Adafruit Touchscreen Arduino library. By default it should be calibrated properly, but make sure you check if the center is displaying accurately.



Make sure you've connected all ground pins properly!!!

Assemble the Robot

IMG_1041.jpg
IMG_0010.JPG
IMG_0013.JPG
unnamed (1).jpg
ezgif-88d4016e748fdb.gif

By now, your 3D printed parts should be ready to go!

Start with the Screen Plate

  1. Attach the tie rods to the upper legs using an M3x8mm screw
  2. Use two M3x5mm spacers per leg to mount to the screen plate
  3. Use M3x18mm screws on each side of the hole

Build the Base

  1. Slide the 3D printed motor spacers onto each motor
  2. Mount motors with M3x10mm screws, wire side facing inward through the center hole

Note:

Stepper motor wires are ridiculously long, so zip-tie them neatly to keep things clean.

  1. Attach wires to the motor drivers, keeping wire orientation consistent
  2. Keep track of which motor correlates to which driver correlates to which Teensy pin (order matters!)

Final Steps

  1. Attach lower legs to motors with M3x25mm screw and nut
  2. (Make sure the triangle side faces up)
  3. Secure the circuit board to the base
  4. Connect upper legs to lower legs
  5. Motor A should be at the top of the triangle, the side that sticks out over the touchscreen

Upload the Code!

IMG_0007.JPG
ezgif-19f829b260853a.gif
Screenshot 2025-07-31 223242.png

Now the fun part, let's make this robot move! In order to do that, we need to set up the main components of the robot, ie the stepper motors and the touchscreen through the Multistepper/AccelStepper and Adafruit Touchscreen Arduino libraries respectively. I've attached the code in my github page, simply download the folder named "BallBalancingBot" and run BallBalancingBot.ino.

I've created the code in a way such that all you need to do is type the functions within the void loop in to change what the robot does. By default, the robot will make the ball balance in the center, but I have also created functions for the robot to move in different patterns (like a square, circle, heart, star, etc). You can check the full list of patterns to move, along with the inputs for the function in the "PIDControllers.h" file that should be included in my Arduino sketch.

Since my robot is using PID controllers, it normally requires a lot of tuning. Fortunately, My PID values have already been tuned to the exact power and dimensions of the robot I created, but making any changes to the power, size, and shape of the robot will change these PID gain values and you might need to adjust them. I'll explain what this all means in the coming steps.


Once again, I highly recommend trying to code this robot yourself as its the best way to learn the process and skills required, mainly PID tuning, inverse kinematics, and piecing together in an elegant and efficient way.

How This All Works

Ball Balancing Robot IK Derivation-2.png
0a6bd58c-5703-4365-95fe-f5bca2b5ed3f.png

Inverse Kinematics

In order to tell our robot which direction to tilt the platform, we need to tilt the arms to that exact position. We do that through inverse kinematics. Inverse kinematics is, as the name implies, the inverse of forward kinematics that we're used to seeing in conventional physics classes.

  1. Forward kinematics: Tells you where the end of an arm will go, given the angles of each joint (what you see in basic physics or mechanics classes).
  2. Inverse kinematics (what we need): You pick a spot for the platform, and the program figures out what angles the arms must be set to in order to achieve it.
  3. Typically, IK can get math-heavy (especially with more joints), but with two joints per arm in this design, the calculations are straightforward, involving mostly just simple matrix math and trigonometry.
  4. My code runs these equations every time you give the robot a new target. This results in getting an angle for each arm, which translates into motor steps to move the platform where you want it.

I highly encourage you to check out my attached PDF if you're curious about the math behind this robot, where I wrote out the derivation for how my model works. Otherwise, just know the robot uses inverse kinematics behind the scenes to handle all the complex geometry for you, so you can simply tell it where you want the ball to go.

PID (Proportional, Integral, Derivative) Controllers

Now we know how the robot can move the platform. But simply being able to move the platform doesn't balance the ball, as we need an algorithm which will sense the ball and tell the platform to move automatically based on its position. That’s where PID controllers come in. These controllers are essential for many robotics and engineering tasks that require precise regulation.

  1. Inputs: The ball’s current position and the desired (target) position.
  2. How it works:
  3. Proportional (Kp * error): Tilts the platform based on how far the ball is from the target (error). A larger error means a larger correction.
  4. Derivative (Kd * error/dt): Reacts to how quickly the ball is moving. This helps slow it down and prevent overshoot.
  5. Integral (Ki * ∫error): Adds up small errors over time which prevents the ball from stopping slightly off-center.
  6. Adjusting the gains (Kp, Ki, Kd) changes how aggressively the robot corrects itself. My setup uses a high Kp (for quick corrections), a moderate Ki (for accuracy), and a very small Kd (to prevent jitters).
  7. I experimented with velocity damping (Kv), but found plain PID works best for reliability.
  8. Two separate PID controllers run for X and Y directions, adjusting the platform every time the touchscreen updates the ball’s position.

Tuning these values to your hardware is key. Start with my settings, then tweak as needed for your build.

If you want a visual representation for PID, I recommend this video, it's an amazing resource to fully understand this topic.

Control Flowchart

I’ve attached a flowchart to visualize this whole system, but here’s the full loop:

  1. Touchscreen reads ball’s x and y position
  2. PID controllers compares it to the target, which outputs an arbitrary value for the x and y axis
  3. Arbitrary value is mapped to a pair of tilt angles for the x and y axes
  4. IK model converts tilt to motor angles
  5. Motor controller turns those angles into stepper instructions
  6. Speed is based on positional error, so motion is smoother

This control flow also makes it so that dynamically changing the target position of the PID controllers will allow us to move the ball however we want to. This is how we are able to create patterns!

Quick Tips

ezgif-6a3848322ed9ee.gif

Here are some quick tips that helped me build and test my robot better. I'll be constantly updating this section as I'm eager to continue making improvements! I hope these will help you on your way to make the perfect ball balancing robot!

General Advice

  1. Please, please, please make sure you have connected the ground pins in your circuit! I don't want to see anyone's robot go up in flames because one of the ground connections were missing!
  2. Test each motor and touchscreen before soldering, as solder is basically permanent!
  3. Keep a multimeter on hand, as it'll help you check if things are powered or not.
  4. Zip-tie long cables to avoid tangles and accidental tugs
  5. I've designed my parts to fit my needs, but change each part to suit your needs. If you think the robot is too big, make it smaller. If something doesn't work, fix it. This is the BEST part about 3D printing and rapid prototyping. It gives you the flexibility to create whatever works best for YOU.
  6. Label all wires, or at least try color coding them. Trust me, it’ll save you hours
  7. The robot's motion can sometimes be a bit jittery, mainly due to inconsistencies with the touchscreen sensing properly. This is why you should calibrate your touchscreen properly and adjust the values accordingly
  8. Adjust PID values slowly! Make one change at a time and observe results

Debugging Tips

  1. If platform moves opposite: swap A/B motor leads or invert direction in code
  2. If only one motor moves, confirm Teensy pin mappings and driver power.


In addition, I've also created a simple Python program which will plot the positions of the robot using serial monitor outputs. Please check it out if you want to see how the robot is thinking in real time!


Everything you need, from CAD files, code, PDFs, and images, are on my GitHub repository.

Good luck, and happy tinkering! :)