Aditya Lab 12
Aditya Lab 12
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.
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:
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.
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.
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 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.
8
3. List of Components
Following are the list of components required for the experiment:
9
4. Circuit Diagrams
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.
ii. The Grounds, G of all the transistors are supplied to the Arduino
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
10. It is demonstrated that the motor can be controlled to any speed and position within
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:
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;
}
if (first_call == true)
{
ui= 0.0 ;
first_call = false;
}
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.
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
[4] Cetinkunt, S. (2009). Mechatronics lab manual. Chicago, IL: Department of Mechanical and
Industrial Engineering
19