TechieYan Technologies

PID Ball Balance

Abstract

To achieve accuracy and sustainability in symmetrical and asymmetrical systems, the positioning of an object is required. In this project, we are going to Position and stabilize the object by using an Arduino UNO. The main aim of this project is to balance a ball in a required position. Here in PID Ball Balance project, PID values are used to balance the ball in the midpoint. To set the ball in the middle point of the balance P, I, and D values should be exact else it will end up stopping at an approximate position. In PID ‘p’ stands for proportional, ‘I’ stands for integral, and ‘D’ stands for derivative. (NOTE:- if you are a newbie then don’t worry about PID). To achieve the desired target PID controller is widely used in industries. To know the distance/position of every instance an ultrasonic sensor is used and to move the balance a small servo motor is used.

Introduction

In this PID Ball balance system, PID controller is a closed-looped feedback mechanism that calculates error values. It is capable of handling the behavior of a system. The necessity of the PID control system increased when the process controller was gaining its fame in 1940. In the modern era, 90% of the control loops use this mechanism. The feedback mechanism works in such a way that first it takes the output value and sends it back to the input so that it can check whether the object is at a set point or not and controls the system according to it. This process is repeated until the object gets into its desired in the system. Kp, Ki, and Kd are the constants that are used to represent proportional, integral, and derivative terms.

Components Required

Hardware Requirements

  • Arduino Uno : Arduino Uno is a microcontroller from the Arduino Family. It was developed by Arduino. cc and is based on the Microchip ATmega328P microcontroller. Uno has 20 I/O pins, 6 are analog pins and 14 are digital pins.
PID Ball Balance System using arduino

For more details,  pinout and datasheet, you can visit : https://docs.arduino.cc/hardware/uno-rev3

  • Servo motor:  SG90 according to the requirement of the project servo motor’s type size can be changed
  • Ultrasonic sensor( HC-SR04):  It is a SONAR-based distance measurement sensor. It provides excellent non-contact range detection from 2 cm to 400 cm with high accuracy and reliable readings.
  • Ball : A round object used to play.

Software Requirements

  • Arduino Ide : Arduino IDE (Integrated development environment) is software that is used to dump the program into boards. Arduino- IDE’s primary use is to build electronics-related projects. Arduino is an open-source platform simple and easy-to-understand platform for coding.

HOW TO INSTALL ARDUINO IDE CLICK ON THE LINK :

https://techieyantechnologies.com/2022/10/installation-of-arduino-ide/

Block diagram

In the following block diagram, the ultrasonic sensor is an input device that calculates the distance, and the servo motor is an output device that is interfaced with the controller(Arduino Uno).

Closed-Loop Mechanism:

It is the closed-loop mechanism used in the device. it is used to maintain the desired set and set the point without human interaction.

Flow Chart

A flowchart is a diagram  that talks about the steps involved in the project and the sequential flow of the code.

So, as it startsfirst it will read the values of the ultrasonic sensor. then calculates the distance. If the distance is equal to the midpoint then the servo will stop,  else the servo will change the angle and make the ball stop at the midpoint.

Pid ball balance system flow diagram

*website used to make Flow Chart and Block Diagram :  https://www.lucidchart.com/pages/

Pin Connection:

Ultrasonic Sensor

Ultrasonic Sensor has 4 pins

The following pin connection is about the connection of ultrasonic pins with Arduino Nano. The Ultrasonic Sensor has 4 pins Vcc, Trig(Trigger), Echo & Ground. Vcc pin of ultrasonic is connected to 5V of Arduino Nano, Trig is connected to D3, Echo is connected to D2, and Ground(GND) is connected to GND

Servo motor 

Servomotor has 3 wires:

The following pin connection is the connection of the servo motor with the Arduino Uno. The servo’s yellow wire is connected to the d9 of Arduino Uno, the servo motor’s red wire is connected to Vin of Arduino, and the servo motor’s brown wire is connected to the GND of Arduino Uno.

Circuit diagram

Follow the below circuit diagram to understand the connection made to different devices.

Note: Make sure to take proper precautions while connecting the components.

Working

The next step after setting up the connection is to upload code to Arduino Uno. power it and the system will be turned on, the distance of the ball will be calculated using an ultrasonic sensor. then as per the readings, the servo will work and with the help of the servo, the ball will be able to stay at the midpoint.  

Conclusion

We developed a model to balance a ball on top of the surface using Arduino, a servo motor, and an ultrasonic sensor.

Working Code

#include <Servo.h>              //Using the Arduino as the controller device,the library enables to control the servo motor
Servo sg90;                     //initialize a servo object to control a servo 
#define PROPORTIONAL 15         //Initial Proportional Gain
#define INTEGRAL 0.02           //Initial Integral Gain
#define DERIVATIVE 10           //Intitial Derivative Gain

float Error1 = 0;               // define variable to calculate error 
float Error2 = 0;               //define variable to calculate error 

void setup() {
  pinMode(2, OUTPUT);           //ultasonic trig pin is connected to pin 2 of Arduino
  pinMode(3, INPUT);            //ultasonic echo pin is connected to pin 3 of Arduino
  sg90.attach(9);               // servo pin is connected to the pin 8 of arduino
  Serial.begin(9600);           // Initiate a serial communication with baud rate of 9600
  sg90.write(50);               // initial position of the servo motor 

}
void loop() {
  PID();                         // calling the function to run the code 
 
}


// function is used to calculate the position of the ball

double Total_length () {                
  digitalWrite(2, LOW);
  delayMicroseconds(4);
  digitalWrite(2, HIGH);
  delayMicroseconds(10);
  digitalWrite(2, LOW);

  double times = pulseIn(3, HIGH);
  double centimeters = times / 29 / 2;    // formula to calculate the position of the ball
  return centimeters;
}

void PID() {                       
  int distance = Total_length  ();       // the maximum position for the ball


  int Mid_Point = 15;                    //initializing the mid point 
  float fault = Mid_Point - distance;     // calculting the fault
   

//formulas to calculate pid values
 
  float P = fault * PROPORTIONAL;   
  float I = Error2 * INTEGRAL;
  float D = (fault - Error1) * DERIVATIVE;

  float total_PID = P + I + D;
  Error1 = fault;
  Error2 = fault+1;
  Serial.println(total_PID);
  int a = (int)total_PID;


a = map(a, -135, 135, 135, 0);    // to plot the values on the graph    


  if (a< 0) {
    a = 0;
  }
  if (a > 135) {
    a = 135;
  }

  sg90.write(a);
}

If You Face Any Issue Feel Free To Contact us Any Time.

+91 7075575787