1. MECHATRONICS 2
Interfacing and PC based
Computer
AIN SHAMS UNIVERSITY
FACULTY OF ENGINEERING
MECHATRONICS ENGINEERING DEPARTMENT
1
Dr. Shady A. Maged
2. Interfacing in the Mechatronics System
ConfigurationMechanical
System
Controller
Actuators
Sensors
Signal
Processing
Power
Amplification
Interfacing
2
3. Interfacing a switch (input device) to a
digital circuit:
Using pull-up resistor Using pull-down resistor
When the switch is open the voltage of the gate
input is pulled up to the level of Vin. When the
switch is closed, the input voltage at the gate
goes to ground.
Pull-up and pull-down resistors are used in electronic logic circuits to ensure that
inputs to logic systems settle at expected logic levels if external devices are
disconnected or high-impedance is introduced. They may also be used at the
interface between two different types of logic devices, possibly operating at different
power supply voltages.
3
5. Mechanical switch
The function of a switch is simple. When we press a switch,
two contacts are joined together and connection is made. Still,
it is not that simple. The problem lies in the imperfection of
mechanical contacts. That is to say, before contact is made or
cut off, there is a short time period when vibration (oscillation)
can occur as a result of unevenness of mechanical contacts, or
as a result of the different speed in pressing a key (this
depends on person who presses the key). The term given to
this phenomenon is called SWITCH (CONTACT) DEBOUNCE.
Bouncing while transition from high to low
Bouncing while transition from low to high
Switch De-bouncing
5
6. Due to this problem, there won't be a clean transition from a state to another, but
instead there will be a series of high and low states spikes as shown in figure 1B.
The 'unknown' range can be recognized by some a digital circuit as 1 or 0, but in a
completely random manner.
This series of spikes can be interpreted by a microcontroller (or any digital circuit)
as if the button was pressed many times. It may even have happened to you
before when you connected a switch to a counter of any kind, and notice that one
press on the button is sometimes counted as more than one push.
Fig. 1A: A simple switch configuration
without a de-bouncing circuit
Fig. 1B: Behavior of a switch without a
de-bouncing circuit
De-bouncing
6
7. There are 2 common solutions to this problem. Analog solution and digital
micro-controller based solution. Both are commonly used, and sometimes,
both are used at the same time to provide a very stable design.
The analog de-bouncing circuit
Fig. 2A: A switch with a de-
bouncing circuit
Fig. 2B: A switch with a de-
bouncing circuit
• The analog solution relies principally on a capacitor, which plays the role of
resisting the voltage changes on the output.
De-bouncing
7
8. The software based de-bouncing
the idea here is not to prevent voltage spikes to occur, but rather to record them
and analyze them in real-time using simple software routines.
The timing T1, T2 and T3 corresponds to the low (logic =0) pulses being sent to the
microcontroller from the switch. T1 and T2 are the length of the reading we want to
get rid off, or more scientifically, that we want to filter. T3 is a valid reading that we
want to take in account. It is clear that the difference between those three periods
is their length, and that is how the micro-controller will be able to differentiate
between valid and un-valid pulses.
8
9. There are many ways to implement digital de-bouncing or filtering using a
microcontroller, one simple method is to make a counter count up as long as
the signal is Low, and reset this counter when the signal is high. If the counter
reach a certain fixed value, which should be 1 or 2 times bigger than T1 or T2
(noise pulses), this means that the current pulse is a valid pulse (corresponds
to T3 in our example).
Another way to de-bounce using software is to wait for a certain time interval until
the switch oscillations disappear. i.e. after the MICROCONTROLLER realizes the
first state transition it waits for a certain period say (100 ms) then it takes the
action that corresponds to this transition.
9
10. Example:
Required: Write a code that turns
the LED on when T2 is pressed,
and turns it off when T1 is
pressed. You have to come over
switch de-bounce in your software
This circuit has 2 inputs and 1 output
Input T1 Output LED Input T2 Output LED
RA3 RB7 RA2 RB7
0 Unchanged 0 Unchanged
1 OFF (0) 1 ON (1)
10
11. RA3 and RA2 are
inputs, RB7 is output
T2
pressed?
T2
pressed?
YES
LED is ON
NO
NO
Call Delay
YES
START
LED is OFF
T1
pressed?
T1
pressed?
CallDelay
YESNO
NO
Flow Chart
11
12. Transistor
A transistor is a semiconductor device used to amplify and switch electronic
signals. Transistors are commonly used as electronic switches. You can imagine
the collector and emitter as two contact points of a switch. If the base is activated,
then both contact points are closed Together; otherwise they are opened.
Interfacing an inductive load to a
digital circuit through a transistor
When the voltage
across an inductive
load is reduced or
removed a sudden
spike voltage is
seen across the
load
• A resistor (e.g., 1k) to limit
the base current while
ensuring the transistor is in
full saturation
• 24 V DC capable of at least
1A of current
• power diode capable of
carrying at least 1A for
flyback protection (Free
Wheeling Diode) used
whenever inductive loads are
switched off by silicon
components
• ground
12
13. Darlington Pair
The above schematic represents a transistor called as a Darlington Pair which
usually comes in a single package. The advantage of this combination is that the
current gain is the product of two individual transistor gains and can exceed 10000.
They may often be found in power circuits for mechatronic systems.
13
14. Seven-Segment Display (multiplexing)
Circuit diagram for Common
Cathode 7-Segment Display
Circuit diagram for Common
Anode 7-Segment Display
•The segments in a 7-segment display are arranged to form a single digit from 0
to F
• Even though LCD displays are more comfortable to work with, 7-segment
displays are still standard in the industry.
• The 8 LEDs inside each display can be arranged with a common cathode or
common anode.
14
15. •The simplest way to drive a display is via a display driver. These are available for up
to 4 displays.
LT =Lamp Test, BI = Blanking Input, LE = Latch Enable
15
16. •Alternatively displays can be driven by a microcontroller and if more than one
display is required, the method of driving them is called "multiplexing.”
• If a single display is to be driven from a microcontroller, 7 lines will be needed plus
one for the decimal point. For each additional display, only one extra line is needed.
• To produce a 4, 5 or 6 digit display, all the 7-segment displays are connected in
parallel. The common line (the common-cathode line) is taken out separately and
this line is taken low for a short period of time to turn on the display.
• Each display is turned on at a rate above 100 times per second, and it will
appear that all the displays are turned on at the same time. As each display is
turned on, the appropriate information must be delivered to it so that it will give the
correct reading.
16
17. Example
Required: Write a code that displays the number 21 on the common cathode 7-
segment displays using transistors where the hardware connections are shown
above.
17
18. Optocoupler combines a LED
and photo-transistor in the same
case. The purpose of an
optocoupler is to separate two
parts of a circuit.
Why optocoupler?
Interference. One part of a circuit may be in a location where it picks up a
lot of interference (such as from electric motors, welding equipment,
petrol motors etc.) If the output of this circuit goes through an optocoupler to
another circuit, only the intended signals will pass through the
optocoupler. The interference signals will not have enough "strength" to
activate the LED in the optocoupler and thus they are eliminated. To protect a
section of the device. Typical examples are industrial units with lots of
interferences which affect signals in the wires. If these interferences affect the
function of control section, errors will occur and the unit will stop working.
Optocoupler
18
19. •Simultaneous separation and intensification of a signal. A signal as low as
3v is able to activate an optocoupler and the output of the optocoupler can be
connected to an input line of a microcontroller. The microcontroller requires an
input swing of 5v and in this case the 3v signal is amplified to 5v. It can also be
used to amplify the current of a signal.
• High Voltage Separation. Optocouplers have inherent high voltage
separation qualities. Since the LED is completely separate from the photo-
transistor, optocouplers can exhibit voltage isolation of 3kv or higher.
Optocouplers can be used as input or output device. They can have
additional functions such as Schmitt triggering (the output of a Schmitt trigger
is either 0 or 1 - it changes slow rising and falling waveforms into definite
low or high values). Optocouplers are packaged as a single unit or in groups of
two or more in one housing. They are also called PHOTO INTERRUPTERS
where a spoked wheel is inserted in a slot between the LED and phototransistor
and each time the light is interrupted, the transistor produces a pulse.Each
optocoupler needs two supplies in order to function. They can be used with one
supply, but the voltage isolation feature is lost.
19
20. Example:
This example is a counter, used for counting products on production line. Let the
sensor be a micro-switch. Each time the switch is closed, the LED is illuminated.
The LED transfers the signal to the photo-transistor and the operation of the photo-
transistor delivers a LOW to input RA4 of a microcontroller.
Required: write a program that counts the products, and indicates the current
count on any of the outputs of the microcontroller.
Input T1 Output LED
RA4/TCK0 PORTB
Each negative edge Increase by 1
20
21. The Relay
The relay is an electromechanical device, which transforms an electrical signal
into mechanical movement. It consists of a coil of insulated wire on a metal core,
and a metal armature with one or more contacts. When a supply voltage was
delivered to the coil, current would flow and a magnetic field would be produced
that moves the armature to close one set of contacts and/or open another set.
When power is removed from the relay, the magnetic flux in the coil collapses and
produces a fairly high voltage in the opposite direction.
21
22. Interfacing an AC load to a digital
circuit through a relay
Interfacing a DC load to a digital
circuit through a relay
Examples:
22
23. Example on Micro:
Required: Write a code that activates the Relay on when T2 is pressed, and
turns it off when T1 is pressed. You have to come over switch debounce in your
software.
23
27. Controlling the power delivered to the motor to control
the speed .
PWM (pulse width modulation) is the most common way to control the
power delivered to the motor, thus the speed of the motor. It's also the
most efficient method. This article does not intent to explain PWM, but
here is how in works, in very simplified way: The microcontroller generate
pulses of electricity for the motor (instead of constant DC voltage). The
frequency of those pulses remain constant, while the ON period of the
pulse is varied to increase or decrease the speed of the motor. the
Average voltage of the wave of pulses increases gradually as the duty
cycle increases (duty cycle is the ratio of the 'ON' period over the total
period of the pulse). As the average voltage increases, the speed of the
motor will increase gradually.
27
28. Controlling the power delivered to the motor to control
the speed .
Making a speed control program
using the PWM theory is very
simple, which is a code
targeted for any microcontroller
to run the motor at half speed
(50/100) and where the Enable
pin of the motor controller is
connected to specific pin of the
microcontroller:
28
30. H-Bridge
High Side
Left
High Side
Right
Lower
Left
Lower
Right Quadrant Description
On Off Off On Motor goes Clockwise
Off On On Off Motor goes Counter-clockwise
On On Off Off Motor "brakes" and decelerates
Off Off On On Motor "brakes" and decelerates
30
35. Brief Circuit explanation
This is an implementation of the L298 to drive motors and inductive
loads up to 4A continuously. Initially the L298 contains 2 separate
channels, each one capable of driving 2A loads. Connecting them
in parallel as in the schematic makes a single 4A driver.
The two wire connections W1 and W2 are to control the direction
of the motor according to that table:
35
36. Brief Circuit explanation
The 2 transistors Q1 and Q2 act as an OR gate enabling the L298 if
any of the two signals W1 or W2 are high. If both W1 and W2 are
low (logic 0), the driver is disabled and provides a high impedance
on its output, causing an eventually connected motor to move
freely with its inertia.
C1 and C2 are 10 nF decoupling capacitors.
D1 to D4 are flyback diodes. Their forward current should
correspond with the the expected load.
The two leads of the motor are directly connected to J1.
The circuit can be powered with 9 to 35V
36
38. The Analog-to-Digital Converter
There is a device commonly
in microcontrollers called
ADC (Analog to digital
converter). An ADC converts
a varying (analog) voltage
level into a corresponding
digital number.
ADC 0808 which has 8 –
analog inputs (8 - channels)
and gives an 8 – bits value
out as the result of
conversion
38
39. ADC0808 (8-channels 8-bit)
8-channels (8-analog input)
Example : 8 - sensors
8 – bit value out as a result
Of the conversion
3-MUX input select
address lines (Appendix A)
DATA BUS
+5V
GROUND
SUPPLY
500 kHz
39
41. CONTROL LINES
Output Enable line for reading
the ADC (TRI STATE OUTPUT LATCH)
End of Convert line that signals the
Host that the conversion is complete
normaly 1 when finish convert to 0.
Start / ALE line that are tied together
And connected as 1 to the host
45. Example on MICRO with built-in ADC
Construct the hardware connection and the flowchart (for a
Microcontroller) that reads analogue value from channel 2 and
displays it on PORTD (lower 8 bits) and PORTB (2 most significant
bits).
45
45
46. Example on MICRO with built-in ADC
Construct the hardware connection and the flowchart (for a
Microcontroller) that reads analogue value from channel 2 and
displays it on PORTD (lower 8 bits) and PORTB (2 most significant
bits).
46
46
RA2 is input, Port D, RB6
and RB7 are outputs
START
Analogue
value=Temp_res
Port D=Temp_res
Port B=Temp_res>>2
49. Example: Open loop Speed Controller
Write a program that control the speed and the direction of
the DC motor using H-bridge and PWM signal with a duty
cycle based on the analogue input comes from
potentiometer
49
50. SOFTWARE APPROACH
1. Initialize the I/O ports
2. Controlling the ADC and reading the digital value (I/p to the micro)
3. Check if the I/p is 0V therefore the motor is OFF
4. Check if the I/p is 5V therefore the motor is ON
5. knowing that the Duty Cycle= t_high/T *100(%)
and to obtain at 5V, D=100%,
6. Calculate t_high and t_low from the following equations
t_high = digital I/P from input port
t_low= 255 - t_high
7. Obtain delay equals to t_high
8. Clear the output and then obtain delay equals to t_low
9. set the output
10. Repeat steps 7,8 and 9.
50
51. Notice how the heat sink is firmly attached to the driver. this
protects the IC from eventually burning-up from stall
currents .
51
54. Objective:
you will learn here how to implement a digital
proportional gain controller, i.e. a servomotor
controller. The objective of this is to write a program
that generates a PWM signal with a duty cycle based
on the output of a proportional gain controller whose
input is a signal from a potentiometer or encoder
mounted on the shaft of the DC motor. This program
is similar in concept to the open loop speed
controller; however the duty cycle of the PWM signal
is now determined from the error of the input-
feedback calculation.
54
55. Problem Statement:
Write a program that controls the position of the DC motor. The
program should implement a proportional control algorithm. The
system diagram looks like
55
63. General software approach
The program should perform the following sequence of tasks:
Sample the position using ADC.
Calculate the amount and direction of control using a P, or a
PID equation
Output the PWM signal based on the control amount (related
to position error)
Recall from control, that the simplest closed-loop,
proportional feedback controller defines the output as:
Output = K. (Input-Feedback) = K.error
You will need to scale your error signal by some value, k, for
your PWM duty cycle to be high enough to achieve acceptable
system time response. You can easily multiply the error signal
by factors of 2
63
65. LogicTTLRS-232
High+2V … +5V-15V … -3V
Low0V … +0.8V+3V … +15V
MAX 232
The MAX232 is an integrated circuit that
converts signals from an RS-232 serial port to
signals suitable for use in TTL compatible
digital logic circuits.
65
84. Data acquisition is the process of measuring an electrical or
physical phenomenon such as voltage, current, temperature,
pressure, or sound. PC-based data acquisition uses a combination
of modular hardware, application software, and a computer to take
measurements. While each data acquisition (DAQ) system is defined
by its application requirements, every system shares a common goal
of acquiring, analyzing, and presenting information. Data
acquisition systems incorporate signals, sensors, actuators, signal
conditioning, DAQ devices, and application software.
Data Acquisition Cards
84
85. Example
NI USB-6008
The National Instruments USB-6008 provides basic data acquisition functionality
Analog Inputs: 8 SE/4 DI · 10 kS/s · 12 bits
Analog Outputs: 2 · 150 S/s · 12 bits
Digital I/O: 12 DIO
Counter/Timers: 1 · 32 bits · 5 MHz
Measurement Type: Voltage
85
86. NI USB-6251 BNC
The National Instruments USB-6251 BNC is a USB high-performance M
Series multifunction data acquisition
Analog Inputs: 8 SE/4 DI · 1.25 MS/s · 16 bits
Analog Outputs: 2 · 2.86 MS/s · 16 bits
Digital I/O: 24 DIO · 1 MHz
Counter/Timers: 2 · 32 bits · 80 MHz
Measurement Type: Quadrature encoder, Voltage
Example
86
88. Measurement System
Differential Mode (DI)
•Differential Mode
• Two channels used for each signal
• ACH 0 is paired with ACH 8, ACH 1 is paired with ACH 9, etc.
• Rejects common-mode voltage and common-mode noise
88
89. Differential Mode
89
Why would I ever want to use
differential mode?
Placing your measurement system in
differential mode will give you better
measurements, because it allows the
amplifier to reject common-mode voltage
and any common-mode noise that is
present in your signal. Common-mode
voltage is any voltage present at the
instrumentation amplifier inputs with
respect to the amplifier ground.
91. The big issue with digital conversion:
A continuous value is made discrete
1. In amplitude (Y axis issues)
• Resolution
• Saturation
2. In time (X axis issues)
• Aliasing
• Leakage
• Frequency resolution
Digital Data Acquisition
91
92. Analogue Digital conversion:
quantization:
A continuous value is compared with a series of fixed,
discrete intervals (states)
encoding:
The interval mean value is converted into a digital, usually
binary, chain of elements
Digital Data Acquisition
92
93. Binary representation:
• Data type length = N bit
• Binary encoding = O / 1
Having only two states possible per element allows
for very robust handling and trasmission systems
since the diffence between states can be high and
electronics is simple and cheap.
Digital Data Acquisition
93
94. DIGITAL RESOLUTION
Having N bit data length 2N different states
3 bit 23 =8 different states (1 byte = 8 bit)
8 bit 28 =256 states
10 bit 210 =1024 states
12 bit 212 =4096 states
14 bit 214 =16384 states
16 bit 216 =65536 states
Digital Data Acquisition
94
95. AD converter transfer function is not linear: output = 2N states
input = continuous value
input
output
Digital Data Acquisition
95
96. Resolution = minimum variation of the input quantity
that can be detected by the AD converter.
It is equal to the value of the least significant bit (the
smallest one)
LSB=“least significant bit”
1 LSB = FS / 2N
Digital Data Acquisition
96
97. Resolution depends on both the full scale input of
DAQ converter and bit length of data field
Es: FS=10 V N=3 bit LSB=1.25 V
FS=10 V N=8 bit LSB=39 mV
FS=10 V N=12 bit LSB=2.44mV
n
FSFS
resolution
2
minmax
Digital Data Acquisition
97
101. SAMPLING:
Conversion of a time continuous value into a chain of values
V
(ti , Vi) i=1,...... N
t
t
V
Digital Data Acquisition
101
102. Both V amplitude and it’s time coordinates are discrete
values depending on ADC capabilities and configuration
SAMPLING TIME tC = ti - ti-1
SAMPLING FREQUENCY fC = 1 / tC
t
V
ti-1 ti ti+1
Digital Data Acquisition
102
103. With sampling frequency can be used to
represent a signal without altering it?
t
V
t
V
both OK, but somehow different
Digital Data Acquisition
103
104. If sampling frequency is too low a problem with frequency
representation can occurr: we have “aliasing”
t
V
Sampling signal is no longer recognizable, and its
frequency seems lower than the original one.
Digital Data Acquisition
104
105. The issue of aliasing is related to the ratio between sampling
frequency fS and signal frequency fA
fS < 2 fA “aliasing” occurs
fS > 2 fA
fS = 2 fA
fS < 2 fA
Digital Data Acquisition
105
106. Nyquist-Shannon theorem:
if a continuous signal with a top limited
bandwidth contains only components with
frequency up to fAmax therefore a coherent
representation could be achieved by a
sampling frequency fS > 2 fAmax
Digital Data Acquisition
106
107. f S = 1 / tS
fA = 1 / TA
being fS > 2fA tS < TA / 2
We require at least two samples
for each half period...
Digital Data Acquisition
107
108. Nyquist Example
Aliased Signal
Adequately Sampled
for Frequency Only
(Same # of cycles)
Adequately Sampled
for Frequency and
Shape
100Hz Sine Wave
100Hz Sine Wave
Sampled at 100Hz
Sampled at 200Hz
Sampled at 1kHz100Hz Sine Wave
108
111. Digital-to-Analog Converters (DACs)
• A DAC accepts digital information and transforms it into an analog voltage.
• The digital information is in the form of a binary number (word) with some fixed number
of bits.
• A unipolar DAC converts a digital word into an analog voltage by scaling the analog
output to be zero when all bits are zero and some maximum value when all bits are
one.
• This can be mathematically represented by treating the binary number that the word
represents as a fractional number.
• In this context, the output of the DAC can be defined as a scaling of some reference
voltage:
• The minimum Vout is zero, and the maximum is determined by the size of the binary
word because, with all bits set to one, the decimal equivalent approaches VR as the
number of bits increases.
•
111