You are on page 1of 7

Digital Speedometer

Project Report

Ayush Rai, Tushar Mishra, Nikunj Saxena MIT, Manipal. Department of
Electronics and Communication 4/22/2010



















NAME Roll No.
Ayush Rai 105
Tushar Mishra 79
Nikunj Saxena 100

Mini-Project in Microcontrollers and Embedded Systems
6
th
Semester, BE.





Digital Speedometer

Page 2

Digital
Speedometer
Project Report
The aim of the project is to design and
build an inexpensive and versatile digital
speedometer. One application can be a
bicycle speedometer.
Speedometer
A speedometer is a device that measures
the instantaneous speed of a land vehicle.
Now universally fitted to motor vehicles,
they started to be available as options in
the 1900s, and as standard equipment
from about 1910 onwards.
Most speedometers are analog. They
employ permanent magnets attached to
the cars transmission to rotate a
speedcup, which is in turn connected to a
fine spring with a needle.
Digital speedometers are a newer
invention, being first available in the
1980s. They all convert the revolutions of
the wheel into speed by using various
electronic devices.
Basic Idea
The core idea is to have a sensor mounted
on the wheel which will enable the
measurement of revolutions. Time taken to
make one revolution can be used to
measure the instantaneous speed.
Design
The design is based around an 8051
microcontroller. This functions as the
brain of the device. It is responsible for
timing the revolutions, performing all the
necessary calculations to convert the raw
data into a meaningful kmph value.
Amongst the plethora of MCS

-51
compatible devices the Atmel AT89S51
was chosen because it is cheap, easily
available and almost a clone of the original
8051. Moreover, its biggest advantage is
that it supports Serial In-System
Programming. This means that a simple
LPT port programmer can be used to
program the device without removing it
from the circuit.
The sensor chosen is a Hall-effect sensor,
the Allegro A3121 (a Unipolar Hall-
effect Sensor). In the presence of a
magnetic field of sufficient strength it
produces a high output. Otherwise the
output is low. It is a simple 3-pin device
available in SIP and SOT23W packages.
For this design the SIP package is suitable.
The speed is calculated and then displayed
on two 7 Segment Displays. Two digits
displaying a maximum of 99 kmph will be
sufficient for most applications. Two BCD -
7 Segment Decoders (IC 4511) are also
needed to drive the 7 segment displays
while using minimum amount of C I/O
pins.
Other components like 7805 Voltage
Regulator and 11.0592 MHz Crystal are
required for power supply and timing
signals. Basic electronic elements like
Resistors, Capacitors, Switches and
LEDs complete the design.
Digital Speedometer

Page 3

Detailed Explanation
The following paragraphs will describe in
detail the exact implementation of the
design and the source code given in the
appendix.
Sensor Placement
As shown in the figure the hall-effect
sensor (A3121) is mounted on the fork of
the bicycle frame. A permanent magnet is
attached to one of the spokes of the wheel
such that it is directly opposite to the
sensor. This way at every revolution of the
wheel the output of the sensor will be high
when the magnet passes in front of it and
will be low otherwise. This effectively
creates a pulse of very short duration.

Code
Logic behind the code
This code basically counts the pulses
generated by the sensor (which are equal
to the number of revolutions) during a
predefined amount of time and converts it
to instantaneous speed by multiplying with
a scaling constant. The speed is then
shown on the 7 segment displays via the
4511 ICs.
Implementation
There are two 16-bit timers in the 8051
microcontroller, both of which can be used
to generate a delay or count external
pulses. In this code we use one of them to
generate a delay of required duration and
the other one to count the number of
pulses generated by the sensor.
Timer0 counts pulses
Timer1 generates delay
The amount of delay given is crucial. It
should be sufficiently large so as to have a
desired level of accuracy and to make the
display readable. If the delay is too small,
even one missed pulse will affect the
instantaneous speed by a large amount. A
large delay ensures better averaging of the
speed. Also if the delay is very small and
the display is updated very frequently it
will be difficult or impossible to read the
numbers displayed. On the other hand if
the delay is very large the changes in
speed will be updated on the display after
a considerable time. The delay chosen for
this design is 3 seconds.
The timers have their interrupts enabled
and the desired action is performed in the
ISR.
Scaling Constant
As explained earlier, the revolutions made
in 3 seconds are multiplied with a scaling
constant to convert it into kmph. The value
of this constant for a typical bicycle wheel
of radius 35cm is 2.6389. The value is
Digital Speedometer

Page 4

obtained as follows-
Revolutions per hour = Rev. per 3 sec * 20
* 60mins.
Kmph = Rev. per hour * Radius of wheel in
km (i.e. 0.00035km)* 2 * pi (i.e.
3.1415926)
Hence, ScalingConstant = 20 * 60 * 2 *
3.1415926 * 0.00035 = 2.6389
Major sections of the code
There are three major parts of the code -
the main function, Timer1 ISR and Timer2
ISR.
The main is responsible prepping the
peripherals and I/O ports for the design. It
loads the suitable values in the TMOD
register to make T0 as counter in mode 2
and T1 as timer in mode 1. THx and TLx
are loaded with the appropriate values.
TH1 & TL1 hold 4DB2h for ~50ms delay
and TH0 is loaded with 00h. Next the
interrupts are enabled and Timer1
interrupt is given the highest priority by
setting the appropriate bits in IE and IP
registers. Finally the program is locked into
an infinite loop doing nothing.
Timer1 ISR looks at a variable called flag
to determine whether ~3 seconds have
passed or not. Initial value of flag is 0.
This ISR is called every 50ms. For 3
seconds to pass, the interrupt should have
been called 60 times.
The ISR checks the value of the flag. If
this value is not equal to 60, it is
incremented, TH1 and TL1 are reloaded
and TF1 is cleared. Otherwise flag is
made 0 and another set of instructions is
executed to perform the following actions
* Reload TH1 and TL1, clear TF1.
* Read the value of TL0 into rps, which is
equal to the revolutions made by the wheel
in 3 seconds, and make TL0 zero.
* Multiply rps with ScalingConstant and
store in kmph.
* Convert kmph into two BCD digits
dechigh and declow.
* Put these BCD digits on Port2 to give the
values to IC 4511.
Timer0 ISR just clears the TF0 bit. Since
T0 is in mode2, TL0 is automatically
reloaded with the value in TH0 (i.e. 00h).
There is very little possibility of this ISR
ever being executed because 8-bit counter
can count up to 255. 255 revolutions in 3
seconds correspond to an instantaneous
speed of over 650 kmph for a wheel of
radius 35cm.
Appendix
1. The circuit diagram
2. The source code

0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
A A
B B
C C
D D
E E
F F
G G
U1
8051
AT89S51
P1B0T2
1
P1B1T2EX
2
P1B2
3
P1B3
4
P1B4
5
P1B5MOSI
6
P1B6MISO
7
P1B7SCK
8
RST
9
P3B0RXD
10
P3B1TXD
11
P3B4T0
14
P3B5T1
15
XTAL2
18
XTAL1
19
GND
20
P2B0A8
21
P2B1A9
22
P2B2A10
23
P2B3A11
24
P2B4A12
25
P2B5A13
26
P2B6A14
27
P2B7A15
28
P0B7AD7
32
P0B6AD6
33
P0B5AD5
34
P0B4AD4
35
P0B3AD3
36
P0B2AD2
37
P0B1AD1
39
P0B0AD0
38
VCC
40
P3B2INT0
12
P3B3INT1
13
P3B6WR
16
P3B7RD
17
PSEN
29
ALEPROG
30
EAVPP
31
U2
LM7805CT
LINE VREG
COMMON
VOLTAGE
U3 4511BT_5V U4 4511BT_5V
U7
CRYSTAL_VIRTUAL
11.052 MHz
J1
J2
U5
7 Segment DispIays
A B C D E F G
CK U6
A B C D E F G
CK
V1
9 V
C1
100nF
C2
10uF
X1
LED
Power Indicator
R1
330O
C3
30pF
C4
30pF
J3
HDR1X10
MaIe Header For ISP Programmer
C5
10uF
R2
8.2kO
270O
R2 to R16
RESET
Title:
Document N:
Date: Sheet of
Revision: Size:
Speedometer2
0001
2010-04-21 1 1
2.0 A
Description:
Manipal nstitute of Technology, Dept. of Electronics and Communication
A two-digit digital speedometer
using 8051 microcontroller.
Sensor_Input
C:\Users\Ayush Rai\Documents\Keil\Speedometer\Speedo 3.c
/* Speedo3.c [31-3-2010]
*
* Version 3.0 : Accuracy Increased. Minor bug fixed.
*
*
* Note : This code is still under development. The programmer intends
* to add more features and change the
* application in future versions.
*
* Application : Digital Speedometer.
* This program, written for an 8051 MCU (AT89S51) displays the speed
* in kilometers per hour on two 7 Segment displays with the revolutions
* of the wheel being sensed by a Hall Effect Sensor mounted on the
* fork of the wheel. (Primary application is a bicycle speedometer)
*
*
*
* Team : Ayush Rai [-raiayush@in.com-]
* Tushar Mishra
* Nikunj Saxena
*
* This software has been developed for the mini-project in
* Microcntrollers and Embedded Systems, by the above mentioned
* students (all undergraduates in Electronics and Communication
* Engineering at MIT, Manipal).
*
* Manipal Institute of Technology [-http://www.manipal.edu-]
*/
#include <reg51.h>
#define ScalingConstant = 2.6389 // 2.6389 = (20 * 60 * 2 * pi *
// radius of wheel in km) [i.e. 0.00035km or 35cm]
// {revolutions are measured every 3 seconds}
//rev per hour = rev. per 3sec*20*60.
//km per hour = rev. per hour*2*pi*radius in km
// i.e. 20*60*radius(in km)*2*pi = ScalingConstant
unsigned char rps,kmph,dechigh,declow,flag = 0;
/****************************************MAIN***************************************/
void main(void)
{
T0=1; // Make P3.4 (T0) as input. Hall-effect Sensor
// (Allegro A1106) output is connected to P3.4
T0=0;
TMOD = 0x16; //Timer0 as Counter in mode2, Timer1 as Timer in mode1;
TL1=0xb2; //4db2 generates a delay of 50ms for 11.052 Mhz Crystal
//(50ms * 20 = 1s)
TH1=0x4d;
TH0=0;
TL0=0;
IE=0x8a; //Enable interrupt for Timer 1 and 0. (EA=1, ET1=1, ET0=1).
IP=0x04; //Timer1 interrupt has highest priority.
TR1=1;
TR0=1;
Page: 1
C:\Users\Ayush Rai\Documents\Keil\Speedometer\Speedo 3.c
while(1);
}
/***********************************Timer1 ISR****************************************/
void timer1() interrupt 3
{
if (flag == 60) //Every 60th interrupt (i.e. every 3s) do the following-
{
TR1=0;
TF1=0;
TL1=0xb2; //Reset Timer1
TH1=0x4d;
TR1=1;

rps = TL0; //TL0 holds Revolutions Per 3 Seconds. (rps is in hex)
kmph = rps*ScalingConstant; //Multiply by ScalingFactor to get kilometres per hour.

//('kmph' is in hex)

flag = 0;
rps = 0;
TL0=0;

dechigh = kmph/10; //Divide hex value by 10 (0xA).
dechigh = dechigh <<4; //The quotient is the higher digit in decimal no.
//Shift to higher nibble.
declow = kmph%10; //The remainder is lower digit in decimal
P2 = dechigh | declow; //Output the decimal values to Port2.
//Port2 connected to IC4511.
}
else //If delay is <1s (i.e. Not the 20th interrupt) -
{flag++; //Increment flag.
TR1=0;
TF1=0;
TL1=0xb2; //Reset Timer1
TH1=0x4d;
TR1=1;
}
}
/************************************Timer0 ISR**************************************/
void timer0() interrupt 1
{TR0=0;
TF0=0; //Reset Timer0
TR0=1;
}
Page: 2