Microcontroller-Based Tachometer: Construction
Microcontroller-Based Tachometer: Construction
Microcontroller-based
Tachometer SUNIL K
UMAR
Prof. K. PADMANABHAN you first place a contrasting colour
mask. A strip of white adhesive tape is
A
tachometer is nothing but a ideal on the spinning object. Position it
simple electronic digital trans- such that the intensity of light reflected rotates.
ducer. Normally, it is used for from the object’s
Parts surface
Listchanges as it Each time the tape spins past
measuring the speed of a rotating shaft. Semiconductors: the probe, the momentary increase
The number of revolutions per minute IC1 - AT89C2051 in reflected light is detected by the
(rpm) is valuable information for un- microcontroller phototransistor. The signal processor
IC2 - ULN2003 current buffer
derstanding any rotational system. For IC3 - CA3140 operational and microcontroller circuit counts
example, there is an optimum speed amplifier the increase in the number of such light
for drilling a particular-size hole in a T1-T4 - BC557 pnp transistors
reflections sensed by it and thereby
T5 - 2N2222 npn transistor
particular metal piece; there is an ideal T6 - L14F1 photo-transistor evaluates the rpm, which is displayed
sanding disk speed that depends on D1 - 1N4007 rectifier diode on the 4-digit, 7-segment display.
DIS1 - KLQ564 4-digit,
the material being finished. You may The phototransistor is kept inside
7-segment display
also want to measure the speed of fans Resistors (all ¼-watt, ±5% carbon): a plastic tube, which has a convex lens
you use. R1-R4 - 1-kilo-ohm fitted at one end. A convex lens of
This easy-to-make photoelectric R5, R6 - 10-kilo-ohm about 1cm diameter and 8-10cm focal
R7-R11 - 1.2-kilo-ohm
tachometer measures the rpm of most RNW1 - 10-kilo-ohm resistor length is a common item used by watch
shop-floor tools and many household network repairers and in cine film viewer toys.
machines without any mechanical or VR1 - 4.7-kilo-ohm preset It can be obtained from them to set up
electrical interface. Capacitors:
the experiment. The phototransistor is
C1 - 10µF, 16V electrolytic
C2, C5, C6 - 0.1µF ceramic disk fixed on a piece of cardboard such that
How it works? C3, C4 - 22pF ceramic disk it faces the lens at a distance of about
Just point the light-sensitive probe tip Miscellaneous: 8 cm. The leads from the phototransis-
S1 - Push-to-on switch
atop the spinning shaft towards the S2 - On/Off Switch tor are taken out and connected in the
spinning blade, disk or chuck and read XTAL - 12MHz circuit shown in Fig. 1. Fig. 2 shows
the rpm. The only requirement is that BATT. - 6V battery the suitable arrangement of phototran-
9 2 • J a n ua ry 2 0 0 8 • e l e c t ro n i c s f o r yo u w w w. e f y m ag . co m
Construction
pins P1.7 through P1.2, and port- select one digit out of the four at a time
3 pin P3.7 are connected to input and to supply anode-drive currents to
pins 1 through 7 of ULN2003. the common anode pin of respective
Port-1 pins are pulled up with digit. Pin configuration of transistor
10-kilo-ohm resistor network BC557 is shown in Fig. 3.
RNW1. They drive all the seven When pin P3.0 of microcontroller
segments of the display with the IC1 goes low, it drives transistor
help of internal inverters. T1 into saturation, which provides
Port-3 pins P3.0 through P3.3 the drive current to anode pin 6 of
of the microcontroller are con- 4-digit, 7-segment, common-anode
Fig. 2: Suitable arrangement of phototransistor nected to the base of transistors display DIS1. Similarly, transistors T2
T1 through T4, respectively, to through T4, respectively, provide sup-
sistor.
ply to common-
The detected sig-
anode pins 8, 9
nal is amplified by
and 12 of DIS1.
transistor 2N2222 (T5)
Thus microcon-
and further amplified
troller IC1 drives
by operational ampli-
the segment in
fier CA3140 (IC3). The
multiplexed man-
reference voltage point
Fig. 3: Pin ner using its port
configuration of for the operational am-
pins. This is time-
transistor BC557 plifier is obtained by
division multi-
resistor divider net-
plexing process.
work comprising R2 and R3. The
Segment data
output from pin 6 of IC3 is fed to pin
and display-en-
12 of microcontroller AT89C2051. Note
able pulse for
that pins 12 and 13 of microcontroller
display are re-
AT89C2051 are the inputs (+ and -) of
freshed every
its internal analogue comparator. Pin
5 ms. Thus, the
13 is adjusted to nearly half the sup-
display appears
ply voltage using a potential divider
to be continuous
comprising resistor R7 and preset VR1
even though it
across the supply.
lights up one by
The pulses picked up by the
one.
phototransistor are sensed by the Fig. 4: A single-side, actual-size PCB layout for microcontroller-based
tachometer Switch S1 is
internal comparator of AT89C2051
used to manually
and, through software, each pulse
reset the micro-
representing one rotation of the object
controller, while
is detected. By counting the number of
the power-on-re-
such pulses, on an average per minute
set signal for the
basis, the RPM is evaluated. It is dis-
microcontroller
played by a software routine to light
is given by C1
up the LED segments of the 4-digit,
and R6. A 12MHz
7-segment display.
crystal is connect-
Circuit description ed to pins 4 and 5
of IC1 to generate
Fig. 1 shows the circuit of the mi-
the basic clock
crocontroller-based tachometer. The
frequency for the
tachometer comprises AT89C2051
microcontroller.
microcontroller, ULN2003 high-current
The circuit uses
Darlington transistor array, CA3140
a 6V battery for
operational amplifier, common-anode
power supply
7-segment (4-digit multiplexed) display
or alternatively
and its four anode-driving transistors.
a mains derived
The AT89C2051 is a 20-pin, 8-bit
low voltage sup-
microcontroller of Intel’s 8051 family
ply. An actual-
made by Atmel Corporation. Port-1
Fig. 5: Component layout for the PCB size, single-side
9 4 • J a n ua ry 2 0 0 8 • e l e c t ro n i c s f o r yo u w w w. e f y m ag . co m
Construction
PCB layout for the tachometer (Fig. 1) bright illumination on the object. the timer program in this case will be
is shown in Fig. 4 and its component Even an LED pen torch could be ‘400.’ This is divided by ‘600,000’ (so
layout in Fig. 5. used here. Avoid the fluctuating many 100/µs present in a minute),
background light from sources such giving a result of ‘1500.’ This gives the
Testing as tubelight. rpm. These digits are displayed on the
The source code of this article is avail- 4-digit, 7-segment display. To perform
able at http://www.electronicsforu. Software the division, subroutine UDIV32 is
com/efycodes/efy-codes.zip at code The software is written in Assembly employed, which is a standard sub-
file ‘tacho.hex.’ Using a program- language and assembled using 8051 routine available for 8051 family for
mer, load the code into the new chip cross-assembler. It is well comment- 32-bit number by 16-bit number divi-
AT89C2051. (Refer the May 2005 issue ed and easy to understand. It uses sion. It has an accuracy of 5 rpm in a
of EFY for article on programmer for AT89C2051’s internal timer for mea- 6000rpm count.
89C51 and 2051.) Then, fit it into the suring the period of one cycle of the EFY note. The source code of this
circuit board and after powering up the rotation in units of 100 microseconds. article is available at http://www.elec-
circuit, test it. Thus if the speed is 1500 rpm, it is 25 tronicsforu. com/efycodes/efy-codes.
For testing, point the probe us- rps, and the time taken for one cycle zip and will also be included in EFY-
ing torchlight for illumination of is 40 ms. CD of February 2008 issue.
the rotating object. For fans, use the The timer uses an interrupt to
light from behind. Hold the probe count overflows every 100 microsec- Prof. K. Padmanabhan retired from Alagappa Col-
lege of Technology, Guindy, Chennai
firmly so as to provide a steady, onds and so the number counted by
Tacho.asm
$mod51 mov 50h,#0FFH ENDD: ret
ORG 0H call refresh DISP1:
AJMP 30H disp: call refresh1 REFRESH:; content of 18 to 1B memory locations are
ORG 0BH ;TIMER 0 INTERRUPT VECTOR djnz 50h,disp ; so many times for a visible time limit output on LEDs
AJMP TIMER0ISR ;Timer 0 Interrupt service routine jmp beg ; only numbers 0 to 9 and A to F are valid data in these
address ;16 Bit Hex to BCD Conversion for 8051 Microcon- locations
ORG 30H troller MOV 18H,r3 ; least significant digit
MOV SP,#60H ;set stack pointer ;This routine is for 16 bit Hex to BCD conversion; MOV 19H,r4 ; next significant digit
MOV P3,#0FFH ;set all port 3 bits high to enable ;Accepts a 16 bit binary number in R1,R2 and returns 5 MOV 1AH,r5
inputs also digit BCD in ;R7,R6,R5,R4,R3(upto 64K ) MOV 1BH,R6 ; most significant digit (max:9999)
MOV P1,#03 ;set port 1 to all zeros expect bits 0,1 Hex2BCD: ;r1=high byte, r7 most significant digit, R2 RET
MOV TMOD,#01100001B ;TIMER 1 - MODE 2 = LSByte refresh1:
COUNTER,TIMR-0 TO 16 bit timer MOV R3,#00D MOV R0,#1bh ; 1b,1a,19,18, holds values for 4 dig-
BEG: MOV TH0,#0ffH ;TIMER REG.0 IS SET TO 0, MOV R4,#00D its
GIVES 64ms MOV R5,#00D MOV R4,#8 ; pin p3.3_ 0 made low one by one starts
MOV TL0,#-99 ; timer low reg. is also so MOV R6,#00D wth 18
setb et0 MOV R7,#00D mov r7,#2 ; decimal pt.on 3rd digit from left (2 nd
setb ea MOV B,#10D fromright)
mov 44h,#0 MOV A,R2 PQ2: CALL SEGDISP
mov 45h,#0 DIV AB dec R0
acall delay MOV R3,B ; mov a,r4
ajmp lowsig MOV B,#10 ; R7,R6,R5,R4,R3 rrc a
delay: mov r2,#10 DIV AB mov r4,a
djnz r2,$ ;wait 20 us MOV R4,B jnc pQ2
ret MOV R5,A PV3:RET
lowsig: jb p3.6,lowsig CJNE R1,#0H,HIGH_BYTE ; CHECK FOR HIGH SEGDISP:mov dptr,#ledcode
call delay BYTE MOV A,@R0
jnb p3.6,$ SJMP ENDD ANL A,#0FH
setb tr0 ; start timer HIGH_BYTE: MOV A,#6 MOVC A,@A+dptr
mov c,p3.6 ;high begins ADD A,R3 segcode:MOV R5,A
mov p3.5,c MOV B,#10 ORL A,#03H ; WE WANT TO USE PORT 1 BITS 0
acall delay DIV AB AND 1 FOR INPUT ANLOG
jb p3.6, $ MOV R3,B ; so retain them high
mov c,p3.6 ;low now ADD A,#5 S3: MOV P1,A ; SEGMENT_PORT
mov p3.5,c ADD A,R4 MOV A,R5 ;we use p3.7 for the segment ‘a’ of display
acall delay MOV B,#10 RRC A ;so get that bit D0into carry
jnb p3.6,$ DIV AB rrc a
mov c,p3.6 ;high begins again MOV R4,B mov p3.7,c ;segment ‘a;
mov p3.5,c ADD A,#2 S1: MOV A,R4 ; get digit code from r4 00001000
clr tr0 ;stop timer ADD A,R5 cpl a ;11110111
clr et0 ;and interrupt by timer MOV B,#10 rrc a ;11111011-1
mov r3,#0 ;number 600000 or 927c0 hex as Dividend DIV AB mov p3.0,c ; output to drive transsitors for digit
mov r2,#09h ; 9 MOV R5,B lighting
mov r1,#27h ;27 CJNE R6,#00D,ADD_IT rrc a ;11111101-1
mov r0,#0c0h ; c0 SJMP CONTINUE mov p3.1,c
mov r5,45h ;divisor is time for one cycle ADD_IT: ADD A,R6 rrc a ;11111110-1
mov r4,44h CONTINUE: MOV R6,A mov p3.2,c
call UDIV32 ;divide 60000/t DJNZ R1,HIGH_BYTE rrc a ;1111111-0 yes low makes left most digit show
mov 40h,r0 MOV B, #10D msdigit
mov 41h,r1 MOV A,R6 mov p3.3,c
mov r1,41h DIV AB S5:
mov r2,40h MOV R6,B S4: ACALL DELAY1 ; let it burn for some time
CALL HEX2BCD MOV R7,A MOV A,#0ffH ; extinguish the digit after that time
9 6 • J a n ua ry 2 0 0 8 • e l e c t ro n i c s f o r yo u w w w. e f y m ag . co m
Construction
MOV P3,A ; to prevent shadow mov r6,#0 mov 07,a
s6: RET mov r5,#0 cpl C
ledcode:DB 7EH,0CH,0B6H,9EH,0CCH,0DAH,0FAH mov r4,#0 jnc div_321
;these are code for the numbers 0 to 9 and A to F mov B,#32 ;set loop count mov r7,7
DB 0EH,0FEH,0CEH,0EEH,0F8H,72H,0BCH,0F6H, div_lp32:clr RS0 ;selet reg.bank 0 mov r6,6
0E2H clr C mov r5,dph
DELAY1:MOV 55h,#0ffH ; 1ms mov a,r0 ;shift highestbit of X mov r4,dpl
N: NOP rlc a div_321: mov a,r0
DJNZ 55h,N mov r0,a rlc a
RET mov a,r1 ;shift next bit of X mov r0,a ; shift result bit into partial quotient
TIMER0ISR:mov th0,#0ffh rlc a mov a,r1
mov tl0,#-90 ; in 100 us steps mov r1,a rlc a
push acc mov a,r2 ;shift next bit of X mov r1,a
mov a,#1 rlc a mov a,r2
clr c mov r2,a rlc a
add a, 44h ;count time btwn pulses mov a,r3 ;shift next bit of X mov r2,a
mov 44h,a rlc a mov a,r3
mov a,#0 mov r3,a rlc a
addc a,45h ;add carry to most sign. byte setb rs0 ;reg. bank 1 mov r3,a
mov 45h,a mov a,r4 ;lowest bit of remainder djnz B,div_lp32
pop acc rlc a mov 7,r7
reti mov r4,a mov 6,r6
; subroutine UDIV32 mov a,r5 ;shift next bit of rem mov 5,r5
;32 bit /16 bit to 32 bit quotient and remainder un- rlc a mov 4,r4
signed mov r5,a mov 3,r3
;input r3,r2,r1,r0 = dividend X mov a,r6 ;shift next bit of rem mov 2,r2
;input r5,r4 = divisor y rlc a mov 1,r1
;output r3-r0 = quotient Q of X/Y mov r6,a mov 0,r0
;r7,r6,r5,r4 =remainder mov a,r7 ;shift next bit of rem clr rs0
;alters acc, flags rlc a pop B
UDIV32: push 08 ;save reg. bank 1 mov r7,a pop dph
push 09 mov a,r4 pop dpl
push 0AH clr C pop 0Fh
push 0BH subb a,04 pop 0EH
push 0CH mov dpl,a pop 0Dh
push 0DH mov a,r5 pop 0Ch
push 0EH subb a,5 pop 0bh
push 0Fh mov dph,a pop 0ah
push dpl mov a, r6 pop 09
push dph subb a,#0 pop 08
push B mov 06,a ret
setb RS0 ;select reg.bank 1 mov a,r7 END
mov r7,#0 subb a,#0
9 8 • J a n ua ry 2 0 0 8 • e l e c t ro n i c s f o r yo u w w w. e f y m ag . co m