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.
README Β· rendered from GitHub
PID-Controlled One-Axis Ball Balancing System
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
π§ͺ Final Project Hardware
β¨ 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
π½οΈ Demo
π 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
- Sensing: Ball position is measured using HC-SR04.
- Filtering: Distance values are filtered (moving average).
- Error Calculation:
error = distance - setpoint - PID Control:
- P: Immediate reaction to error
- I: Compensates for past errors
- D: Predicts upcoming trends
- Servo Adjustment: Beam angle is changed proportionally to the PID output.
- 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 = 5Ki = 0.01Kd = 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
| Component | Description |
|---|---|
| Beam | 30β50 cm acrylic/wood beam, pivot-mounted |
| Ball | Lightweight ping-pong ball |
| Servo Motor | SG90 (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 Supply | 5V via USB or external battery pack |
| Optional Display | 16x2 I2C LCD (PID values, position, error) |
| Accessories | Breadboard, jumpers, brackets, bearings, screws, etc. |
π Repository Contents
PID-Controlled-One-Axis-Ball-Balancing-System.inoβ Core Arduino codeProject_Proposal.pdfβ Initial planningProject_Presentation.pptxβ Final presentationProject_Report.pdfβ Detailed documentationDemo Video.mp4β Final working demoCicuit 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
Embedded2025FreeRTOS 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
Embedded2024Fully Automated Egg Incubator
ATmega328P climate control and egg turning that hatched every pigeon egg in 18 days.
- ATmega328P
- Arduino IDE
- Atmel Studio
- Proteus