Using Push Button Switch with PIC Microcontroller

Using Push Button Switch with PIC Microcontroller

I hope that you have already read the first tutorial of Hi Tech C, LED Blinking using PIC Microcontroller. In that tutorial we blink LEDs connected to PORTB by writing to entire PORT and TRIS registers. In some cases we may want to set or reset individual bits of these registers. For that we can use the bit addressable feature of these registers. We have already seen in the previous tutorial that TRIS register is used to set the direction of each bits and PORT register is used to set the status of each bits in a port. A HIGH (1) at a bit of TRIS register makes the corresponding pin input and LOW (0) makes it output. In Hi-Tech C we can use following  methods to write to TRIS and PORT registers.


Writing a Register

TRISB = 0x0F // In binary TRISB = 0b00001111

PORTB = 0xF0 // In binary PORTB = 0b11110000

Writing Bit

TRISB0 = 1 // Makes RB0 a input pin

RB0 = 1 // Makes RB0 HIGH (VDD)

In this tutorial we will define a pin as input and another pin as output. A push button switch is connected to the input pin and the output pin drives an LED. When the switch is pressed, the LED glows for 2 seconds.

Circuit Diagram

Using Push Button Switch - PIC Microcontroller

Using Push Button Switch – PIC Microcontroller

Pin RD0 is defined as an input pin, which is used to connect push button switch and pin RB0 is defined as an output pin, which drives an LED. RD0 is pull up to VDD using a 10KΩ resistor such that when the switch is not pressed the pin will be at the potential VDD and when the switch is pressed it will be grounded. A 470Ω resistor is connected in series with LED to limit the current through it.

Hi-Tech C Code

#include <htc.h>
#define _XTAL_FREQ 8000000
void main()
  TRISD0 = 1; //RD0 as Input PIN
  TRISB0 = 0; //RB0 as Output PIN
  RB0 = 0; //LED OFF
    if(RD0 == 0) //If Switch is presseed
      __delay_ms(100); //Provides required delay
      if(RD0 == 0) //If Switch is still pressed
        RB0 = 1; //LED ON
        __delay_ms(2000); //2 second delay
        RB0 = 0; //LED OFF

I hope that most parts of the above program are explained in its comments. 100ms delay between reading status of switch is provided for following reasons.

  • The above program executes in 8MHz but the time period required for closing and opening the connection of switch varies from 100 – 300ms. Thus a delay must be provided while reading status of a switch. For eg: If you haven’t used delay while reading switch status which is used to counting, it will count multiple times with a single press.
  • It will avoid false triggering by makes sure that the switch is being pressed after a delay.
  • It will provide Switch Debouncing

Download Here

You can download Hi-Tech C files and Proteus files here…

Share this post

Comments (19)

  • sujith thycattussery

    How to set input, output, high, law.. pin by pin in hi tech c?

    December 5, 2013 at 12:06 am
  • Ligo George

    RB0 = 0 // makes 0th bit of portb low..
    RB0 = 1 // makes 1st bit of portb high..

    December 5, 2013 at 7:42 pm
  • sujith thycattussery

    please post a eeprom tutorial (16f877a)

    December 10, 2013 at 11:04 pm
  • Ligo George

    Thanks for the suggestion… We will post it soon..

    December 12, 2013 at 4:12 pm
  • Justin

    why included pic.h in this code ???

    January 21, 2014 at 11:32 pm
  • Mohamed SARSOUT

    do you mean: RB1=1 ?

    August 20, 2014 at 7:15 pm
  • Ligo George

    I don’t get your question. Please elaborate

    August 20, 2014 at 7:45 pm
  • Mohamed SARSOUT

    RB0 = 1 // makes 1st bit of portb high.., I think it is false
    the correct is:
    RB1=1, or makes 0st bit of portb high

    August 20, 2014 at 8:45 pm
  • Ligo George

    Yes. you are right.. I corrected it .

    August 20, 2014 at 9:47 pm
  • fayaz khan

    i want to control the firing angle of the triac in ac voltage controller by using a push button,please help me.
    thank you

    August 26, 2015 at 8:38 pm
  • fayaz khan

    by using 2 push buttons one for incrementing and other for decrementing firing angle of the triac (say in 9 steps one step 10 degrees)…can anybody please help me with code

    August 26, 2015 at 9:15 pm
  • adityaukey

    this quetion is really ppissing me off..some help pls admin?

    November 3, 2015 at 10:05 pm
  • edwin

    hello sir george,i have two questions,i’m newbie in microntrollers, my first question is,if i have asm files what software will i use i will convert to HEX FILES?
    NO.2,If i have HEX files coul i convert it to ASM files?

    November 21, 2015 at 8:26 am
  • Ligo George

    It is very simple. Declare a char variable and increment it during each key press. Write that character to PORTD.
    PORTD = a;

    November 21, 2015 at 2:07 pm
  • Ligo George

    You can you MPLAB IDE for that. No need of any compilers like Hi-Tech C or XC8.

    November 21, 2015 at 2:37 pm
  • Ligo George

    Above example is 100% working ? Make sure that your oscillator frequency is 8MHz and Pull up resistor for the switch is there.

    August 27, 2016 at 12:48 pm
  • vikram

    hello sir i am using push button and LCD , so my task is how to display on off action of push button on ‘LCD when i press every time
    means for 1 st press it should display on and second press it should display off , cycle repeats
    plz tell me how to write c code for this my mail ID [email protected] Mob No 9028614218

    September 23, 2016 at 5:56 pm
  • Ligo George

    Dear Vikram,
    Kindly use our forums ( ) for free technical support. Ping me on my email [email protected] if you need premium support.

    November 27, 2016 at 12:41 pm
  • muzhaffar

    Greetings, sir. Before I ask, I would like to thank you for all the tutorials you’ve created and posted in this website.

    I have a few question to ask regarding to my circuit in proteus. I’ve constructed the circuit you gave in the picture above. I also built the program using the hitech C compiler using Mplab software. And then, when I try to run the simulation in the proteus using the program i built, it didnt even say error or something like that, instead the led in the circuit itself wont light up. I tried to reinstall all my programming software (mplab, proteus, hitech c). it doesnt work. What should I do. This is really stressing me out. Your tutorials are the only source I have to learn about embedded programming.

    Thank you.

    July 24, 2017 at 12:31 pm

Leave a Reply

Your email address will not be published. Required fields are marked *