Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

HoChiMinh City University of Technology Mechatronics Department

HoChiMinh City University of ME2009 - Control System Technology


Technology
Lab Assignment 1
Faculty Mechanical of Engineering
Arduino, Led, Photoelectric Sensor, and
Mechatronics Department
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.

ME2009 Control system technology 1


HoChiMinh City University of Technology Mechatronics Department

2. LAB DEVICES
2.1 Arduino Uno Board

Figure 1. Arduino Uno Board


2.2 Control System Kit 01

Figure 2. Control System Kit 01

Module No. Device Name Signal


1 Terminal

ME2009 Control system technology 2


HoChiMinh City University of Technology Mechatronics Department

+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

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.

ME2009 Control system technology 3


HoChiMinh City University of Technology Mechatronics Department

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

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

ME2009 Control system technology 4


HoChiMinh City University of Technology Mechatronics Department

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: \
Documents\Arduino\libraries). You can find or change the location of your sketchbook folder
at File > Preferences > Sketchbook location (shown in Figure 7)

ME2009 Control system technology 5


HoChiMinh City University of Technology Mechatronics Department

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 6


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 7


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 8


HoChiMinh City University of Technology Mechatronics Department

3.4 WIRING DIAGRAM AND ELECTRICAL CIRCUIT

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 9


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 10


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);

ME2009 Control system technology 11


HoChiMinh City University of Technology Mechatronics Department

24. }
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

ME2009 Control system technology 12


HoChiMinh City University of Technology Mechatronics Department

• Pin description: • Red - 5V • Green – GND • Yellow - Digital output


• 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 13


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 Figure 17. The principle of
necessary, otherwise, the 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 74HC595

Arduino pin and use 'analogWrite' to control the Figure 18. Pin of 74HC595
brightness of the LEDs. This pin is active low, so
we 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
Figure 19. Seven- segment Led
as DP below). 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 14


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

ME2009 Control system technology 15


HoChiMinh City University of Technology Mechatronics Department

indicates the current position of the shaft, making them angle transducers. The output of
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 16


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 17


HoChiMinh City University of Technology Mechatronics Department

HoChiMinh City University of ME2009 - Control System Technology


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 18


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 19

You might also like