[Research] Balancing of a Ball on Beam using Arduino as a PID controller - BeeLab

## Sunday, October 22, 2017

I used Arduino Uno as a PID controller to balance a small ping-pong ball on a 4-bar mechanism. And used Matlab to plot the position of the ball vs time.

## Control Loop: • An ultrasonic sensor detects the actual position of the ball (the "output ball position" in the above figure).
• The output ball position is compared to the "setpoint" (the position where we want the ball to be)
• PID algorithm on the Arduino is used to compute the angle of the servo motor based on the difference between the output ball position and the setpoint (the error).

In this video:
• setpoint = 15 cm
• Kp= 2.5
• Ki=0
• Kd=1

## Parts used:

• Arduino UnoPing)))
• ultrasonic sensor
• 20 Kg.cm Digital Servo Motor
• 5 V DC power supply

## Connections: ## The dimensions of my 4-bar mechanism

You can change the dimensions of the mechanism, the weight of the ball, or any other parameter. But then you will have to change the Kp, Ki, and Kd values in the code to get a good system performance.

## Arduino code, Matlab code, and PID library: Link

• You must download "PID_v1" library for Arduino and move its whole folder to: Documents > Arduino > libraries
Referent:

### How to get the values of Kp, Ki, & Kd?

- You can try to guess the values, then apply it in the code and monitor the performance of the system as you change these values. Don't worry too much about  Ki in this system, you can set it to zero. Firstly, guess the value of Kp, then keep changing the value of Kd until you reach a satisfying performance for the system. This is called manual tuning and it takes a long time.
- Another thing you can do is to build a mathematical model on SIMULINK and use its automatic tuner that can give you three values for Kp, Ki, and Kd.  Of course, the mathematical model will not 100% represent the real physical system, therefore, you will have to slightly change the 3 obtained values, which means you'll have to do some manual tuning anyway.
- If you are familiar with Simulink, you can check out these detailed tutorials: