Disclaimer: This is an example of a student written essay.
Click here for sample essays written by our professional writers.

Any scientific information contained within this essay should not be treated as fact, this content is to be used for educational purposes only and may contain factual inaccuracies or be out of date.

Strategies to Program an Arduino Device

Paper Type: Free Essay Subject: Mechanics
Wordcount: 3968 words Published: 8th Feb 2020

Reference this

Robotics Systems Project

1 Introduction:


1.1 Abstract

The investigation into different ways to program an Arduino device as well as PID control systems to design a self-balancing robot using an inverse pendulum. The primary goal of this exercise is to allow us to learn how to split a project into small parts that can be distributed amongst the team, teaching us teamwork. This project also lets us learn more about systems and engineering in general with a much more active approach.

Get Help With Your Essay

If you need assistance with writing your essay, our professional essay writing service is here to help!

Essay Writing Service

1.2 Report layout

The remainder of this report is structured as follows. In section 1 we have a brief explanation on the devices used to allow the robot to function and how it may be tuned. In section 2 there is information of what a PID controller is and how it can be tuned. In section 3 the layout of the circuit and the testing phases undertaken to evaluate the strengths and weaknesses of the design and tune them if possible. Section 5 covers the coding used in the project.

2 Build Overview

The first aim was to build an adequate chassis to hold all the components in place and be balanced evenly to allow the robot to readjust in the same way for both moving forwards, backwards, left and right. The chassis was built symmetrically, and all components lay along the centre of gravity of the chassis to further assist balance. Shock absorbers in the form of two longer pieces of metal where used to take the brunt of the impact if the robot collided with a wall or collapsed when testing/on the course to avoid damaging the more delicate instruments such as the line follower or accelerometer as they are quite fragile.

2.1 Algorithm and function of components

The algorithm for the line follower designed was adequate however in practice we were unsuccessful in implementing the algorithm in real environment, however, given another week or so we would have been able to refine our code and follow the track in its entirety.

The code uses a PID library as the formula for balancing the robot using the gyroscope as the error around setpoint = 0. The output of the PID computation was directly used as the speed for the motors, this finished the feedback response system. Given more time the PID could be tuned to more stability. The line follower algorithm would’ve followed the PID. The line follower returns numbers 0, 1, 2 and 3 depending on what combination of black/white the infrared sensors detect. The robot would slowly accelerate to a speed of 50 using a while loop in the direction indicated by the line follower state.

The accelerometer functioned as intended and the robot can read the angle and adjust itself back to its resting position at 90-degrees to the surface its placed on. The issue with the robots balancing was caused by the crudely adjusted PID as it proved time consuming to micro-adjust and we simply ran out of time and as such the robot can just about stay upright but no long before falling over onto the shock absorbers. Given more time we are certain that the PID could be tuned correctly and the robot would balance and be able to connect the balancing of the robot with the line follower thus giving the desired goal of a robot that can navigate the course set.

3 The control loop

The code for the robot was written using mblock as a base to figure out commands such as setting the motor speed and obtaining values for the gyroscope and line follower. After this the mblock code was cleaned in the Arduino IDE as there were lots of unnecessary lines. The final step was implementing The PID library [1] from the Arduino library manager and writing out the finished code.


  1. First 7 double values are set:
  • Setpoint – this is the value that the PID tries to reach.
  • Input – this is the error entering the PID.
  • Output – this is the result after bringing the Input through the PID computation.
  • Kp, Ki, Kd – these are the values of the proportional, the integral and the derivative components of the PID respectively. Insert values here
  • angle – this is the same as the Input, this is used to keep track of the angle of the robot outside the PID.
  1. The gyroscope, motor_9(9) and motor_10(10) are set.
  2. The gyroscope and the PID are initiated, Setpoint is set to 0 since this is the value we want.

Balancing loop:

  1. Input and angle are set to the value of the gyroscope using “gyro_1.getAngle(2)”
  2. The PID library used had trouble with positive Input values. This was solved by pretending that positive values are negative and then inverting the output. The “angle” double was useful for this.
  3. If angle<0 then multiply Input by -1.
  4. If angle<0 then motor_9 speed = Output and motor_10 speed = Output multiplied by -1.*
  5. Else motor_9 speed = Output multiplied by -1 and motor_10 speed = Output.*
  6. The gyroscope reading is then updated.

*will be commented out after the line following loop

3.1 Not yet implemented controls:

Line follower

  1. Declare line follower (MeLineFollower linefollower_7(7))
  2. Create double Output2
  3. Second PID is created with Input = 50 + Output of Balancing PID.
  4. Setpoint is set to 50.
  5. Ki, Kp and Kd to be under tuned as to not interfere with the balancing PID too much, Ki, Kp and Kd for balancing loop must be tuned again.
  1. If the line follower returns 0 then Motor_9(Output2), Motor_10(Output2), this will make the robot turn right on the spot until it finds the line again.(90o) ((linefollower_7.readSensors())==(0)).
  2. If the line follower returns 1 set Motor_9(0), Motor_10(Output2* -1) this will make the robot turn right.
  3. If the line follower returns 2 set Motor_9(Output2), Motor_10(0), this will turn left.
  4. If the line follower returns 3 set Motor_9(Output2), Motor_10(Output2 * -1), this will cause the robot to move forward.

The PID values we have now allow the robot to remain upright, but it is not steady enough to follow a line at the same time. While we could not get the robot to balance perfectly in the given time we are confident that given another week we could fine tune the PID to get the robot to balance smoothly and another week to implement and tune the line follower code.

4 Gyroscope:

The 3-axis accelerometer can measure the change of acceleration in X, Y, and Z axes. By perceiving the total inertial force in a specific direction, the accelerometer can measure the acceleration and gravity. The 3-axis accelerometer means it can detect the motion or gravity of an object in 3D space. The accelerometer can be used to measure the gravity g. If the module is still and completely motionless, the force that the earth gravity exerts on it is about 1 g. If it is placed vertically, it will detect the force exerting on it along Y-axis is about 1 g. If it is placed at a certain angle, it will detect that the force of 1 g distributes on different axis. When it is in motion or vibration in 3D space, the Me 3-Axis accelerometer and Gyro Sensor will detect the force greater than 1 g in one or more axes, and acceleration as well. Velocity and displacement can be obtained by integrated the acceleration. When an object rotates around an axis, the angular velocity is generated. The 3-axis angular velocity sensor (gyro) can detect the change of angular velocity on X, Y, and Z axes.
The gyroscope is used at the error in the PID system, allowing the robot to balance. There were some issues with this as the “0” angle would sometimes vary between tests and requires the Arduino to be reset.

5 The line Follower:

The line follower would begin with both sensors of the line follower on the white line, the robot would only be able to move forwards in a straight line if both the sensors returned 1(ON). If only one of the sensors was above the line then the sensor would return 1, 0 or 0, 1 indicating a right or left turn. It turns as when the right sensor reads 0, the left wheel toggles off while the right wheel remains on causing the robot to turn to the left. We tackled the 90-degree turn by implementing a system where as soon as the sensor reads 0, 0 the bot will rotate until it returns to the track. The various combinations are given a number from 1 to 4 for the line follower to interoperate. E.g. 1,1 = 4

Figure 1

Figure 2

Figure 3

The line follower is mounted on the bottom of the chassis as close to the ground as possible due to its limited range and it needs to be in the centre so that it turns left and right in the same way.

 6 Principle of the inverted pendulum:

The robot relays heavily on the principle of an inverted pendulum to balance. For the robot to balance/correct itself as it begins to tilt too far in one direction the robot begins to accelerate. Due to the increasing speed of robot, the bottom of the robot is now moving faster than the top and bringing the top and the bottom onto one central line thus re-entering the robot. Using a PID control and an accelerometer this is possible to asses the offset angle and readjust to the centre. The accelerometer obtains the necessary speed and angle and the PID ensures it re-centres through oscillating until it is stable again.

6.1 Force equations:

Figure 4

X = Ax + Bu

Y = V (Change of position in time =Velocity)

V = -e Θ + u (momentum balance)

Θ = q (Rate of change of momentum)

Q = Θ – u

e = ratio of m2/(m1 + m2)  

Figure 5

m1 = Weight at bottom,

m2 = Weight at top

u = Force of the object


7 Introduction to PID:

Drawn from[Tehrani, Kambiz & Mpanda, Augustin. (2012). PID Control Theory. 10.5772/34364.]

7.1 Introduction

Feedback control is a control mechanism that uses information from measurements. In a feedback control system, the output is sensed. PID devices are widely used in feedback control of industrial processes on the market since 1939 and have remained the most widely used controller in process control to this day. Thus, the PID controller can be understood as a controller that takes the present, the past, and the future of the error into consideration. A proportional–integral–derivative controller (PID controller) is a method of control loop feedback. This method is composed of three controllers:

1. Proportional controller (PC)

2. Integral controller (IC)

3. Derivative controller (DC)

7.2 Role of a Proportional Controller (PC)

In a PID, the role of the proportional controller is dependent on current error, the integral is proportional to the past error and the derivative is proportional to future error. Weighing these values and using them to create a feedback system with minimum error and oscillations. PID’s are commonly used in many engineering projects to create stabile systems. In a proportional system, steady state error is inversely proportional to the proportional gain. This proportional response can be adjusted by changing Kp, this multiplies the error and therefore increases the speed of re-adjustment. This has the downside of creating overshoot. This can be shown as the following equation:

P = (KP)(error(t))      

A high Kp results in a large change to the output for a given small change in error. This has the potential to destabilise the system. On the flip side a low Kp results in a low output for a given high change in error. If the Kp is too low it can be too slow to respond to an error, this would mean that something like a self=balancing robot would just slowly faceplant. In summary the Kp reduces the rise time of the system response and steady state error, but it can never eliminate it. The proportional band is expressed as a percentage so:

PB% = 100/Kp

7.3 Role of an Integral Controller (IC)

The integral controller is proportional to the magnitude of the error as well the it’s duration, in this way it is said to be proportional to the past error. The integral in in a PID controller is the sum of the instantaneous error over time and gives the accumulated offset that should have been corrected previously. The integral is the sum of the error over time and gives the accumulated offset. Increasing Ki reduces steady state error but makes the transient response unstable, creating oscillations. The integral term is given by:



7.4 Role of a Derivative Controller (DC)

The derivative (Kd) is proportional to the slope of the error over time. The derivative part of the PID slows the rate of change of the output. d has the effect of reducing overshoot and stabilising the system and improving the transient response. A Kd that is too high will create even more oscillations. The derivative term is given by:


The effects of Kp, Ki and Kd are shown in figure 6.

Figure 6

Table 1. A PID controller in a closed-loop system

7.5 PID controller (PIDC)

The typical structure for a PID system is show in figure 7. figure 8 shows the structure for a PID control system. The error signal e(t) generates the proportional integral and derivative actions in the equation. The resulting signal is weighted by Kp Ki, and Kd and is used as the output u(t).

Figure 7

Figure 8

Figure 9


Block diagram of the PID controller:




where Kp is the proportional gain, TI is the integral time constant, TD is the derivative time constant, KI =KP /TI is the integral gain and KD =KPTD is the derivative gain. The ‘‘three term” functionalities are highlighted below. The terms KP , TI and TD definitions are:

  • The proportional term: providing an overall control action proportional to the error signal through the all pass gain factor.
  • The integral term: reducing steady state errors through low frequency compensation by an integrator.
  • The derivative term: improving transient response through high frequency compensation by a differentiator.

1.3 The Ziegler–Nichols tuning method

 The Ziegler–Nichols tuning method of tuning the PID that is quite simple and does not require advanced calculations. It was created by John G. Ziegler and Nichols in the 1940’s. To start off the Ki and Kd of the system must be set to 0. The Kp is the increased from 0 until it reaches the ultimate gain; Ku, at this point the output of the System oscillates constantly. The Ku and Tu are then used to tune the PID, the table for this tuning method is shown in figure 9.


Figure 10


Figure 11


Figure 12

Figure 13


8 Circuit and component lay out:

Figure 14

The batteries located on the lower deck of the robot are connected to the Arduino on the upper deck are connected to the Arduino. The accelerometer is connected to the Arduino and is on the top along side the Arduino. The Arduino is also directly connected to the line follower which is on its own stage near the ground. Both motors are connected to the Arduino and are situated on either side of the line follower so that the line follower is central.

9 Testing phases:

The centre of gravity and weight distribution were the first things tested on the robot to ensure that the robot was evenly balanced. This was done by tying a string to either side of the robot and observing if it tilted forwards or backwards. The robot had no weight bias to either side and thus the chassis standard we were targeting was met.

The robot was tested in an environment like that of the final object, a track consisting of various corners. First the line follower was tested to find out if the infrared sensor would recognise the white tape/track and trigger the appropriate response to the wheels. The line follower correctly detected the white line and triggered the appropriate response which was the left sensor triggering the right wheel and the right sensor triggering the left wheel.

Figure 15

The balance of the robot was then tested on a flat surface with open space, so it can manoeuvre if need be. The PID controller tuning was the first test we preformed, the Kp, Ki and Kd values where adjusted until the robot was able to balance.The robot was unable to balance to a high standard due to the lack of time afforded to tuning the PID controller and jittered back and forth rapidly before collapsing to one side.

Figure 16

10 Evaluation:

The robot’s algorithm and the logic utilized worked well in theory but failed to successfully balance even in the absence of a course to follow due to the crudely tuned PID but given more time to work out its kinks the robot could balance and traverse the course. The line follower worked as intended however the lack of balance crippled the robot’s ability to traverse the course. Given the time to fix the issues specified above the robot line follower would be able to carry out the appropriate functions to traverse the course.

11 Ethical responsibilities

Ethics –a set of moral principles that govern a person’s behaviour or the conducting of an activity (Oxford Dictionary)[4].

The Makeblock kits provided are to be re-used by other students in later years, it is the responsibility of the people using these kits to ensure they are returned in the same condition as they were found. A 20-euro deposit has been paid to ensure that if any parts do are damaged or go missing they can be replaced. This is to allow future students to be able to work on their projects and to minimize expenditures for the university.

No soldering or 3D printing was used. No waste products have been produced in the process of creating the robot. Rechargeable batteries were used as they are more environmentally and economically friendly.

12 Conclusion

In conclusion the robot used a PID to balance roughly. The line following portion of the project is not implemented fully as it requires the robot to balance first, so a simple version is implemented to display the robot following a line. The code was first ran on mblock to assist in call commands for the line follower, motors and gyroscope. Afterwards everything was done inside the Arduino IDE using the Arduino language. Our team ran out of time on the project and if we were to do it again a lot more effort would have to be put in at the start of the semester. However, this project has thought us a lot about teamwork and work ethic which is of great value for future projects.

13 References.

[1]: https://www.youtube.com/watch?v=crw0Hcc67RY&t=400s

[2]: https://www.makeblock.com/project/me-3-axis-accelerometer-and-gyro-sensor

[3]: [Tehrani, Kambiz & Mpanda, Augustin. (2012). PID Control Theory. 10.5772/34364.]

[4]: Notes by Dr John Dooley and Dr Seamus Mcloone



Cite This Work

To export a reference to this article please select a referencing stye below:

Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.

Related Services

View all

DMCA / Removal Request

If you are the original writer of this essay and no longer wish to have your work published on UKEssays.com then please: