top of page

Switch Button interface Using PIC 16F877A micro-controller 

By Bernice Zuiya

Introduction

                                   Switch button interface with a micro-controller PIC, this is basically one of the most important project which anyone who for his first time want to interface a push button with a micro-controller. many device which are surrounding us which we are friendly use as peripheral of desktop are interfacing interfacing similarly as a push button interface, but here in this project I will not go in deep up to explaining different mode of interfacing a micro-controller using different module such USART (Universal Synchronous Asynchronous Trans receiver), USB (Universal Serial Bus), SPI (Serial Peripheral Interface), I2C (Inter Integrated Circuit), but I will simply show how to use a push button as an input to a micro-controller 16F877A.  

Working:

                    To interface a push button with a micro-controller is a very basic and simple thing which doesn't request to be an engineer to understand what is the principle behind this, first we need to know that a push button as only two state (ON and OFF ) where a ON State represent a close circuit and OFF represent an open circuit. in electronic when a is said to be closed it basically means the difference of electric potential across the two terminal is different to zero volt, inversely when it is said to be open which means zero volt is measured across the two terminal, read more here

  now from the two images presented above, let see the one which represent a switch in symbolic form. it has 3 terminal the following maybe the two different configuration:  

  1. WITH PULL DOWN RESISTOR

         a pull down is a resistance add in the circuit which its value may vary from 4k7 to 10k, in this case a pull down resistance is used to give a low state output when the switch open and when the switch is closed it gives a state high, basically the terminal 1 is connected to a positive terminal of supply , 2 is connected to the common ground and 3 is the output connected to one pin of our micro-controller. 

   2. WITH A PULL UP RESISTOR

the pull up resistor is actually the revers connection of supply to the circuit, which basically means that 1 is connected to ground , 2 is connected to positive terminal of supply and finally 3 is the output terminal, when the switch is open the the output is high and when it is close the output is low.

     I assume that you should have at least a basic basic knowledge of programming embedded system using any environment such as mikroC compiler for PIC in this case. connect the output of the pull down circuit to the first pin of port D of the micro-controller, then create a new project with the following instruction to setup properly you project !

  • create a new project using mikroC compiler (Device : PIC 16F877A )

  • Set the pin RD0 as an input Pin TRISD.B0 = 1;

  • Set the condition as shown in program left side !

when the switch is pressed the LCD Screen will display a message (switch pressed) else.. depending to what you want to use this. 

         The circuit diagram shown below is the very basic diagram required for blinking an led using the 16F877A micro-controller, you need to respect the connections to get best result.

  • a resistor of value 4k7 ohm is connected in series with +5 volt and the reset pin (1st pin)

  • Q1 is the crystal oscillator in this case its frequency oscillation is 20 MHz.

  • supply: the circuit is connected with a voltage maximum equal 5 volt

  • S1 where is a switch button and R4 is a pull down resistor

  • C1 & C2 are of 22pF.

Circuit Diagram

Code and Comment

sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;


unsigned int j;
void main() 
{
 LCD_Init();
 LCD_Cmd(_LCD_CURSOR_OFF);
 LCD_Cmd(_LCD_CLEAR);
 TRISD.B0=1;
 while(1){
         if( PORTD.B0 = 1){
                LCD_Out(1,1,"switch pressed");
               delay_ms(1000);
             }
               else{
                       LCD_Out(1,1,"switch Released");
                       delay_ms(1000);
              }
        }
 }

Download the full project here

This code were written using mikroC compiler software, you can copy and compile it or modify it as well, this is used to blink an led OFF and ON (second). 

bottom of page