In these series of measurement projects with PIC microcontroller, we’re gonna discuss several projects like how to measure DC or AC Voltage, DC or AC Current, Resistance, Capacitance, Frequency, Light, Temperature, Humidity, Pressure, Rain, Soil moisture, Water level, Distance etc.

In this project, we are going to learn how to measure frequency of a signal using a PIC microcontroller and display it on an LCD display.

There are different methods that can be used to measure the frequency of an external signal, this is possibly the simplest method that we’re gonna use in this project, the signal whose frequency is to be measured is connected to the clock input of a microcontroller counter (timer). The counter is enabled for a known time window, and the total count is read. Since each count corresponds to a clock pulse of the external signal, we can easily determine its frequency.

For example, assuming that the time window is set to 1 s and the counter reaches 500 at the end of 1s, then the frequency of the signal is 500Hz. Thus, we can simply display the counter value as the frequency of the signal in hertz. This way, using a 16-bit counter, we can measure frequencies up to 65,535 Hz. If using a 32-bit counter, the maximum frequency that we can measure will be 4,294,967,295 Hz. For the measurement of high frequencies, we can actually use a 16-bit counter and increment a variable each time the counter overflows (65535–0). The total count can then be found by multiplying this variable by 65536 and adding the current counter reading.

The nice thing about using a 1 s time window is that we can directly display the frequency in hertz by simply reading the counter value. This also means that the measurement resolution is 1 Hz, which is acceptable for most measurements. Increasing the time window to 10 s will increase the resolution to 0.1 Hz. On the other hand, decreasing the time window to 0.1 s will reduce the resolution to 10 H z. Figure 1 below shows the circuit diagram of our project.

Figure 1: Digital Frequency Counter Circuit Diagram

In this design, the external signal whose frequency is to be measured will be connected to RA4 (Timer0 clock input: T0CKI ), Timer0 is used in a 16-bit counter mode and the frequency will be displayed on the LCD connected to PORTB of the PIC18F45K22 microcontroller.

Timer1 is used to create the 1 s time window. I t is not possible to generate a 1 s delay using Timer1 since the microcontroller clock frequency is high. Instead, the timer is configured to generate an interrupt every 250 ms, and when 4 interrupts are generated, it is assumed that 1 s has elapsed.

Assuming a prescaler setting of 8, the value to be loaded into Timer1 registers to generate interrupts at 250 ms (250,000 μs) intervals can be calculated from the following:

or

Decimal 3036 is equivalent to 0x0BDC in hexadecimals. Thus, TMR1H =0x0B and TMR1L = 0xDC.

MikroElektonika Timer Calculator can also be used to generate the required code as seen on figure 2 below:

Figure 2: Timer Calculator

Nowadays prototyping PCBs are getting cheaper, a very good news for makers, hobbyists and electronic startups with a limited budget. You can get 10 PCBs (1-2 layers) for only \$5 with PCBWay.com , a China Shenzhen-based manufacturer specializing in PCB prototyping, small-volume production and PCB Assembly service all under one roof.

Before sending Gerber files to manufacture your Printed Circuit Boards, it’s always good practice to check them one more time to make sure that everything is correct with a Gerber viewer to avoid bad surprises. These things do happen. Airwires not properly routed, general layout of your components especially connectors, track width etc.

PCBWay Free Online Gerber Viewer