You are on page 1of 15

HoChiMinh City University of Technology Mechatronics Department

HoChiMinh City University of Technology ME2009 - Control System Technology


Faculty Mechanical of Engineering Lab Assignment 2
Mechatronics Department
Control a DC Motor with Arduino

1. INTRODUCTION
In lab assignment 1, we have learned how to use Arduino to acquire various types of the
signal from a sensor such as a digital photosensor, potentiometers, incremental rotary encoder.
In this lab assignment, we will turn our attention to actuator control, which is an important part
of an automatic system.
The goal of this lab is to learn how to control a DC servo motor using the Arduino
microcontroller Mega 2560 board (shown in Figure 1) and the L298P Motor Shield driver
(shown in Figure 2). We take a look at some basic techniques for controlling DC motors. We
will use the reading from a rotary encoder attached to the back of the motor as our feedback
signal. We will learn how to design a controller for the motor, and drive the motor to a set-
point.

2. LAB DEVICES
2.1 Arduino Mega 2560 Board

Figure 1. Arduino Mega 2560 Board

ME2009 Control system technology 1/ 13


HoChiMinh City University of Technology Mechatronics Department

2.2 DC motor driver

Figure 2. L298P Motor Shield

2.3 USB cable


USB 2.0 Type A Male to Type B Male cable used to communicate between PC and Arduino
Mega 2560 board.

Figure 3. USB 2.0 Cable Type A Male to Type B


2.4 Wires
Used to connect Arduino Mega board with Control System Kit 02.

Figure 4. Breadboard wires

2.5 Screwdriver
Used to screwing and unscrewing screws on the Control System Kit 02.

ME2009 Control system technology 2/ 13


HoChiMinh City University of Technology Mechatronics Department

Figure 5. Cross Slot Screwdriver

2.6 Control System Kit 02

+5V 0V OUT M+ M- +5V 0V A B +5V KP 0V +5V KI 0V +5V KD 0V

Figure 6. Control System Kit 2

Module No. Device Name Signal


1 Terminal
+5V: high voltage supply for incremental encoder
0V: high voltage supply for incremental encoder
M+: positive power supply for the motor
2 DC Servo Motor
M-: negative power supply for the motor
A: pulse signal of phase A of the encoder
B: pulse signal of phase B of the encoder

+5V : (Red) high voltage of the power supply


Photoelectric
3 0V: (Green) ground of the power supply
Sensor
OUT : (Yellow) Digital output

+5V: the high voltage of the power supply


4 Potentiometers
0V:) ground of the power supply

ME2009 Control system technology 3/ 13


HoChiMinh City University of Technology Mechatronics Department

KP: Output of Blue Potentiometer


KI: Output of Yellow Potentiometer
KD: Output of Red Potentiometer

3. PROCEDURE
3.1 Set up your Arduino Programming Environment
You should set up the Arduino programming environment and Encoder Library on your PC.
You can review the guideline in Lab assignment 1 to install the software.
3.2 Wiring diagram and electrical circuit
Device Name Control System Kit 02 Arduino Mega

+5V: high voltage supply for incremental encoder 5V


0V: high voltage supply for incremental encoder GND
DC Servo Motor
A: pulse signal of phase A of the encoder Pin 2
B: pulse signal of phase B of the encoder Pin 3

+5V : (Red) high voltage of the power supply 5V


Photoelectric Sensor 0V: (Green) ground of the power supply GND
OUT : (Yellow) Digital output Pin 8

+5V: the high voltage of the power supply 5V


0V: the ground of the power supply GND
Potentiometers KP: Output of Blue Potentiometer Pin A0
KI: Output of Yellow Potentiometer Pin A1
KD: Output of Red Potentiometer Pin A2

Device Name Control System Kit 02 Driver shield


M+ : positive power supply for the motor M1 +
DC Servo Motor
M- : negative power supply for the motor M1 -

Arduino Mega Driver shield

ME2009 Control system technology 4/ 13


HoChiMinh City University of Technology Mechatronics Department

Pin 12 Motor 1 Direction control


Pin 10 Motor 1 PWM control
PWM mode
Pin 11 Motor 2 PWM control
Pin 13 Motor 2 Direction control

ME2009 Control system technology 5/ 13


HoChiMinh City University of Technology Mechatronics Department

KIT +5V 0V OUT M+ M- +5V 0V A B +5V KP 0V +5V KI 0V +5V KD 0V

+5V GND 8 2 3 A0 A1 A2

MEGA 2560 MEGA 2560


M1+ M1-

Driver Shield

OUT A B
GND (8) (2) (3)

KP KD
(A0) (A2)

KI
(A1)

Figure 7. Wiring diagram between Mega 2560 board, a motor shield and the Kit 02

ME2009 Control system technology 6/ 13


HoChiMinh City University of Technology Mechatronics Department

3.3 Method control of DC motor


3.3.1 Method control the speed of DC motor
We can control the speed of the DC motor by simply controlling the input voltage to
the motor: If change the value of power supply (12V, 6V, 5V,…) the speed of the motor
will be changed. The most common method of doing that is by using PWM signal.
PWM or pulse width modulation is a technique which allows us to adjust the average
value of the voltage that’s going to the electronic device by turning on and off the power
at a fast rate. The average voltage depends on the duty cycle, or the amount of time the
signal is ON versus the amount of time the signal is OFF in a single period of time.

Figure 8. Pulse with modulation


So depending on the size of the motor, we can simply connect an Arduino PWM output
to the base of a transistor or the gate of a MOSFET and control the speed of the motor by
controlling the PWM output. The low power Arduino PWM signal switches on and off the gate
at the MOSFET through which the high power motor is driven.
3.3.2 Method control the direction of DC motor
For controlling the rotation direction, we just need to inverse the direction of the current
flow through the motor:
- Connect M+ to VDC and M- to GND: motor turn clockwise
- Connect M- to 12VDC and M+to GND: motor turn counterclockwise

ME2009 Control system technology 7/ 13


HoChiMinh City University of Technology Mechatronics Department

M+ M+

12V
12V M-
M-
GND GND

Figure 9. Change the rotation direction of the motor


The most common method of doing that is by using an H-Bridge. An H-Bridge circuit
contains four switching elements, transistors or MOSFETs, with the motor at the center
forming an H-like configuration. By activating two particular switches at the same time we can
change the direction of the current flow, thus change the rotation direction of the motor.

Figure 10. H-Bridge circuit

3.3.3 DFRobot 2A Motor L298N Driver Shield


The L298N is a dual H-Bridge motor driver which allows speed and direction control of two
DC motors at the same time. The module can drive DC motors that have voltages between 5
and 35V, with a peak current up to 2A. Using this shield, you can control both the speed and
direction of DC motor using Arduino board.
Code example to change direction and speed of DC motor with the driver shield:

ME2009 Control system technology 8/ 13


HoChiMinh City University of Technology Mechatronics Department

// Define pin for Motor


int SPEED1 = 10; // Motor 1 PWM control
int DIR1 = 12; // Motor 1 Direction control
int SPEED2 = 11; // Motor 2 PWM control
int DIR2 = 13; // Motor 2 Direction control

int pwm_value = 0;

void setup() {
pinMode(DIR1, OUTPUT);
pinMode(DIR2, OUTPUT);
}

void loop() {
pwm_value = 100; // PWM value
digitalWrite(DIR1,HIGH); // change the direction of DC motor
analogWrite(SPEED1, pwm_value); //change the speed of DC motor
}

3.4 Determine the position of DC motor


The motor rotation angle will determine by an incremental rotary encoder that integrated
at the end of the motor. As the motor rotates, the encoder also rotates and generates the
square wave pulse output on pins A and B (shown as ) that can be used for determining
the rotated position. We will use Arduino mega board to receive the signal on pins A and
B of the encoder to calculate the position of the motor shaft.

Figure 11. The pulse output of the encoder


Example to get to get pulses of encoder:

// Define pin for Encoder


#include <Encoder.h> // add Encoder Library to the current project
Encoder myEncoder(2,3); // phase A and B of encoder

long motorPos = 0;

void setup() {
Serial.begin(9600); // setup serial
myEncoder.write(0); // reset encoder counter to 0
}

void loop() {
motorPos = myEncoder.read(); // read pulses of encoder
Serial.print("Pulses of encoder: "); // show counter on screen
Serial.println(motorPos);
}

ME2009 Control system technology 9/ 13


HoChiMinh City University of Technology Mechatronics Department

3.5 Potentiometer
The Arduino microcontroller can read analog inputs between 0 and 5 V and generate
corresponding integer values. This is accomplished using the analogRead() function. It takes
about 100 microseconds (0.0001 s) to read an analog input, so the maximum reading rate is
about 10,000 times a second. We can use this function to get the analog value of a potentiometer
with the example below:

// Define pin for potentiometer


int POT1 = A0; // select the input pin for the potentiometer
long ADvalue = 0;

void setup() {
Serial.begin(9600); // setup serial
}

void loop() {
ADvalue = analogRead(POT1); // read AD value
Serial.print("Value of Potentiometer 1: "); // show value on screen
Serial.println(ADvalue);
}

3.6 Photoelectric Sensor


The DFRobot Adjustable Infrared Sensor Switch is a set of transmitter and receiver in one of
the photoelectric switch sensor. The switching signal output differs in accordance with the
obstacles. It remains HIGH when no obstacles and remains LOW when there are obstacles.
There is also a red led on its back to indicate the sensor status. The signal from the sensor can
be captured by a digital input pin of Arduino with the example below:
// Define Photoelectric Sensor pin
int PHOTO_SENSOR = 8; // select the pin for the Photoelectric Sensor
output

void setup() {
Serial.begin(9600); // setup serial
pinMode(PHOTO_SENSOR, INPUT); // set the digital pin 8 as input
}

void loop() {
if (digitalRead(PHOTO_SENSOR)== LOW)
{
Serial.print("There is a obstacles ");
}
else
{
Serial.print("There is nothing ");
}
}

ME2009 Control system technology 10/ 13


HoChiMinh City University of Technology Mechatronics Department

3.7 Control Position of DC motor with P (Proportional) controller

START
Position control

Set: desired_angle = 900 (degree)

Set initial value


 current_angle = 0;
 err = 0;
 pwm_value = 0;
 direction = 0;
 kp = 50;

Get value from sensor


 current_angle = get pulse of encoder and calculate the angle (degree);
 Show current_angle to screen
 err = desired_angle – current_angle;
 pwm_value = kp * err;
 direction = 0;

Change motor direction


pwm_value < 0 ? YES  pwm_value = abs(pwm_value);
 direction = 1;

NO

Validate pwm value


 If (pwm_value < 30) then pwm_value = 30;
 If (pwm_value > 255) then pwm_value = 255;

Apply control value to motor


 Set pwm_value to motor
 Set direction to motor

Waitting 100 miliseconds

Figure 12. Flowchart of DC position control with P- controller

ME2009 Control system technology 11/ 13


HoChiMinh City University of Technology Mechatronics Department

3.8 Control Speed of DC motor with P (Proportional) controller

START
Speed control

Set desired_speed = 100


(rpm)

Set initial value


 current_speed = 0;
 err = 0;
 pwm_value = 0;
 direction = 0;
 kp = 50;

Get value from sensor


 current_speed = get pulse of encoder and calculate the speed (rpm);
 Show current_speed to screen
 err = desired_speed – current_speed;
 pwm_value = kp * err;
 direction = 0;

Change motor direction


pwm_value < 0 ? YES  pwm_value = abs(pwm_value);
 direction = 1;

NO

Validate pwm value


 If (pwm_value < 30) then pwm_value = 30;
 If (pwm_value > 255) then pwm_value = 255;

Apply control value to motor


 Set pwm_value to motor
 Set direction to motor

Waitting 100 miliseconds

Figure 13. Flowchart of DC speed control with P- controller

ME2009 Control system technology 12/ 13


HoChiMinh City University of Technology Mechatronics Department

4. REFERENCE

[1] Arduino.cc, The open-source Arduino Software (IDE), 2018. Available:


http://arduino.cc/en/Main/Software. [Accessed: 1- May- 2018].

[2] howtomechatronics.com, Arduino DC Motor Control Tutorial – L298N, 2017.


Available: https://howtomechatronics.com/tutorials/arduino/arduino-dc-motor-
control-tutorial-l298n-pwm-h-bridge/. [Accessed: 7- May- 2018].

[3] www.dfrobot.com, Arduino Motor Shield (L298N), 2017. Available:


https://www.dfrobot.com/wiki/index.php/Arduino_Motor_Shield_(L298N)_(SKU:
DRI0009). [Accessed: 1- May- 2018].

[4] howtomechatronics.com, How Rotary Encoder Works and How To Use It with
Arduino, 2016. Available: https://howtomechatronics.com/tutorials/arduino/rotary-
encoder-works-use-arduino/. [Accessed: 1- May- 2018].

ME2009 Control system technology 13/ 13


HoChiMinh City University of Technology Mechatronics Department

HoChiMinh City University of Technology ME2009 - Control System Technology


Faculty Mechanical of Engineering Lab Assignment 2
Mechatronics Department
Control a DC Motor with Arduino

REPORT
Date:………………… Group:……………… Duration: 5 class hours
Full Name Student ID Signature Grade

Task 1. Check the below answers: (0.5 points)


 Can you install Arduino IDE successful? Yes  No 
 Can you connect Mega 2560 board with PC successful? Yes  No 
 Can you make Led at Pin 13 on the board flash every 1 second? Yes  No 
 Can you install the Encoder Library successful? Yes  No 
 Can you run the code in section 3.6 Photoelectric Sensor? Yes  No 

Task 2. Typing and run the example in section 3.5 Potentiometer and answers the
question below (1 point)
Potentiometer to ADC has a minimum ADC value: adc _ max  .............
1024
Potentiometer to ADC has a maximum ADC value: adc _ min  .............
0
The resolution of AD convert of Arduino 2560 is n  .........
10 bits

Task 3. Wire program to read signal of A and B channel of the encoder, determine the
resolution of the encoder (1 point)
800
Number of pulse per round of encoder : res _ encoder  ............. (ppr)
Determine the equation to calculate the speed of motor if the Arduino receives pulses
in 100 milliseconds. 600ppm
speed  ........................................................................................
600n/800 (rpm)
speed  ........................................................................................
600n/800 x 360/60 (degree/s)

ME2009 Control system technology 1/ 2


HoChiMinh City University of Technology Mechatronics Department

Task 4. Typing and run the example in section 3.3 Method control of DC motor (1 point)
 Can you run the code in section successful? Yes  No 
 Modify your code to add a value 30 to a pwm_value variable every 1 second. If the to
pwm_value > 255, it will be set to 0. (Hint: use For loop and delay function)
0% 25% 50% 75% 100%
    

Task 5. Use your code in task 4 then modify it to reverse the direction of rotation of motor
as having an obstacle in front of the photoelectric sensor? (1 point)

0% 25% 50% 75% 100%


    

Task 6. Write a program to change the speed of motor depend on the value ADC of the
potentiometer. As we turn the potentiometer from minimum value to maximum value,
why is the speed of motor changed many times? Please fix this problem? (1 point)
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………

0% 25% 50% 75% 100%


    

Task 7. Write a program to control the motor to the angle = 900 (1.5 point)
0% 25% 50% 75% 100%
    

Task 8. Write a program to control the motor rotate to angle 900 if we put an obstacle in
front of the photoelectric sensor; otherwise, the motor will return to 00 (1.5 points)
0% 25% 50% 75% 100%
    

Task 9. Write a program to control the speed of motor = 500 rpm (1.5 point)
0% 25% 50% 75% 100%
    

ME2009 Control system technology 2/ 2

You might also like