Nicolas Hammje's Ball-Balancing Bot Uses OpenCV on a Raspberry Pi to Stop a Ball Dead in Its Tracks

Driven by Python, this 3D-printed robot catches a ball and then adjusts its cardboard plate to keep it from rolling off.

Maker Nicolas Hammje has put together a Raspberry Pi-powered ball-balancing bot, designed to keep its target ball on a flat tray through a servo-powered tilting process — using a Raspberry Pi Camera Module and OpenCV to track its efforts.

"A simple Python program locates the ball using OpenCV," Hammje writes of the project's driving software. "The ball is tracked, which gives you X and Y coordinates in regards to the plate. I then have the program calculate the error on each line relative to the servo, as there are three servos each 120° apart it gets complicated but it’s really just trigonometry."

Despite only adjusting the tray, made of cardboard to help dampen the bounce as the ball is initially thrown into place, across two axes, Hammje chose to use three servos in total. "The platform rests entirely on those three servos. Three makes it a stable base. Six would be ideal ([a] Stewart platform)," the maker explains. "Could also use a support in the middle, and then two servos perpendicular."

Tracked via a Camera Module and OpenCV, this ball is put in its place by a three-servo plate-bot. (📹: Nicolas Hammje)

In addition to the cardboard tray and three servos, the project uses a Raspberry Pi 4 Model B single-board computer connected to a Raspberry Pi Camera Module v2 as the vision source and a Waveshare Servo HAT to control the servo motors. Everything is connected together using a 3D-printed chassis, while the motor arms connect to the plate via universal joints designed for drilling.

More information on the project is available in Hammje's Reddit thread, while the source code and design files have been published to GitHub under an unspecified open-source license.

Gareth Halfacree
Freelance journalist, technical author, hacker, tinkerer, erstwhile sysadmin. For hire: freelance@halfacree.co.uk.
Latest articles
Sponsored articles
Related articles
Get our weekly newsletter when you join Hackster.
Latest articles
Read more
Related articles