0% found this document useful (0 votes)
11 views17 pages

Ball Balancing System Using Ultrasonic Sensor and Servo Motor With An Arduino

The document is a report on a ball balancing system using an Arduino, ultrasonic sensor, and servo motor, developed by students at the Birla Institute of Technology and Science. The project aims to balance a ball on a bar by continuously monitoring its distance and adjusting the servo motor using a PID control algorithm. It discusses the components used, the working of the code, calibration methods, real-life applications, and limitations of the project.

Uploaded by

Tony
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views17 pages

Ball Balancing System Using Ultrasonic Sensor and Servo Motor With An Arduino

The document is a report on a ball balancing system using an Arduino, ultrasonic sensor, and servo motor, developed by students at the Birla Institute of Technology and Science. The project aims to balance a ball on a bar by continuously monitoring its distance and adjusting the servo motor using a PID control algorithm. It discusses the components used, the working of the code, calibration methods, real-life applications, and limitations of the project.

Uploaded by

Tony
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

A

Report
On

Ball Balancing system using Ultrasonic Sensor


and Servo Motor with an Arduino
Prepared For
Prof. Puneet Mishra
&
Prof. Surekha Bhanot

By
Kaustubh Mishra 2020A8PS1558P
Chaitanya Chauhan 2020A8PS1815P
Rajarshi Misra 2020A8PS1822P
Snigdha Gupta 2020A8PS1793P

At

BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE,


PILANI (Rajasthan)
April 2023
Acknowledgement

We would like to thank Prof. Puneet Mishra and Prof


Surekha Bhanot, and the entire team of instructors of the
course Industrial Instrumentation and Control (INSTR
F343) for their guidance and immense help throughout the
course of this project. Through this project, we have learnt
and understood the real-world implementation of the
topics we learn in class and gained a real insight into their
use and application. We hope to learn more and pursue
similar projects in the future.
Aim of the Project

Ball balancing using Arduino UNO, ultrasonic sensor and


stepper motor. This project intends to balance a ball on a
bar attached to a stepper motor when the bar is removed
from equilibrium. It also uses PID for controlling the
stepper motor.

Components Used:

1. Arduino UNO
2. Breadboard
3. Ultrasonic sensor
4. Jumper wires
5. Stepper Motor
6. Bar/scale etc.
Purpose/Significance of each
component used

1. ULTRASONIC SENSOR
WHAT IS AN ULTRASONIC SENSOR?

An ultrasonic sensor is an instrument that measures the distance to an


object using ultrasonic sound waves.

An ultrasonic sensor uses a transducer to send and receive ultrasonic


pulses that relay back information about an object’s proximity.
High-frequency sound waves reflect from boundaries to produce distinct
echo patterns.
In this project we use the HCSR-04 ultrasonic sensor,

The HC-SR04 ultrasonic sensor is a low-cost sensor that is commonly used


to measure distances. It works by emitting an ultrasonic sound wave and
then measuring the time it takes for the wave to bounce back after hitting
an object. The sensor contains both a transmitter and a receiver, and is
able to calculate the distance to the object based on the time it takes for the
sound wave to travel to the object and back.
The HC-SR04 sensor has four pins: VCC, GND, Trig, and Echo. VCC and
GND are used to power the sensor, while Trig and Echo are used to
communicate with it.

HOW DO THE ULTRASONIC SENSORS IN THE PROJECT WORK?


1. Ultrasonic sensors work by sending out a sound wave at a frequency
above the range of human hearing. The sound wave is sent by the
transmitter.
2. The sound wave bounces back if it gets any obstacle in its path back
to its receiver
3. Based on the time taken the distance is measured
CHARACTERISTIC VALUES
1. Vcc=5V
2. Detection Angle: 15 degrees
3. Obstacle Detection Range: 2cm to 4m
4. Output type: Analog and Digital
5. Working Frequency: 40kHz

2. ARDUINO UNO
Arduino is an open-source electronics platform based on easy-to-use
hardware and software. The Arduino Uno is a microcontroller board. It has
14 digital input/output pins, 6 analog inputs, a 16 MHz crystal oscillator, a
USB connection, a power jack, an ICSP header, and a reset button. In our
project, basically collects the input from the Ultrasonic sensors through
analog pins A0 and A1.

3. JUMPER WIRES
Male-to-male jumper wires, female-to-female and male-to-female jumper
wires have been used in our project to connect the various components.

4. BREADBOARD
A breadboard allows us to connect the components without any soldering.
The components can hence be reused, and this is very useful for
prototyping.

5. SERVO MOTOR SG90

A servo motor is an electromechanical device that produces torque and


velocity based on the supplied current and voltage. A servo motor works as
part of a closed loop system providing torque and velocity as commanded
by a servo controller utilizing a feedback device to close the loop. The
feedback device supplies information such as current, velocity, or position
to the servo controller, which adjusts the motor action depending on the
commanded parameters.
Circuit Design
Circuit layout made using Tinkercad
BLOCK DIAGRAM

Basic Premise/Concept Used


The basic premise behind the project is that we are constantly monitoring
the distance of the object from both the sensors and are usign the
difference in distances to calculate the error. Based on that error, we are
changing the angle of the servomotor accordingly, so as to bring the ball
back to the setpoint, which in this case is the centre of the ruler or 7.5cm
from both the sensors.

Working of the Code


This is an Arduino code for a PID-based ball balancing system using an
ultrasonic sensor and servo motor. Here's how it works:

First, the code defines the pins for the ultrasonic sensors, the servo object,
and the constants for the PID algorithm (Kp, Ki, and Kd). The setpoint is
also defined, which is the desired distance from the ball to the sensors.

In the setup function, the ultrasonic sensors and the servo are initialized,
and the initial position of the servo is set to 90 degrees. The variables for
the PID algorithm are also initialized.

In the loop function, the distance from the ball to the sensors is measured
using the measureDistance function, which sends a pulse to the sensor
and measures the time it takes for the echo signal to return. The average
distance from the sensors is then calculated.

The error is calculated as the difference between the setpoint and the input
(average distance from the sensors). The derivative term is calculated by
subtracting the last error from the current error and dividing by the time
elapsed since the last measurement. The integral term is calculated by
summing the errors over time.

The output is calculated by multiplying each term (error, integral, and


derivative) by its respective constant (Kp, Ki, and Kd) and adding them
together. This output is then used to set the angle of the servo motor, which
will move the platform to balance the ball.
Finally, the variables for the PID algorithm are updated, and the loop starts
over to measure the distance and calculate the output again.

Overall, the code continuously measures the distance from the ball to the
sensors, calculates the error, and adjusts the angle of the servo to balance
the ball using the PID algorithm.

Code

#include <Servo.h>

// Define the pins for the ultrasonic sensors


const int trigPin1 = 3;
const int echoPin1 = 5;
const int trigPin2 = 2;
const int echoPin2 = 4;

// Define the servo object


Servo servo;

// Define the constants for the PID algorithm


const double Kp = 1;
const double Ki = 0;
const double Kd = 0;

// Define the variables for the PID algorithm


double setpoint = 7.5; // The desired distance from the ball to the sensors
double input, output;
double error, last_error, sum_error;
unsigned long last_time;

void setup() {
// Initialize the ultrasonic sensors
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);

// Initialize the servo


servo.attach(9);

// Set the initial position of the servo


servo.write(90);

// Initialize the variables for the PID algorithm


last_time = millis();
sum_error = 0;
}

void loop() {
// Measure the distance from the ball to the sensors
double distance1 = measureDistance(trigPin1, echoPin1);
double distance2 = measureDistance(trigPin2, echoPin2);

// Calculate the average distance from the sensors


input = (distance1 + distance2) / 2;

// Calculate the error


error = setpoint - input;

// Calculate the derivative term


unsigned long current_time = millis();
double dt = (current_time - last_time) / 1000.0;
double derivative = (error - last_error) / dt;
// Calculate the integral term
sum_error += error;
double integral = sum_error * Ki;

// Calculate the output


output = Kp * error + Ki * integral + Kd * derivative;

// Set the angle of the servo


servo.write(90 + output);

// Update the variables for the PID algorithm


last_error = error;
last_time = current_time;
}

// Function to measure the distance using an ultrasonic sensor


double measureDistance(int trigPin, int echoPin) {
// Send a pulse to the sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Measure the time it takes for the echo signal to return


long duration = pulseIn(echoPin, HIGH);

// Calculate the distance in centimeters


double distance = duration * 0.034 / 2;

// Return the distance


return distance;
}
Calibration of PID

In general, the gains of Kp, Ki, and Kd must be modified by the user for the
system to function optimally. Following the general procedures should aid in
tailoring a circuit to match one's system and environment, despite the
absence of a fixed set of rules dictating the values to be used for each
system. A PID circuit will typically slightly overshoot the set point value
before rapidly damping out to the set point value.

In the project, manual tuning of the parameters was found to be the


easiest. Since it was not possible to get accurate data, the values of Kp, Ki
and Kd were selected based on observation(i.e. The values that seemed to
balance the ball properly). It was done in the following manner:
1. The proportional gain was set to 1, and Kd and Ki were set to zero.
2. The value of Kp was increased till oscillations were obtained.
3. The value of Kd was then increased to ensure that the oscillations
dampened quickly.
4. Also, some overshoot was observed, and in order to reduce that, the
value of Ki was increased.
5. The values of Kp, Ki and Kd have been provided in the code.
Calibration of Ultrasonic Sensor
Calibrating the ultrasonic sensor is an important step to ensure accurate
distance measurements and reliable operation of the ball balancing system.
The steps we undertook were:
Connect the ultrasonic sensor to the Arduino board and upload a simple
sketch to read the distance measurements from the sensor and print them
to the serial monitor.
Place an object at a known distance from the sensor, such as a wall or
piece of cardboard, and record the distance measurement from the serial
monitor.
Repeat step 2 for different distances, ranging from a few centimetres to
several meters, and record the distance measurements.

Using the data we get from this setup, we find the error in the real reading
vs the reading we get from the sensor and then map it accordingly so as to
display only the actual distance. We mostly got accurate readings through
the ultrasonic sensor.

Real life Applications

Ball balancing systems using PID controllers and sensors such as


ultrasonic sensors and servo motors can have many real-life applications in
various fields, some instances being

Robotics: Ball balancing robots can be used in industrial automation,


assembly line production, and other tasks that require precise control and
stability.
Entertainment: Ball balancing toys and games are popular among children
and adults alike. Examples include the popular toy "Sphero" and the game
"Labyrinth".

Education: systems like these are often used in educational settings to


teach students about feedback control systems, sensors, and
programming.

Sports: Some sports such as table tennis, billiards, and air hockey involve
maintaining a ball or puck in a stable position, and ball balancing systems
can be used to improve players' skills and performance.

Healthcare: Ball balancing systems can be used in rehabilitation therapy to


help patients with balance and coordination issues improve their motor
skills.

Limitations of our Project


- Much of our data and constants used was determined not by
mathematical/sofware based models but by basic observation and
empirical analysis, which implies that our model is not the most
accurate

- The platform we used as a beam balance is a makeshift model, and


not 3D printed or manufactured with the project in mind. This also
leads to some non-idealities with the movement of the ball
- The Ultrasonic sensor we used is not the most state-of-the-art and
has several limitations, the most prominent one being its accuracy
since it's not able to resolve distances below 1cm

- The PID algorithm used in this project is designed to maintain a


constant distance between the ball and the sensors. However, it may
not be able to handle external disturbances such as wind or
vibrations, which can affect the stability of the system.

References

1. Arduino: Wikipedia |https://en.wikipedia.org/wiki/Arduino


2. “What Is an Arduino?” What Is an Arduino? - SparkFun
Learn,https://learn.sparkfun.com/tutorials/what-is-an-arduino/all
3. Open SOurce| Arduino Basics https://opensource.com/resources/what-arduino
4. University of Illinois at Urbana Champaign. “M801: Ultrasonic Sensor
Characterisation.” ECE 110 FA2019,
https://courses.engr.illinois.edu/ece110/fa2019/.
5. https://maxbotix.com/blogs/blog/how-ultrasonic-sensors-work#:~:text=An%20ultr
asonic%20sensor%20is%20an,information%20about%20an%20object's%20prox
imity. | How Ultrasonic Sensors Work
6. http://pubs.sciepub.com/acis/3/1/1/ | Design and Implementation of Ball and
Beam System Using PID Controller

You might also like