You are on page 1of 27

Relay,

Optoisolators and
Stepper Motor
LECTURE# 21

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 1


Last Lecture
ADC Programming in AVR
ADC Examples for AVR
Using ADC Interrupts

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 2


Assembly Example
Program 13-1 ATmega32 vs. 328p LDI R16, 0xFF
Takes input from ADC channel 0 and OUT DDRB, R16 ;make Port B an output
display on PORTB and PORTD OUT DDRD, R16 ;make Port D an output
LDI R16, 0 ATmega328p
.INCLUDE "M32DEF.INC" ATmega32 OUT DDRC, R16 ;make Port A an input for ADC
LDI R16, 0xFF LDI R16, 0x87 ;R16 = 0b 1000 0111
OUT DDRB, R16 ;make Port B an output STS ADCSRA, R16 ;enable ADC and select ck/128
OUT DDRD, R16 ;make Port D an output LDI R16, 0xC0 ;R16 = 0b 1100 0000, 1.1V Vref
LDI R16, 0 STS ADMUX, R16 ;ADC0 Channel, right-justified data
OUT DDRA, R16 ;make Port A an input for ADC READ_ADC:
LDI R16, 0x87 ;R16 = 0b 1000 0111 LDS R16, ADCSRA
OUT ADCSRA, R16 ;enable ADC and select ck/128 SBR R16, 6 ;set bit 6 (ADSC) in R16
LDI R16, 0xC0 ;R16 = 0b 1100 0000, 2.56V Vref STS ADCSRA, R16 ;copy R16 to ADCSRA for setting ADSC
OUT ADMUX, R16 ;ADC0 channel, right-justified data KEEP_POLLING: ;wait for end of conversion
READ_ADC: LDS R16, ADCSRA
SBI ADCSRA, 6 ;start conversion SBRS R16, 4 ;skip next instruction, if ADIF is set
KEEP_POLING: ;wait for end of conversion RJMP KEEP_POLLING ;keep polling ADIF flag
SBIS ADCSRA, 4 ;skip next instruction, if ADIF is set LDS R16, ADCSRA
RJMP KEEP_POLING ;keep polling ADIF flag SBR R16, 4; ;write 1 to bit 4 (ADIF) in R16,
SBI ADCSRA, 4 ;write 1 to clear ADIF flag STS ADCSRA, R16 ;copy R16 to ADCSRA for clearing ADIF flag
IN R16, ADCL ;read ADCL first LDS R16, ADCL ;read ADCL first
OUT PORTD,R16 ;give the low byte to PORTD OUT PORTD,R16 ;give the low byte to PORTD
IN R16, ADCH ;read ADCH after ADCL LDS R16, ADCH ;read ADCH after ADCL
OUT PORTB,R16 ;give the high byte to PORTB OUT PORTB,R16 ;give the high byte to PORTB
RJMP READ_ADC ;keep repeating it RJMP READ_ADC ;keep repeating it

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 3


Sensor Interfacing
LM35
LM35 Converts Temperature in to electrical signal
◦ Three pin (VCC, VOUT and GND)
◦ 10 mV/C
◦ Each degree increases the output of the sensor by 10mV and vice versa

What is the most suitable choice for 𝑉𝑟𝑒𝑓 ?


Write and program to display temperature value on PORTD
◦ In assembly and C

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 4


LM35
Voltage Reference (5 V)
If we use 𝑉𝑟𝑒𝑓 = 5 V Dout Vin (mV) Temperature (°C)
1 4.88 0.488
◦ Step size = 4.88 mV (for 10-bit ADC of 2 9.76 0.976
AVR) 3 14.64 1.464
◦ 10mV/°𝐶 is not a multiple of step size 4 19.52 1.952
5 24.4 2.44
◦ So it will be difficult for calculating 8 39.04 3.904
temperature value 10 48.8 4.88
◦ Consider Dout = 50 20 97.6 9.76
30 146.4 14.64
◦ 𝑉𝑖𝑛 = 50 × 4.88 mV = 244.14 mV 40 195.2 19.52
244.14 𝑚𝑉
◦ Then Temperature will be = 50 244 24.4
10 𝑚𝑉/𝐶
80 390.4 39.04
24.41 °𝐶, which is not a proper value
100 488 48.8
200 976 97.6
Look at the table and fractional values of 400 1952 195.2
Temperature it results 800 3904 390.4
1000 4880 488
1024 4997.12 499.712

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 5


LM35
Voltage Reference (2.56 V)
If we use 𝑉𝑟𝑒𝑓 = 2.56 V Dout Vin (mV) Temperature (°C)
1 2.5 0.25
◦ Step size = 2.5 mV (for 10-bit ADC) 2 5 0.5
◦ 10mV/°𝐶 is a multiple of step size (2.5 mV) 3 7.5 0.75
4 10 1
◦ Consider Dout = 50 5 12.5 1.25
◦ 𝑉𝑖𝑛 = 50 × 2.5 mV = 125 mV 8 20 2
125 𝑚𝑉 10 25 2.5
◦ Temperature will be = 12.5 °𝐶
10 𝑚𝑉/𝐶 20 50 5
30 75 7.5
The temperature value result is proper 40 100 10
50 125 12.5
If we want to have Dout = temperature 80 200 20
◦ Divide the result by 4 100 250 25
200 500 50
◦ Use ADLAR = 1 and ignore the LSB two bits 400 1000 100
in ADCL 800 2000 200
◦ Read only ADCH (which is temperature) 1000 2500 250
1024 2560 256

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 6


LM35
Voltage Reference (1.1 V)
If we use 𝑉𝑟𝑒𝑓 = 1.1 V Dout 𝐕𝐢𝐧 (mV) Temperature (°C)
1 1.07 0.1
◦ Step size = 1.074 mV (for 10-bit ADC) 2 2.15 0.2
◦ 10mV/°𝐶 is not a multiple of step size 3 3.22 0.3
4 4.30 0.4
◦ So, it won’t be straight-forward calculation 5 5.37 0.5
◦ Consider Dout = 50 8 8.59 0.9
◦ 𝑉𝑖𝑛 = 50 × 1.074mV = 53.71 mV 10 10.74 1.1
20 21.48 2.1
53.71 mV
◦ Then Temperature will be = 5.4 °𝐶, 30 32.23 3.2
10 mV/𝐶
which is not a proper value 40 42.97 4.3
50 53.71 5.4
80 85.94 8.6
Look at the table and fractional values of 100 107.4 10.7
Temperature it results 200 214.8 21.5
400 429.7 43.0
800 859.4 85.9
1000 1074 107.4
1024 1100 110.0

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 7


DAC
Reverse operation of ADC
◦ DAC converts a digital input to analog output (voltage or current)
𝐷𝑖𝑛 𝐷𝑖𝑛
◦ 𝑉𝑜𝑢𝑡 = 𝑉𝑟𝑒𝑓 × 2𝑟𝑒𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛
or 𝐼𝑜𝑢𝑡 = 𝐼𝑟𝑒𝑓 × 2𝑟𝑒𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 8


MC1408 DAC (or DAC0808)
In the MC1408 (DAC0808),
◦ the digital inputs are converted to current (𝐼𝑜𝑢𝑡 )
𝐷𝑖𝑛 𝐷7 𝐷6 𝐷5 𝐷4 𝐷3 𝐷2 𝐷1 𝐷0
◦ 𝐼𝑜𝑢𝑡 == 𝐼𝑟𝑒𝑓 × 2𝑟𝑒𝑠.
= 𝐼𝑟𝑒𝑓 × 2
+ 4
+ 8
+ 16
+ 32
+ 64
+ 128
+ 256

Assume 𝐼𝑟𝑒𝑓 = 2 mA,


255
◦ maximum analog output 𝐼𝑜𝑢𝑡 = 2 mA × 256 = 1.9922 mA

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 9


Converting Iout to voltage
We can put a simple resistor 𝑅𝑜𝑢𝑡 to obtain voltage 𝑉𝑜𝑢𝑡 = 𝐼𝑜𝑢𝑡 × 𝑅𝑜𝑢𝑡
◦ Load impedance may cause inaccuracy in the output voltage produced
◦ A better method is to use a trans-impedance amplifier (TIA)

TIA

We know from inverting amplifier


𝑅𝑓
𝑉𝑜𝑢𝑡 = − × 𝑉𝑖𝑛
𝑅𝑖𝑛
𝑉𝑖𝑛
Where, 𝐼𝑖𝑛 = due to virtual GND
𝑅𝑖𝑛
Combining the above two equations
𝑉𝑜𝑢𝑡 = −𝑅𝑓 × 𝐼𝑖𝑛

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 10


Relay
A relay is an electrically controllable switch
A Relay provides isolation between
◦ The control circuitry and the circuitry being controlled
◦ Like isolation between microcontroller and motor
◦ To protect the microcontroller from the back-EMF of the motor back

Relay can be either Electromechanical or Solid State


◦ Solid State Relays (SSRs) have no moving components
◦ Made with FETs etc.

Electromechanical Relays use a coil magnetic field to open or close a


switch
◦ Has three components coil, spring and contacts

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 11


Electromechanical Relay
When coil is not energized
(Normally Close Contact)
◦ The spring pulls the armature, and, NC
◦ Common and NC terminals are
connected
NO
When current is provided to coil (Normally Open Contact)
◦ The coils magnetic field pulls the
armature and,
◦ Common and NO terminals are
connected

The relay shown is


Common Terminal Coil Terminals
◦ Single Pole, Double Throw (SPDT)
◦ Because it has single common terminal
and two NO and NC terminals

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 12


Electromechanical Relay cont.
Relay Types
◦ Single Pole Single Throw
◦ Single Pole Double Throw
◦ Double Pole Double Throw

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 13


Driving a Relay
The coil of an Electromechanical Relay
◦ Requires significant amount of current (may not be provided by μC pins)
◦ Also generates back EMF

When connecting a relay to an AVR


◦ Connect a driver (ULN2803) in between

Consider the example


◦ Controlling 12V lamp status
◦ Using electromechanical relay (106462)
◦ The relay is driven using ULN2803

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 14


Solid State Relay (SSR)
Contains no moving parts
◦ No coil, spring or mechanical switch

Relay is made with semiconductor materials


Due to no moving parts
◦ The relay can switch at much faster rate compared to Electromechanical
counterpart
◦ Offers Longevity

Consider the example of controlling


◦ A fan from AVR
◦ Using SSR

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 15


Reed Switch
A switch which is closed in proximity of a magnetic field
◦ Otherwise open

The switch contacts are enclosed in a glass


◦ Suitable for humid environments

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 16


Optoisolator (optocoupler)
Used to isolate two parts of a system
◦ Protecting one part of the circuit from hazards
of the other part
◦ Like AVR and Motor

Also used in modems


◦ To prevents surges due to lightening

A single optoisolator is made using


◦ An LED and a photodiode (or photo transistor)

Consider the example


◦ No driver is needed
◦ Given that the optoisolator output can provide
enough current to lamp

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 17


Stepper Motor
A DC motor in which each rotation is divided A
◦ In equal steps
Translates electrical pulses
◦ Into mechanical movement N
D B
◦ Movement in steps (unlike DC motor) S

Used in disk drivers, printers and robots etc.


◦ Where controlled movement is needed
◦ Unlike DC motor C

Commonly have
◦ permanent magnet rotor (also called shaft)
◦ Four stator windings
◦ With center-tapped common
◦ This type of motor is called
◦ Four phase or uni polar

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 18


Stepper Motor cont.
A conventional motor runs freely
◦ While a stepper motor moves in fixed repeatable increment
◦ Hence have a precise movement

The controlled movement is possible by multiple controlled poles


◦ Based on principle of same poles repel, opposite attract

The stator poles are determined by the current sent through


◦ Example if we energize winding A and B A

◦ The average north pole will be between A and B


◦ The rotor will move accordingly, as shown in figure
D B

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 19


Stepper Motor Sequence
A simple four-step
sequence of stepper
Counter-
motor is shown Clockwise Step # Winding A Winding B Winding C Winding D Clockwise

◦ Does not mean that 1 1 0 0 1


motor has four steps in 2 1 1 0 0
one rotation 3 0 1 1 0

◦ Means there are four 4 0 0 1 1


terminals

A A A A

1 2 3 D
4 B
D B D B D B

C C C C

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 20


Step Angle, and,
Steps Per Revolution
A four-step stepper motor A A A
D B
◦ May have more than four steps in a rotation D B
C C

B C D
Motors shown in the right figure, haveD C B

A A
◦ step angles of 90°, 45°and 30°, and B D
D B
C A C
◦ steps per revolution of 4, 8, 12
◦ Usually, higher steps and smaller step angles are available
A
C C

A A

C C

A A

C C
A

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 21


Interfacing stepper motor
asd

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 22


Half stepping
Counter-
If an application requires Clockwise Step # Winding A Winding B Winding C Winding D
Clockwise
1 1 0 0 1
smaller movements
2 1 0 0 0
◦ Half stepping can be
3 1 1 0 0
used
4 0 1 0 0

Holding Torque 5 0 1 1 0
6 0 0 1 0
◦ “With the motor shaft at
7 0 0 1 1
standstill or zero rpm
8 0 0 0 1
condition, the external
A A A A
torque required to break
away the shaft from its 1 2 N
3 4
holding position. This is D B D B D B D B

N
S
S

measured with rated A A A A

voltage and current C C C C

applied to the motor” 5 6 S 7 D


8 B
D B D B

N
D B

S
N

C C C C

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 23


Other Stepper Motor types
So far we were discussing uni-polar stepper motor
◦ Which has two common terminals (for A/B and C/D)
◦ Current flows in one direction only
If there are no common terminals
◦ It is universal
Bipolar allow current to flow in either direction
◦ Like C = NOT(A) and D = NOT(B)

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 24


Driving Heavy Motors
Use Transistor as drivers
◦ Recall 𝐼𝐶 = 𝛽𝐼𝐵
◦ If you provide 𝐼𝐵 from AVR
◦ 𝐼𝐶 can be used to drive motor

Darlington pair
◦ Have two pair transistors
◦ So, 𝐼𝐶2 ≅ 𝛽1 𝛽2 𝐼𝐵1

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 25


Another Interfacing
Using Opto-isolators for better isolation

End of Lecture

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 26


Stepper Motor Programming
Write a program (assembly and C) for stepper motor rotation
◦ Example 3 and 4 of the book ;initialize stack pointer
LDI R20,0xFF ;Port B as output
◦ A Switch is connected to PA.7 OUT DDRB,R20
CBI DDRA,7 ;make PA7 an input
◦ If SW=1, rotate clock wise LDI R20,0x66 ;starting phase value
◦ IF SW=0, rotate ani-clockwise L1: OUT PORTB,R20 ;PORTB = R20
IN R16,PINA
BST R16,7 ;T=PINA.7
Instructions BRTS CW
◦ BST Rd, b LSR R20 ;shift right
BRCC OV1 ;if not carry skip next
◦ ;Bit Store from Register to T flag in SREG ORI R20,0x80
OV1:
◦ BRTS k RCALL DELAY ;wait
RJMP L1 ;repeat
◦ ;Branch if T flag is set
CW:
◦ BRTC k LSL R20 ;shift left
BRCC OV2 ;if not carry skip next
◦ ;Branch if T flag is cleared ORI R20,0x01
OV2:
RCALL DELAY ;wait
RJMP L1 ;repeat

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 27

You might also like