Today we will learn how to read digital inputs from a push button switch. A digital input has only two values: 1 and 0. The configuration of the push button switch is same as that of the reset switch except it goes to a different port pin. The status of the switch will be read through RC1 and every time when it is pressed, an LED connected to RC0 will be toggled ON and OFF.

Required Theory

You must be familiar with the digital I/O ports of PIC16F688 and their direction settings. If you are not, read Digital I/O Ports in PIC16F688.

Circuit Diagram

To our last setup (see Lab 1: Flashing an LED), connect a push button switch to RC1 as shown below. The input ports of a PIC microcontroller have very high input impedance, and therefore, during normal condition when the switch is open, the RC1 pin is pulled 'High' through the external 10K resistor. When the switch is pressed, RC1 is pulled to ground and the microcontroller will read it as logic 'Low'. In this way, a simple push button switch with a pull-up resistor can be used to provide digital inputs (1 and 0) to the microcontroller.

Circuit for reading digital inputs.

Prototyped circuit on the breadboard


Since the switch is connected to RC1, this port pin must be defined as input. This is done by setting the corresponding bit in the TRISC register. Again, don't forget to disable the comparators and ADC channels. The code for this experiment is provided below. You should compile it with the MikroC compiler and load the output hex file inside the PICMicro. The MikroC compiler has an in-built library routine (named Button) for reading digital inputs from push button switches. The syntax is,

You can see that the Button command has debounce features built in.

Debouncing a switch

When a mechanical switch (like a push button) is pressed or released to make or break an electrical contact, the switch tends to make multiple transitions between 'open' and 'close' before coming to the stable final state. This is called switch bounce and this activity may go on for tens of milliseconds. We cannot see this bouncing but a microcontroller is relatively fast as compared to the action of the switch, and is, therefore, able to recognize this as multiple button presses and respond accordingly. Therefore, the switch must be debounced before it's status is read by a microcontroller. Debouncing circuits require extra hardware and thus raises the cost of the embedded system. The easiest solution is to implement debouncing in software. The simplest logic is to wait for a few milliseconds (5-20 ms) after the key press or release is detected, and read the final state of the switch again to make sure it is a valid button press or release.

Note that the configuration bits setting is same as in Lab 1.

Lab 2: Toggle LED with a Tact Switch Input
Internal Oscillator @ 4MHz, MCLR Enabled, PWRT Enabled, WDT OFF
Copyright @ Rajendra Bhatt
Oct 8, 2010
// Define LED @ RC0 and Tact switch @ RC1
sbit LED at RC0_bit;
sbit Switch at RC1_bit;
#define Switch_Pin 1
#define Switch_Port PORTC
#define Debounce_Time 20
void main() {
ANSEL = 0b00000000; //All I/O pins are configured as digital
CMCON0 = 0x07 ; // Disbale comparators
TRISC = 0b00000010; // PORTC All Outputs
TRISA = 0b00001000; // PORTA All Outputs, Except RA3
LED = 0;
do {
if (Button(&Switch_Port, Switch_Pin, Debounce_Time, 0)) {
if (!Switch) {
while (!Switch); // Wait for release of the button
} while(1); // Infinite Loop

Download the complete mikroC project files


Every time the switch is pressed, the LED toggles ON and OFF. Once the switch is pressed, the microcontroller will wait for its release before it detects another press.

 6 01share7