0% found this document useful (0 votes)
24 views59 pages

Embedded Thesis

This document appears to be a thesis submitted by Trinh Vi Hao to Ton Duc Thang University in Ho Chi Minh City, Vietnam in 2022. The thesis investigates speed and position control of a DC motor using an Arduino, motor driver, and encoder. MATLAB and Simulink are used to build and simulate the control system model. A PID controller is designed and tuned using the System Identification and PID Tuner Toolboxes. The motor's speed is controlled using PWM signals from the Arduino, and its position is obtained from the encoder. Data from the physical system is communicated to MATLAB using serial communication.

Uploaded by

ht250101
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)
24 views59 pages

Embedded Thesis

This document appears to be a thesis submitted by Trinh Vi Hao to Ton Duc Thang University in Ho Chi Minh City, Vietnam in 2022. The thesis investigates speed and position control of a DC motor using an Arduino, motor driver, and encoder. MATLAB and Simulink are used to build and simulate the control system model. A PID controller is designed and tuned using the System Identification and PID Tuner Toolboxes. The motor's speed is controlled using PWM signals from the Arduino, and its position is obtained from the encoder. Data from the physical system is communicated to MATLAB using serial communication.

Uploaded by

ht250101
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/ 59

VIETNAM GENERAL CONFEDERATION OF LABOR

TON DUC THANG UNIVERSITY


FACULTY OF ELECTRICAL AND ELECTRONIC ENGINEERING

TRINH VI HAO

SPEED AND POSITION CONTROL OF


DC MOTOR

EMBEDDED SYSTEM THESIS


CONTROL AND AUTOMATION
ENGINEERING

HO CHI MINH CITY, 2022


VIETNAM GENERAL CONFEDERATION OF LABOR
TON DUC THANG UNIVERSITY
FACULTY OF ELECTRICAL AND ELECTRONIC ENGINEERING

TRINH VI HAO – 419K0002

SPEED AND POSITION CONTROL OF


DC MOTOR

EMBEDDED SYSTEM THESIS


CONTROL AND AUTOMATION
ENGINEERING

Lecturer
PhD. Tran Duc Anh Minh

HO CHI MINH CITY, 2022

i
ACKNOWLEDGEMENT

I would like to express gratitude to Mr. Minh for teaching and imparting
knowledge and experience so that I could complete the project and, more
importantly, equip myself with the essential knowledge and abilities useful in the
future. In addition, I would want to show my thankfulness to the school's
management as well as the instructors who worked tirelessly to ensure that I
completed the course successfully. I would like to express my gratitude to my
parents, who reared me and provided the opportunities for me to study at Ton Duc
Thang University.
I wish everyone of the teachers on the Board of Directors good health and
many accomplishments in the teaching. I wish you and your family good health,
happiness, and plenty of joy. Wish Ton Duc Thang University will always be the
first and most dependable choice for many parents and students eager to begin their
university careers.
Sincerely thanks!

Ho Chi Minh City, May 2nd,2022


Author

Trinh Vi Hao

ii
The thesis was carried out at Ton Duc Thang University
Advisor: ........................................................................................
........................................................................................
(Title, full name and signature)

This thesis is defended at the Undergraduate Thesis Examination Committee was


hold at Ton Duc Thang University on… /…/……

Confirmation of the Chairman of the Undergraduate Thesis Examination Committee


and the Dean of the faculty after receiving the modified thesis (if any).

CHAIRMAN DEAN OF FACULTY

…………………………. ………………………………

iii
THE THESIS HAS BEEN ACCOMPLISHED
AT TON DUC THANG UNIVERSITY

I hereby declare that this thesis was carried out by myself under the
guidance and supervision of ……………………………..; and that the work
contained and the results in this thesis are true and have not been either
submitted anywhere for any previous purpose or published in any other
literature. The data and figures presented in this thesis are for analysis,
comments, and evaluations from various resources by my own work and have
been fully acknowledged in the reference part.
In addition, other comments, reviews and data from other authors, and
organizations used in this thesis have been acknowledged, and explicitly
cited.
I will take full responsibility for any fraud detected in my thesis. Ton
Duc Thang University is unrelated to any copyright infringement caused on
my work (if any).

Ho Chi Minh City, May/2022


Student

Trinh Vi Hao

iv
v
TON DUC THANG UNIVERSITY THE SOCIALIST REPUBLIC OF VIETNAM
FACULTY OF ELECTRICAL – Independence – Freedom - Happiness
ELECTRONIC ENGINEERING ----------------------
-------------------

THESIS IMPLEMENTATION MONITORING SHEET


Embedded System Thesis / Semester 2 (2021 – 2022)

Full Name : Trinh Vi Hao Student’s ID: 419K0002


Major : Control and Automation Engineering Phone : 0345620776
Thesis topic : Speed and Position control of DC motor

Amount of Work Instructor’s


Week/Date
Already Done Continue Working Signature

1 Speed and Position


Choosing Topic
21/02/2022 control of DC motor
Choose appropriate
2 Look up for “Data-driven
hardware (MCUs, Motors,
28/02/2022 Control” from MATLAB
Drivers,…)

3 Read and Write from Encountered Bugs and


7/03/2022 Arduino to MATLAB Flaws found and fixed

4 Building a simulation Run the Motor in Open-


14/03/2022 model of DC motor loop through Simulink
Find out how to read
5 Encountered Bugs and
signals from Encoder
21/03/2022 Flaws found
(attached)
Problems with the

6 encoder, purchase a new Still, bugs and errors are


28/03/2022 one, use different found. Arduino’s problem.
methods

7 Bugs from Arduino’s Data from Encoder read


4/04/2022 fixed, model runs fine successfully to MATLAB

vi
8 System Identification, PID’s controller errors
11/04/2022 PID, Close-loop system fixed. Testing.
9 Building the model base Complete the Report
18/04/2022
10 Final Checking report Submit
25/04/2022

vii
SPEED AND POSITION CONTROL OF DC MOTOR
ABSTRACT

Using Arduino module UNO R3 and L298N Motor Driver module to control
speed and position of unknown parameters DC Motor, the model is built,
monitor and simulate with MATLAB and Simulink. MATLAB and Simulink
System Identification Toolbox and PID tuner is used to configure a PID
Controller. Speed of the motor is controlled by using PWM. The host computer
is where the model is controlled, Arduino board and the host computer
communicating through Serial Communication Protocol. With MATLAB, this
can be done by running the model on “External Mode”, the motor is running
physically and it’s data (Speed and Position) is displayed on the host computer.

viii
CONTENTS

LIST OF FIGURES ........................................................................................................ XI

ABBREVIATIONS ...................................................................................................... XIV

CHAPTER 1.INTRODUCTION ...................................................................................... 1

1.1 REASON FOR CHOOSING TOPIC .................................................................................... 1


1.2 TARGET IMPLEMENTATION ......................................................................................... 1
1.3 OBJECT AND SCOPE OF THE STUDY .............................................................................. 2
1.4 RESEARCH METHOD .................................................................................................... 2
1.5 PRACTICAL SIGNIFICANCE .......................................................................................... 3

CHAPTER 2.THEORETICAL BASIS. .......................................................................... 4

2.1 CONTROL SYSTEM ...................................................................................................... 4


2.1.1 Discrete-time transfer function ........................................................................... 4
2.1.2 PID Controller .................................................................................................... 7
2.1.3 PWM .................................................................................................................... 7
2.2 ENCODER SIGNAL PROCESSING ................................................................................... 9
2.2.1 RPM..................................................................................................................... 9
2.2.2 Position ............................................................................................................... 9
2.2.3 Direction control ................................................................................................. 9
2.2.4 Interrupts ........................................................................................................... 10
2.3 MATLAB AND SIMULINK ........................................................................................ 10
2.3.1 System Identification Toolbox ........................................................................... 11
2.3.2 PID Tuner.......................................................................................................... 11

CHAPTER 3.RESEARCH METHOD .......................................................................... 13

3.1 SIMULATION MODEL ................................................................................................ 13


3.1.1 Components ....................................................................................................... 13
3.1.2 Building the Model ............................................................................................ 19
3.2 SIMULATION RESULTS .............................................................................................. 38

ix
CHAPTER 4.DATA ANALYSIS ................................................................................... 41

4.1 COMMENTS ............................................................................................................... 41

CHAPTER 5.CONCLUSION ......................................................................................... 42

5.1 CONCLUSION ............................................................................................................ 42


5.1.1 Conclusion 1...................................................................................................... 42
5.1.2 Conclusion 2...................................................................................................... 42
5.2 THESIS DEVELOPMENT .............................................................................................. 43

REFERENCES................................................................................................................. 44

x
LIST OF FIGURES

2-1 SAMPLING,QUANTIZATION AND CODING OF AN ANALOG SIGNAL ..... 5

2-2 DISCRETE-TIME TRANSFER FUNCTION ..........................................................6

2-3 G(Z) REPRESENTED BY DIFFERENCE EQUATION ........................................ 6

2-4 UNIT CIRCLE IN Z-PLANE..................................................................................... 6

2-5 PID CONTROLLER EQUATION ........................................................................... 7

2-6 PWM DUTY CYCLE AND IT’S OUTPUT VOLTAGE ........................................ 8

2-7 MATLAB AND SIMULINK SUPPORT PACKAGE FOR ARDUINO


HARDWARE ................................................................................................................... 10

2-8 SYSTEM IDENTIFICATION INTERFACE ......................................................... 11

2-9 PID TUNER INTERFACE ...................................................................................... 12

3-1 ARDUINO S.R.L, “UNO R3 DATASHEET”, 2021 ............................................... 13

3-2 ARDUINO UNO R3 BOARD ................................................................................... 14

3-3 L298N MODULE, MANUFACTURED BY “MINH HA” ....................................15

3-4 GA25-370 DC MOTOR ............................................................................................ 15

3-5 ENCODER CONNECTION PINS .......................................................................... 16

3-6 18650 LITHIUM-ION CELLS ................................................................................. 16

3-7 COMPLETE CIRCUIT ............................................................................................ 17

3-8 VOM MEASURED VOLTAGE .............................................................................. 18

3-9 MODEL CONFIGURATION PARAMETERS ..................................................... 19

3-10 RUN ON TARGET MODE .................................................................................... 20

3-11 FLOWCHART OF OPEN-LOOP SYSTEM ........................................................ 21

3-12 OPEN-LOOP SYSTEM BLOCKS ........................................................................ 22

3-13 SPEED MEASUREMENT ..................................................................................... 22

3-14 INSIDE THE SUBSYSTEM................................................................................... 23

xi
3-15 MATLAB FUNCTION BLOCK ............................................................................ 23

3-16 MOTOR RUNS AT 100% PWM ........................................................................... 24

3-17 VOLTAGE DROP ON MOTOR DRIVER MODULE ....................................... 25

3-18 FROM PWM TO VOLTAGE BLOCK ................................................................ 26

3-19 COMPLETE OPEN-LOOP MODEL ................................................................... 26

3-20 “LOGGING” PANEL INSIDE SCOPE’S PARAMETER.................................. 27

3-21 DATA1 AND DATA2 AS LOGGED DATA FROM SIMULINK ...................... 27

3-22 DATA SIGNALS ARRAYS.................................................................................... 28

3-23 IMPORT DATA PANEL ........................................................................................ 28

3-24 IMPORTED DATA NAMED “Z1” AND “Z2” AND THEIR TIMEPLOT ......29

3-25 ESTIMATE TRANSFER FUNCTION ................................................................. 29

3-26 ESTIMATED TRANSFER FUNCTION .............................................................. 30

3-27 FLOWCHART OF PID TUNER ........................................................................... 31

3-28 TRANSFER FUNCTION STEP RESPONSE TO TUNE PID CONTROL ......32

3-29 PID BLOCK PARAMETER .................................................................................. 32

3-30 SYSTEM RESPONSE WITH PI TUNED ............................................................33

3-31 FINAL PARAMETERS .......................................................................................... 33

3-32 FLOWCHART FOR CLOSED-LOOP FEEDBACK .......................................... 34

3-33 COMPLETE MODEL OF CONTROL SYSTEM ............................................... 35

3-34 CONTROL SIGNAL SUBSYSTEM ..................................................................... 35

3-35 HARDWARE SUBSYSTEM.................................................................................. 36

3-36 MATLAB FUNCTION FOR CONTROLLING THE MOTOR ........................ 36

3-37 SENSOR SUBSYSTEM .......................................................................................... 37

3-38 SETPOINT 20RPM ................................................................................................. 38

3-39 SETPOINT 60RPM ................................................................................................. 38

3-40 SETPOINT 40RPM ................................................................................................. 39

xii
3-41 SETPOINT 80RPM ................................................................................................. 39

3-42 SETPOINT 100RPM ............................................................................................... 40

3-43 SCOPE GRAPH ...................................................................................................... 40

xiii
ABBREVIATIONS

PPR Pulses per Revolution


PID Proportional, Integral, Derivative
PWM Pulse-width modulation
BWD Backward
FWD Forward
PWR Power

xiv
EMBEDDED SYSTEM THESIS
Page 1
_______________________________________________________________________________

CHAPTER 1. INTRODUCTION

1.1 Reason for choosing topic

MATLAB and Simulink are potential programs in the field. They interact with
each other so well that is convenient for users such as simulation the model and
save logged data into workspace in MATLAB, and from that data, users can process
any way they want with it. The later versions of MATLAB have a package apps
built for Arduino programming and Simulink, too. Controlling DC motor with
Arduino through MATLAB and Simulink can help building the model, simulate,
control and log the feedback or simulation data to the workspace at the same time.
Choosing the topic will not only having myself to get used to Simulink and
MATLAB but also knowing the fundamental properties as well as working
principles of a DC motor. By saying that I mean, from this model, I can evaluate it
into controlling small RC cars, robots or robot arms with different kinds of motors.

1.2 Target Implementation

Controlling DC motor with MATLAB has a variety of ways, in this topic, I will
use Data-driven control method. This method is recommended when the plant – DC
motor – parameters, such as, are unknown. Use open loop and sensors to log the
simulation data of the plant to workspace, then use MATLAB built-in toolboxes to
estimate the plant transfer function. Having the transfer function allows us to
configure the parameters of PID controller to put in the closed-loop system. With
setpoint is the input by user, the plant will work as accurate as the transfer function
fitted in.

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 2
_______________________________________________________________________________

1.3 Object and scope of the study

The object of this topic is to both study on programming Arduino with


MATLAB and simulating the model with Simulink. MATLAB and Simulink
provide “MATLAB Support Package for Arduino Hardware” and “Simulink
Support Package for Arduino Hardware” which includes functional libraries built-in
for Arduino. Also, DC motors are still common used in electricmechanical
engineering, they are suitable for small research with simple construction.
Having said how wide applications MATLAB and Simulink brought, however,
in this topic, I will only focus on using System Identification Toolbox™ for
estimating transfer function from logged data from open-loop model with Run on
Target hardware on the Arduino. Having estimated transfer function, use PID tuner
app from Simulink to get P,I and D to put it in the PID controller . The plant is then
run with 5-10% error in comparison with the setpoint. For the motor to feedback
signal, I will use Hall-sensor Encoder, signals from Encoder is calculated with
speed – in RPM – formula and feedback to the controller. Controlling the speed by
using PWM to adjust the voltage provided to the motor through a Motor Driver
module using IC L298N. Encoder signals also provide the motor’s shaft position in
degree.

1.4 Research method

Combining resources from learned theory courses of Control System and


MATLAB programming courses with MATLAB Documentations that cover this
topic, also hardware’s datasheets, specifically, Arduino module and L298N module.
DC motor’s datasheet is not required since this controlling method is for unknown
plant. With the topic using mostly MATLAB, researching on MATLAB
Documentation for functions, commands, blocks and toolboxes are helpful.
Checking on Arduino’s forum on how Arduinos are programmed also helps. Bugs
are inevitably, searching for previous bugs encountered fixing experience from both
MATLAB and Arduino forum is somewhat convenient to complete the project.

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 3
_______________________________________________________________________________

1.5 Practical Significance

As mentioned DC motors are widely used in different fields, although they’re


slowly being replaced by BLDC motors. Studying on how to to control it via
MATLAB will equips myself with deeper knowledge on MATLAB capabilities and
how actually DC motors in robotics, automation are used and controlled. Also, this
model can be upgraded to control small RC cars, robot arms, or use a different kinds
of motors to control like stepper motors, servo motors to construct machines like
small 3D printers, CNC-alike machines for wood processing or mimic a conveyer
belt like in factories.

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 4
_______________________________________________________________________________

CHAPTER 2. THEORETICAL BASIS.

2.1 Control System

Control System is controller, which controls a controlled system, a plant or a


process. A controller controls a plant with transfer function, which in this topic, the
transfer function is needed to control the DC motor speed. To control the DC motor
as exact as we want, a feedback control system – closed loop system, which
affected by the signals from the sensor feedback to the controller, the controller will
process the signals (error) to see if the plant meets the setpoint or not to output an
appropriate signal to control the plant. In this project, I will use a digital control
system as Arduino UNO R3 is a digital hardware, as well as, since the signals from
encoder are digital, the transfer function will be estimated as discrete-time transfer
function.

2.1.1 Discrete-time transfer function

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 5
_______________________________________________________________________________

A discrete-time transfer function or overall the Digital control system


processes digital signal. A common way to achieve digital signal from analog
signal is to record analog signal sample values at equally spaced instants of time.

2-1 Sampling,quantization and coding of an analog signal

The digital words are the outputs of the A/D converter, which is Analog-
Digital converter, which input is a Continuous-time signal. In reverse, a D/A
converter is used when digital signal is needed to convert to analog signal.
To describe the digital control system, we need a discrete-time transfer
function which is need z-transform, or discrete-time transfer function is
continuous-time transfer function written in z-plane.

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 6
_______________________________________________________________________________

2-2 Discrete-time Transfer function

2-3 G(z) represented by difference equation

The terminology used in connection with G(s) is also directly applicable in the
case of G(z). The order of G(z) is determined by the denominator polynomial of
G(z), the roots of denominator are called poles, and the roots of numerator are
called zeros. The stability is defined if the poles of the transfer function lie inside
the unit circle.

2-4 Unit circle in z-plane

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 7
_______________________________________________________________________________

2.1.2 PID Controller

PIDs output a control signal to a plant from errors feedback from the sensors,
that is, makes the plant work accurately. It can be either only P controller, PI
controller or PID controller, which PID controller is faster than others. A PID
has tunable parameters Kp, Ti, and Td.

2-5 PID controller equation

2.1.3 PWM

Pulse-width Modulation is used, in this case, to control the output voltage


from the supply source, by adjusting the duty cycle of the pulse generator from 0
– 100% (which is mapped from 0 – 255), the output voltage should be average
voltage from on-off period of the pulses.

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 8
_______________________________________________________________________________

2-6 PWM Duty cycle and it’s output Voltage

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 9
_______________________________________________________________________________

2.2 Encoder signal processing

In this topic specifically, the Hall-effect sensor encoder is used to feedback


the signal of the DC motor. The encoder shaft is connected directly to output
shaft of the rotor, which is the integrated circuit of the encoder is attached to the
the back of the motor. Chosen encoder has 11 pulses per revolution (PPR), as
it’s connected to the rotor shaft, not the main output shaft, so PPR must be
multiplied with the gear ratio to get the right output PPR for calculating the
RPM. The motor used in this project has a gear ratio of 1:45, that is for 45
revolutions of the rotors, the main shaft turns 1 revolution. So the output pulses
is encoder PPR multiply with 45 (gear ratio) equals to 495 per revolution of the
motor.

2.2.1 RPM

RPM is rounds of the motor output per minute, it’s calculated by the rated of
change in position divided by the rate of change in time in seconds and multiply
by 60 as there are 60 seconds in 1 minute.

2.2.2 Position

Position monitoring of the motor is simpler than the RPM as it’s calculated
by counting the output PPR and multiply with 360 to gives the position of the
motor in degrees, as 360 is the degree per revolution.

2.2.3 Direction control

Encoder used for DC motor usually has dual channel outputs, often marked
as Channel A and Channel B, the two channels output pulses in 90 degree phase
difference, one comes after another, channel B comes after channel A if the
motor spins forward or clockwise direction, and channel A comes after channel
B if the motor spins backward or counter-clockwise direction.

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 10
_______________________________________________________________________________

2.2.4 Interrupts

For the digital hardware to process the signals from the encoder, interrupt
pins are required. For the UNO, pin 2 and pin 3 are the suitable for this task, for
whenever there is a RISING edge of encoder signal, these pins will detect and
call a subsystem to process the signals, Simulink has a built-in blocks for these
tasks as well.

2.3 MATLAB and Simulink

To build the model, Simulink is mostly used over MATLAB, as MATLAB is


only used to store the data in the workspace or to call basic commands to
process the data. To use the Arduino with MATLAB and Simulink, specific
support package is required.

2-7 MATLAB and Simulink Support Package for Arduino Hardware

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 11
_______________________________________________________________________________

2.3.1 System Identification Toolbox

This toolbox is built-in MATLAB toolbox, interact well with Simulink


blocks. It’s a library with functions, Simulink blocks and provide an app to
estimate transfer functions from logged data. In this topic where plant is
unknown, this toolbox is a game-changing tool. Access this toolbox by typing
command “systemIdentification”.

2-8 System Identification Interface

2.3.2 PID Tuner

Like the System Identification Toolbox, the PID Tuner is available when
there’s a transfer function of the model.

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 12
_______________________________________________________________________________

2-9 PID Tuner Interface

There’s “Type” panel for choosing either P, PI or PID, and a slider to


monitor the step response of the model plotted in below. The control parameters
then can be choose to export to the workspace or directly to the PID block in
Simulink.

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 13
_______________________________________________________________________________

CHAPTER 3. RESEARCH METHOD

3.1 Simulation Model

3.1.1 Components

3.1.1.1 Arduino UNO R3 Board

A popular Arduino board uses AVR 16ATMega328P

3-1 Arduino S.r.l, “UNO R3 Datasheet”, 2021

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 14
_______________________________________________________________________________

3-2 Arduino UNO R3 Board

3.1.1.2 L298N Motor Driver V3

Also a popular Motor Driver module, uses IC L298 to drive the motor
Configuration :
- Vs DC 5V ~ 35V
- I(max) 2A
- I(no-load) 10mA
- Control Pins INT1, IN2, INT3, INT4
- Motor Output OUT1, OUT2, OUT3, OUT4
- H-Bridge pins ENA, ENB

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 15
_______________________________________________________________________________

3-3 L298N module, manufactured by “Minh Ha”

3.1.1.3 GA25-370 DC Motor

A Brushed 12VDC Motor with 130RPM, attached with Hall-effect sensor


gear ratio is 1:45 and the resolution of encoder is 11 pulses per revolution.

3-4 GA25-370 DC Motor

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 16
_______________________________________________________________________________

3-5 Encoder Connection pins

3.1.1.4 INR18650-25R Lithium-ion Rechargeable Cell

- Nominal voltage 3.6V


- Nominal discharge capacity 2,500mAH
o Charge : 1.25A, 4.20V,
Used 3 cells with voltage of 11.8V to power the motor through the Motor Driver
module.

3-6 18650 Lithium-ion Cells

3.1.1.5 Wiring the Model

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 17
_______________________________________________________________________________

This model requires digital connection only. Firstly, to control direction,


connect INB, INA from L298N module to pin 5, pin 6 of Arduino. For PWM
signal, remove the jumper in the L298N module, then connect ENA to pin 11
(one of the PWM signal output pins of UNO R3). For Encoder feedback
signal, connect Encoder A (yellow) to pin 2 on Arduino, and Encoder B
(green) to pin 3 on Arduino. Output from 3 cells combined will apply to the
L298N VCC and GND, the Motor (+) and (-) will connect to OUTA and
OUTB on the L298N. Finally, power the Motor driver module by connecting
+5V to the 5V source on the Arduino and GND to the GND accordingly,
also, supply Arduino’s 3.3V source to the Encoder (+) and GND to the
Encoder (-).

3-7 Complete Circuit

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 18
_______________________________________________________________________________

Note that, with 3 cells used, theoretically, I will have a 11.8V power
supply, however, practically, the voltage it’s only 10.8V measured with
VOM. So the voltage data that logged into MATLAB in Simulink will be
measured by VOM for accuracy.

3-8 VOM measured Voltage

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 19
_______________________________________________________________________________

3.1.2 Building the Model

After the Simulink support package for Arduino hardware is installed,


there’s a library in “Library Browser” for Arduino called “Simulink Support
Package for Arduino Hardware” contains Arduino supported Blocks,
configured only for Ardunno’s hardware. However, to actually interact with
Arduino through Simulink, it’s need to run Simulink in “Run on Hardware”
mode, but only after Arduino’s Hardware is configured in “Model
Configuration Parameters”

3-9 Model Configuration Parameters

In this project, UNO board is used, so hardware board is set to “Arduino


Uno”. For Simulink to recognize the board easily, set the “COM port number”

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 20
_______________________________________________________________________________

manually to wherever “COM” port your board is connected to your computer.


Simulink is now set up and ready for the Arduino to be host-controlled.

3-10 Run on Target mode

“Hardware” panel for “Run on Target” mode is now visible. Set “Mode” to
“Run on Board”, and “Monitor & Tune” to run the program and monitor it at the
same time.
System Identification Toolbox required datas from open-loop system to
estimated transfer function. The flowchart of the model is shown below

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 21
_______________________________________________________________________________

3-11 Flowchart of Open-loop system

First, control the model by sliding the PWM “Slider gain” which is also
connected to a constant “255” block in the Simulink model, the “Slider gain”
will decide the percentage from 0 to 1 (100%) of PWM to the “pin ~11”, which
is digital PWM output signal of the Arduino board. As seen from the flowchart,
Pin 2 outputs pulses read from the encoder, which is then processed by the
MATLAB function block to calculate the RPM. In the other branch, PWM
percentage levels (0, 0.25, 0.5, 0.75, 1) is then converted to referenced measured
output voltage at L298N’s motor output terminals. Both RPM and Voltage
outputs are connected to the scope to be logged into the MATLAB workspace.

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 22
_______________________________________________________________________________

Theoretically said, now I will build the model in Simulink to run the motor in
open-loop system by getting blocks in “Library Browser” and connected like the
figure below.

3-12 Open-loop System Blocks

The “Direction Cons” block is to set value to “1” for the Motor turns
Clockwise, ad set to “-1” for the Motor turns Counter-clockwise, the Direction
Constant is compared to 0, the ouput of the comparison is 1 if “TRUE” and 0 if
“FALSE” and both “FALSE” or 0 when the Direction constant is 0 to stop the
Motor.
“PWM Constant” is set to 255 as PWM only takes value from 0 – 255, what
control the value of PWM is the slider gain “PWM Control” map the value from
0 to 100%. Now the motor is ready to run in both directions and various speeds.

3-13 Speed Measurement

Add blocks to calculate the speed from encoder. For speed measurement,
only a channel is needed as there’s no need another one to feedback direction.
“External Interrupt” block is needed to read signals from encoder as mentioned

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 23
_______________________________________________________________________________

before, the IRQ output of this block should be connected to a “Function-Call


Subsystem” – a function is called whenever a RISING edge from encoder is
detected.

3-14 Inside the Subsystem

Whenever the function is called, the initial value becomes “1” and output to
the “pulse” output port, while also save the previous values to be added up later
with “Unit Delay” port, which remembers 1 value and save it for the set
sampling time. This is also called the counter-up function commonly used in
Simulink to count pulses from the encoder. The “Digital Clock” is the real

3-15 MATLAB Function Block

simulation time, which automatically counts up in the matching the simulation


time. The MATLAB function block takes 4 inputs, the total pulses, delayed
pulses, digital time, delayed digital time to calculated the delta time and delta
pulses inside the MATLAB function

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 24
_______________________________________________________________________________

The Function block output DeltaPulse and DeltaTime, these are then divided
and go through the “Pulses to RPM” gain block to output the RPM, visualized
by the Scope and Display.
Now, the voltage applied is needed to log input-output data. However, the
percent of PWM is not proportional to Voltage, so for each percent step 25%
from 0 to 100%, I will measure the output voltage realistically with the VOM
and write a MATLAB Function to comparison PWM from slider gain to output
measured Voltage.

3-16 Motor Runs at 100% PWM

It can be seen that at even 100%, the motor can only runs at 103RPM
compared to it’s specification is 130RPM, this is due to voltage drop on Motor
Driver module. As measured with VOM, with 100% PWM and a 10.8V power
supply, it can only output 9.23V to the Motor, which is ~1.6V dropped.

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 25
_______________________________________________________________________________

3-17 Voltage drop on Motor Driver Module

To deal with this problem, I measured the output voltage to the motor 4
times, at 25%, 50%, 75% and 100% PWM for accurate voltage – speed graph,
this is done by writing a MATLAB function like below.

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 26
_______________________________________________________________________________

3-19 From PWM to Voltage block

3-18 Complete Open-loop model

Additionally, our Simulink model will now look like this figure above.
Now the model is ready to log the data to the workspace. Go to the Scope’s
configuration to get access to “Logging” panel.

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 27
_______________________________________________________________________________

3-20 “Logging” panel inside Scope’s parameter

Check the “Log data to workspace” then name the “Variable name” freely, the
“Save Format” should be “Structure”.

3-21 Data1 and Data2 as Logged Data from Simulink

Here, the Logged Data from scope in Simulink is named Data1 for Datasets
of the first simulation, and Data for the Datasets of the second simulation.
There’s are 2 arrays inside each struct of Data. First array is named
“Data1.signals(1).values” stores “Voltage” and the second array is named
“Data1.signals(2).values” stores “RPM”, the Data2 values are the same.

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 28
_______________________________________________________________________________

3-22 Data Signals Arrays

Assign “x1”,”x2” for Data1 and Data2 “voltage” with command “x1 =
Data1.signals(1).values” and as same as x2. Continuously, for “y1” and “y2” for
Data1 and Data2 “RPM” as “y1 = Data2.signals(2).values”.
The datas are now ready to be put in “System Identification Toolbox”. Import
data in Time-domain time.

3-23 Import Data panel

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 29
_______________________________________________________________________________

3-24 Imported Data named “z1” and “z2” and their Timeplot

Set “z1” as “Working data” and “z2” as “Validation Data”. Set “Estimate”
panel to “Transfer Function Estimation”

3-25 Estimate Transfer Function

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 30
_______________________________________________________________________________

Set to estimate “Discrete-time” transfer function. For random poles and


zeros, I will set initial values as 5 poles and 5 zeros. Press “Estimate” to estimate
the transfer function.

3-26 Estimated Transfer Function

From the figure above, it can be seen that the estimated transfer function fit
up to 88%, which is acceptable. Drag “tf1” to “To Workspace” panel to export
the transfer function to Workspace.
Flowchart for the PID tuner is shown below

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 31
_______________________________________________________________________________

3-27 Flowchart of PID tuner

The constant block can be replaced by step response block just in case. Error
calculation block is where in this case equals to 1 (constant) subtract to the
feedback signal which is output signal of the transfer function.
The PID controller is ready to be tuned. Create a new Simulink model to tune
PID

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 32
_______________________________________________________________________________

3-28 Transfer Function Step Response to tune PID Control

Set “Linear Analysis Point” for transfer function input as “Input


Perturbation” and output as “Open-loop Output”.

3-29 PID block Parameter

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 33
_______________________________________________________________________________

Press “Tune” below to Tune the PID.

3-30 System Response with PI tuned

Use the slider gain in the upper of the panel to tune PID, in this project, I
only use PI controller as PID requires a stronger and faster host-computer.

3-31 Final Parameters

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 34
_______________________________________________________________________________

The PI block is now ready to put in the final model to control DC motor. The
following flowchart for the closed-loop system is illustrated below.

3-32 Flowchart for Closed-loop feedback

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 35
_______________________________________________________________________________

3-33 Complete model of Control System

With the PI is tuned, I can construct a complete closed-loop system.


Moreover, the output of the PI should be saturated from 0V to 9V to adjust the
control signal to the hardware accurately because maximum measured voltage
applied to the motor is 9.23V with 103RPM. So the slider gain “RPM set” is
only allow user to slide between 0 and 101RPM as 0.23V difference can affect
the output RPM.

3-34 Control Signal subsystem

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 36
_______________________________________________________________________________

The control signal process the output signal from PI controller and output a
control signal to control the Arduino pins. The gain is to convert PID output to
PWM signal “255/y(t)”.

3-35 Hardware subsystem

The Hardware subsystem is where the host computer communicate with the
Arduino, where Pin 11 is where PWM signal input, pin 5 and pin 6 is for
Clockwise and Counter-Clockwise direction control. The MATLAB function is
to compute the control signal, output the PWM (pwr) and direction (fwd, bwd).

3-36 MATLAB Function for controlling the Motor

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 37
_______________________________________________________________________________

The “abs()” function get the absolute value of u, which is PWM signal output.
The “status” gets input from “Toggle” button which “1” for Forward mode, and
“0” for Backward mode.

3-37 Sensor Subsystem

This block contain the sensor from Encoder, the subsystem inside is as same as
the blocks where I use to measure speed from open-loop system (figure 3-4).
The model is now ready to run. Press “Monitor and Tune” to run the motor.

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 38
_______________________________________________________________________________

3.2 Simulation results

3-38 Setpoint 20RPM

3-39 Setpoint 60RPM

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 39
_______________________________________________________________________________

3-40 Setpoint 40RPM

3-41 Setpoint 80RPM

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 40
_______________________________________________________________________________

3-42 Setpoint 100RPM

3-43 Scope Graph

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 41
_______________________________________________________________________________

CHAPTER 4. DATA ANALYSIS

4.1 Comments

As seen from figure 3-22 to 3-26 , Firstly ,the RPM setpoint is 20RPM, from the
feedback signal, the error display is 0.6 and the actual RPM is 19.39, this
number however fluctuate as the signal is digital, and the transfer function
estimate before only fits 88%. Set the speed from 20 to 100 (max speed) with
step size is 20 and observe through the scope to see how accurate the system is.
Sometime, the errors get negative or positive or even 0 error.

Graphically, figure 3-27, display a whole picture of how the system response to
the control signal. It can be seen that the transfer function estimated from the
beginning affect the whole process, choosing it with high fit percent can
construct a well-response controller.

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 42
_______________________________________________________________________________

CHAPTER 5. CONCLUSION

5.1 Conclusion

5.1.1 Conclusion 1

MATLAB and Simulink Toolboxes are helpful if used the right way. From the
unknown plant problem, I can still can find the transfer function of the controller
if the Input/Output data is logged carefully. Finding the best fit transfer function
from varying poles and zeros is quite challenging, however, the higher the
transfer function fits, the higher accuracy it would be in afterward construction.
PID controllers are common used in control system, however, while doing this
project, the PID mode seems to make the simulation to run slower, sometimes,
the motor runs but the host-computer does not show the feedback that is why PI
mode controller is chosen to run this system.

5.1.2 Conclusion 2

Modeling an Arduino on Simulink and MATLAB is easier than ever as they


have full support on it. Built-in libraries cover mast amount of application when
using Arduino. External mode for Arduino is convenient for both building and
simulating the model. However, the host-computer requires enough power
processer and memory to run complex model, even in this project, there is an
easier and shorter way to construct the model, however, the host-computer
configuration is not capable to do so. Not only that, MATLAB requires a
original Arduino board to run perfectly, using CH340 or third company types of
Arduino, can sometime cause trouble. The host-computer can not withstand
complex block like Simulink Encoder block, 2 external interrupt at a time,
makes it impossible to measure the position of the motor shaft accurately when
changing the direction.

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 43
_______________________________________________________________________________

5.2 Thesis development

With basis learned from embedded system basis, developing the model to
controlling a different kind of motor such as stepper, servo, etc. step by step
constructing machines like 3D printer or CNC resemblance at low-budget. Deep
learning on micro-controllers and coding on MATLAB and Simulink as there
are still a vast amount of fields that MATLAB and Simulink provide that I have
not mentioned.

Speed and Position Control of DC motor


EMBEDDED SYSTEM THESIS
Page 44
_______________________________________________________________________________

REFERENCES

English
Arduino S.r.l. (2021). Arduino® UNO R3. Arduino UNO R3 datasheets. [Revised
June 2021]

MATLAB Documentation. (2022). System Identification Overview. Retrieved


March 28, 2022 website: https://www.mathworks.com/help/ident/gs/about-system-
identification.html

MATLAB Documentation. (2022). System Identification Toolbox. Retrieved March


31, 2022 website: https://www.mathworks.com/products/sysid.html#app

Pravallika Vinnakota, MathWorks. (2013). Motor Control with Arduino: A Case


Study in Data-Driven Modeling and Control Design. Retrieved February 23, 2022
website: https://www.mathworks.com/company/newsletters/articles/motor-control-
with-arduino-a-case-study-in-data-driven-modeling-and-control-design.html

M. Gopal. (2003). Digital Control and State Variable Methods : Conventional and
Neuro-fuzzy control systems. New Delhi, India: Tata McGraw-Hill.

Speed and Position Control of DC motor

You might also like