0% found this document useful (0 votes)
78 views

Aditya Lab 12

This project aims to control the position of a DC motor using an encoder for feedback and PWM signals from an Arduino. An H-bridge circuit and MOSFETs are used to control the direction and speed of the motor. An encoder disk and two optointerrupters form an incremental rotary encoder to measure the motor position. The group designed the circuit, mounted the motor and encoder, and attempted to tune the PID gains to control the motor position using an Arduino microcontroller.

Uploaded by

Sujeet Pawar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Aditya Lab 12

This project aims to control the position of a DC motor using an encoder for feedback and PWM signals from an Arduino. An H-bridge circuit and MOSFETs are used to control the direction and speed of the motor. An encoder disk and two optointerrupters form an incremental rotary encoder to measure the motor position. The group designed the circuit, mounted the motor and encoder, and attempted to tune the PID gains to control the motor position using an Arduino microcontroller.

Uploaded by

Sujeet Pawar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

ME 511 Final Project Report

Closed Loop DC Motor Position Control


May 5, 2017

NAME UIN EMAIL

Aditya Deshpande (Leader) 670643579 [email protected]


Rohan Kunte 660182867 [email protected]
Sanket Rairker 661484906 [email protected]
Piyush Shirode 669099463 [email protected]
Shruti Rajurkar 675340804 [email protected]
Table of Contents
Section Page
Section Title
No. No.

1 Objective and Summary 4

2 Description of experiment 5

3 List of components 9

4 Circuit Diagrams 10

5 Experiment Procedure 11

6 Result 12

7 Conclusion 14

8 References 15

2
List of Figures and Tables

Page
Fig. No. Table/Figure
No.

Fig 1: Working principle DC Motor


1 5

2 Fig 2: Optointerrupter working 6

3 Fig 3: Example of a simple H - bridge circuit for a dc motor 8

4 Fig 4: MOSFET Transistor 8

5 Fig 5: Circuit Diagram Schematic 10

Fig 6: Circuit image with DC Motor, H Bridge with Arduino


6 10
microcontroller

7 Table 1: List of components for Lab #12 9

8 Table 2: Resistor reference 9

3
1. Objective and Summary

This project aims to control the position of a DC motor using a position feedback sensor and PWM
output signal in conjunction with an H-bridge amplifier circuit. Objective of this lab was to design
a system for motion control of a DC motor using Arduino and a stepper motor controller IC and
then write software to control the position and direction of the DC motor. This experiment is
designed to master the control of DC motor motion and to control the position of the DC motor
shaft using a homemade disk attached to the DC motor shaft with four teeth and two opto-
interrupters. A ramp function (i.e. a predefined function) was also executed. The control is
achieved using a PWM signal from the Arduino microcontroller together with an H-bridge and
PID logic. An incremental rotary encoder in the form of a disk along with photo-interrupters is
used. Feedback to execute closed loop function is gained from the incremental rotary encoder. We
designed the circuit, created a setup to mount the motor and the incremental rotary encoder. We
attempted to find the gain values using Ziegler Nichols Method of Tuning. Ran the code. Hence,
we learnt to control the speed, position of a DC motor, we also learnt about using ramp function.
The experiment provides deep understanding of functioning and applications of DC motor in wide
range of industries. Thus, controlling the DC motor is becomes very crucial skill.

4
2. Description

DC Motor

A motor is an electrical machine which converts electrical energy into mechanical energy. The
principle of working of a DC motor is that "whenever a current carrying conductor is placed in a
magnetic field, it experiences a mechanical force". The direction of this force is given by Fleming's
left hand rule and its magnitude is given by:

F = BIL
where,
B = magnetic flux density
I = current and
L = length of the conductor within the magnetic field.

DC motors are usually classified of the basis of their excitation configuration, as follows:

 Separately excited (field winding is fed by external source)


 Self-excited
o Series wound (field winding is connected in series with the armature)
o Shunt wound (field winding is connected in parallel with the armature)
o Compound wound -
 Long shunt
 Short shunt

Fig 1: Working principle DC Motor

5
Encoder

The front end of the shaft encoder comprises of an LED which emits IR signal.
When light emitted by the IR LED is blocked because of the alternating slots of the encoder disc
(also known as index disc), conduction level of the photo transistor/diode changes. This change
can be detected by a discrete hardware or by a microcontroller. In short, a photo-interrupter is
composed of an infrared emitter on one side and an infrared detector on the other by emitting a
beam of infrared light from one side to the other, the photo-interrupter can detect when an object
passes between them, breaking the beam.

Fig 2: optointerrupter work

In this experiment, we performed the DC motor closed loop control. To perform closed loop
control, we need position control. Encoder is a device which converts the information from one
format to another for speed standardization. There are two types of encoders, viz. absolute and
incremental encoders. An "absolute" encoder maintains position information when power is
removed from the system. Incremental encoders are used to track motion and can be used to
determine position and velocity. This can be either linear or rotary motion. Because the direction
can be determined, very accurate measurements can be made. The two output wave forms are 90
degrees out of phase, which is what quadrature means. These signals are decoded to produce a
count pulse or a countdown pulse. For decoding in software, the A & B outputs are read by
software, either via an interrupt on any edge or polling, and it is used to decode the direction. We
have used the incremental rotary encoder. Encoder disk and opto-interrupter together makes the
rotary encoder. We have used the handmade incremental encoder disk with 4 holes. Higher number
of holes will increase the resolution of position sensor. Optointerrupter circuit is made with 100
kΩ and 100 Ω resistor to limit the maximum current to be 50 mA (according to the specifications
of optointerrupter).

Optoisolator

Optoisolator uses optical path to couple an electric signal from two isolated circuits. It is generally
used in power system to transfer digital information in circuits where potential difference is up to
5000 volts. Application of optoisolator includes providing voltage isolation, coupling data circuits

6
or to be used within optical encoder as a means of detecting rising edge on an encoder wheel to
detect position.

Two main components of optoisolator are as follows:

 Light emitter: converts electric input signal to light, typically a near infrared LED.
 Light detector: detects light and convert it back to electric signal or modulates electric
current flowing from external power supply. Light detector can be a photo resistor,
photodiode, phototransistor or SRC. Emitter and detector are tailored to have matching
wavelengths so that maximum coupling is achieved.

H- Bridge and MOSFET

H-Bridge Circuit comprising of four MOSFET power transistors, four diodes (1N4003) and the
motor. Each MOSFET pair on each side of H-bridge consists of a p-channel on the top and a n-
channel on the bottom as shown in the figure. The motor connects in between the two legs. The p-
channel sources terminal is connected to 9 V and the drain terminals are connected to motor leads,
whereas the sources terminals of n-channel are connected to ground and drain terminals are
connected to motor leads. The gate terminals of left side MOSFET pair are connected to PWM1
and right side MOSFET pair is connected to PWM2.

In this experiment, switching is done using an ‘H-bridge’ motor control circuit. The signal is sent
to the H-bridge circuitry on the breadboard which sends the correct polarity to the battery leads
wired to the motors to accomplish the desired rotation. The MOSFETs of the H-Bridge acts as
switches which can be used to control the speed of the motor, as well as the rotational direction. If
a pulse width modulation (PWM) signal is applied to In1 and logic high to In4, then the MOSFET
switches would create a path from Vs to GND through the motor. This would make the motor
rotate forward. By varying the duty cycle of the pulse width modulated signal at In1, the speed of
the motor would change correspondingly. Likewise, if we applied the PWM signal to In3 and logic
high to In2, the same would occur, with the exception that the rotational direction would be
reversed. By using the PWM signal, we are essentially switching the motor on and off at a certain
rate to control the speed. The diodes represented in the H-Bridge are called ‘fly back diodes’.
These diodes provide a path for the current to dissipate when the motor switches from on to off.
Since the shaft of the motor itself is spinning, stopping the motor without those diodes in place,
will cause a large spike in current that could potentially burn out the MOSFETs of the H-Bridge.

7
Fig 3: Example of a simple H - bridge circuit for a dc motor

A MOSFET transistor is a three-terminal semiconductor device in which current, flowing from the
drain-source terminals, is controlled by the voltage on the gate terminal. A MOSFET can be used
as a switch, by controlling the voltage provided to the gate. For an enhancement n-MOSFET
(NMOS), if the gate voltage is sufficiently high, current flows from the drain to the source.
Conversely, for a p-MOSFET (PMOS), with a sufficiently high gate voltage, current flows from
source to drain. We have used the IRF510 and IRF9520 MOSFET transistors.

Fig 4: MOSFET Transistor

8
3. List of Components
Following are the list of components required for the experiment:

Item Quantity Description


DC Motor 1
Opto-isolator 1 www.jameco.com
Potentiometer 1
IRF511 (MOSFET) 2 www.newark.com
IRF9520 (MOSFET) 2 www.newark.com
1N4003 Diode 4 www.jameco.com
Opto-interrupter 2 www.jameco.com
Card Board and
Disk with holes 1
Tape
Microcontroller with
Arduino UNO 1
ATmega328P
Mechatronics Lab
1 set -
Equipment

Table 1: List of components for Lab #12

The resistance value is found from the following table:

Table 2: Resistor reference

9
4. Circuit Diagrams

Fig 5: Circuit diagram Schematic

Fig 6: Circuit image with DC Motor, H Bridge


with Arduino microcontroller

10
5. Experiment Procedure

1. The Disk for the encoder is made using a thick paper/card board material by hand.

2. As per the circuit diagram mentioned above, the H-bridge and optoisolator circuit

is built.

i. The Source, S of the transistors IRF9520 are connected to the 9V supply

ii. The Grounds, G of all the transistors are supplied to the Arduino

iii. The Drains, D of the IRF9520 and IRF511 are connected.

3. We connected the circuits with the Arduino UNO and the DC Motor.

4. Write the program for closed loop and PID control for speed and position control.

5. We calculated the Kp, Ki and the Kd gains by the trial and error method.

6. Check the closed loop position control system for clockwise and anti-clockwise

rotation.

7. The 1st option was given to choose direction and 2nd option was given to choose

velocity or position control.

8. Option 1 was chosen when speed is to be controlled.

9. Option 2 was chosen when the position control is to be controlled.

10. It is demonstrated that the motor can be controlled to any speed and position within

the given range of rpm.

11. Then we gave verified the smallest position accuracy (22.5o) that can be achieved.

12. We repeated experiment with 8 blade encoder to achieve 11.25 o position accuracy.

11
RESULT:

The following code is written for position and speed control of dc motor. User inputs the variable to
be controlled i.e speed, position, direction of rotation of motor. Based on the user input we have 2
cases –speed control, position control.
1st case is the position control, where the current position of the motor was found by Opto-
interrupter and the rotary encoder using external ISR function. To find position of motor IRS
function is called. As the encoder passes through the opto-interrupter a pulse is sent to Arduino via
output channels (pins 2 and 3). Whenever ISR is called, new level of A& B is read and they are
stored in the memory. When both are low (0,0) and rising edge of A tigers IRS1 i.e. 10 giving value
0010, similarly next will be 1011 1101 and 0100 completing one period. Thus, we have 4 string of
numbers describing clockwise and 4 describing anticlockwise. Whenever there is clockwise rotation
we get +1, -1 for anticlockwise and 0 everywhere else. With 4 bits, we have 16 unique numbers
hence we get resolution of 16 positions per revolution.
Whenever current position count was smaller or greater than desired (user input) motor moves
clockwise or anticlockwise until desired position is reached and stops. In above code, we made motor
stop after 100 counts are achieved.

For case 2 – motor moves for a predefined time and final and initial positions are recorded.
Difference between initial and final position gives distance travel, this divided by predefined time
gives actual motor speed.
Error is calculated between the actual and desired speeds and this error is used to tune pid gains. This
pid value is mapped to PWM to control speed.
Formula for calculating gains and mapping to pmw is –

12
CODE:

int pin1= 10; // set motor pin to aurdino 10


int pin2= 9; // set motor pin to aurdino 9

int encA=2; // set encaoder A to pin 2


int encB=3;// set encoder B to pin 3

int encAvalue; // varible to save Aencoder value


int encBvalue; // variable to store b encoder value
static int x=0;
int xd=100; // desired encoder count
int speed_req = 50; // required speed
int motorspeed;
int motorspeedpositive;
int motorspeednegative;
int motorspeed1;
int motorspeed2;
int Kp=1.35; // proportional gain
int Ki=0.35; // integral gain
int Kd=0.28;// derivative gain
boolean first_call=true;
static int ui;
int t=millis();
int errorold=0; // error between desired and actual value
int errornew=0;

int Speed_actual=0 ; // variable to store actual speed


int Speed_desired=0;//variable to store desired speed
int control = 1; //call position control
int control=2;//call speed control
int LOOPTIME =300ms
int e_p=0;// variable to store encoder count
int p_a=0;// variable to store actual position in degrees
int cmd_signal=0;

void setup() {
// pin declaration
pinMode(encA, INPUT);
pinMode(encB, INPUT);

pinMode(pin1,OUTPUT);
pinMode(pin2,OUTPUT);

Serial.begin(9600);

13
//calling interrupt
attachInterrupt(digitalPinToInterrupt(2), ISR1, CHANGE);
attachInterrupt(digitalPinToInterrupt(3), ISR2, CHANGE);
motorspeedpositive=Kp*xd+Ki*xd+Kd*xd;
motorspeednegative=-motorspeedpositive;
}

PidSpeedValue=0; // Variable to store PID output


pwm_value = 0; // Variable to store pwm command signal
errornew=errorold;
errorold = abs(finalValue) - abs(initialValue); // Calculating error between desired speed and
actual speed
ui=Ki*(errorold+errornew);
PidSpeedValue = ui+ (Kd * (errorold - errornew)) + (Kp * errorold);
void loop()
{
encAvalue=digitalRead(encA);// reading encoder A value
encBvalue=digitalRead(encB); // reading encoder B value
Serial.print(encAvalue); // display value
Serial.print(encBvalue);

//Checking which type of control has user entered


if(Control == 1)
{

if (first_call == true)
{

ui= 0.0 ;
first_call = false;
}

ui = ui + Ki * (abs(xd) -abs(x)); // intergral gain

if(xd>0)
{
motorspeed= Kp * (abs(xd) - abs(x) ) + ui + Kd * (errornew - errorold) ;// pid algorithm clockwise
motorspeed1=map(abs(motorspeed),0,motorspeedpositive,0,50);// pwm mapping
}
else
{
motorspeed= -(Kp * (abs(xd) - abs(x) ) + ui + Kd * (errornew - errorold)) ;// pid algo anticlockwise
motorspeed2=map(abs(motorspeed),0,motorspeednegative,0,50);// pwm maping
}
// print data

14
Serial.print("x: ");
Serial.print(x);
Serial.print('\n');
Serial.print("motorspeed: ");
Serial.print(motorspeed);
Serial.print('\n');

if (motorspeed>0)
*{
analogWrite(pin1,abs(motorspeed1)); // for clockwise
analogWrite(pin2,0);
}

if (motorspeed<0)
{
analogWrite(pin2,abs(motorspeed2)); // for anticlockwise
analogWrite(pin1,0);
}
}
else if(Control == 2)
{
if ((millis()-End_time)>=LOOPTIME)
{ //Waiting to execute the loop untill a predefined time to count encoder pulses
End_time=millis(); //Variable to store current time
E_p = (abs(x)); //Variable to store current position counts
P_a = ((x/16)*360); //Calculating the current postion in degrees
speed_a = ((x/16)*(60*(1000/LOOPTIME))); //Calculating the Current Speed in rpm
encoder_pos=0; //Reset the encoder position count to zero
cmd_signal = PidSpeed(speed_desired,speed_act); //Getting the PID command pwm signal to
control speed
analogWrite(9,cmd_signal); //Sending the command signal to motor pin 1 to rotate clockwise
analogWrite(10,0); //Sending 0 to motor pin 1 to

int ISR1()
{
if (encAvalue== HIGH && encBvalue == LOW )
{
x=x+1;
}
else if (encAvalue == LOW && encBvalue == HIGH)
{

15
x=x+1;
}
else if (encAvalue == HIGH && encBvalue == HIGH)
{
x=x-1;
}
else if (encAvalue == LOW && encBvalue == LOW)
{
x=x-1;
}
return x;

int ISR2()
{
if (encAvalue== HIGH && encBvalue == HIGH )
{
x=x+1;
}
else if (encAvalue == LOW && encBvalue == LOW)
{
x=x+1;
}
else if (encAvalue == HIGH && encBvalue == LOW)
{x = x - 1 ;
}
else if (encAvalue == LOW && encBvalue == HIGH)
{
x=x-1;
}
return x;

16
Links for Videos:
 4 Blade encoder video:
https://www.youtube.com/watch?v=cVk7P5KKjAY
 4 Blade encoder serial monitor:
https://www.youtube.com/watch?v=whRe2pWE1XM
 8 Blade encoder video:
https://www.youtube.com/watch?v=UGuL9r7ojAA
 8 Blade encoder Serial Monitor Clockwise:
https://www.youtube.com/watch?v=0R1RwtizcdU
 8 Blade encoder Serial Monitor AntiClockwise:
https://www.youtube.com/watch?v=EYePQPL_juU

17
Conclusion:

Position of a dc motor was controlled by using 2 Interrupt sub routines. Each IRS is triggered
when interrupt changes its state (low to high or high to low). These ISR counts up or counts
down based on the motor direction. Program controls position by setting desired rising and
falling edge numbers, when the count equals desired count the motor stop. Gains are tuned such
that motor stops at zero speed. However, due to low precision there was some steady state error
in the motor position even with precise integral control. When the value of kp is low (kp<1)
speed reduces gradually as motor reaches its target position. With increase in the value of kp
response time reduces. Too high proportional gain results in overshooting of motor and it starts
oscillating about the target position. Similarly, high value of integral gain results in instability.
Integral gain accumulates past error into offset and ensure exact target is achieved rather than
settling for value close to the target. Derivative control calculates the future error based on slope
of cumulative past error and is responsible for stabilizing the system. When displaced from the
original position motor return to the desired position.

PID gains for position control:

KP=1.35
Kd=0.28
Ki=0.35

As we used 4 bladed encoder with proper dimensions such that both optointurrupters are at 90
degree phase shift we have 16 positions for each revolution. Thus, we have 1/16th of revolution
as a minimum position control that we can achieve i.e.

Minimum position resolution we can achieve using 4 blade encoder = 22.5 Degree
Minimum position resolution we can achieve using 8 blade encoder = 11.25 Degree

To prevent opto isolator from damaging, we mapped motor speed between 0 to 50 PWM values.
Thus, needs a minimum starting value to overcome friction, below which it doesn’t start.
Problems faced-
 Opto interrupter is sensitive and doesn’t respond well to reflective surfaces. Only black
surface works fine since it obstructs light path completely therefore we had to change the
encoder multiple time.
 Opto interrupter circuit is prone to loose connections due to equipment malfunctioning.
 The opto interrupter were giving readings even when encoder was not passing through it
this was solved by connecting common ground to both circuits.

18
1. References

[1] Brian W.Evens, Arduino Programming Notebook,


http://playground.arduino.cc/uploads/Main/arduino_notebook_v1-1.pdf

[2] Brian Evans, Beginning Arduino Programming, Technology in Action,


http://www.hfremote.us/files/Arduino.pdf

[3] Julien Bayle, C Programming for Arduino, http://www.lakos.fs.uni-


lj.si/images/Predmeti/MK/2014/C%20Programming%20for%20Arduino.pdf

[4] Cetinkunt, S. (2009). Mechatronics lab manual. Chicago, IL: Department of Mechanical and
Industrial Engineering

[5]2016. [Online]. Available: https://uic.blackboard.com/bbcswebdav/pid-4949531-dt-content-


rid-
51189209_2/courses/2016.spring.me.511.19582/Mechatronics_Cetinkunt_SecondEdition_Labs.
pdf. [Accessed: 15- Oct- 2016].

[6]"Rotary encoder", En.wikipedia.org, 2017. [Online].


Available: https://en.wikipedia.org/wiki/Rotary_encoder. [Accessed: 05- May- 2017].

19

You might also like