You are on page 1of 44

HoChiMinh City University of Technology

Faculty Mechanical of Engineering


Mechatronics Department

Laboratory Tutorial
Control System Technology
(ME2009)

Ho Chi Minh City, Aril 2018


Table of Contents

Laboratory 01: Arduino, Led, Photoelectric Sensor, and Encoder ..................................... 01


Laboratory 02: Control a DC Motor with Arduino.............................................................. 19
Laboratory 03: Sequential Circuit Design Using Relays ..................................................... 33
HoChiMinh City University of Technology Mechatronics Department

HoChiMinh City University of Technology ME2009 - Control System Technology


Faculty Mechanical of Engineering Lab Assignment 1
Mechatronics Department
Arduino, Led, Photoelectric Sensor, and Encoder

1. INTRODUCTION
Arduino (shown in Figure 1) is an open source computer hardware and software company.
It is designed to facilitate interaction with the physical world via sensors while being able to
perform calculations and various functions. Nowadays, Arduino is fast becoming one of the
most popular microcontrollers on the market. Its ease of use, extensive software library and
most importantly, its low cost have come to make it as popular as it is today.
Many projects using the Arduino can be found on http://www.hackaday.com. In order to
start having fun with the Arduino, free software can be found at
http://arduino.cc/en/Main/Software for Macs, Windows and Linux operating systems. This
website also provides tons of easy tutorials for you to start. Tutorials can be found at
http://arduino.cc/en/Tutorial/HomePage.
In this lab, you will become familiar with the Arduino, interact with the other hardware,
e.g. led, Photoelectric Sensor, incremental rotary encoder and some of their applications. You
will learn how to use the Arduino to interface between hardware on the control system Kit 01
board (shown in Figure 2) and software (the code). You will also learn how to use a led
module to display number, detect an object by using Photoelectric Sensor and measure an angle
with an incremental rotary encoder.

2. LAB DEVICES
2.1 Arduino Uno Board

Figure 1. Arduino Uno Board

ME2009 Control system technology 1


HoChiMinh City University of Technology Mechatronics Department

2.2 Control System Kit 01

+ 5V 0V A B Z CLOCK + 5V LATCH DATA 0V +5V 0V OUT

Figure 2. Control System Kit 01

Module No. Device Name Signal


1 Terminal
+5V : (Brown) high voltage of the power supply
0V: (Blue) ground of the power supply

2 Rotary sensor A : (Black) pulse signal of phase A output


B : (White) pulse signal of phase B output
Z : (Orange) pulse signal of phase Z output

+5V : the high voltage of the power supply


0V: the ground of the power supply

3 Led module CLOCK: shift register clock input (SHCP)


LATCH: storage register clock input (STCP)
DATA: serial data input (DS)

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

4 Photoelectric Sensor 0V: (Green) ground of the power supply


A : (Yellow) Digital output

ME2009 Control system technology 2


HoChiMinh City University of Technology Mechatronics Department

2.3 USB cable


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

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

2.4 Wires
Used to connect Arduino Uno board with Control System Kit 01.

Figure 4. Breadboard wires

2.5 Screwdriver
Used to screwing and unscrewing (inserting and removing) screws on the Control System Kit
01.

Figure 5. Flat Tip Screwdriver

ME2009 Control system technology 3


HoChiMinh City University of Technology Mechatronics Department

3. PROCEDURE
3.1 Set up your Arduino Programming Environment
The Arduino programming environment needs to be set up on a machine that you have access
to. It can be a Windows PC, Mac or Linux machine and it can be a laptop or desktop. Get the
latest version free software from the website ( http://arduino.cc/en/Main/Software). You should
choose the Installer (.exe) that installs directly everything you need to use the Arduino Software
(IDE), including the drivers.
When the download finishes, proceed with the installation and please allow the driver
installation process when you get a warning from the operating system. Follow instructions to
finish the installation as shown in Figure 6

Figure 6. Arduino installation process

3.2 Installing Additional Arduino Libraries


Libraries are a collection of code that makes it easy for you to connect to a sensor,
display, module, etc. For example, the Encoder library makes it easy to count pulse output of
the rotary encoder. There are hundreds of additional libraries available on the Internet for
download. To use the additional libraries, you will need to install them.
When you want to add a library manually, you need to download it as a ZIP file and close
all Arduino IDE, expand ZIP file and put in the proper directory (Default is:

ME2009 Control system technology 4


HoChiMinh City University of Technology Mechatronics Department

\Documents\Arduino\libraries). You can find or change the location of your sketchbook folder
at File > Preferences > Sketchbook location (shown in Figure 7)

Figure 7. Location of Arduino library folder


Open Arduino IDE after installing the additional libraries, you can find the installed
library examples in File > Examples > your_library > example_ name
For example, The Basic example of Encoder Library can be found in Figure 8

Figure 8. Open an example of an additional library

ME2009 Control system technology 5


HoChiMinh City University of Technology Mechatronics Department

3.3 Start a new Arduino program


In this first task, we simply connect Arduino board using a USB cable to PC and upload a
simple piece of code to it (shown in Figure 9). Connect Arduino board with USB cable and
USB port on your PC and wait till PC has a device driver for Arduino installed.

Figure 9. Connect Uno board to PC

Choose the icon Arduino on your computer to start a new Arduino program ( Sketch)
and Write a simple program name as Blink as shown in Figure 10. Press Verify button to check
your code.

Figure 10. Make a new sketch

ME2009 Control system technology 6


HoChiMinh City University of Technology Mechatronics Department

Figure 11. Select your Arduino board type Tools>Boards>Arduino UNO

Figure 12. Select your serial port, Tools>Serial Port>COM 7 (COM # depending what
COM port is free during setup)
Click upload button to upload the code to Arduino. Wait until “Done Uploading”
message status appear. The LED should blink and you have successfully installed your
Arduino.

ME2009 Control system technology 7


HoChiMinh City University of Technology Mechatronics Department

3.4 WIRING DIAGRAM AND ELECTRICAL CIRCUIT

+ 5V 0V A B Z CLOCK + 5V LATCH DATA 0V +5V 0V OUT

Figure 13. Uno board and Control System Kit 01 Terminal

Device Name Control System Kit 01 Arduino Uno Board


+5V : (Brown) high voltage of the power
Pin 5V
supply
Pin GND
0V: (Blue) ground of the power supply
Rotary sensor Pin 2
A : (Black) pulse signal of phase A output
Pin 3
B : (White) pulse signal of phase B output
Not use
Z : (Orange) pulse signal of phase Z output

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


0V: the ground of the power supply Pin GND
Led module CLOCK: shift register clock input (SHCP) Pin 4
LATCH: storage register clock input (STCP) Pin 5
DATA: serial data input (DS) Pin 6

ME2009 Control system technology 8


HoChiMinh City University of Technology Mechatronics Department

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


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

4. REFERENCE

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


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

[2] Instructables.com, Arduino - Blinking Led, 2018. Available:


http://www.instructables.com/id/Arduino-Blinking-LED. [Accessed: 1- May-
2018].

[3] Sunfounder.com, Driving 7-Segment Display by 74HC595, 2016. Available:


https://www.sunfounder.com/learn/rfid-kit-v2-0-for-arduino/lesson-20-driving-7-
segment-display-by-74hc595-rfid-v2-0-for-arduino.html [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 9


HoChiMinh City University of Technology Mechatronics Department

5. APPENDIX
5.1 Arduino Help
You will find other Arduino functions use them in your sketches at the path Help >
Reference. The structure and use of functions is fully explained.

Figure 14. Arduino IDE Reference

5.2 Sample Solutions: Display number zero “ 0 ” on led module


1. const int clockPin = 4; // clock pin (SHCP) of 74HC595
2. const int latchPin = 5; // latch pin (STCP) of 74HC595
3. const int dataPin = 6; // Data pin (DS) of 74HC595
4. const int sensorPin = 8; // Pin connected Photoelectric Sensor
output
5. // variable
6. int count = 0;
7. int chuctrieu = 0;
8. int trieu = 0;
9. int tramnghin = 0;
10. int chucnghin = 0;
11. int nghin = 0;
12. int tram = 0;
13. int chuc = 0;
14. int donvi = 0;
15. long no_pulse = 1234;
16. long a = 0;
17. int check = 1;

18. void setup()


19. {
20. pinMode(latchPin, OUTPUT);
21. pinMode(dataPin, OUTPUT);
22. pinMode(clockPin, OUTPUT);
23. pinMode(sensorPin, INPUT);
24. }

ME2009 Control system technology 10


HoChiMinh City University of Technology Mechatronics Department

25. void loop()


26. {
27. a = 0;
28. so(a);
29. digitalWrite(latchPin, LOW);
30. digitalWrite(latchPin, HIGH);
31. }
32. void so(int num)
33. {
34. switch (num) {
35. case 0:
36. digitalWrite(dataPin, HIGH); digitalWrite(clockPin, LOW);
digitalWrite(clockPin, HIGH); // dau cham
37. digitalWrite(dataPin, HIGH); digitalWrite(clockPin, LOW);
digitalWrite(clockPin, HIGH); // canh g
38. digitalWrite(dataPin, LOW); digitalWrite(clockPin, LOW);
digitalWrite(clockPin, HIGH); // canh f
39. digitalWrite(dataPin, LOW); digitalWrite(clockPin, LOW);
digitalWrite(clockPin, HIGH); // canh e
40. digitalWrite(dataPin, LOW); digitalWrite(clockPin, LOW);
digitalWrite(clockPin, HIGH); // canh d
41. digitalWrite(dataPin, LOW); digitalWrite(clockPin, LOW);
digitalWrite(clockPin, HIGH); // canh c
42. digitalWrite(dataPin, LOW); digitalWrite(clockPin, LOW);
digitalWrite(clockPin, HIGH); // canh b
43. digitalWrite(dataPin, LOW); digitalWrite(clockPin, LOW);
digitalWrite(clockPin, HIGH); // canh a
44. break;
45. }
46. }

5.3 Photoelectric Sensor


The DFRobot Adjustable Infrared Sensor Switch is a set of transmitter and receiver in one of
the photoelectric switch sensor. The detection distance can be adjusted according to the
demand. The DFRobot Adjustable Infrared Sensor Switch is small, easy to use, inexpensive,
easy to assemble and can be widely used in a robot to avoid obstacles, interactive media,
industrial assembly line, and many other occasions. 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.

Specification:
• Power supply: 5V • Working Current: <100mA
• Adjustable detection range: 3cm - 80cm
• Pin description: • Red - 5V • Green – GND • Yellow - Digital output

ME2009 Control system technology 11


HoChiMinh City University of Technology Mechatronics Department

• Digital output: •"0" - found barrier (~0V) • "1" - no barrier (~4V)

5.4 DFRobot 8 Character x 7 Segment SPI LED Module

Figure 15. LED Module


This is 8 digital bits serial LED display that is controlled using a serial interface. Instead of
using up a dozen-or-so of your microcontroller’s pins to control the LEDs, all you need is one.
You can control all digits, decimal points, the colon, and the apostrophe using SPI interface
which allows more than 2 modules can be serial linked. With Interface Shield For Arduino
module can be plug and play. There are 8 - 74HC595 Shift Register to control the 8 digits 7
segments Led Display.

Figure 16. Connect three LED Module

5.5 The 74HC595 Shift Register


The 74HC595 consists of an 8−bit shift register and a storage register with three−state
parallel outputs. It converts the serial input into parallel output so that you can save IO ports of
an MCU. The 74HC595 is widely used to indicate multipath LEDs and drive multi-bit segment
displays. "Three-state" refers to the fact that you can set the output pins as either high, low or
"high impedance." With data latching, the instant output will not be affected during the shifting;
with data output, you can cascade 74HC595s more easily.

ME2009 Control system technology 12


HoChiMinh City University of Technology Mechatronics Department

The shift register holds what can be thought of


as eight memory locations, each of which can be a 1
or a 0.
To set each of these values on or off, we feed
in the data using the 'Data' and 'Clock' pins of the
chip.
The clock pin needs to receive eight pulses. At
the time of each pulse, if the data pin is high, then a
1 gets pushed into the shift register. Otherwise, it is
a 0. When all eight pulses have been received, then
enabling the 'Latch' pin copies those eight values to
the latch register. This is necessary, otherwise, the Figure 17. The principle of 74HC595
wrong LEDs would flicker as the data was being
loaded into the shift register.
The chip also has an OE (output enable) pin,
this is used to enable or disable the outputs all at
once. You could attach this to a PWM capable
Arduino pin and use 'analogWrite' to control the
brightness of the LEDs. This pin is active low, so we Figure 18. Pin of 74HC595

tie it to GND.
A seven-segment display is a LED module
composed of 8 LEDs. 7 of the LEDs are for
segments of one digit (shown as A to G below) and
the other LED is for the decimal point (shown
as DP below).
Figure 19. Seven- segment Led
Display module

The schematic diagram to use one 74HC595 control one digit 7 segments Led Display
module shown in Figure 20

7-Segment Display 74HC595 SunFounder Uno R3


a Q7
b Q6
c Q5
d Q4

ME2009 Control system technology 13


HoChiMinh City University of Technology Mechatronics Department

e Q3
f Q2
g Q1
DP Q0
VCC 5V
CE, GND GND
SHCP (Clock) 4
STCP (Latch) 5
DS (Data) 6
MR 5V

Figure 20. Connect 74HC595 with one digit 7 segments Led Display and Uno board

Figure 21. Breadboard test

5.6 Rotary Encoder


A rotary encoder, also called a shaft encoder, is an electro-mechanical device that
converts the angular position or motion of a shaft or axle to an analog or digital signal. There
are two main types: absolute and incremental (relative). The output of absolute encoders
indicates the current position of the shaft, making them angle transducers. The output of

ME2009 Control system technology 14


HoChiMinh City University of Technology Mechatronics Department

incremental encoders provides information about the motion of the shaft, which is typically
further processed elsewhere into information such as speed, distance, and position. Rotary
encoders are used in many applications that require precise shaft unlimited rotation—including
industrial controls, robotics, rotating radar platforms.
There are many different types of rotary encoders which are classified by either Output
Signal or Sensing Technology. The particular rotary encoder that we will use in the lab
assignment is an incremental rotary encoder and it’s the simplest position sensor to measure
rotation. This rotary encoder is also known as quadrature encoder or relative rotary encoder
and its output is a series of square wave pulses.

Figure 22. Rotary Encoder

How Rotary Encoder Works:


Principle generate the square wave pulses: The encoder has a disk with evenly spaced
contact zones that are connected to the common pin C and two other separate contact pins A
and B, as illustrated below.

Figure 23. The pulse output of the encoder


When the disk will start rotating step by step, the pins A and B will start making contact
with the common pin and the two square wave output signals will be generated accordingly.

ME2009 Control system technology 15


HoChiMinh City University of Technology Mechatronics Department

Any of the two outputs can be used for determining the rotated position if we just count
the pulses of the signal. However, if we want to determine the rotation direction as well, we
need to consider both signals at the same time.
We can notice that the two output signals are displaced at 90 degrees out of phase from
each other. If the encoder is rotating clockwise the output A will be ahead of output B.

So if we count the steps each time the signal changes, from High to Low or from Low
to High, we can notice at that time the two output signals have opposite values. Vice versa, if
the encoder is rotating counterclockwise, the output signals have equal values. So considering
this, we can easily program our controller to read the encoder position and the rotation
direction.

ME2009 Control system technology 16


HoChiMinh City University of Technology Mechatronics Department

HoChiMinh City University of Technology ME2009 - Control System Technology


Faculty Mechanical of Engineering Lab Assignment 1
Mechatronics Department
Arduino – Led – Photoelectric Sensor - Encoder

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

Task 1. Check the below answers:


 Can you install Arduino IDE successful? Yes  No 
 Can you connect Uno board with PC successful? Yes  No 
 Can you make the Blink program? Yes  No 
 Can you build the Blink program successful? Yes  No 
 Can you upload the Blink program to Uno board? Yes  No 

Task 2. Modify the Blink program to make the LED flash 5 times (0.5 seconds/time),
pause 2 seconds then flash again.
…………………………………………… ………………………………………………..
…………………………………………… ………………………………………………..
…………………………………………… ………………………………………………..
…………………………………………… ………………………………………………..
…………………………………………… ………………………………………………..
…………………………………………… ………………………………………………..
…………………………………………… ………………………………………………..

Task 3. Write a program to display a number 0, 1, 2, 3, 4, 6, 7, 8, 9 on led module.


0% 25% 50% 75% 100%
    

ME2009 Control system technology 17


HoChiMinh City University of Technology Mechatronics Department

Task 4. Write a program to display a number “123456” on led module.


0% 25% 50% 75% 100%
    

Task 5. Write a program to display a number “ -123456” on led module.


0% 25% 50% 75% 100%
    

Task 6. Write a program to detect the Photoelectric Sensor signal


 If the Photoelectric Sensor signal is “LOW”, a number “ -123456” is displayed on the
led module,
 And if the Photoelectric Sensor signal is “HIGH”, the led module will be OFF.
0% 25% 50% 75% 100%
    

Task 7. Use Encoder library to get the pulse input of rotary encoder and show the value
on the led module
0% 25% 50% 75% 100%
    

Task 8. The resolution of the rotary encoder is: ……… pulse/ revolution (PPR)

Task 9. Write a program to display the integer angle (degree) of the rotary encoder. If
Photoelectric Sensor signal is “LOW”, the value will be reset to “00”

0% 25% 50% 75% 100%


    

Task 10. Write a program to display the real angle (degree) of the rotary encoder. If
Photoelectric Sensor signal is “LOW”, the value will be reset to “00”

0% 25% 50% 75% 100%


    

ME2009 Control system technology 18


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 L298N 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 19


HoChiMinh City University of Technology Mechatronics Department

2.2 DC motor driver

Figure 2. DFRobot 2A Motor Driver 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.

Figure 5. Cross Slot Screwdriver

ME2009 Control system technology 20


HoChiMinh City University of Technology Mechatronics Department

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


0V:) ground of the power supply

4 Potentiometers KP: Output of Blue Potentiometer


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

ME2009 Control system technology 21


HoChiMinh City University of Technology Mechatronics Department

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

Pin 4 Motor 1 Direction control


Pin 5 Motor 1 PWM control
PWM mode
Pin 6 Motor 2 PWM control
Pin 7 Motor 2 Direction control

ME2009 Control system technology 22


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)

M1+

M1-

GND

12V

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

ME2009 Control system technology 23


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 24


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 25


HoChiMinh City University of Technology Mechatronics Department

// Define pin for Motor


int SPEED1 = 4; // Motor 1 Direction control
int DIR1 = 5; // Motor 1 PWM control
int DIR2 = 6; // Motor 1 PWM control
int SPEED2 = 7; // Motor 1 Direction control

int pwm_value = 0;

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

void loop() {
pwm_value = 100; // PWM value
digitalWrite(M1,HIGH); // change the direction of DC motor
analogWrite(E1, 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(115200); // 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 26


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(115200); // 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(115200); // 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 27


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 28


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 29


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 30


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  .............
Potentiometer to ADC has a maximum ADC value: adc _ min  .............
The resolution of AD convert of Arduino 2560 is n  ......... bits

Task 3. Wire program to read signal of A and B channel of the encoder, determine the
resolution of the encoder (1 point)
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.
speed  ........................................................................................ (rpm)

ME2009 Control system technology 31


HoChiMinh City University of Technology Mechatronics Department

speed  ........................................................................................ (degree/s)

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 32


HoChiMinh City University of Technology Mechatronics Department

HoChiMinh City University of Technology ME2009 - Control System Technology


Faculty Mechanical of Engineering Lab Assignment 3
Mechatronics Department
Sequential Circuit Design Using Relays

1. INTRODUCTION
A relay is a set of switches that are actuated when electric current passes through a coil of wire.
Nowadays, relays are used quite commonly in industrial applications. Although micro-processor
based controllers are commonly used, using relays as a controller is sometimes a reasonable solution
in sequential processes.
Objectives:
 Overview the multi-purpose relay.
 Have experience in wiring and testing electrical circuits.
 Implement the method of sequential circuit design using relays.

2. EQUIPMENT
2.1 Devices
- 4 multi-purpose relays of 4 poles.

- 5 push buttons (one representing the ON button and the other four
representing sensor signals).

- 4 time-delay relays for time-driven sequence operations.

- 35 connecting coded cables and 1 cable of power plug.

- 2 screw drivers.

ME2009 Control system technology 33


HoChiMinh City University of Technology Mechatronics Department

2.2 Specifications:
a) Electromechanical Relays (EMR): this is a device that uses an electromagnet to provide the force to
close (or open) switch contacts, in other words, an electrically powered switch. When the
electromagnet, called the coil, is energized, it pulls down on the spring-loaded armature. Relay
contacts are described as being one of two kinds: normally open contacts (NO), which are open in
the unenergized state, and normally closed contacts (NC), which are closed in the unenergized state.
By convention, the symbol always depicts the relay in the unenergized state.

For the relay MY4N, pin numbers are assigned as follows

.
b) Push-button switches: these are almost always the momentary type – pressure must be
maintained to keep the switch activated. There are two configurations possible: normally open
(NO) and normally closed (NC).

ME2009 Control system technology 34


HoChiMinh City University of Technology Mechatronics Department

Time-delay relays: they are control relays that have provisions for a delayed switching action.
Solid-state time-delay relays are used in most newer systems that require delay relays. The delay
in switching is usually adjustable, e.g. the knob is for setting the delay. These units are based on
the delay involved when (1) charging a capacitor or (2) counting high-speed clock pulses with a
digital counter: the higher the count, the longer the delay. The delay may take place when the
coil is energized or when the coil is deenergized. An arrow is used to identify the switching
direction in which the time delay takes place.

For the time-delay relays AH3-3, pin numbers are assigned as follows

3. EXPERIMENTAL CONTENTS
3.1 Time duration: 5 academic periods for each group of 4 students.
3.2 Requirements

3.2.1. Equipment testing


- Supplying AC power to the coil of relays to test their operations. Pay attention to switching
contacts when the coil is energized.
- Supplying AC power to the coil of time-delay relays to test their operations. Pay attention to
LEDs ON and UP when the coil is energized under the effect of timing value.

ME2009 Control system technology 35


HoChiMinh City University of Technology Mechatronics Department

3.2.2. Case study


The figure below illustrates a bending tool with pneumatic drive.

Small bent workpieces can be produced to good advantage without the need for an eccentric or
hydraulic press by using standard commercially available components to create a basic
configuration and pneumatic components to provide the necessary motions in numerous directions.
The illustration shows a bending sequence (from I to IV). The lateral bending jaws can be activated
only after the vertical motion has been completed. The sequence control system thus requires
signals supplied by proximity switches. The finished workpiece must be pushed away from the
bending ram. In the case of full automation, the insertion of a new workpiece blank can be
combined with the ejection of a finished workpiece. This system is described in a diagram as
follows:

Cylinder A
A1

A2
LS1

LS6 LS5
Cylinder B LS2 Cylinder C

LS3 LS4
B1 B2 C2 C1

ME2009 Control system technology 36


HoChiMinh City University of Technology Mechatronics Department

Each pneumatic cylinder is connected through a 4/3 directional valve. Therefore, there are 6
manipulated signals:
 A1 = 1: Cylinder A extends.
 A2 = 1: Cylinder A retracts.
 B1 = 1: Cylinder B extends.
 B2 = 1: Cylinder B retracts.
 C1 = 1: Cylinder C extends.
 C2 = 1: Cylinder C retracts.

Six limit switches are used to detect positions of the cylinders as follows:
 LS1 = 1: the cylinder A is at the retracted end position.
 LS2 = 1: the cylinder A is at the extended end position
 LS3 = 1: the cylinder B is at the retracted end position
 LS4 = 1: the cylinder B is at the extended end position
 LS5 = 1: the cylinder C is at the retracted end position
 LS6 = 1: the cylinder C is at the extended end position

With the diagram above, the sequence operations could be described as:
- Step 1: the cylinder A extends.
- Step 2: then the two cylinders B and C extend together.
- Step 3: when the cylinders B and C reach to the end, they retract at the same time.

- Step 4: when the cylinders B and C are retracted completely, the cylinder A starts
retracting.

The students are required to design the controller for the above system. Note that experimental
results in part 4 need to be completed and submitted to the instructor.
a) Using relays, implement a control circuit to meet the desired sequence in which push-
button switches are used as limit switches.
b) Replace the four push-button switches with four time-delay relays, respectively. Then,
revise the control circuit so that the system operates automatically when the ON button
is the pressed.

3.3 Guidelines of solving problem


3.3.1. Sequential Circuit Design

ME2009 Control system technology 37


HoChiMinh City University of Technology Mechatronics Department

With the statement list above, the system could also be expressed in a Sequential Function Chart
(SFC) as follows

Start

ON

Step 1 Cylinder A extends

LS2

Step 2 Cylinders B & C extend

LS4 & LS6

Step 3 Cylinders B & C retract

LS3 & LS5

Step 4 Cylinder A retracts

LS1

From the SFC, there are four steps operating in sequence. Therefore, the procedure for the design
of an event-driven sequential circuit could be applied. Basic concepts of the design are:
 Use of a control relay for each step in the process.
 When the process is in a given step. the control relay associated with that step is energized
and all other relays are deenergized  each control relay represents a "state" of the
controller.
 There is a one-to-one correspondence between steps in the process and states of the
controller, and the step numbers are used to identify the states.
 Each state in the controller consists of one rung in the controller ladder diagram.

Assume that the process has three consecutive steps: step i, step j, and step k. The rung in the
ladder diagram that establishes step j in a sequential control circuit has a general form:

ME2009 Control system technology 38


HoChiMinh City University of Technology Mechatronics Department

Where:

Rj
Coil the control relay k, representing State j
Rk
NC contact of the control relay k, terminating State j when the process is in Step k.
Ri
NO contact of the control relay i, informing State j be ready when the process is in
Step i.
Cij
the condition required to move from State i to State j.
Rj
NO contact of the control relay j, self-holding State j.

3.3.2. Relay circuit implementation


Note and mark used pin numbers in the control circuit so that the circuit wiring could be monitored
comfortably. Do wiring for the control circuit in ladder diagram with push-button switches as limit
switches as follows:
R2 Button A
R1
10 2 13 14
R1
5 9

R3 R1 Button B

R2
10 2 7 11 13 14
CRj
5 9

R4 R2 Button C

R3
10 2 7 11 13 14
R3
5 9

Button E R3 Button D

R4
7 11 13 14
R4
5 9

ME2009 Control system technology 39


HoChiMinh City University of Technology Mechatronics Department

4. Experimental results
4.1 Plot the control circuit in ladder diagram with time-delay relays as the transition condition

4.2 Circuit implementation


 Using push-button switches
Does not run Incomplete run Work well

Comments: …………………………………………………………………………………………

 Using time-delay relays


Does not run Incomplete run Work well

Comments: …………………………………………………………………………………………

Instructor’s name:
Date: Room: Time of report:
Student’s names: Student’s IDs:
 …………………………………………………………  …………………..
 …………………………………………………………  …………………..
 …………………………………………………………  …………………..
 …………………………………………………………  …………………..

ME2009 Control system technology 40


HoChiMinh City University of Technology Mechatronics Department

REFERENCES:
[1] R.N. Bateson, Introduction to Control System Technology, 7th Edition, Prentice Hall, 2002.
[2] Christopher T. Kilian, Modern Control Technology: Components and Systems, 2nd Edition,
Delmar Thomson Learning, 2001.

Appendices: this lab procedures could be similarly applied to followings:

Example 1:

The drilling, countersinking and chamfering of small workpieces in medium-sized and large
batches are typical work operations in mechanical engineering. It can be well worth while to
develop special devices for these operations. In this example, the workpiece carriers consist of
high-powered pneumatic chucks which are indexed about a horizontal axis with the aid of a rotary
indexing table. Pneumatic pick-and-place units are used for loading and unloading. If the device
is used on a drilling/milling machine with a vertical working spindle, a further working operation
can be carried out in the loading position. A hydraulic cushioning cylinder can be connected up in
parallel to cushion the feed motion.

ME2009 Control system technology 41


HoChiMinh City University of Technology Mechatronics Department

Example 2:

Certain workpieces tend to deform easily during processing and clamping. In the interests of
accuracy and to prevent deformity, these must be fitted with a temporary clamp sleeve to allow
further processing. This is achieved by using the system shown above, which is partially
automated. The clamp sleeves are first placed into a carrier by hand. The workpiece is then moved
into the clamping position against the stop of the more powerful right-hand cylinder. This is
followed by longitudinal pressing. After this, the stop cylinder retracts again, allowing the feed
cylinder in a second action to push the finished workpiece onto the outward transfer conveyor belt.
The clamp sleeves must be removed again after processing has been completed. The workpieces
are moved by hand from the conveyors to the side setdown area.

ME2009 Control system technology 42

You might also like