The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by tharisheeni04, 2022-11-16 22:50:48

PLC_LDMICRO_NOTES_students (1)

PLC_LDMICRO_NOTES_students (1)

DJM40082 PLC MED

5) RESET

Trto Citems
----{RES}---- ----{RES}----

This instruction resets a timer or a counter. TON and TOF timers
are automatically reset when their input goes false or true, so
RES is not required for these timers. RTO timers and CTU/CTD
counters are not reset automatically, so they must be reset by
hand using a RES instruction. When the input is true, the counter
or timer is reset; when the input is false, no action is taken. This
instruction must be the rightmost instruction in its rung.

F) EDGE
a) One Short Rising
In the PLC ladder diagram a one shot instruction creates a
one shot signal output. For the one shot
rising OSF instruction, it output a high pulse for one PLC
cycle time - for example 10 mS, whenever its input true.
This output pulse only create one's until the system is
reset.

This instruction is an auxiliary to other instruction in the
same rung. It's place in front of other instructions to make
them run only once when the program started.
Let take a look at a simple ladder diagram example.

51

DJM40082 PLC MED

An OSF activate the turn off delay T0F that create a high
delay time for 10 second before it's fall. So the output
coil Y0 turned on for only 10 second in the PLC program.
b) One Shot Low Level
A one-shot low level (OSL) instruction is a normally output
true instruction. Whenever its input is false then it output
false for a PLC scan cycle.
For more details let see its timing diagram.

52

DJM40082 PLC MED

This instruction is useful in the case that the user needs to
trigger the action on the falling to low level of the input
signal.
In the following example, we create a sample monitoring of
this instruction. Whenever the user switch the input
contact to the ground, the contact makes a input false
signal to the OSL. This OSL again trigger a low level pulse
of one PLC scan cycle of 10 mS.

The output coil Y0 is normally output true in the overall
PLC program. The output coil Y1 outputs true when there's
nothing happen.
G) MASTER CONTROL RELAY

-{MASTER RLY}-

By default, the rung-in condition of every rung is true. If a
master control relay instruction is executed with a rung-in
condition of false, then the rung-in condition for all following
rungs becomes false. This will continue until the next master
control relay instruction is reached (regardless of the rung-in

53

DJM40082 PLC MED

condition of that instruction). These instructions must
therefore be used in pairs: one to (maybe conditionally) start
the possibly-disabled section, and one to end it.

H) COMPARE

[var ==] [var >] [1 >=]
-[ var2 ]- -[ 1 ]- -[ Ton]-

[var /=] [-4 < ] [1 <=]
-[ var2 ]- -[ vartwo]- -[ Cup]-

If the input to this instruction is false then the output is false.
If the input is true then the output is true if and only if the
given condition is true. This instruction can be used to
compare (equals, is greater than, is greater than or equal to,
does not equal, is less than, is less than or equal to) a variable
to a variable, or to compare a variable to a 16-bit signed
constant.

I) MOVE

{destvar := } {Tret := }

-{ 123 MOV}- -{ srcvar MOV}-

When the input to this instruction is true, it sets the given
destination variable equal to the given source variable or
constant. When the input to this instruction is false nothing
happens. You can assign to any variable with the move
instruction; this includes timer and counter state variables,
which can be distinguished by the leading `T' or `C'. For
example, an instruction moving 0 into `Tretentive' is
equivalent to a reset (RES) instruction for that timer. This
instruction must be the rightmost instruction in its rung.

J) ARITHMETIC OPERATION

{ADD kay :=} {SUB Ccnt :=}
-{ 'a' + 10 }- -{ Ccnt - 10 }-

{MUL dest :=} {DIV dv := }
-{ var * -990 }- -{ dv / -10000}-

When the input to this instruction is true, it sets the given
destination variable equal to the given expression. The
operands can be either variables (including timer and counter
variables) or constants. These instructions use 16 bit signed
math. Remember that the result is evaluated every cycle when
the input condition true. If you are incrementing or

54

DJM40082 PLC MED

decrementing a variable (i.e. if the destination variable is also
one of the operands) then you probably don't want that;
typically you would use a one-shot so that it is evaluated only
on the rising or falling edge of the input condition. Divide
truncates; 8 / 3 = 2. This instruction must be the rightmost
instruction in its rung.

A NOTE ON USING MATH
====================

Remember that LDmicro performs only 16-bit integer math.
That means that the final result of any calculation that you
perform must be an integer between -32768 and 32767. It
also mean that the intermediate results of your calculation
must all be within that range.

For example, let us say that you wanted to calculate y =
(1/x)*1200, where x is between 1 and 20. Then y goes
between 1200 and 60, which fits into a 16-bit integer, so it is
at least in theory possible to perform the calculation. There
are two ways that you might code this: you can perform the
reciprocal, and then multiply:

|| {DIV temp :=} ||

||---------{ 1 / x }----------||

|| ||

|| {MUL y := } ||

||----------{ temp * 1200}----------||

|| ||

Or you could just do the division directly, in a single step:

|| {DIV y :=} ||
||-----------{ 1200 / x }-----------||

Mathematically, these two are equivalent; but if you try them,
then you will find that the first one gives an incorrect result of
y = 0. That is because the variable `temp' underflows. For
example, when x = 3, (1 / x) = 0.333, but that is not an
integer; the division operation approximates this as temp = 0.
Then y = temp * 1200 = 0. In the second case there is no
intermediate result to underflow, so everything works.

If you are seeing problems with your math, then check
intermediate results for underflow (or overflow, which `wraps
around'; for example, 32767 + 1 = -32768). When possible,
choose units that put values in a range of -100 to 100.

55

DJM40082 PLC MED

When you need to scale a variable by some factor, do it using
a multiply and a divide. For example, to scale y = 1.8*x,
calculate y = (9/5)*x (which is the same, since 1.8 = 9/5),
and code this as y = (9*x)/5, performing the multiplication
first:

|| {MUL temp :=} ||

||---------{ x * 9 }----------||

|| ||

|| {DIV y :=} ||

||-----------{ temp / 5 }-----------||

This works for all x < (32767 / 9), or x < 3640. For larger
values of x, the variable `temp' would overflow. There is a
similar lower limit on x.

K) A/D CONVERTER READ Aname

--{READ ADC}--

LDmicro can generate code to use the A/D converters built in
to certain microcontrollers. If the input condition to this
instruction is true, then a single sample from the A/D
converter is acquired and stored in the variable `Aname'. This
variable can subsequently be manipulated with general
variable operations (less than, greater than, arithmetic, and so
on). Assign a pin to the `Axxx' variable in the same way that
you would assign a pin to a digital input or output, by double-
clicking it in the list at the bottom of the screen. If the input
condition to this rung is false then the variable `Aname' is left
unchanged.
For all currently-supported devices, 0 volts input corresponds
to an ADC reading of 0, and an input equal to Vdd (the supply

56

DJM40082 PLC MED

voltage) corresponds to an ADC reading of 1023. If you are
using an AVR, then connect AREF to Vdd. You can use
arithmetic operations to scale the reading to more convenient
units afterwards, but remember that you are using integer
math. In general not all pins will be available for use with the
A/D converter. The software will not allow you to assign non-
A/D pins to an analog input. This instruction must be the
rightmost instruction in its rung.

L) SET PWM DUTY CYCLE

duty_cycle
-{PWM 32.8 kHz}-

LDmicro can generate code to use the PWM peripheral built in
to certain microcontrollers. If the input condition to this
instruction is true, then the duty cycle of the PWM peripheral
is set to the value of the variable duty_cycle. The duty cycle
must be a number between 0 and 100; 0 corresponds to
always low, and 100 corresponds to always high. (If you are
familiar with how the PWM peripheral works, then notice that
that means that LDmicro automatically scales the duty cycle
variable from percent to PWM clock periods).
You can specify the target PWM frequency, in Hz. The
frequency that you specify might not be exactly achievable,

57

DJM40082 PLC MED

depending on how it divides into the microcontroller's clock
frequency. LDmicro will choose the closest achievable
frequency; if the error is large then it will warn you. Faster
speeds may sacrifice resolution.
This instruction must be the rightmost instruction in its rung.
The ladder logic runtime consumes one timer to measure the
cycle time. That means that PWM is only available on
microcontrollers with at least two suitable timers. PWM uses
pin CCP2 (not CCP1) on PIC16 chips and OC2 (not OC1A) on
AVRs.

58

DJM40082 PLC MED

EXERCISE #3
Write and simulate the following Ladder Diagram. Give a comment on
particular event.
i) OSR & OSL

59

DJM40082 PLC MED

ii) ADC 1

iii) ADC 2 - Battery Charger
iv) PWM 1

60

DJM40082 PLC MED

v) PWM2 – DC motor speed control
vi) PWM3 – Increase & Decrease button

61

DJM40082 PLC MED

vii) PWM4 – Forward & Reverse Button

62

DJM40082 PLC MED

M)UART (SERIAL) RECEIVE

var
--{UART RECV}--

LDmicro can generate code to use the UART built in to certain
microcontrollers. On AVRs with multiple UARTs only UART1
(not UART0) is supported. Configure the baud rate using
Settings -> MCU Parameters. Certain baud rates may not be
achievable with certain crystal frequencies; LDmicro will warn
you if this is the case.
If the input condition to this instruction is false, then nothing
happens. If the input condition is true then this instruction
tries to receive a single character from the UART. If no
character is read then the output condition is false. If a
character is read then its ASCII value is stored in `var', and
the output condition is true for a single PLC cycle.

N) UART (SERIAL) SEND

var
--{UART SEND}--

LDmicro can generate code to use the UARTs built in to certain
microcontrollers. On AVRS with multiple UARTs only UART1 (not
UART0) is supported. Configure the baud rate using Settings ->
MCU Parameters. Certain baud rates may not be achievable
with certain crystal frequencies; LDmicro will warn you if this is
the case.
If the input condition to this instruction is false, then nothing
happens. If the input condition is true then this instruction
writes a single character to the UART. The ASCII value of the
character to send must previously have been stored in `var'.
The output condition of the rung is true if the UART is busy
(currently transmitting a character), and false otherwise.
Remember that characters take some time to transmit. Check
the output condition of this instruction to ensure that the first
character has been transmitted before trying to send a second
character, or use a timer to insert a delay between characters.
You must only bring the input condition true (try to send a
character) when the output condition is false (UART is not
busy). Investigate the formatted string instruction (next) before
using this instruction. The formatted string instruction is much
easier to use, and it is almost certainly capable of doing what
you want.

63

DJM40082 PLC MED

a) UART Single

i) UART Simple
This program initially use a microcontroller A
(Transmitter) communicating with microcontroller B
(Receiver) to energize an output from it.
i) UART Simple TX

64

DJM40082 PLC MED

Explaination:
a) When Xon activate , R1 will be energize. Then with

MOVE instruction, data ‘49’ will send via UART SEND
to microcontroller B R1.
b) Next , if Xoff avtivated , data ‘50’ will send via UART
SEND, thats why , it will change instruction to RX for
another task.
c) Rung 3 was to to activate signal UART SEND and OSR
was initially to allow an easy data transfer to prevent
from data crossing.
ii) UART Simple RX

Explaination:
This RX programming will received an instruction send
by previous TX porgram.
a) When Receiver received data ‘49’, the instruction will

COMPARE to check data status. If data received as
same as set, it will activate an output Y9.
b) ‘49’ was an ASCII code translated to 1 ‘50’ = 2.

65

DJM40082 PLC MED

c) When data ‘50’ received, COMPARE in Rung 3 will
activate internal_relay R2 and this will suddenly
deactivated Y9.

66

DJM40082 PLC MED

EXERCISE #4
Write and simulate the following Ladder Diagram. Give a comment on
particular event.

ii) UART PWM

iii) Thermometer

67

DJM40082 PLC MED

iv) 7 segment
68

DJM40082 PLC MED

v) Stepper Motor with ULN2803 Darlington Pair

69

DJM40082 PLC MED

vi) UART_ADC_PWM
1) TX

2) RX

70

DJM40082 PLC MED

71

DJM40082 PLC MED

CHAPTER 7
INTERFACE

72

DJM40082 PLC MED

7.0 Interfacing between Microcontroller Board and Devices

Interfacing can be defined as transferring data between
microcontrollers and interfacing peripherals such as sensors,
keypads, microprocessors, analog to digital converters or
ADC, LCD displays, motors, external memories, even with
other microcontrollers, some other interfacing peripheral
devices and so on or input devices and output devices. These
devices that are interfacing with PIC16F877
microcontroller are used for performing special tasks or
functions are called as interfacing devices.
Interfacing is a technique that has been developed and being
used to solve many composite problems in circuit designing
with appropriate features, reliability, availability, cost, power
consumption, size, weight, and so on. To facilitate multiple
features with simple circuits, microcontroller is
interfaced with devices such as ADC, keypad, LCD display
and so on.

a) Standard Interfacing Circuits
1. The Standard Transistor Interfacing Circuit
2. Using a Darlington Driver IC
3. The Standard FET Interfacing Circuit

b) Output Device Interfacing
1. LED
2. Relay
3. Signal Lamp
4. Buzzer
5. Piezo Sounder & Speakers
6. Solar & DC (“toy”) Motors
7. Unipolar Stepper Motor
8. Bipolar Stepper Motor
9. Radio-Control Servo
10. Counter Module
11. Seven Segment Display
12. Solenoid & Isonic Solenoid Valve
13. Smart Wire / Springs

c) Input Device Interfacing
1. Switches
2. Potentiometers
3. Light Dependant Resistor (LDR)
4. Thermistor

73

DJM40082 PLC MED

d) Advanced Component Interfacing
1. Liquid Crystal Display (LCD)
2. Serial Communication with a Computer

7.1 STANDARD INTERFACING CIRCUITS
7.1.1 The Transistor Interfacing Circuit

Many output devices will require a transistor switching circuit.
In most cases a darlington pair formed from two transistors is
ideal.

However this circuit requires that two separate transistors are
used. It is possible to buy a device that contains the two
transistors in a single package. This transistor is called the
BCX38B, and can switch currents up to 800mA. This is the
transistor used in all the circuits through this book.

Note that it is usual to connect a back emf suppression diode
across the output device. This is essential with devices such as
relays, solenoids and motors which create a back emf when
power is switched off. The diode type 1N4001 is the device
recommended.

74

DJM40082 PLC MED

7.1.2 Using a Darlington Driver IC
If a number of output devices are being controlled it may be
necessary to use a number of output transistors. In this case it
will often be more convenient to use a ULN2003 Darlington
driver IC. This is simply a 16 pin ‘chip’ that contains 7
darlington transistors similar in value to the BCX38B. The
‘chip’ also contains internal back emf suppression diodes and
so no external 1N4001 diodes are required.

A device called the ULN2803 Darlington Driver IC is also
available. This is identical to the ULN2003 except that it is an
18 pin device and contains 8 darlington pairs instead of 7. If it
is necessary to pass relatively high currents through a device
it can be useful to ‘pair up’ drivers as shown with this circuit.

7.1.3 The Power MOSFET Interfacing Circuit
Power MOSFETs can be used instead of darlington transistor
pairs to switch medium power devices. The standard MOSFET
circuit is shown below. The device IRF530 is a suitable power
MOSFET to use in this circuit. Note that it is usual to connect a
back emf suppression diode across the output device. This is
75

DJM40082 PLC MED

essential with devices such as relays, solenoids and motors
which create a back emf when power is switched off. The diode
type 1N4001 is the device recommended.

7.2 OUTPUT DEVICE INTERFACING

7.2.1 Interfacing with Leds

The light-emitting diode is a semiconductor device capable of
emitting light when the current flow occurs through it. Led is a
common component In many devices and it finds various
applications in the day to day life. The factors such as reduced
power consumption, small size, switching speed, Longer
lifespan and durability make them a better choice over
incandescent light sources. The led finds applications from
simple indicators to advanced communication systems. This
tutorial will explain the method of interfacing LEDs with
PIC16F877A microcontroller and to develop a suitable
firmware. LED blinking is the most basic project with a
microcontroller to see a physical output. One can understand
the concept of input-output configurations of the general-
purpose I/O port in a microcontroller with a simple LED
blinking project.

Interfacing LED with Microcontroller Pin

The PIC microcontroller PIC16F877 is one of the most famous
microcontrollers in the industry. It has a total number of 40
pins and among which 33 pins are general-purpose input-
output pins. PIC16f877 has 40 pins among which 33 are
general-purpose input-output pins.The input/output pins are
integrated as 5 ports, PORTA,PORTB,PORTC,PORTD,PORTE.
These ports are used for input /output operations by the
microcontroller. There will be a data direction register (TRIS)
and a port register ( PORT) corresponding to each port in the
microcontroller. TRIS register is used for configuring the port

76

DJM40082 PLC MED

as input or output and the value in the port register will be the
state of the pins of the corresponding port.

The above image is the simple way of interfacing a LED with a
microcontroller. The anode is connected to the microcontroller
and cathode connected to ground through a resistor. When the
voltage at the microcontroller pin is high the LED will be
turned ON and when the voltage is low the LED will be turned
off.

7.2.2 Interfacing with Relay

Relay works same as typical switch. Mechanical relays use
temporary magnet made from electromagnetic coil. When we
provide enough current across this coil, it became energized
and pulls an arm. Due to that the circuit connected across the
relay can be closed or open. The Input and Output don’t have
any electrical connections and thus it isolates input and
output. Learn more about relay and its constructions here.
Relays can be found in different voltage ranges like 5V, 6V,
12V, 18V etc. In this project we will use 24V relay as our
working voltage is 24 Volts here.

Construction and working
Relays are made up of an electromagnet and a set of contacts
generally based on Single Pole Double Throw (SPDT) or Double
Pole Double Throw (DPDT) switching method. It has 3 pins to
perform a function –

• COM = Common, always connect to NC; it is the
• NC moving part of the switch.
• NO = Normally Closed, COM is connected to this
when the relay coil is off.
= Normally Open, COM is connected to this
when the relay coil is on.

77

DJM40082 PLC MED

.

7.2.3 Interfacing with DC Motor
DC Motors are electromechanical devices which use the
interaction of magnetic fields and conductors to convert the
electrical energy into rotary mechanical energy.

Electrical DC Motors are continuous actuators that convert
electrical energy into mechanical energy. The DC motor
achieves this by producing a continuous angular rotation that
can be used to rotate pumps, fans, compressors, wheels, etc.
As well as conventional rotary DC motors, linear motors are
also available which are capable of producing a continuous
liner movement. There are basically three types of
conventional electrical motor available: AC type Motors, DC
type Motors and Stepper Motors.
AC Motors are generally used in high power single or multi-
phase industrial applications were a constant rotational torque
and speed is required to control large loads such as fans or
pumps.

78

DJM40082 PLC MED

The “Brushed” DC Motor
A conventional brushed DC Motor consist basically of two
parts, the stationary body of the motor called the Stator and
the inner part which rotates producing the movement called
the Rotor or “Armature” for DC machines.
The motors wound stator is an electromagnet circuit which
consists of electrical coils connected together in a circular
configuration to produce the required North-pole then a South-
pole then a North-pole etc, type stationary magnetic field
system for rotation, unlike AC machines whose stator field
continually rotates with the applied frequency. The current
which flows within these field coils is known as the motor field
current.
These electromagnetic coils which form the stator field can be
electrically connected in series, parallel or both together
(compound) with the motors armature. A series wound DC
motor has its stator field windings connected in series with the
armature. Likewise, a shunt wound DC motor has its stator
field windings connected in parallel with the armature as
shown.

Series and Shunt Connected DC Motor

The rotor or armature of a DC machine consists of current
carrying conductors connected together at one end to
electrically isolated copper segments called the commutator.
The commutator allows an electrical connection to be made via
carbon brushes (hence the name “Brushed” motor) to an
external power supply as the armature rotates.
The magnetic field setup by the rotor tries to align itself with
the stationary stator field causing the rotor to rotate on its
axis, but can not align itself due to commutation delays. The
rotational speed of the motor is dependent on the strength of
the rotors magnetic field and the more voltage that is applied
to the motor the faster the rotor will rotate. By varying this
applied DC voltage the rotational speed of the motor can also
be varied.

79

DJM40082 PLC MED

a) DC MOTOR Foward and Reverse Control
i) H-bridge with four switches.
An H-bridge is a simple circuit that lets you control a
DC motor to go backward or forward. Normally use in a
microcontroller, to control motors.

A DC motor spins either backward or forward,
depending on how you connect the plus and the minus.
If you close switch SW1 and SW4, you have plus
connected to the left side of the motor and minus to
the other side. And the motor will start spinning in one
direction.

If you instead close switch SW2 and SW3, you have
plus connected to the right side and minus to the left
side. And the motor spins in the opposite direction.

80

DJM40082 PLC MED

ii) H-bridge with Four Transistors.

Since the transistor can be a switch, we will be able to
make the motor spin in either direction by turning on
and off the four transistors in the circuit above.

What Transistors To Use?
The transistors you choose must:

• Handle enough current
• Use PNP (or pmos) at the top
• Have a low voltage drop between collector and

emitter
Current
The most important thing is that all the transistors can
handle enough current for the motor. Otherwise it will
burn out.
For example, if the motor draws 1 Ampere of current,
you need transistors that can handle a minimum of 1
Ampere.
PNP (or pmos) transistors at the top
Next, we have chosen PNP transistors on the top, and
NPN transistors on the bottom.

81

DJM40082 PLC MED

What turns the transistor on or off is the voltage
difference between the base and the emitter.
With PNP transistors at the top, we can use a higher
voltage for VCC than we use for the base of the
transistors.
For example, we can use 3.3V outputs from a
microcontroller and 9V for Vcc.
That won’t work if you have NPNs at the top since the
emitter will be 0.7V lower than the base. Because that
turns into 3.3V – 0.7V = 2.6V at the positive side of the
motor, no matter what VCC voltage you choose.
Low voltage drop between collector and emitter
TIP12x transistors give a 2V drop from the emitter to
collector.
In such a configuration, you’d end up with a loss of 4V
over the transistors. I was trying to connect this to a
microcontroller, using its 5V supply, but failed because
it was only 1V left for the motor!
Here’s a nice article/rant about the topic: Stop using
antique parts!
Basically, it says that the TIP transistors are antiques
that you shouldn’t use anymore exactly because of this
huge voltage drop.
Choose transistors with low voltage drop. For example
BD135/BD136 or MOSFET transistors.

Protection diodes and PWM mode
A side-effect of how a motor works is that the motor
will also generate electrical energy. When we disable
the transistors to stop running the motor, this energy
needs to be released on some way.
If you add diodes in the reverse direction for the
transistors, we give a path for the current to take to
release this energy. Without them, you risk that the
voltage rises and damages your transistors.

82

DJM40082 PLC MED

b) DC MOTOR Pulse Width Speed Control

We said previously that the rotational speed of a DC motor is
directly proportional to the mean (average) voltage value on
its terminals, and the higher this value, up to maximum
allowed motor volts, the faster the motor will rotate. In other
words more voltage more speed. By varying the ratio
between the “ON” (tON) time and the “OFF” (tOFF) time
durations, called the “Duty Ratio”, “Mark/Space Ratio” or
“Duty Cycle”, the average value of the motor voltage and
hence its rotational speed can be varied. For simple unipolar
drives the duty ratio β is given as:

and the mean DC output voltage fed to the motor is given
as: Vmean = β x Vsupply. Then by varying the width of
pulse a, the motor voltage and hence the power applied to
the motor can be controlled and this type of control is

called Pulse Width Modulation or PWM.

Another way of controlling the rotational speed of the motor
is to vary the frequency (and hence the time period of the
controlling voltage) while the “ON” and “OFF” duty ratio times
are kept constant. This type of control is called Pulse
Frequency Modulation or PFM.
With pulse frequency modulation, the motor voltage is
controlled by applying pulses of variable frequency for
example, at a low frequency or with very few pulses the
average voltage applied to the motor is low, and therefore
the motor speed is slow. At a higher frequency or with many
pulses, the average motor terminal voltage is increased and
the motor speed will also increase.
Then, Transistors can be used to control the amount of power
applied to a DC motor with the mode of operation being
either “Linear” (varying motor voltage), “Pulse Width
Modulation” (varying the width of the pulse) or “Pulse
Frequency Modulation” (varying the frequency of the pulse).

83

DJM40082 PLC MED

Figure 7. : Driving DC Motor Speed using PWM
7.2.4 DC Stepper Motor

Stepper motors are very accurate motors that are commonly
used in computer disk drives, printers and clocks. Unlike dc
motors, which spin round freely when power is applied,
stepper motors require that their power supply be
continuously pulsed in specific patterns. For each pulse, the
stepper motor moves around one ‘step’, often 7.5 degrees
(giving 48 steps in a full revolution). There are two main
types of stepper motors - Unipolar and Bipolar.

i) Unipolar Stepper Motor
Unipolar motors usually have four coils which are
switched on and off in a particular sequence. Bipolar
motors have two coils in which the current flow is
reversed in a similar sequence. Use of bipolar motors is
covered in the next section. Each of the four coils in a
unipolar stepper motor must be switched on and off in
a certain order to make the motor turn. Many
microprocessor systems use four output lines to control
the stepper motor, each output line controlling the
power to one of the coils.

84

DJM40082 PLC MED

As the stepper motor operates at 12V, the standard
transistor circuit is required to switch each coil. As the
coils create a back emf when switched off, a
suppression diode on each coil is also required. The
table below show the four different steps required to
make the motor turn.

Look carefully at the table, and notice that a pattern is
visible. Coil 2 is always the opposite (or logical NOT) of
coil 1. The same applies for coils 3 and 4. It is therefore
possible to cut down the number of microcontroller pins
required to just two by the use of two additional NOT
gates.
Fortunately the darlington driver IC ULN2803 can be
used to provide both the NOT and darlington driver
circuits. It also contains the back emf suppression
diodes so no external diodes are required. The
complete circuit is shown below.

Before programming, there is another pattern to notice
in the stepping sequence. Look at this table, which just
shows coil 1 and coil 3.

85

DJM40082 PLC MED

ii) Bipolar Stepper motor
Stepper motors are very accurate motors that are
commonly used in computer disk drives, printers and
clocks. Unlike dc motors, which spin round freely when
power is applied, stepper motors require that their
power supply be continuously pulsed in specific
patterns. For each pulse, the stepper motor moves
around one ‘step’, often 7.5 degrees (giving 48 steps in
a full revolution). There are two main types of stepper
motors - Unipolar and Bipolar. Unipolar motors usually
have four coils which are switched on and off in a
particular sequence. Bipolar motors have two coils in
which the current flow is reversed in a similar
sequence. Use of unipolar motors is covered in the
previous pages. The bipolar stepper motor has two coils
that must be controlled so that the current flows in
different directions through the coils in a certain order.
The changing magnetic fields that these coils create
cause the rotor of the motor to move around in steps.
The circuit that is normally used to control one of the
coils is shown below. Notice how there are four ‘control’
transistors, that are switched on in ‘pairs’. Therefore
with two coils there are four control transistor pairs
86

DJM40082 PLC MED

(Q1-Q4) which must be switched on and off in a certain
sequence.

Notice that as the coils create a back emf when
switched off 8 suppression diodes (4 on each coil) are
also required.
The table below show the four different steps required
to make the motor turn

Fortunately the motor driver L293D has been
specifically designed to provide this transistor switching
circuit. The L293D contains all 8 transistors and diodes
within one 16 pin package.

Four pins from the microcontroller are connected to the
four transistor ‘pairs’ via IC pins 2, 7, 10 and 15.

87

DJM40082 PLC MED

7.2.5 DC Radio Control Servo
Servos are used in most radio controlled cars and planes to
control the steering mechanism. They are accurate devices
that always rotate the same amount for a given signal, and
so are ideal for use in many automated machines.

A typical servo has just three connection wires, normally red,
black and white (or yellow). The red wire is the 5V supply,
the black wire is the 0V supply, and the white (or yellow)
wire is for the positioning signal. The positioning signal is a
pulse between 0.75 and 2.25 milliseconds (ms) long,
repeated about every 18ms (so there are roughly 50 pulses
per second). With a 0.75ms pulse the servo moves to one
end of its range, and with a 2.25ms pulse the servo moves to
the other. Therefore, with a 1.5ms pulse, the servo will move
to the central position. If the pulses are stopped the servo
will move freely to any position. Unfortunately servos require
a large current (up to 1A) and also introduce a large amount
of noise on to the power rail. Therefore in most cases the
servo should be powered from a separate power supply, as
shown below. Remember that when using two power supplies
the two 0V rails must be joined to provide a common
reference point.

88

DJM40082 PLC MED

7.2.5 Counter module

The Counter Module is a numeric LCD display module that
can be used to show a ‘counter’ value. To increment the
counter a pulse (between 1 and 1.5V) must be applied to the
counter pad 3. As the PIC microcontroller operates at 5V a
potential divider formed from resistors must be used to
reduce the PIC microcontroller output signal to 1.5V. As the
counter uses it’s own, internal, 1.5V battery, the two 0V rails
must also be connected.

7.2.6 Seven Segment Display

A seven segment display contains seven LED ‘bars’ that can
be lit up in different combinations to show the ten digits 0 to
9. In theory each ‘bar’ could be connected to one
microcontroller output pin, but this would use up 7 of the 8
available pins!
A better solution is to use a dedicated integrated circuit, such
as the CMOS 4511B to control the seven segment display.
This IC controls the seven segment display according to the

89

DJM40082 PLC MED

binary ‘code’ on the four data lines. This system uses four
pins rather than 7.

7.2.7 Solenoid & Solenoid Valves

A solenoid consists of a steel plunger inside an electric coil
which is wrapped around a tube. When the coil is energised a
magnetic field is created, and this draws the plunger into the
tube. When the coil is de-energised a spring pushes the
plunger back out of the tube. To control a solenoid the
standard MOSFET circuit is used.

The isonic solenoid valve can be used to control air flow
through a pneumatic system. Isonic valves are ideal for
battery operated products as operate at a low voltage and
draw much less current than traditional solenoid valves. The
standard transistor switching circuit can be used to drive the
isonic valve.

90

DJM40082 PLC MED

7.2.8 Smart Wire & Smart Springs

Shape Memory Alloy wire or springs are ‘smart’ materials
that can be used to create mechanical actuation
(movement). When an electric current is passed through
the wire it heats up and so contracts with a large pulling
force. When the current is removed the wire cools and so
expands again (a ‘traditional’ steel spring is sometimes
used to pull the smart wire/spring taut as it cools). Smart
wire or springs draw a relatively large current, and so the
standard FET interfacing circuit should be used to
interface to the microcontroller.

7.2.9 Signal Lamp
To interface a signal lamp the standard transistor
interfacing circuit is used. Note that if a different power
supply is used for the signal lamp, the 0V rails of each
power supply must be connected to provide a common
reference. If a battery is used as the power supply, it is
worth remembering that LEDs draw much less current
than lamps. Therefore, if a simple ‘indicator’ is required, a
LED will be a better solution than a lamp as the batteries
will last far longer.
91

DJM40082 PLC MED

7.2.10 Buzzer
To interface a buzzer the standard transistor interfacing
circuit is used. Note that if a different power supply is
used for the buzzer, the 0V rails of each power supply
must be connected to provide a common reference. If a
battery is used as the power supply, it is worth
remembering that piezo sounders draw much less
current than buzzers. Buzzers also just have one ‘tone’,
whereas a piezo sounder is able to create sounds of
many different tones.

7.2.11 Piezo Sounder & Speaker
A piezo sounder or speaker can be used to produce
many different sounds, whereas a buzzer can only
produce a single tone. Buzzers produce a noise when
power is applied, but a piezo or speaker requires a
pulsed signal to generate the noise. Fortunately this is
very easy to generate from the microcontroller by using
the BASIC ‘sound’ command.

92

DJM40082 PLC MED

7.2.12 Solar & DC “Toy” Motors
Many projects require the use of a cheap dc motor to
create rotational movement. There are a number of
ways motors can be interfaced to the microcontroller.

This circuit uses a darlington transistor to switch the
motor on and off. This circuit will work with ‘solar’
motors, but may not function correctly with cheap dc
‘toy’ motors. This is because this type of motor
introduces a lot of electrical ‘noise’ on to the power rails.
This noise can affect the microcontroller, and in some
cases can completely stop the control program
functioning.

Electrical noise can be reduced by soldering suppression
capacitors across the motor contacts, as shown. Use a
220nF polyester (non polarised) capacitor.
In order to switch medium power motors, a power
MOSFET is used instead of a darlington transistor. The
MOSFET circuit is shown below. The device IRF530 is a
suitable power MOSFET to use in this circuit.

93

DJM40082 PLC MED

On many occasions it may be necessary to control two
motors. A convenient and cheap approach would be to
use a motor driver IC such as the L293D. This IC will
allow control of two dc motors, using four data lines
from the microcontroller. Naturally, if only one motor is
to be controlled then only two output lines are used.

94

DJM40082 PLC MED

7.3 INPUT DEVICE INTERFACING
7.3.1 Switches
There are a large variety of switches available, but the
majority all have two ‘contacts’ which are either ‘open’
(off) or ‘closed’ (on). The two circuits shown below can be
used with almost all switches.

With this circuit the input pin is low when the switch is
open and high when the switch is closed.

With this circuit the input pin is high when the switch is
open and low when the switch is closed.

Switch Bounce
All mechanical switches ‘bounce’ when the switch opens or
closes. This means that the switch contacts ‘bounce’
against each other before settling. As the PIC
microcontroller operates so quickly it is possible that in

95

DJM40082 PLC MED

some programs the microcontroller may register 2 or 3 of
these ‘bounces’ instead of just registering one ‘push’.

The simplest way to debounce a circuit is to simply add a
time delay (pause 100) after the if... command. If the
section of code after the push is quite long this time delay
will occur naturally (as the other code commands are
carried out) and so is unnecessary. However if the code
does not have a long delay, as in the following example, a
pause command can be used instead.

7.3.2 Potentiometer

A potentiometer (or ‘variable resistor’) has a spindle that can
be moved to change the resistance value of the
potentiometer. This can be used to measure rotational or
linear movement
The readADC command is used to measure the value of the
resistance by carrying out an Analogue to Digital Conversion.
The value of the resistance is given a ‘value’ between 0 and
255 which is then stored in a variable. After storing the
reading in the variable, the if...then command can be used to
perform different functions.

96

DJM40082 PLC MED

7.3.3 Light Dependant Resistor (LDR)
A Light Dependant Resistor (LDR) is a resistor that
changes in value according to the light falling on it. A
commonly used device, the ORP-12, has a high resistance
in the dark, and a low resistance in the light. Connecting
the LDR to the microcontroller is very straight forward,
but some software ‘calibrating’ is required.

It should be remembered that the LDR response is not
linear, and so the readings will not change in exactly the
same way as with a potentiometer. In general there is a
larger resistance change at brighter light levels. This can
be compensated for in the software by using a smaller
range at darker light levels. Experiment to find the most
appropriate settings for the circuit.
7.3.4 Thermistor

A thermistor is a resistor that changes in value according to
it’s heat. In actual fact all resistors change in value as they
heat up or cool down, but thermistors are manufactured to
show a large resistance change. Connecting the thermistor to

97

DJM40082 PLC MED

the microcontroller is very straight forward, but some
software ‘calibrating’ is required.
It should be remembered that the thermistor response is not
linear, and so the readings will not change in exactly the
same way as with a potentiometer. In general there is a
larger resistance change at lower temperatures. This can be
compensated for in the software by using a smaller range at
higher temperatures. Experiment to find the most
appropriate settings for the circuit.

98

DJM40082 PLC MED

7.4 ADVANCED COMPONENT INTERFACING

7.4.1 Liquid Crystal Display (LCD)

A Liquid Crystal Display is an electronic device that can be
used to show numbers or text. There are two main types of
LCD display, numeric displays (used in watches, calculators
etc) and alphanumeric text displays (often used in devices
such as photocopiers and mobile telephones). The display is
made up of a number of shaped ‘crystals’. In numeric
displays these crystals are shaped into ‘bars’, and in
alphanumeric displays the crystals are simply arranged into
patterns of ‘dots’. Each crystal has an individual electrical
connection so that each crystal can be controlled
independently. When the crystal is ‘off’ (i.e. when no current
is passed through the crystal) the crystal reflect the same
amount of light as the background material, and so the
crystals cannot be seen. However when the crystal has an
electric current passed through it, it changes shape and so
absorbs more light. This makes the crystal appear darker to
the human eye - and so the shape of the dot or bar can be
seen against the background. It is important to realise the
difference between a LCD display and an LED display. An LED
display (often used in clock radios) is made up of a number
of LEDs which actually give off light (and so can be seen in
the dark). An LCD display only reflects light, and so cannot
be seen in the dark.

LCD Characters
The table on the next page shows the characters available
from a typical LCD display. The character ‘code’ is obtained
by adding the number at the top of the column with the
number at the side of the row. Note that characters 32 to
127 are always the same for all LCDs, but characters 16 to
31 & 128 to 255 can vary with different LCD manufacturers.

99

DJM40082 PLC MED

Therefore some LCDs will display different characters from
those shown in the table. Characters 0 to 15 are described as
‘user-defined’ characters and so must be defined before use,
or they will contain ‘randomly shaped’ characters. For details
on how to use these characters see the LCD manufacturers
data sheets.

7.4.2 Serial Interfacing to a Computer
Most computers can ‘talk’ to other devices by serial
communication. Serial communication uses a common
‘protocol’ (or code) where characters are converted into
numbers and then transmitted via cables. A computer mouse
normally ‘communicates’ serially with a computer, and
computer modems work by turning these numbers into
sounds to travel down telephone lines. As all computers use
the same ASCII code for transmitting and receiving
characters it is relatively easy to program the PIC
microcontroller to ‘talk’ to any type of computer. All that is
100


Click to View FlipBook Version