This document summarizes the key aspects of a project to build a frequency measuring device using a PIC microcontroller. It includes a block diagram of the system with components like the PIC, LCD display, voltage regulator and near zero detection circuit. The near zero detection circuit detects pulses from the AC mains supply which are counted by the PIC microcontroller to calculate the frequency. The frequency is then displayed on the LCD screen. The document also provides the program code and flowchart for the PIC microcontroller to implement this frequency measurement functionality.
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
Measuring Mains Frequency Using PIC Microcontroller
1. CHAPTER 1
INTRODUCTION
1.1 Introduction
As already mentioned in the abstract, power systems are mainly characterized by
their voltage and frequency. In general we can broadly classify the power systems
across the world into four major groups
1. 100-127V, 50Hz.
2. 100-127V, 60Hz.
3. 220-240V, 50Hz.
4. 220-240V, 60Hz.
(The voltages mentioned above are the R.M.S values and the peak values can
be obtained by multiplying the corresponding voltages with 1.414)
Presently, the frequencies employed mainly are 50Hz (20ms time period) and
60Hz (16.66ms time period). In India the normal system frequency is 50Hz. As
electricity cannot be stored, the instantaneous generation must match the demand
being taken from the system. If the instantaneous demand is higher than the
generation, the system frequency will fall. Conversely, if the instantaneous
generation is higher than the demand, the frequency will rise. System frequency
will therefore vary around the 50 Hz target and Grid has statutory obligations to
maintain the frequency within +/- 0.5Hz around this level.
So there is a requirement of continuous monitoring of mains frequency in
keeping a check on power fluctuations which is highly important as there are
many frequency sensitive instruments like the pulse dependent clocks which will
run slower or faster dependent on mains frequency. Also some losses acquired in
AC machines are dependent on the supply frequency.
1
2. 1.2 Objective:
The main objective of the project is to build an inexpensive frequency measuring
device, especially here to measure the supply mains frequency using a PIC
microcontroller. The range of measurement of frequency is from 10Hz to 125KH
(the pre scaler value set to 4).
1.3 Outline of the project:
1. The AC mains supply is first stepped down using a step down transformer ( ), and
is fed to a near zero detector circuit.
2. This output is given to a microcontroller which measures the frequency based on
an algorithm and displays it on an LCD screen.
2
5. Firstly the mains supply is stepped down from 230V to 9V by means of a step
down transformer and then it is rectified using a bridge rectifier. The full wave
rectified signal is applied to at the base of transistor whose output is normally
low. As the input signal to the transistor drops to a value below 0.7V the transistor
turns off and the collector voltage rises to +5V supply voltage as shown in the
fig.3. Three pulses of this output constitute one full period of mains frequency.
These pulses are then fed to the PIC microcontroller.
2.4 PIC18F4520 Microcontroller
The output of the near zero detector is fed to the 2nd
pin of PORTC of the PIC.
The input to this pin is continuously monitored for a high pulse (by the command
BTFSS)
The PIC microcontroller starts an accurate timer when the first pulse arrives and
the timer stops when the third pulse arrives. Thus the timer count is proportional
to the period and hence the frequency of the waveform. The timer count which is
in hex is converted into ASCII and then to the real frequency by an appropriate
formula and is displayed on an LCD display.
2.6 Cost and Management
S.No Components Quantity Cost(rupees)
1. Transformer (220V/9V) 1 100.00
2. Diodes (IN4008) 4 2.00
3. Resistors (10K Ohms) 3 1.00
4. PIC 18f4520(sampled) 1 70.00
5. Crystal Oscillator 1 0.50
6. LCD Screen(16X2) 1 150.00
7. Voltage regulator IC (LM7805) 1 5.00
8. PCB, Capacitors and Miscellaneous 50.00
Total cost = 278
NOTE:UART to USB converter(required for data loging in computer) 500rs
5
6. CHAPTER-3
OPERATION
3.1 Operation in detail
The output pulses of the near zero detector circuit are counted using 16-bit timer
or counter of the microcontroller (Here timer1 is used). A 20 MHz crystal
oscillator with a counting period is 0.2micro sec and maximum count is 65536. In
perfect 50Hz signal with 20ms period the maximum count will be 25000. The
frequency of the waveform is given by the formula. (Pre-scaler 4)
F= (1.25x106
)
Count
By considering that a difference of one count can be measured, the accuracy
measurement is then given by approximately 0.001Hz. For counting 3 pulses we
have roughly eight instructions and the minimum time required by the PIC to
execute these completely is around 8micro s.
6
7. 3.2 Algorithm
Initiate global program variables
Configure LCD
clear timer/ counter TMR1
Wait until the first low to high transition occurs
start timer/ counter TMR1
wait until second low to high
transition of the pulse
wait until third low to high pulse
transition of the pulse
get timer/ counter value
calculate the frequency
display the frequency on LCD
wait for 5 seconds
END
7
8. 3.3 Description of the Algorithm
Basically the program runs in an endless loop. In the main loop the following steps
are performed:
1. Initially Timer1 which is a 16-bit register is cleared ( actually two 8 bit registers
TMR1H, TMR1L are cleared).
2. The prescaler value is set so that the frequency is divided by 4 i.e., 20/(4x4) =
1.25.
3. Now the PORTA 2 is configured as i/p port and is continuously monitored and the
timer is started at the starting of the first pulse and stopped at the ending of the
third pulse.
4. Now the value from the TMRIH and TMRIL are brought to two registers NUMH
NUML, i.e the value of the count present in hex format. Then it is converted into
the ASCII format.
5. PORTD and PORTC are configured as o/p ports and PORTD for LCD data (i.e
they are connected to db0 to db7) and PORTC(4,5,6) is for the LCD control.
Then the count value is sent to the LCD (detailed program is appended in
appendix A)
8
9. CHAPTER 4
RESULTS AND DISCUSSION
4.1 RESULT
1. Near-Zero detector was set up and soldered on PCB and it was tested for the
required output.
2. The Program written for this project was simulated for results (on PROTEUS
V7.2) and then is flashed on the PIC microcontroller.
3. LCD is interfaced with the microcontroller and the timer 1 count is displayed on
the LCD Screen.
9
10. CHAPTER 5
CONCLUSION
5.1 CONCLUSION
An inexpensive frequency measuring device using PIC18f4520 was developed and
constructed on a PCB and tested for results.
5.2 FUTURE SCOPE OF THE PROJECT:
If can be looked into, the project can be extended to data logging i.e., the readings
acquired in these measurements will be logged into a computer by the following
procedure:
1. After the measurement the output of the PIC microcontroller will be given to a
MAX 232 IC Module which converts the PIC TTL to RS232 protocol. The RS-
232 serial communication protocol is a standard protocol used in
asynchronous serial communication. It is the primary protocol used over modem
lines. It is the protocol used by the MICRO STAMP11 when it communicates
with a host PC.
2. Through this serial communication protocol the measurement data is logged into
the computer and this data can be used for various purposes like plotting graphs of
frequency versus time.
Similarly measuring various power system parameters like voltage, current and
power factor at the same time i.e., developing a multi-purpose Meter and logger,
10
11. employing a microcontroller or microprocessor modules like RASPBERRY PI,
ARDUINO etc.
References
PIC 18f4520 Datasheet: ww1.microchip.com/downloads/en/devicedoc/39631e.pdf
Rs232 standards: http://www.omega.com/techref/pdf/RS-232.pdf
11
13. B7 MOVLW 0X20
MOVWF T1CON
CLRF TRISD
CLRF TRISC
BSF TRISA,2
MOVLW 0H
MOVWF TMR1H
MOVWF TMR1L
CALL LDELAY
B1 BTFSS PORTA,2
BRA B1
B2 BTFSC PORTA,2
BRA B2 ; we left first pulse to make sure we dont start counting in the middle of
the wave
BSF T1CON,0
B3 BTFSS PORTA,2
BRA B3
B4 BTFSC PORTA,2
BRA B4
B5 BTFSS PORTA,2
BRA B5
B6 BTFSC PORTA,2
BRA B6 ;here we get time for three pulses
BCF T1CON,0
MOVFF TMR1H,_ui_NumH ; we start converting 16bit hex value we got in
above timer to ascii values to be displayed on lcd
MOVFF TMR1L, _ui_NumL
13
22. APPENDIX B
RS232 PROTOCOL
Fig.4
The above figure shows the relationship between the various components in a
serial ink. These components are the UART, the serial channel, and the interface
logic. An interface chip known as the universal asynchronous receiver/transmitter
or UART is used to implement serial data transmission. The UART sits between
the host computer and the serial channel. The serial channel is the collection of
wires over which the bits are transmitted. The output from the UART is a
standard TTL/CMOS logic level of 0 or 5 volts. In order to improve bandwidth,
remove noise, and increase range, this TTL logical level is converted to an RS-
232 logic level before being sent out on the serial channel. This conversion is
done by the interface logic shown in the above figure. Usually in the systems the
interface logic is implemented by the comm stamp.
A frame is a complete and nondivisible packet of bits. A frame includes both
information (e.g., data and characters) and overhead (e.g., start bit, error checking
and stop bits). In asynchronous serial protocols such as RS-232, the frame
consists of one start bit, seven or eight data bits, parity bits, and stop bits. A
timing diagram for an RS-232 frame consisting of one start bit, 7 data bits, one
parity bits and two stop bits. Note that the exact structure of the frame must be
22
23. agreed upon by both transmitter and receiver before the comm-link must be
opened.
Most of the bits in a frame are self-explanatory. The start bit is used to signal the
beginning of a frame and the stop bit is used to signal the end of a frame. The
only bit that probably needs a bit of explanation is the parity bit. Parity is used to
detect transmission errors. For even parity checking, the number of 1's in the data
plus the parity bit must equal an even number. For odd parity, this sum must be an
odd number. Parity bits are used to detect errors in transmitted data. Before
sending out a frame, the transmitter sets the parity bit so that the frame has either
even or odd parity. The receiver and transmitter have already agreed upon which
type of parity check (even or odd) is being used. When the frame is received, then
the receiver checks the parity of the received frame. If the parity is wrong, then
the receiver knows an error occurred in transmission and the receiver can request
that the transmitter re-send the frame.
23