Skip to content
Awais Asghar.
Embedded & IoT2025

PID Ball Balancing System

A one-axis beam that settles a ball at a target distance in under 30 seconds.

An ultrasonic sensor measures ball position, a PID loop drives a servo to tilt the beam, and Kp, Ki and Kd can be tuned live over serial. Includes stability detection and an automatic reset to neutral.

PID Ball Balancing System thumbnail

README Β· rendered from GitHub

PID-Controlled One-Axis Ball Balancing System

Project Status Platform Language IDE PID Control License

PID-based one-axis ball balancing robot using Arduino. Uses an ultrasonic sensor and servo motor to stabilize a ball at a target distance. Real-time tuning of Kp, Ki, Kd via Serial. Includes stability detection and automatic neutral reset. The system stabilizes within 30s using empirically tuned parameters.


πŸ–ΌοΈ Project Overview

System Diagram


πŸ§ͺ Final Project Hardware

Hardware Picture Demo Picture


✨ Features

  • PID Control for dynamic stability.
  • Real-time sensor feedback using IR or ultrasonic sensors.
  • Servo motor-driven beam for accurate actuation.
  • Low-cost microcontroller implementation (Arduino/STM32).
  • Serial Monitor & LCD integration for live debugging.

πŸ› οΈ System Architecture

Control Flowchart


πŸ“½οΈ Demo

Watch the demo video


πŸ”Œ Circuit Diagram

Circuit Diagram


🧾 Project Summary

This Arduino-based system controls the position of a ball on a beam using PID control and real-time feedback from an ultrasonic sensor. The ball is returned to a setpoint by tilting the beam via a servo motor, compensating for positional error.


βš™οΈ How It Works

  1. Sensing: Ball position is measured using HC-SR04.
  2. Filtering: Distance values are filtered (moving average).
  3. Error Calculation: error = distance - setpoint
  4. PID Control:
    • P: Immediate reaction to error
    • I: Compensates for past errors
    • D: Predicts upcoming trends
  5. Servo Adjustment: Beam angle is changed proportionally to the PID output.
  6. Stability Monitoring: If error stays < 3 cm for 2.5 seconds, servo resets to neutral.

🧠 Control Strategy

A classic PID (Proportional-Integral-Derivative) loop is implemented:

Error = Desired Position - Actual Position  
PID Output = Kp * Error + Ki * ∫Error + Kd * (dError/dt)

Final tuned values:

  • Kp = 5
  • Ki = 0.01
  • Kd = 9

Main Code Logic

error = distance - setpoint;
integral += error;
derivative = error - previous_error;
output = -(Kp * error + Ki * integral + Kd * derivative);
servoAngle = constrain(neutralAngle + output * 1.2, 0, 180);
myServo.write(servoAngle);
  • PID gains can be tuned via Serial commands.
  • System resets to neutral if error stabilizes or ball goes out of range.

πŸ§ͺ PID Tuning via Serial Monitor

Kp 5         β†’ Set proportional gain  
Ki 0.01      β†’ Set integral gain  
Kd 9         β†’ Set derivative gain  
neutral 34   β†’ Set neutral servo angle  
servo -20    β†’ Move servo from neutral

πŸ”© Hardware Components

ComponentDescription
Beam30–50 cm acrylic/wood beam, pivot-mounted
BallLightweight ping-pong ball
Servo MotorSG90 (light load) or MG996R (high torque)
Position Sensor- IR (GP2Y0A21) for analog
- Ultrasonic (HC-SR04) for digital timing
Microcontroller- Arduino Uno (simple)
- STM32 Blue Pill (faster)
Power Supply5V via USB or external battery pack
Optional Display16x2 I2C LCD (PID values, position, error)
AccessoriesBreadboard, jumpers, brackets, bearings, screws, etc.

πŸ“ Repository Contents

  • PID-Controlled-One-Axis-Ball-Balancing-System.ino – Core Arduino code
  • Project_Proposal.pdf – Initial planning
  • Project_Presentation.pptx – Final presentation
  • Project_Report.pdf – Detailed documentation
  • Demo Video.mp4 – Final working demo
  • Cicuit Diagram.jpg – Wiring reference (check spelling)
  • Project Picture.jpg – Setup image

🧰 Tech Stack

  • Arduino Uno
  • HC-SR04 Ultrasonic Sensor
  • SG90 Servo Motor
  • Serial Monitor for real-time control
  • PID control logic (manual tuning)

βœ… Results

  • System stabilized within 30 seconds of a disturbance
  • PID parameters: Kp = 5, Ki = 0.01, Kd = 9
  • Stability detection reduces unnecessary movement

πŸš€ Future Enhancements

  • 2-axis (X-Y) balancing system
  • Vision-based tracking using OpenCV or IR grid
  • Self-tuning PID or ML-based control
  • Wireless monitoring via Bluetooth/Wi-Fi
  • MATLAB or Python-based simulation-first approach

πŸ‘¨β€πŸ’» Authors

This project was created at National University of Sciences & Technology (NUST) as part of the Linear Control Systems course project by:

  • Awais Asghar
  • Muhammad Ashar Javid
  • Ameer Hamza
  • Muhammad Hammad Sarwar

πŸ“œ License

This project is licensed under the MIT License. See LICENSE file for details.


πŸ“š References

  • Γ…strΓΆm & HΓ€gglund, Advanced PID Control, ISA
  • Ogata, Modern Control Engineering, Pearson
  • Arduino PID Library
  • MathWorks MATLAB Control Toolbox

More in Embedded

Related projects

See all
  • Embedded2025

    FreeRTOS Smart Energy Monitor

    STM32F746 tasks compute Vrms, Irms and kWh; an ESP32 pushes it to a cloud dashboard.

    • STM32F746
    • FreeRTOS
    • ESP32
    • C/C++
  • Embedded2024

    Crowd-Sourced AQI Monitor

    Six sensors, Bluetooth to a phone app, and AQI within 1% of the US Embassy station.

    • Arduino UNO
    • C/C++
    • PMS5003
    • MQ-series
  • Embedded2024

    Fully Automated Egg Incubator

    ATmega328P climate control and egg turning that hatched every pigeon egg in 18 days.

    • ATmega328P
    • Arduino IDE
    • Atmel Studio
    • Proteus