Edited Project 2nd April
Edited Project 2nd April
BY
APRIL 2025
DECLARATION
We, the undersigned students declare that, this thesis is our original work and has not been
submitted elsewhere for consideration for the award of any degree. The information and
materials used from other sources are cited as they appear in the text.
By:
Signature Date
Supervisor
Signature Date
This project is dedicated to our parents who have never failed to give us financial and moral
support, during the time we developed our system and for teaching us that even the biggest task
can be accomplished if it is done one step as a time.
We dedicate this project to all the people who have worked hard to help us complete this project.
ii
ACKNOWLEDGEMENTS
To begin, we’d like to thank Dr. Martino Ojwok, our primary supervisor, for his tireless
guidance and support. His intellectual guidance and constructive critique put us on check during
the whole exercise. We are glad to have had an encounter with him as our supervisor and a
mentor for our project. His critical comments and genuine concerns were great in making this
project a success.
We also would like to appreciate all our lecturers for their time, effort and understanding in
helping us succeed in our studies. Their vast wisdom and wealth of experience have inspired us
throughout our studies. In addition, we’d like to express our gratitude to everyone in the
University of Juba. Thanks to their generosity and encouragement, our time spent studying there
has been truly rewarding. To conclude, we’d like to thanks God, our parents, our colleagues and
our friends. It would have been impossible to finish our studies without their unwavering support
over the past few years.
ABSTRACT
The precise control of DC motor speed is essential in various industrial and automation
systems. This project presents a comprehensive approach to designing and controlling the
speed of DC motor using an Arduino Nano microcontroller. The system integrates multiple
control techniques, including Pulse Width Modulation for speed regulation and Proportional-
Integral-Derivative control for enhanced accuracy and stability. A potentiometer is used as an
analog input to set the desired speed or position, while the Arduino Nano processes the input
and generates a PWM signal to drive the motor. The PID control algorithm is implemented to
minimize errors and improve dynamic response by continuously adjusting the PWM output
based on feedback from the motor's actual speed. The motor driver circuit, L298N, interfaces
the microcontroller with the DC motor, ensuring proper voltage and current levels. This
model demonstrates the effectiveness of combining PWM and PID methods for precise motor
control, offering a cost-effective and scalable solution for applications in robotics,
automation, and embedded systems.
Keywords: DC motor, Arduino Nano, PWM, PID control, potentiometer and motor driver.
iv
LIST OF ABBREVIATIONS AND ACRONYMS
DC Direct Current
Kp Proportional Gain
Ki Integral Gain
Kd Derivative Gain
e(t): Error
TABLE OF CONTENTS
DECLARATION........................................................................................................................
DEDICATION...........................................................................................................................
ACKNOWLEDGEMENTS....................................................................................................
ABSTRACT..............................................................................................................................
LISTS OF FIGURES................................................................................................................
LIST OF TABLES....................................................................................................................
CHAPTER ONE.......................................................................................................................
INTRODUCTION.....................................................................................................................
1.1 Introduction....................................................................................................................
1.3 Objectives.............................................................................................................................
CHAPTER TWO......................................................................................................................
LITERATURE REVIEW.........................................................................................................
vi
CHAPTER THREE..................................................................................................................
MATERIALS.............................................................................................................................
3.0 Introduction.........................................................................................................................
3.1 DC Motor.............................................................................................................................
3.6 Microcontroller..................................................................................................................
3.7 Sensors................................................................................................................................
CHAPTER FOUR...................................................................................................................
4.1 Introduction.......................................................................................................................
4.2.4 Potentiometer.................................................................................................................
4.3 Setup...................................................................................................................................
viii
4.3.3.1 Designing the code on the Arduino IDE....................................................................
4.4 Results...........................................................................................................................
4.4.3 Discussion........................................................................................................................
CHAPTER FIVE.....................................................................................................................
5.1 Conclusion..........................................................................................................................
5.2 Recommendations.............................................................................................................
Appendices...............................................................................................................................
LISTS OF FIGURES
x
LIST OF TABLES
CHAPTER ONE
INTRODUCTION
1.1Introduction
Control is one of the most fundamental factors that enables us to use devices to their
maximum potential and with the highest accuracy, where we can achieve high efficiency,
total control of the dc motor, and improved precision. This project will therefore focus on
designing and controlling of speed of DC motors using Arduino Microcontroller.
DC motors were the first form of motors widely used, as they could be powered from existing
direct-current lighting power distribution systems. A DC motor's speed can be controlled over
a wide range, using either a variable supply voltage or by changing the strength of current in
its field windings. Small DC motors are used in tools, toys, and appliances. The Universal
motor can operate on direct current but is a lightweight brushed motor used for portable
power tools and appliances. Larger DC motors are currently used in propulsion of electric
vehicles, elevator and hoists, and in drives for steel rolling mills. [1]
DC motors are widely used in various applications due to their simplicity and effectiveness in
converting electrical energy into mechanical energy. Due to this, the demand of dc motors
has increased rapidly necessitating more development in the field of control.
However, controlling the speed and behavior of these motors can be challenging, especially
in applications requiring precise control.
The objective of this project is to implement a speed control mechanism using an Arduino
microcontroller, which will enable real-time adjustments to the motor's speed based on
feedback from sensors or user inputs. Additionally, the project aims to develop a model for a
DC motor that can effectively predict its performance characteristics, including speed, torque,
and response to input voltage changes.
1
University of Juba-School of Engineering & Architecture
1.3 Objectives
Objectives are divided into two categories; specific and general objectives.
2
Chapter Two: Literature Review
CHAPTER TWO
LITERATURE REVIEW
Several techniques have been explored for controlling the speed of DC motors using the
Arduino Nano:
Kumar et al. (2019) [2] ) implemented a PWM-based speed control system using an
Arduino Nano and an L298N motor driver. The system allowed for manual speed adjustment
via a potentiometer connected to an analog input of the Arduino.
The Arduino Nano generated a PWM signal with a variable duty cycle, which is fed to a
motor driver L298N. The duty cycle determined the average voltage applied to the motor,
thus controlling its speed. The system provided smooth speed control, but the speed varied
with changes in load.
Singh and Patel (2020) [3] developed a closed-loop speed control system using an
Arduino Nano, an L298N motor driver, and an optical encoder. A PID controller was
implemented in the Arduino firmware to maintain the desired speed.
In close loop control, an encoder is attached to the motor shaft to provide real-time speed
feedback. The Arduino Nano reads the encoder pulses and adjusts the PWM signal to
maintain the desired speed. By doing so, the system achieved precise speed control with
minimal error, even under varying load conditions.
Rahman et al. (2021 [4]) demonstrated the effectiveness of PID control in maintaining motor
speed under varying load conditions. PID Control: A Proportional-Integral-Derivative (PID)
controller is implemented on the Arduino Nano to achieve precise speed control. The PID
3
University of Juba-School of Engineering & Architecture
algorithm adjusts the PWM duty cycle based on the error between the desired and actual
speed and the system achieved high accuracy and stability.
Gupta et al. (2018) [5] implemented a sensorless speed control using back EMF
measurement, achieving reasonable accuracy but with limitations at low speeds. This was
done through back EMF Measurement:
The back electromotive force (EMF) of the motor is proportional to its speed. The Arduino
Nano measures the back EMF during the off periods of the PWM signal and uses it to
estimate the motor speed.
Banzi & Shiloh, 2014 [6], in this paper powering the Arduino Nano is simple; it can be
connected via USB or powered through an external source, with a recommended voltage
range of 7 to 12 volts (Arduino, n.d.). This flexibility is ideal for both stationary and portable
projects. The board also includes an onboard LED connected to pin 13, which serves as a
useful tool for debugging and monitoring the program's execution
4
Chapter Two: Literature Review
di a
V a =R i a + L +e (2.1)
dt b
Where;
dω
J + βω=T e −T L
dt
(2.2)
Where:
5
University of Juba-School of Engineering & Architecture
e b=K b ω
(2.3)
T e =K t i a
(2.4)
Applying the Laplace transform, the above modelling equations can be expressed in terms of
the Laplace variable s.
V a ( s )=R I a ( s ) + LS I a ( s ) + E b (s ) (2.5)
Eb ( s ) =K b ω(s) (2.6)
T e (s)−K t i a (s ) (2.8)
V a ( s )=¿ (2.9)
6
Chapter Two: Literature Review
V a ( s )−K b ω(s)
I a ( s )= (2.11)
R + LS
K t i a ( s ) −TL
ω ( s )= (2.12)
( JS + β )
where;
7
Chapter Three: Materials
CHAPTER THREE
MATERIALS
3.0 Introduction
This chapter will give detailed description on the components used in design and
speed control of dc motors using Arduino nano microcontroller.
3.1 DC Motor
A direct current (DC) motor is a fairly simple electric motor that uses electricity and a
magnetic field to produce torque, which causes it to turn. At its most simple, it requires two
magnets of opposite polarity and an electric coil, which acts as an electromagnet. The
repellent and attractive electromagnetic forces of the magnets provide the torque that causes
the motor to turn anyone who has ever played with magnets knows that they are polarized,
With a positive and a negative side. The attraction between opposite poles and the repulsion
of similar poles can easily be felt, even with relatively weak magnets. A DC motor uses these
properties to convert electricity into motion. As the magnets within the motor attract and
repel one another, the motor turns [7]
A DC motor is powered by direct current, which converts electrical energy into mechanical
energy. This mechanical energy causes rotational movement within the motor, helping to
power a vast majority of industry applications [8]
8
Chapter Three: Materials
The permanent magnet motor uses a magnet to supply field flux, permanent magnet
DC motors have excellent starting torque capability with good speed regulation, a
disadvantage of permanent magnet DC motors is they are limited to the amount of load they
can drive, these motors can be found on low power applications.
In a series DC motor, the field is connected in series with the armature, the field is
wound with a few turns of large wire because it must carry the full armature current. A
characteristic of series motors is the motor develops a large amount of starting torque.
However, speed varies widely between no load and full load. Series motors cannot be used
where a constant speed is required under varying loads. Additionally, the speed of a series
motor with no load increases to the point where the motor can become damaged, some load
must always be connected to a series-connected motor. Series-connected motors generally are
not suitable for use on most variable speed drive applications.
In a shunt motor the field is connected in parallel (shunt) with the armature windings,
the shunt-connected motor offers good speed regulation, The field winding can be separately
excited or connected to the same source as the armature. An advantage to a separately excited
shunt field is the ability of a variable speed drive to provide independent control of the
armature and field, the shunt-connected motor offers simplified control for reversing. This is
especially beneficial in regenerative drives
Compound motors have a field connected in series with the armature and a separately excited
shunt field, the series field provides better starting torque and the shunt field provides better
speed regulation, however the series field can cause control problems in variable speed drive
applications and is generally not used in four quadrant drives.
9
University of Juba-School of Engineering & Architecture
DC motors are widely used in various applications due to their simplicity, reliability, and ease
of control. Here are some common applications:
Electric Vehicles: DC motors are extensively used in electric vehicles (EVs) for propulsion.
They provide high torque at low speeds, which is essential for vehicle acceleration. The speed
and torque can be easily controlled, making them suitable for applications in cars, bikes, and
scooters [9].
Robotics: In robotics, DC motors are used to drive wheels, robotic arms, and other
mechanisms. Their ability to provide precise control over speed and position makes them
ideal for applications in automated systems. [10]
Speed Control: One of the primary advantages of DC motors is their ability to provide
precise speed control. By adjusting the voltage and current supplied to the motor, users can
easily vary the speed, making them ideal for applications where speed regulation is crucial.
High Starting Torque: DC motors are capable of producing high starting torque, which
makes them suitable for applications that require a significant amount of torque to start
moving, such as electric vehicles and cranes.
Simplicity of Design: The construction of DC motors is relatively simple, which can lead to
lower manufacturing costs and easier maintenance. This simplicity also allows for
straightforward integration into various systems.
10
Chapter Three: Materials
Reversibility: DC motors can be easily reversed by changing the direction of the current
flow, providing flexibility in applications that require bidirectional operation.
Heat Generation: DC motors tend to generate more heat during operation, especially under
heavy load conditions. This can necessitate additional cooling measures and can affect the
motor's lifespan.
Commutation Issues: The commutation process in DC motors can lead to issues such as
sparking and electrical noise, which may interfere with sensitive electronic equipment.
Power Supply Dependency: DC motors require a DC power supply, which can limit their
use in applications where AC power is more readily available or where energy efficiency is a
concern.
Vin is the input voltage of the board and it is used when an external power source is used
from 7V to 12V
5V is the regulated power supply voltage of the nano board and it is used to give the
supply to the board as well as the components.
3.3V is the minimum voltage which is generated from the voltage regulator on the board.
GND is the ground pin of the board
RST Pin (Reset); This pin is sued to reset the microcontroller
Analog pins (A0-A7): These pins are used to calculate the analog voltage of the board
within the range of 0V to 5V
Digital Pins (D0-D13); These pins are used as an input otherwise outputs pins (0V & 5V)
Serial Pins (Tx, Rx): These pins are used to transmit and receive TTL serial data
PWM (3,5,6,9,11): These pins are used to provide 8-bit of PWM output
SPI (10, 11, 12 & 13); These pins are used for supporting SPI communication
Inbuilt LED (13): This pin is used to activate the LED
12
Chapter Three: Materials
The L298N Motor Driver Module is a high-power motor driver for driving DC and
stepper motors. This module consists of a L298 motor driver IC and a 78M05 5V regulator.
L28N Module can control up to 4 DC Motors or 2 DC motors with directional and sped
control. It uses a L298N PWM system, which can control voltage using square wave pusles.
The wider the pulses, the faster the motor will rotoate. However, the exact pulse width will
vary depending on the type of motor being used.
13
University of Juba-School of Engineering & Architecture
IN1 & IN2 Motor A input pins. Used to control the spinning direction of motor A
IN3 & IN4 Motor B input pins. Used to control the spinning direction of motor B
Motor encoders are rotary encoders that measure the position and speed of a motor's
shaft. They provide feedback to the motor control system, allowing it to adjust the motor's
speed and position in real-time
A servo motor encoder is a tool that gauges the speed and location of a servo motor's
rotation. It is made up of a rotating disc with parallel, uniformly spaced slots or lines that
block a light beam and generate electrical signals that represent the location and speed of the
motor.
14
Chapter Three: Materials
Servo motor encoders come in two primary categories: incremental and absolute. Absolute
encoders offer encoders provide information on changes in position and speed with respect to
a reference point information on the precise location of the motor shaft.
In conclusion, servo motor encoders are crucial parts for precise positioning and control of
servo motors in a variety of applications, including CNC machines, robotics, and automation.
[14]
In conclusion, servo motor encoders are crucial parts for precise positioning and control of
servo motors in a variety of applications, including CNC machines, robotics, and automation.
Devices called stepper motor encoders are employed to gauge the speed and position
of a stepper motor. As stepper motors lack internal encoders, unlike servo motors, external
encoders must be used to give exact feedback and closed-loop control.
By giving feedback on the location, speed, and direction of the stepper motor, stepper motor
encoders enable precise control of its motions. They are frequently utilized in systems for
automation, machine tools, and robotics.
It's vital to remember that using stepper motor encoders makes the control system more
complex and expensive. Yet, they can offer important advantages in terms of performance,
accuracy, and dependability, particularly in high-precision applications.
15
University of Juba-School of Engineering & Architecture
3.6 Microcontroller
It is a printed circuit board (PCB) with circuitry and hardware designed to facilitate
experimentation with a certain microcontrollers’ boards feature. Our focus on this project will
be on Arduino nano microcontroller.
3.7 Sensors
In the context of speed control of a DC motor using the Arduino Nano microcontroller,
several sensors can be employed to provide feedback and enhance the performance of the
control system. Each sensor plays a critical role in ensuring accurate speed regulation and
responsiveness.
It can detect the position of a magnetic field. When placed near the motor's rotor, it can
provide information about the rotor's position, which is essential for implementing closed-
loop control strategies [15]. The Arduino Nano can utilize this data to adjust the PWM (Pulse
Width Modulation) signals sent to the motor driver, thereby controlling the speed more
effectively.
Current sensors such as the ACS712, are also valuable in this application. These sensors
measure the current flowing through the motor, allowing the Arduino Nano to monitor the
16
Chapter Three: Materials
load on the motor (ACS712, n.d.). By analyzing the current draw, the microcontroller can
make intelligent adjustments to avoid overloading the motor and ensure stable operation.
Current sensors like the LM35 can be utilized to monitor the motor's operating temperature.
Overheating can lead to reduced performance and potential damage, so having temperature
feedback allows the Arduino Nano to implement protective measures, such as reducing the
motor speed or shutting it down if necessary [16].
The mini-USB -cable is used to transfer the program from the computer to the Arduino nano
board.
3.9 Breadboard
It is a solderless construction base used for developing an electronic circuit and wiring for
projects with microcontroller boards like Arduino.
A breadboard is helpful in building circuits without any soldering. Certain contacts are
connected with each other. Therefore, it is possible to connect many cables with each other
without soldering or screwing them together. The image below shows in color, which
contacts are connected.
A jump wire is a short electrical wire with a solid tip at each end (or only "tinned"
often without it), typically used to interconnect the components in a breadboard. A jump wire
(also referred to as a jumper wire or jumper) is an electrical wire, or group of wire, with a
connector or pin at each end (or often without, simply "tinned") that is usually used without
soldering to interconnect the components of a breadboard or other prototype or test circuit,
internally or with other devices or components. In the slots given in a breadboard, the header
connector of a circuit board, or a piece of test equipment, individual jump wires are fitted by
inserting their "end connectors." Disclosed is an enhanced arrangement of jump wire for a
printed circuit board. The arrangement requires at least one jump wire with its opposite ends
17
University of Juba-School of Engineering & Architecture
bent in the same direction to face each other, and at least one slot given on the printed circuit
board's upper surface. The slot has holes created at its opposite ends to permit the opposite
ends of the jump wire to be inserted. By inserting the opposite ends into the reverse through
holes, bending the opposite ends, and then welding these bent ends to a printed circuit pattern
shaped on the lower or rear surface of the printed circuit board, the jump wire can be
mounted 18 and installed in the slot. This arrangement allows the mounting density of
components and devices to be increased without fear of short-circuiting on the printed circuit
board.
The development process for Arduino involves using the Arduino development environment,
which is a user-friendly software application designed to facilitate the writing and uploading
of code to an Arduino board. The environment is made up of various tools that work in
tandem to simplify the process, making it accessible to individuals with varying levels of
experience.
The Serial Monitor is a useful tool in the Arduino development environment that can
help with debugging by showing any serial information being transmitted by the Arduino
18
Chapter Three: Materials
board. When the Serial Monitor is opened, a new window will display real-time data being
sent and received by the board.
This feature is particularly useful for identifying and troubleshooting any issues with your
code or circuitry during the development process. The Serial Monitor can be used to view
sensor readings, debug communication protocols, and troubleshoot any other issues that may
arise.
The Arduino Nano continuously reads the voltage level from the potentiometer, which ranges
from 0 to 5 volts. This analog value is then mapped to a suitable PWM (Pulse Width
Modulation) value that corresponds to the desired speed of the DC motor. The mapping
process converts the analog input into a range suitable for controlling the motor's speed
(Arduino, n.d.). For example, a voltage of 0 volts might correspond to a PWM value of 0%
(motor stopped), while 5 volts might correspond to a PWM value of 100% (motor running at
full speed).
Once the PWM value is determined, the Arduino sends a signal to the motor driver circuit,
which regulates the power delivered to the DC motor based on the PWM input. By adjusting
the potentiometer, the user can smoothly control the speed of the motor, allowing for precise
operation in various applications, such as robotics, conveyor systems, or any project requiring
variable speed control.
Specifications:
19
University of Juba-School of Engineering & Architecture
PWM is a modulation technique used to encode a message into a pulsing signal. The
average value of voltage fed to the load is controlled by turning the switch between supply
and load on and off at a fast rate. The longer the switch is on compared to the off periods, the
higher the total power supplied to the load.
Pulse width modulation control works by switching the power supplied to the motor on and
off very rapidly. The DC voltage is converted to a square wave signal, alternating between
fully on (nearly 12v) and zero, giving the motor a series of power “kicks”. Pulse width
modulation technique (PWM) is a technique for speed control which can overcome the
problem of poor starting performance of a motor.
A DC motor's speed is controlled by varying the voltage applied to it. However, directly
varying the voltage is not efficient or practical in many cases. Instead, we use Pulse Width
Modulation (PWM) to control the effective voltage supplied to the motor.
The Arduino digital pins either gives us 5V (when turned HIGH) or 0V (when turned LOW)
and the output is a square wave signal.
20
Chapter Three: Materials
TOFF (Off Time): It is the time when the signal is low. Period: It is the sum of on time and off
time.
Duty Cycle: It is the percentage of time when the signal was high during the time of period.
T on
Duty cylce= × 100 (2.13)
T on+ T off
So, at 50% duty cycle and 1Hz frequency, the speed will be high for half a second and will be
low for the other half second. If we increase the frequency to 50Hz (50 times ON and OFF
per second), then the speed will be reduced to half its maximum speed.
The power applied to the motor can be controlled by varying the width of these applied
pulses and thereby varying the average DC voltage applied to the motor’s terminals. By
changing or modulating the timing of these pulses the speed of the motor can be controlled,
ie, the longer the pulse is “ON”, the faster the motor will rotate and likewise, the shorter the
pulse is “ON” the slower the motor will rotate.
When the duty cycle is 0%, the motor will stop completely because there is no voltage
difference. When the duty cycle is 50%, the motor will rotate at half the speed of the
maximum speed because the voltage is half the full voltage. When PWM is in 100%
condition, the motor rotates with maximum speed because of the continuous output of PWM.
In other words, the wider the pulse width, the more average voltage applied to the motor
terminals, the stronger the magnetic flux inside the armature windings and the faster the
motor will rotate and this is shown below. [17]
The Arduino Nano has specific pins (e.g., D3, D5, D6, D9, D10, D11) that support PWM
output. These pins can generate a PWM signal with a resolution of 8 bits (values from 0 to
255).
21
University of Juba-School of Engineering & Architecture
In the processing industries, different control strategies are used nowadays, according to their
needs and in a practical way, insufficient permanent precision, instability, too high a response
time, too great an overrun, with regard to the specifications of a specification. It is therefore
often necessary to integrate an organ called a corrector (Regulator) into the loop, the
objective of which is to improve the performance of the system. It is the control mechanism
that uses the information from the measurements of the controlled variable to manipulate a
variable in order to obtain the desired result. The controller is driven by the error between the
actual process output and the setpoint [18].
[ ]
t
1 d
U ( t )=K p e ( t ) + ∫ e ( t ) dt +T d e (t) (2.14)
Ti 0 dt
22
Chapter Three: Materials
Where u(t) and e(t) denote the control and the error signals respectively, and K p, Ti and Td are
the parameters to be tuned. The corresponding transfer function is given as;
[
K(s) = kp 1+
1
T i (s )
+T d (s)
] (2.15)
These functions have been enough to the most control processes. Because the structure of
PID controller is simple, it is the most extensive control method to be used in industry so far.
The PID controller is mainly to adjust an appropriate proportional gain (Kp), integral gain
(KI), and differential gain (KD) to achieve the optimal control performance.
PID controllers have many types of uses, including controlling robotics, temperature,
speed, and positioning.
de
u=Kpe+ Kd + Ki ∫ edt
dt
(2.16)
Where;
23
University of Juba-School of Engineering & Architecture
PID controller functions with closed-loop system. Variable (e) specifies tracking
error, difference among desired output (r) and actual output (y). Error signal (e) is
provided to PID controller, and controller evaluates derivative and integral of error
signal with time [24], then processed to calculate a new process input. This input will
try to adjust the measured process value back to the desired set point. [21]
The alternative to a closed loop control scheme such as the PID controller is an open
loop controller (no feedback) that is in many cases not satisfactory, and is often
impossible due to the system properties. By adding feedback from the system output,
performance can be improved
The basic idea is that the controller reads the system state by a sensor then it subtracts
the measurement from a desired reference to generate the error value. [22] The error
will be managed in three ways, to:
24
Chapter Three: Materials
A proportional (P) controller uses only proportional action to correct deviations from
desired parameter value while acting on the error signal generated by the controller
output.
Proportional-Integral (PI) controller also uses the integral action to maintain closed-
loop performance for extended time periods at steady-state conditions. As a result, it
is very stable, but it may cause system overshoot due to its delayed corrective action.
25
University of Juba-School of Engineering & Architecture
26
Chapter Four: Results and Discussion
CHAPTER FOUR
4.1 Introduction
This chapter will involve vivid methodology used in the project followed by the
results and finally the discussion of the results
The microcontroller that reads the potentiometer input and generates the PWM signal for
speed control.
The motor whose speed needs to be controlled and provides feedback also
Acts as an interface between the Arduino and the motor. It amplifies the control signals from
the Arduino to drive the motor.
4.2.4 Potentiometer
Provides an analog input to the Arduino. By rotating the potentiometer, the user can vary the
voltage at the analog input pin, which is then used to control the motor speed.
The Arduino Nano can be powered via USB, but the motor and L298N require an external
power source (e.g., a 9V battery or 12V adapter) because motors typically draw more current
than the Arduino can supply.
26
Chapter Four: Results and Discussion
Figure 4.12: Components Required to Build a PID Enabled Encoder Motor Controller
4.3 Setup
Connect the 5V from the motor driver to the 5V of the Arduino nano inside a breadboard
Connect the GND from the motor driver to the GND on the Arduino Nano inside breadboard
Connect the +12V from the motor driver to the Vin of the Arduino Nano inside a breadboard
Connect the `IN3` and `IN4` pins of the L298N motor driver to digital pins (D3 and D4) on
the Arduino Nano.
Connect the motor terminals to the output terminals OUT3 and OUT4 of the L298N motor
driver.
Connect the right end of the potentiometer to 5V, the left one end to GND, and the middle pin
(wiper) to an analog input pin (A0) on the Arduino Nano.
Power the motor driver and the motor with an external power source, 9V battery.
27
University of Juba-School of Engineering & Architecture
motorPin1=3 - Assigns digital pin 3 to control one side of the motor while
motorPin2=4 – which assigns digital pin 4 to control the other side of the motor. The
potentiometerPin= A0 assigns analog pin A0 to read the potentiometer value
In the setup Function, void setup (), Serial.begin(9600) which initializes serial
communication at 9600 baud rate for debugging/monitoring is used.
Serial.println(z) prints the mapped PWM value to the serial monitor for debugging
while analogWrite(motorPin1,z) writes the PWM signal to motorPin1 to control motor speed
and since motorPin2 is held LOW, this creates a voltage difference that makes the motor spin
in one direction
Connect the Arduino Nano to the computer via USB and upload the code using the
Arduino IDE after verifying the code. Then manually rotate the potentiometer observing the
motor speed changing accordingly.
28
Chapter Four: Results and Discussion
Connect the 5V from the motor driver to the 5V of the Arduino nano inside a
breadboard
Connect the GND from the motor driver to the GND on the Arduino Nano inside
breadboard
Connect the +12V from the motor driver to the Vin of the Arduino Nano inside a
breadboard
Connect the `IN3` and `IN4` pins of the L298N motor driver to digital pins (D3 and
D4) on the Arduino Nano.
Connect the motor terminals to the output terminals OUT3 and OUT4 of the L298N
motor driver.
Connect the ENA pin of the motor driver to the D9 on the Arduino nano
Power the motor driver and the motor with an external power source, 9V battery.
int enablePin=9; // ENA - PWM pin for speed control (enable pin of H-bridge)
motorPin1 and motorPin2 control the motor's direction by setting different IGH/LOW
combinations
enablePin controls the motor speed using PWM (Pulse Width Modulation)
Setup Function
29
University of Juba-School of Engineering & Architecture
void setup() {
Main Loop
digitalWrite(motorPin2, LOW);
30
Chapter Four: Results and Discussion
digitalWrite(motorPin2, HIGH);
The sensor used is a magnetic incremental encoder directly attached to the motor
shaft. This encoder has a disc with magnetic poles and two inductive sensors mounted in
quadrature, generating square signals shifted by a quarter of a period as the disc rotates.
The detection of rising and/or falling edges allows determining the number of pulses per
revolution or the motor's rotation direction.
31
University of Juba-School of Engineering & Architecture
The calculation of the error and the control voltage will be done by the Arduino board. To
drive the 12V DC motor, a power interface board in the form of an H-bridge, built around an
L298, will be used. [24]
The encoder works by observing changes in the magnetic field generated by a magnet
attached to the motor shaft. The engine rotates the encoder outputs periodically. If we select
encoder Pin A before encoder Pin B, the encoded DC motor will rotate clockwise (CW). On
the other hand, if we select the Pin B encoder before the Pin A encoder, the encoded DC
motor will rotate in a counterclockwise direction (CCW).
The ground pin (Black Wire) of the encoder connects to Arduino GND while the red and
white wires on the encoder connects to the output terminals OUT3 and OUT4 of the L298N
motor driver.
The encoder A (Yellow) connects to pin D2 of Arduino. Pin D2 from Arduino will record
each time there is an ascending digital signal from Encoder A. The B encoder (Green Wire)
connects to pin D3 of Arduino. The signal that is read from pin D3 on Arduino.
The VCC pin (Blue Wire) of the motor connects to the 5V pin in Arduino. This pin is
responsible for providing power to the encoder program.
IN1 (Pin 7) & IN2 (Pin 6): Direction control (HIGH/LOW combinations)
Encoder:
32
Chapter Four: Results and Discussion
ENCA triggers an interrupt on every rising edge (each time the motor moves).
Serial.println("Target_Velocity,Velocity_Motor_A");
float targetVel = 100 * sin(millis() / 1000.0); // Oscillates between -100 and 100
The PID controller adjusts motor power to match the target speed.
If the motor is too slow, it speeds up; if too fast, it slows down.
The encoder counts "ticks" per second which are converted to speed.
Serial.print(","); // Separator
34
Chapter Four: Results and Discussion
4.4 Results
In the open loop control using potentiometer and PWM, motor speed varied
proportionally with potentiometer position.
The response time was fast especially with PWM adjustments while there was no overshoot
since there was no feedback.
Controlling the Speed of a DC Motor using PWM allows precise adjustment for
optimal performance. Feedback mechanisms, such as encoders, ensure desired speed is
maintained by comparing it with the actual speed.
Before entering the values of Kp (proportional gain), Ki (integral gain), and Kd (derivative
gain), it was important to consider the desired performance and characteristics of the
motor. These parameters determine how the controller responds to changes in speed, errors,
and disturbances. By tuning these values appropriately, the motor's speed control system can
achieve stability, responsiveness, and optimal performance.
35
University of Juba-School of Engineering & Architecture
By observing the motor's speed control system without PID parameters, we gathered
valuable insights such as response time, steady-state error, and sensitivity to disturbances.
This information served as a baseline for evaluating the effectiveness of the PID tuning
process.
Figure 4.14: DC motor speed control after inputting the PID values
36
Chapter Four: Results and Discussion
Before entering PID values, the engine speed control system presents some
limitations. I experienced slower response time, higher steady state error, and difficulty
maintaining the desired speed under various conditions. However, after entering appropriate
PID values (Kp, Ki, and Kd), remarkable improvements in system performance can be
observe
After tuning the PID parameters, the motors demonstrate faster response time,
reduced steady- state error, and improved ability to maintain the required speed under
different conditions. The control system becomes more robust and adaptive, effectively
compensating for disturbances and achieving better stability.
Kp = 2.5
Ki = 0.8
Kd = 0.3
4.4.3 Discussion
The open-loop system showed poor load regulation as expected. Without feedback, the
controller couldn't compensate for speed variations caused by changing loads. This method is
only suitable for applications where precise speed control isn't required and loads remain
constant.
The PID controller significantly improved speed regulation under varying loads. The integral
term eliminated steady-state error, while the derivative term helped reduce overshoot during
setpoint changes. Tuning was critical, excessive integral gain caused oscillations, while too
much derivative gain made the system sluggish.
Manually connecting the components was tedious and sometimes the results wouldn’t
be satisfactory.
37
University of Juba-School of Engineering & Architecture
38
Chapter Five: Conclusion and Recommendations
CHAPTER FIVE
5.1 Conclusion
In conclusion, this project has successfully tackled the position and speed control of a
DC motor equipped with an encoder, using the L298 driver and Arduino nano. Thanks to the
methodology described in the previous chapters, we have achieved significant results and
made valuable contributions to the field of automation and robotics.
The project began with introduction of the topic itself then complete understanding of the
hardware components involved, including DC motors, encoders and the L298 motor driver.
By integrating these elements with Arduino nano, we were able to develop a robust control
system capable of precise, reproducible movements.
The modelling and control techniques implemented in this project proved effective in
achieving the desired performance. The mathematical models derived in Chapter 2 provided a
solid basis for the development of the control algorithms. Through careful tuning and testing,
we were able to achieve precise position control and accurate speed regulation.
This project paves the way for various applications in the fields of automation and robotics.
Precise control of motor position and speed can be exploited in industrial automation, robotic
arm control, numerically controlled machines, etc. The control system developed can be
enhanced and adapted to different motor configurations and applications.
In conclusion, this project has provided a complete solution for controlling the speed of a DC
motor with potentiometer, PWM and an encoder using the L298 driver and Arduino. Overall,
PID control is a critical tool in maximizing the capabilities of DC motors, resulting in
efficient and reliable operation in various applications.
39
University of Juba-School of Engineering & Architecture
5.2 Recommendations
This project opens up several promising avenues for future research and development
in the field of DC motor position and speed control with encoders using the L298 driver and
Arduino. Here are some potential avenues to explore:
Manually mounting the components each and every time together which was the
challenge and it can be improved by using a Printed Circuit Board (PCB) which is a
flat board made of insulating material (like fiberglass or plastic) with thin layers
of copper traces that connect electronic components without messy wires.
Optical Encoder with Higher PPR (e.g., 500 PPR) compared to our 210RPM for more
precise speed feedback.
Advanced control techniques: Further research can be carried out into advanced
control techniques such as adaptive control, neural networks or machine learning
algorithms to improve system performance, robustness and adaptability to different
operating conditions.
[1] Karapidakis, John Syllignakis & E. S., Automatic Speed control of a DC Motor using
40
Chapter Five: Conclusion and Recommendations
[2] Kumar, A., Sharma, R., & Singh, P., ''PWM - Based Speed Control of DC Motor Using
Arduino Nano.'', International Journal of Engineering Research & Technology (IJERT),
8(5), 123-127, 2019.
[3] Singh, R., & Patel, S. , ''Closed - Loop Speed Control of DC Mootr Using Arduino
Nano and PID Controller.'', Journal of Electrical Engineering 15(3), 45-52, 2020.
[4] Rahman, M., Islam, M., & Hossain, M., Implementation of PID Controller for DC
Motor Speed Control Using Arduino Nano., International Journal of Advanced Research
in Electrical, Electronics and Instrumentation Engineering, 10(2), 89-95, 2021.
[5] Gupta, S., Yadav, V., & Tiwari, R., Sensorless Speed Control of DC motor using Back
EMF Measurement with Arduino Nano., International Journal of Innovative Research,
in Science, Engineering and Technology, 7(4), 345-350, (2018).
[6] M, Banzi M. & Shiloh, Getting Started with Arduino, Maker Media, Inc, 2014.
[12] Ahmed, M. R., Electric Motors and Drives: Fundamentals, Types and Applications,",
Elsevier, 2010)..
[13] Banzi, M., & Shiloh, M., Getting started with Arduino, Maker Media, lnc., 2014.
[14] B.B. & Eddie Smigiel, Discover the constitution of servomotors, Strasbourg, 2019.
[15] - Mohammed, S. M., Ramesh, P., & Kumar, R., Design of DC Motor Speed Control
System using Hall Effect Sensor, Journal of Electrical Engineering and Automation,
2(1), 15-20., 2020.
[16] - Rao, P. S., Bala, M. R., & Reddy, R., Temperature Sensor Based Motor Control
41
University of Juba-School of Engineering & Architecture
[17] Https://www.electronics-tutorials.ws/blog/pulse-width-modulation.html.
[18] B. W. Bequette, ., Process control: modeling, design, and simulation, Prentice Hall
Professional, 2003.
[19] Hote, U. Pande and Y. V, "Real time Design of PID Controller for Process Control
System, International Conference on Smart Technologies in Computing, Electrical and
Electronics (ICSTCEE), 2020.
[21] Farouk Zouari, et.al.,, “Ädaptive Internal Model Control of a DC Motor Drive System
Using Dynamic Neural Network", Journal of Software.
[22] Kaya, Ibrahim, ''IMC based automatic tuning method for PID controllers in a smith
Predictor configuration, Science Direct, 2004.
[23] ]https://plcynergy.com/pid-controller/.
[24] https://www.informatique-et-electronique.fr/index.php/electronique-tutos/
asservissementde-position-d-un-moteur-a-courant-continu.
[25] B. C. &. G. F. Kuo, Automatic Control Systems, John Wiley & Sons, 2009 .
[26] W. L. Zha J. Z., Dynamic Simulation of Double Closed Loop Speed-adjustment System
for DC Motor, [J]. Digital Technology and Application, 2022.
[28] Dejan, Arduino DC Motor Control Tutorial – L298N | PWM H-Bridge, [Online].
Available: https : / / howtomechatronics . com / tutorials /, 2017.
42
Chapter Five: Conclusion and Recommendations
43
Appendices
Appendices
int motorPin1=3;
int motorPin2=4;
int potentiometerPin=A0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(motorPin1,OUTPUT); // Motor Pin 1
pinMode(motorPin2,OUTPUT); //Motor Pin 2/ digitalWrite(4,LOW);
//Normally low in this pin
digitalWrite(motorPin2, LOW);
pinMode(potentiometerPin,INPUT);
}
void loop() {
// put your main; code here, to run repeatedly:
int s=analogRead(potentiometerPin);
int z=map(s,0,1024,0,255);
Serial.println(z);
analogWrite(motorPin1,z);
}
44
Appendices
void setup() {
// put your setup code here, to run once:
pinMode(motorPin1,OUTPUT); // Motor Pin1
pinMode(motorPin2,OUTPUT); // Motor pin2
pinMode(enablePin, OUTPUT); //Enable A
}
void loop() {
// put your main code here, to run repeatedly:
Serial.begin(9600);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
for(int pwmValue=0; pwmValue <=255; pwmValue++){
analogWrite(enablePin, pwmValue); //set the speed of the motor ;
Serial.println(pwmValue);
delay(100); //wait for 1000 milliseconds
}
45
University of Juba-School of Engineering & Architecture
46
Appendices
#include <util/atomic.h>
void setup() {
Serial.begin(9600);
pinMode(ENCA, INPUT);
pinMode(ENCB, INPUT);
attachInterrupt(digitalPinToInterrupt(ENCA), readEncoder, RISING);
void loop() {
// Set target velocity (RPM or arbitrary units)
// Using sine wave for demonstration (range -100 to 100)
float targetVel = 100 * sin(millis() / 1000.0); // 1 second period
47
University of Juba-School of Engineering & Architecture
48
Appendices
// Motor direction
int dir = 1;
if (u < 0) dir = -1;
void setMotor(int dir, int pwmVal, int pwm, int in1, int in2) {
analogWrite(pwm, pwmVal);
if (dir == 1) {
digitalWrite(in1, HIGH);
49
University of Juba-School of Engineering & Architecture
digitalWrite(in2, LOW);
} else if (dir == -1) {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
} else {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
}
void readEncoder() {
int b = digitalRead(ENCB);
if (b > 0) {
posi++;
} else {
posi--;
}
50