Tag Archives: timer

Programmable digital timer switch using a PIC Microcontroller

Digital timer switches are used to control the operation of electrical devices based on a programmed schedule. This project describes a programmable digital timer based on the PIC16F628A microcontroller that can be programmed to schedule the on and off operation of an electrical appliance. The appliance is controlled through a relay switch. This timer switch allows you to set both on and off time. That means, you can program when do you want to turn the device on and for how long you want it to be remained on. The maximum time interval that you can set for on and off operation is 99 hours and 59 minutes. The project provides an interactive user interface using a 16×2 character LCD along with 4 push buttons.

Programmable digital timer

Note: (June 30, 2016) A revised version of this project with added new features is posted here.

Circuit Design

The circuit diagram of this project is shown below. A 5V relay is driven by a PN2222 transistor that is controlled by RB3 pin of PIC16F628A. Digital inputs from the 4 push buttons are read through port pins RA2, RA3, RA4, and RB0. The functions of these push buttons are discussed in the operation section below. A standard 16×2 character LCD is used in the project to display the device status, program menu and time. The LCD is operated in 4-bit mode, therefore, only 6 I/O pins of PIC16F628A are required to drive it. A piezoelectric buzzer provides audible tone when the timer is started and stopped. It also beeps when the device is turned on or off. The + 5V power supply for the circuit is derived from a LM7805 regulator IC. The input to the regulator is given from a 9V DC wall adapter.

Read more

Timers and counters


Most microcontrollers are equipped with one or more precision timing systems that can be used to perform a variety of precision timer functions including generating events at specific times, determining the duration between two events, or counting events. Example applications that require generating events include generating an accurate 1 Hz signal in a digital watch, keeping a traffic light green for a specific duration, or communicating bits serially between devices at a specific rate, etc.

Theory of operation

The main component of such a timing system is a free running binary counter. The counter increments for each incoming timing pulse. The counter counts continuously from 0 to 2n-1, where n is the number of bits in the counter. Since it runs independently, it can count inputs or clock pulses concurrently while the microcontroller is executing the main program. If the input clock to the binary counter has a fixed known frequency, we can make an accurate measurement of time interval by counting the pulses from the clock. For example, if a particular clock’s frequency is 1 MHz (period 1 µs), and we have counted 3000 pulses on the clock signal, then the elapsed time is 3000 microseconds.

In microcontrollers, the input clock to the internal timing system is software selectable. The microcontroller’s own clock source (which is fixed) can be selected as the clock input to the free running counter module of the timing system. In such a case, the timing system is known to be working as a timer because it is driven by a fixed known frequency clock. However, the input clock to the timing system can also be provided through an external I/O pin of the microcontroller in which case it is known to be working as a counter, and will count the external pulses which may appear at random intervals. These pulses could be manual inputs from a push button, or more likely, would be produced by some other signal source, like a road sensor that generates a pulse every time a car is passed over it.

A functional structure of a simple timer module is shown below. This has a free running 16-bit up counter that increments its value on each clock pulse. Thus, the 16-bit output count represents the number of pulses arrived since the counter was last reset to zero. We can convert this value into time interval by knowing the frequency (or period) of the input clock signal.

Suppose, we want to measure the time elapsed between any two successive events. Lets assume that when the first event occurs, the timer is reset to zero, and when the second event occurs, the timer output is 25000. If we know that the input clock has period of 1 µs, then the time elapsed between the two events is 25000×1 µs = 25 milliseconds.

Since this timer’s counter can count from 0-65535 (216-1), this particular arrangement can measure time ranging from 0 to 65535 x 1 µs = 65.535 milliseconds, with a resolution of 1 µs. The maximum time interval a timer can measure is known as the timer’s range, whereas the resolution of a timer defines the minimum interval it can measure.

The timer module has an additional output, overflow flag, that indicates the counter reaches its maximum output, in which case the timer rolls over to 0. In microcontrollers, the built-in timers generate timer overflow interrupts when they roll over back to 0 after reaching their maximum counting range. We can count the number of times the timer overflows, and thus extend the range of time we can measure. In such a case,

elapsed time = (stop count – start count) + number of overflows x (2n-1),

where, n = 16 for a 16-bit counter.

A more advanced timer has a prescaler (shown below). A prescaler is essentially a configurable clock-divider circuit. Depending on the selected configuration bits, the prescaler output could be the same as the input signal, or it may have half the frequency, one-fourth the frequency, one-eighth the frequency, etc. Thus, a prescaler can extend a timer’s range by reducing its resolution. For example, if we use a prescaler to our previous 16-bit timer to divide the clock frequency by 8, then the new range of the timer will be 65535 x 8 µs = 524.280 milliseconds. But the new resolution is reduced to 8 µs.

Timers in microcontrollers have additional configurable features such as enabling or stopping the counter, enabling or disabling interrupt generation when the counter overflows, selecting the clock edge for incrementing the counter, etc.

Recent Entries »