You are on page 1of 4

CONSTRUCTION

MICROCONTROLLER-BASED
TACHOMETER UMAR
SUNIL K
 PROF. K. PADMANABHAN you first place a contrasting colour
mask. A strip of white adhesive tape

A
tachometer is nothing but a is ideal on the spinning object. Posi-
simple electronic digital trans- tion it such that the intensity of light
ducer. Normally, it is used for PARTS LIST reflected from the object’s surface
measuring the speed of a rotating Semiconductors: changes as it rotates.
shaft. The number of revolutions per IC1 - AT89C2051 Each time the tape spins past
minute (rpm) is valuable information microcontroller
the probe, the momentary increase
IC2 - ULN2003 current buffer
for understanding any rotational sys- IC3 - CA3140 operational in reflected light is detected by the
tem. For example, there is an optimum amplifier phototransistor. The signal processor
T1-T4 - BC557 pnp transistors
speed for drilling a particular-size hole T5 - 2N2222 npn transistor and microcontroller circuit counts
in a particular metal piece; there is an T6 - L14F1 photo-transistor the increase in the number of such light
ideal sanding disk speed that depends D1 - 1N4007 rectifier diode reflections sensed by it and thereby
DIS1 - KLQ564 4-digit,
on the material being finished. You 7-segment display evaluates the rpm, which is displayed
may also want to measure the speed Resistors (all ¼-watt, ±5% carbon): on the 4-digit, 7-segment display.
of fans you use. R1-R4 - 1-kilo-ohm The phototransistor is kept inside
This easy-to-make photoelectric R5, R6 - 10-kilo-ohm a plastic tube, which has a convex lens
R7-R11 - 1.2-kilo-ohm
tachometer measures the rpm of most RNW1 - 10-kilo-ohm resistor fitted at one end. A convex lens of
shop-floor tools and many household network about 1cm diameter and 8-10cm focal
machines without any mechanical or VR1 - 4.7-kilo-ohm preset
length is a common item used by
Capacitors:
electrical interface. C1 - 10µF, 16V electrolytic watch repairers and in cine film viewer
C2, C5, C6 - 0.1µF ceramic disk toys. It can be obtained from them to
How it works? C3, C4 - 22pF ceramic disk set up the experiment. The
Just point the light-sensitive probe tip Miscellaneous: phototransistor is fixed on a piece of
S1 - Push-to-on switch
atop the spinning shaft towards the S2 - On/Off Switch cardboard such that it faces the lens at
spinning blade, disk or chuck and read XTAL - 12MHz a distance of about 8 cm. The leads
the rpm. The only requirement is that BATT. - 6V battery
from the phototransistor are taken out

Fig. 1: Circuit of microcontroller-based tachometer

92 • JANUARY 2008 • ELECTRONICS FOR YOU WWW.EFYMAG.COM


CONSTRUCTION
The AT89C2051 is a 20-pin, Port-3 pins P3.0 through P3.3 of the
8-bit microcontroller of Intel’s microcontroller are connected to the
8051 family made by Atmel Cor- base of transistors T1 through T4, re-
poration. Port-1 pins P1.7 spectively, to select one digit out of
through P1.2, and port-3 pin P3.7 the four at a time and to supply an-
are connected to input pins 1 ode-drive currents to the common an-
through 7 of ULN2003. Port-1 ode pin of respective digit. Pin con-
pins are pulled up with 10-kilo- figuration of transistor BC557 is shown
ohm resistor network RNW1. in Fig. 3.
They drive all the seven seg- When pin P3.0 of microcontroller
Fig. 2: Suitable arrangement of phototransistor ments of the display with the IC1 goes low, it drives transistor T1
help of internal inverters. into saturation, which provides the
and connected in the drive current to
circuit shown in Fig. 1. anode pin 6 of 4-
Fig. 2 shows the suit- digit, 7-segment,
able arrangement of common-anode
phototransistor. display DIS1.
The detected signal Similarly, transis-
is amplified by transis- tors T2 through
Fig. 3: Pin
tor 2N2222 (T5) and T4, respectively,
configuration of further amplified by provide supply
transistor BC557 operational amplifier to common-an-
CA3140 (IC3). The reference voltage ode pins 8, 9 and
point for the operational amplifier is 12 of DIS1. Thus
obtained by resistor divider network microcontroller
comprising R2 and R3. The output IC1 drives the
from pin 6 of IC3 is fed to pin 12 of segment in mul-
microcontroller AT89C2051. Note that tiplexed manner
pins 12 and 13 of microcontroller using its port
AT89C2051 are the inputs (+ and -) of pins. This is time-
its internal analogue comparator. Pin division multi-
13 is adjusted to nearly half the sup- plexing process.
ply voltage using a potential divider Segment data
comprising resistor R7 and preset VR1 Fig. 4: A single-side, actual-size PCB layout for microcontroller-based
and display-en-
across the supply. tachometer able pulse for
The pulses picked up by the display are re-
phototransistor are sensed by the in- freshed every 5
ternal comparator of AT89C2051 and, ms. Thus, the dis-
through software, each pulse repre- play appears to
senting one rotation of the object is de- be continuous
tected. By counting the number of such even though it
pulses, on an average per minute ba- lights up one by
sis, the RPM is evaluated. It is dis- one.
played by a software routine to light Switch S1 is
up the LED segments of the 4-digit, 7- used to manually
segment display. reset the
microcontroller,
Circuit description while the power-
Fig. 1 shows the circuit of the on-reset signal
microcontroller-based tachometer. The for the
tachometer comprises AT89C2051 microcontroller is
microcontroller, ULN2003 high-current given by C1 and
Darlington transistor array, CA3140 R6. A 12MHz
operational amplifier, common-anode crystal is con-
7-segment (4-digit multiplexed) dis- nected to pins 4
play and its four anode-driving tran- and 5 of IC1 to
sistors. Fig. 5: Component layout for the PCB

94 • JANUARY 2008 • ELECTRONICS FOR YOU WWW.EFYMAG.COM


CONSTRUCTION
generate the basic clock frequency for torchlight for illumination of the ro- overflows every 100 microseconds and
the microcontroller. The circuit uses a tating object. For fans, use the light so the number counted by the timer
6V battery for power supply or alter- from behind. Hold the probe firmly program in this case will be ‘400.’ This
natively a mains derived low voltage so as to provide a steady, bright illu- is divided by ‘600,000’ (so many 100/
supply. An actual-size, single-side PCB mination on the object. Even an LED µs present in a minute), giving a result
layout for the tachometer (Fig. 1) is pen torch could be used here. Avoid of ‘1500.’ This gives the rpm. These
shown in Fig. 4 and its component lay- the fluctuating background light from digits are displayed on the 4-digit, 7-
out in Fig. 5. sources such as tubelight. segment display. To perform the divi-
sion, subroutine UDIV32 is employed,
Testing Software which is a standard subroutine avail-
The source code of this article is avail- The software is written in Assembly able for 8051 family for 32-bit number
able at http://www.electronicsforu. language and assembled using 8051 by 16-bit number division. It has an
com/efycodes/efy-codes.zip at code cross-assembler. It is well commented accuracy of 5 rpm in a 6000rpm count.
file ‘tacho.hex.’ Using a programmer, and easy to understand. It uses EFY note. The source code of this
load the code into the new chip AT89C2051’s internal timer for mea- article is available at http://
AT89C2051. (Refer the May 2005 issue suring the period of one cycle of the www.electronicsforu. com/efycodes/
of EFY for article on programmer for rotation in units of 100 microseconds. efy-codes.zip and will also be included
89C51 and 2051.) Then, fit it into the Thus if the speed is 1500 rpm, it is 25 in EFY-CD of February 2008 issue. 
circuit board and after powering up rps, and the time taken for one cycle
the circuit, test it. is 40 ms. Prof. K. Padmanabhan retired from Alagappa
For testing, point the probe using The timer uses an interrupt to count College of Technology, Guindy, Chennai

TACHO.ASM
$mod51 mov r2,40h DIV AB
ORG 0H CALL HEX2BCD MOV R6,B
AJMP 30H mov 50h,#0FFH MOV R7,A
ORG 0BH ;TIMER 0 INTERRUPT VECTOR call refresh ENDD: ret
AJMP TIMER0ISR ;Timer 0 Interrupt service rou- disp: call refresh1 DISP1:
tine address djnz 50h,disp ; so many times for a visible time limit REFRESH:; content of 18 to 1B memory locations are
ORG 30H jmp beg output on LEDs
MOV SP,#60H ;set stack pointer ;16 Bit Hex to BCD Conversion for 8051 Microcontroller ; only numbers 0 to 9 and A to F are valid data in
MOV P3,#0FFH ;set all port 3 bits high to enable in- ;This routine is for 16 bit Hex to BCD conversion; these locations
puts also ;Accepts a 16 bit binary number in R1,R2 and returns 5 MOV 18H,r3 ; least significant digit
MOV P1,#03 ;set port 1 to all zeros expect bits 0,1 digit BCD in ;R7,R6,R5,R4,R3(upto 64K ) MOV 19H,r4 ; next significant digit
MOV TMOD,#01100001B ;TIMER 1 - MODE 2 Hex2BCD: ;r1=high byte, r7 most significant digit, R2 MOV 1AH,r5
COUNTER,TIMR-0 TO 16 bit timer = LSByte MOV 1BH,R6 ; most significant digit (max:9999)
BEG: MOV TH0,#0ffH ;TIMER REG.0 IS SET TO 0, MOV R3,#00D RET
GIVES 64ms MOV R4,#00D refresh1:
MOV TL0,#-99 ; timer low reg. is also so MOV R5,#00D MOV R0,#1bh ; 1b,1a,19,18, holds values for 4 digits
setb et0 MOV R6,#00D MOV R4,#8 ; pin p3.3_ 0 made low one by one starts
setb ea MOV R7,#00D wth 18
mov 44h,#0 MOV B,#10D mov r7,#2 ; decimal pt.on 3rd digit from left (2 nd
mov 45h,#0 MOV A,R2 fromright)
acall delay DIV AB PQ2: CALL SEGDISP
ajmp lowsig MOV R3,B ; dec R0
delay: mov r2,#10 MOV B,#10 ; R7,R6,R5,R4,R3 mov a,r4
djnz r2,$ ;wait 20 us DIV AB rrc a
ret MOV R4,B mov r4,a
lowsig: jb p3.6,lowsig MOV R5,A jnc pQ2
call delay CJNE R1,#0H,HIGH_BYTE ; CHECK FOR HIGH PV3:RET
jnb p3.6,$ BYTE SEGDISP:mov dptr,#ledcode
setb tr0 ; start timer SJMP ENDD MOV A,@R0
mov c,p3.6 ;high begins HIGH_BYTE: MOV A,#6 ANL A,#0FH
mov p3.5,c ADD A,R3 MOVC A,@A+dptr
acall delay MOV B,#10 segcode:MOV R5,A
jb p3.6, $ DIV AB ORL A,#03H ; WE WANT TO USE PORT 1 BITS 0
mov c,p3.6 ;low now MOV R3,B AND 1 FOR INPUT ANLOG
mov p3.5,c ADD A,#5 ; so retain them high
acall delay ADD A,R4 S3: MOV P1,A ; SEGMENT_PORT
jnb p3.6,$ MOV B,#10 MOV A,R5 ;we use p3.7 for the segment ‘a’ of display
mov c,p3.6 ;high begins again DIV AB RRC A ;so get that bit D0into carry
mov p3.5,c MOV R4,B rrc a
clr tr0 ;stop timer ADD A,#2 mov p3.7,c ;segment ‘a;
clr et0 ;and interrupt by timer ADD A,R5 S1: MOV A,R4 ; get digit code from r4 00001000
mov r3,#0 ;number 600000 or 927c0 hex as Dividend MOV B,#10 cpl a ;11110111
mov r2,#09h ; 9 DIV AB rrc a ;11111011-1
mov r1,#27h ;27 MOV R5,B mov p3.0,c ; output to drive transsitors for digit light-
mov r0,#0c0h ; c0 CJNE R6,#00D,ADD_IT ing
mov r5,45h ;divisor is time for one cycle SJMP CONTINUE rrc a ;11111101-1
mov r4,44h ADD_IT: ADD A,R6 mov p3.1,c
call UDIV32 ;divide 60000/t CONTINUE: MOV R6,A rrc a ;11111110-1
mov 40h,r0 DJNZ R1,HIGH_BYTE mov p3.2,c
mov 41h,r1 MOV B, #10D rrc a ;1111111-0 yes low makes left most digit show
mov r1,41h MOV A,R6 msdigit

96 • JANUARY 2008 • ELECTRONICS FOR YOU WWW.EFYMAG.COM


CONSTRUCTION
mov p3.3,c push B mov a,r7
S5: setb RS0 ;select reg.bank 1 subb a,#0
S4: ACALL DELAY1 ; let it burn for some time mov r7,#0 mov 07,a
MOV A,#0ffH ; extinguish the digit after that time mov r6,#0 cpl C
MOV P3,A ; to prevent shadow mov r5,#0 jnc div_321
s6: RET mov r4,#0 mov r7,7
ledcode:DB 7EH,0CH,0B6H,9EH,0CCH,0DAH,0FAH mov B,#32 ;set loop count mov r6,6
;these are code for the numbers 0 to 9 and A to F div_lp32:clr RS0 ;selet reg.bank 0 mov r5,dph
DB 0EH,0FEH,0CEH,0EEH,0F8H,72H,0BCH,0F6H, clr C mov r4,dpl
0E2H mov a,r0 ;shift highestbit of X div_321: mov a,r0
DELAY1:MOV 55h,#0ffH ; 1ms rlc a rlc a
N: NOP mov r0,a mov r0,a ; shift result bit into partial quotient
DJNZ 55h,N mov a,r1 ;shift next bit of X mov a,r1
RET rlc a rlc a
TIMER0ISR:mov th0,#0ffh mov r1,a mov r1,a
mov tl0,#-90 ; in 100 us steps mov a,r2 ;shift next bit of X mov a,r2
push acc rlc a rlc a
mov a,#1 mov r2,a mov r2,a
clr c mov a,r3 ;shift next bit of X mov a,r3
add a, 44h ;count time btwn pulses rlc a rlc a
mov 44h,a mov r3,a mov r3,a
mov a,#0 setb rs0 ;reg. bank 1 djnz B,div_lp32
addc a,45h ;add carry to most sign. byte mov a,r4 ;lowest bit of remainder mov 7,r7
mov 45h,a rlc a mov 6,r6
pop acc mov r4,a mov 5,r5
reti mov a,r5 ;shift next bit of rem mov 4,r4
; subroutine UDIV32 rlc a mov 3,r3
;32 bit /16 bit to 32 bit quotient and remainder un- mov r5,a mov 2,r2
signed mov a,r6 ;shift next bit of rem mov 1,r1
;input r3,r2,r1,r0 = dividend X rlc a mov 0,r0
;input r5,r4 = divisor y mov r6,a clr rs0
;output r3-r0 = quotient Q of X/Y mov a,r7 ;shift next bit of rem pop B
;r7,r6,r5,r4 =remainder rlc a pop dph
;alters acc, flags mov r7,a pop dpl
UDIV32: push 08 ;save reg. bank 1 mov a,r4 pop 0Fh
push 09 clr C pop 0EH
push 0AH subb a,04 pop 0Dh
push 0BH mov dpl,a pop 0Ch
push 0CH mov a,r5 pop 0bh
push 0DH subb a,5 pop 0ah
push 0EH mov dph,a pop 09
push 0Fh mov a, r6 pop 08
push dpl subb a,#0 ret
push dph mov 06,a END 

98 • JANUARY 2008 • ELECTRONICS FOR YOU WWW.EFYMAG.COM

You might also like