Using Push Button Switch with PIC Microcontroller

Using Push Button Switch with PIC Microcontroller

This article is meant for beginners in the field of microcontrollers. When I started with microcontrollers, as everyone I also need to learn how to interface a switch with microcontroller. Here we use PIC Microcontroller 16F877A and MikroC Pro compiler. This tutorial assumes you have basic knowledge about programming PIC Microcontroller, else you read the article Blinking LED using PIC Microcontroller.

Push to On Switch

Push to On Switch

Push to Off Switch

Push to Off Switch

In this tutorial we use a push button switch, when we press on it an LED glows for a second. Push Buttons are mechanical switches. Then can make or break connection between two terminals and comes back to stable state when released. They are called as Push to ON or Push to OFF switches respectively.

Circuit Diagram

Using Push Button Switch - PIC Microcontroller

Using Push Button Switch – PIC Microcontroller

Note: VDD and VSS of the pic microcontroller is not shown in the circuit diagram. VDD should be connected to +5V and VSS to GND.

Push button switch is connected to the first bit of PORT D (RD0) which is configured as an input pin. Which is connected to a pull up resistor such that this pin is at Vcc potential when the switch is not pressed. When the switch is pressed this pin RD0 will be grounded. The LED is connected to the first bit of PORT B (RB0) and a resistor is connected in series with it to limit the current.


MikroC Program

void main()
{
  TRISD.F0 = 1; //Configure 1st bit of PORTD as input
  TRISB.F0 = 0; //Configure 1st bit of PORTB as output
  PORTB.F0 = 0; //LED OFF
  do
  {
    if(PORTD.F0 == 0)   //If the switch is pressed
    {
       Delay_ms(100);    //Switch Debounce
       if(PORTD.F0 == 0)//If the switch is still pressed
       {
         PORTB.F0 = 1; //LED ON
         Delay_ms(1000); //1 Second Delay
         PORTB.F0 = 0; //LED OFF
       }
    }
 }while(1);
}

In the above program you may noticed that the switch is checked twice with a 10 millisecond delay. This is to avoid invalid clicks by Switch Bouncing.

Share this post

Comments (147)

  • Raja

    In the line PORTB.F0 what it denotes .f0?

    January 29, 2013 at 6:28 pm
  • Ligo George

    F0 denotes the first pin of PORTB …… F1 denotes second pin…

    PORT register determines the logic level on a pin… while TRIS register determines whether a pin is input or output…

    January 29, 2013 at 8:41 pm
  • Qamar

    Very helpful. Thanks man

    February 4, 2013 at 4:22 pm
  • Reda Edris

    Thanks man

    March 23, 2013 at 11:23 am
  • sneha

    thank you so much.Can you help me for stepper motor project??? I want to do project based on stepper motor with speed increment and decrement switch using pic 18f4550.
    Please give reply.Thank you.

    August 2, 2013 at 3:33 pm
  • Ligo George

    Please Read this article : http://www.electrosome.com/stepper-motor-pic-microcontroller/

    You should make some changes in the program…. in PIC18F4550 when you write output to a pin use LAT register, eg: LAT.F0 = 1; and use PORT register to read data from it, eg: if(PORTD.F0 == 1)…. and use TRIS register to write direction…
    Adjust the delay between stepping to control the speed of the stepper motor..

    August 3, 2013 at 7:23 am
  • swany

    how do i use Button in Miscellaneous lib??

    August 7, 2013 at 8:37 pm
  • Ligo George

    I don’t get your question… can you please elaborate…??

    August 8, 2013 at 7:41 am
  • cliff rayner

    Hi..i need some help here. im doing a project that need to move 3 motors move 180 degree back and forth in sequence with delay time of 5 seconds each using PIC.this project need only one switch to operate. when the switch is pressed,the first motor will rotate 180 degree back and forth.after the first motor back to its original position,the second motor will operate same as the first motor.same goes to the third motor.the operation stop when the third motor back to its original position.

    i need help on circuit diagram as well as coding that can be use in proteus software.

    September 16, 2013 at 9:51 pm
  • Ligo George

    If you want to control exact degree.. then use servo motor or stepper motor………. If the application requires good torque use stepper motor… the programming and circuit will depends on the type of motor you are using…

    September 17, 2013 at 7:39 am
  • cliff rayner

    for this time,im using a 12v dc motor .i do this project is just only to proof the concept ..can u help me in doing the coding and circuit?i need to use PIC too..

    September 17, 2013 at 10:17 am
  • cliff rayner

    changing of idea here,ignore the degree of rotation..i just want it to rotate clockwise and anticlockwise automatically.

    September 17, 2013 at 11:15 am
  • Ligo George

    Then it is simple try control the motor using L293D..

    http://www.electrosome.com/dc-motor-l293d-pic-microcontroller/

    if(switch pressed)
    {
    rotate motor 1 clockwise;
    Delay .. use the required delay here..
    rotate motor 1 anti-clockwise..
    Delay
    rotate motor 2 clockwise
    Delay
    rotate motor 2 anti-clockwise
    Delay.

    ….
    ..
    }

    September 18, 2013 at 7:29 am
  • Dinesh

    Hi, I have a doubt in LCD interfacing,I have connected PIC16f877a and LCD in breadboard,how can i give a power supply to breadboard and also how to dumb my code in to PIC.

    October 3, 2013 at 12:10 pm
  • Ligo George

    To write program to pic microcontroller you need a pic programmer..

    October 3, 2013 at 7:10 pm
  • nik

    i’ve make a little adjustment to your circuit…i disable the switch debounce function… can u add another push button on RD1 to switch off the led….im using mikroC

    October 13, 2013 at 12:30 pm
  • Ligo George

    yes.. you can add it…
    if(PORTD.F0 == 0)
    {
    PORTB.F0 = 1;
    }
    if(PORTD.F1 == 0)
    {
    PORTB.F0 = 0;
    }

    October 14, 2013 at 7:28 am
  • nik

    yeah thanks man….do i have to add another power supply if i wanna use 24v 5pin relay in this circuit??

    October 14, 2013 at 1:56 pm
  • Ligo George

    Yes….use 5V for pic and use 24V for relay…

    October 16, 2013 at 5:30 pm
  • nik

    excuse me sir… how do i know whether my picbroken or not….because i just bought it and test above code on a breadboard…the pic also didnt work on pic kit

    October 27, 2013 at 12:35 am
  • Ligo George

    Did your programmer detect the pic??
    if yes try blinking led..

    October 27, 2013 at 5:15 pm
  • nik

    pic detected….but blinking led not working 🙁

    November 4, 2013 at 6:12 pm
  • Ligo George

    If the circuit diagram and program are correct .. it should work.. ..
    Try using the hex file and circuit diagram provided by us..
    .. Try changing your crystal..

    November 8, 2013 at 8:23 am
  • nik

    if i use a timer for 30 second to switch on led 6&7….is there any way that i can turn on led before the timer times up….
    here is my command
    void main() {
    TRISB.F0 = 1;//THERMISTOR
    TRISB.F1 = 1;//SWITCH MANUAL
    TRISB.F6 = 0;//MOTOR
    TRISB.F7 = 0;//SOLENOID
    PORTB.F6 = 0;//INITIAL STATE MOTOR
    PORTB.F7 = 0;//INITIAL STATE SOLENOID

    do
    {
    if(PORTB.F0==0)
    {
    PORTB.F6 = 1; //ON AFTER 5 SECOND
    PORTB.F7 = 1;
    }
    if(PORTB.F1==0)
    {
    delay_ms(30000);
    PORTB.F6 = 1;//RELAY MOTOR ON
    PORTB.F7 = 1;//RELAY SOLENOID ON
    }

    }while(1);

    }

    November 9, 2013 at 11:37 am
  • Ligo George

    MOTOR and SOLENOID is turned on in the if clause … but it is not turned off anywhere except at the beginning…

    November 14, 2013 at 7:35 am
  • gaurav thakar

    // LCD module connections

    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;

    // End LCD module connections

    char txt1[] = “BEST”;

    char txt2[] = “WAY”;

    char txt3[] = “FOR”;

    char txt4[] = “Learning”;

    char i; // Loop variable

    void Move_Delay() { // Function used for text moving

    Delay_ms(500); // You can change the moving speed here

    }

    void main(){

    // ANSEL = 0; // Configure AN pins as digital I/O

    // ANSELH = 0;

    // C1ON_bit = 0; // Disable comparators

    // C2ON_bit = 0;

    Lcd_Init(); // Initialize LCD

    // Lcd_Cmd(_LCD_CLEAR); // Clear display

    // Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

    // Lcd_Out(1,6,txt3); // Write text in first row

    // Lcd_Out(2,6,txt4); // Write text in second row

    // Delay_ms(2000);

    // Moving text

    i = 0;

    PORTA = 0;

    //LCD_Config(&PORTD,1,0,2,7,6,5,4);

    // PORTB = 0;

    TRISA = 0x00;

    do

    {

    if(PORTA.F0 == 0)

    { Lcd_Cmd(_LCD_CLEAR); // Clear display

    Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

    Lcd_Out(1,6,txt3); // Write text in first row

    i=i+1;

    delay_ms(50);

    }

    if ( PORTA.F1 == 0)

    {

    Lcd_Cmd(_LCD_CLEAR); // Clear display

    Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

    Lcd_Out(1,6,txt2); // Write text in first row

    Delay_ms(100);

    }

    }

    while(1);

    }

    this code interfacing button with LCD in micro C but not working please tell me what the problem in my code

    November 27, 2013 at 5:42 pm
  • sachin rana

    hello sir i want to know about the programming of power factor correction using atmega 16 plz tell me the code how can i make thus projects

    November 27, 2013 at 9:19 pm
  • Ligo George

    Try after disabling the adc.. .and configure pin PORTA.F0 as digital input by writing to ADCON registers… if you are using PIC16F877A..

    November 28, 2013 at 4:18 pm
  • Ligo George

    Refer this : http://electrosome.com/automatic-power-factor-controller-using-microcontroller/
    and try searching on google…
    Please don’t ask us to develop complete code for you…. we have no time for that… We will only provide support for projects in our site…

    November 28, 2013 at 4:24 pm
  • GAURAV THAKAR

    i am working hard but still i cant done, my last code i share here please help me for button and lcd interfacing in 16g877a in micro c

    // LCD module connections

    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;

    // End LCD module connections

    char txt1[] = “BEST”;

    char txt2[] = “WAY”;

    char txt3[] = “FOR”;

    char txt4[] = “Learning”;

    char i; // Loop variable

    void Move_Delay() { // Function used for text moving

    Delay_ms(500); // You can change the moving speed here

    }

    void main(){ // Configure AN pins as digital I/O

    ADCON1 = 7;

    // Disable comparators

    CMCON = 7;

    Lcd_Init(); // Initialize LCD

    TRISA = 0xFF;

    do

    {

    if(PORTA.F0 == 0)

    { Lcd_Cmd(_LCD_CLEAR); // Clear display

    Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

    Lcd_Out(1,6,txt3); // Write text in first row

    i=i+1;

    delay_ms(5);

    }

    if ( PORTA.F1 == 0)

    {

    Lcd_Cmd(_LCD_CLEAR); // Clear display

    Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

    Lcd_Out(1,6,txt2); // Write text in first row

    Delay_ms(10);

    }

    }

    while(1);

    }

    November 28, 2013 at 10:03 pm
  • gaurav thakar

    thank you ligo now my code is working

    November 29, 2013 at 5:56 pm
  • Ligo George

    Thanks for the feedback..

    November 29, 2013 at 6:02 pm
  • PRABHAT RANJAN

    i want to trigger vibrating wire sensor by using a square wave with the interval 1 to 2 ms provide me a code because my code is not working properly

    February 21, 2014 at 10:34 am
  • abdulla

    Could please give me some codes for the tuning lights.

    March 15, 2014 at 9:46 am
  • james

    helo can u help..

    March 30, 2014 at 9:11 pm
  • Anurag Rabadia

    hi i want to do on the led when switch is pushed.
    if the same switch is again pushed then led will be turned off..please help.

    May 22, 2014 at 5:15 pm
  • Anurag Rabadia

    hi i want to do on the led when switch is pushed.
    if the same switch is again pushed then led will be turned off..please help.

    Can you please help….?

    May 22, 2014 at 5:48 pm
  • Ligo George

    Yes..
    try this logic
    led = off
    if(switch on)
    {
    led = !led (! = not)
    }

    June 12, 2014 at 9:24 pm
  • IVO

    Hello George.. I need help that might be a different switch debouncing. I need to press a key and hold it for 3 seconds to enter in some type o programming mode. “lets say, turn ON an LED, or change some variable value” After I entered I should press and hold the key again for 3 seconds to exit.. If I use the same explanation for deboucing with more delay can Will that work safely?

    June 14, 2014 at 1:19 am
  • joynal

    hear is push button switch when press led on and automatic off.

    how can i do coding mikroc when push button press led on and again press push button led off.

    August 5, 2014 at 4:38 pm
  • Ligo George

    do like this :
    if(switch pressed)
    {
    PORTB.F0 = !PORTB.F0;
    }

    August 5, 2014 at 5:19 pm
  • joynal

    not working hare code:

    void main()
    {
    TRISD.F0 = 1; //Configure 1st bit of PORTD as input
    TRISB.F0 = 0; //Configure 1st bit of PORTB as output
    PORTB.F0 = 0; //LED OFF
    do
    {
    if(PORTD.F0 == 0) //If the switch is pressed
    {
    Delay_ms(10); //Switch Debounce
    if(PORTD.F0 == 0)//If the switch is still pressed
    {
    PORTB.F0 = !PORTB.F0;
    }
    }

    }while(1);

    }

    August 5, 2014 at 5:55 pm
  • Ligo George

    Try this :
    void main()
    {
    TRISD.F0 = 1; //Configure 1st bit of PORTD as input
    TRISB.F0 = 0; //Configure 1st bit of PORTB as output
    PORTB.F0 = 0; //LED OFF
    do
    {
    if(PORTD.F0 == 0) //If the switch is pressed
    {
    Delay_ms(100); //Switch Debounce
    if(PORTD.F0 == 0)//If the switch is still pressed
    {
    PORTB.F0 = !PORTB.F0;
    }
    }
    Delay_ms(200);
    }while(1);

    }

    August 5, 2014 at 7:53 pm
  • joynal

    Thank you it’s working,
    and now how can coding fast switch press fast led on, and 2nd switch press 2nd led on.

    August 6, 2014 at 1:05 pm
  • joynal

    how can i do coding mikroc when 1st switch press 1st led on and 2nd switch press 2nd led on?

    August 7, 2014 at 11:44 am
  • d67akash

    Do you kindly give some example how to configure PIC12F675 8 pin microcontroller I/Os in micro c

    August 8, 2014 at 8:03 pm
  • Ligo George

    Try this logic :
    if(switch 1 pressed)
    {
    LED 1 ON;
    }
    if(switch 2 pressed)
    {
    LED 2 ON;
    }

    August 9, 2014 at 8:55 pm
  • Ligo George

    Hello, use GPIO instead of PORTB, PORTD etc.. and TRISIO instead of TRISB, TRISD etc..

    August 9, 2014 at 9:00 pm
  • joynal

    1st switch working but 2nd switch not working. here is code

    void main()
    {
    TRISD.F0 = 1; //Configure 1st bit of PORTD as input
    TRISB.F0 = 0; //Configure 1st bit of PORTB as output
    PORTB.F0 = 0; //LED OFF
    do
    {
    // LED 1
    if(PORTD.F0 == 0) //If the switch is pressed
    {
    Delay_ms(10); //Switch Debounce
    if(PORTD.F0 == 0)//If the switch is still pressed
    {
    PORTB.F0 = !PORTB.F0; // LED out port
    }
    }
    Delay_ms(10); // LED Delay

    // LED 2

    if(PORTD.F1 == 0) //If the switch is pressed
    {
    Delay_ms(10); //Switch Debounce
    if(PORTD.F1 == 0)//If the switch is still pressed
    {
    PORTB.F1 = !PORTB.F1; // LED out port
    }
    }
    Delay_ms(10); // LED Delay

    }while(1);

    }

    August 10, 2014 at 12:49 pm
  • Hannah

    Hi. How can I use the same button to change the pattern of the blinking of the led? for example;

    Button’s 1st press: the led will turn on and stays on
    Button’s 2nd press: the led will blink 5 times
    Button’s 3rd press: the led will blink 3 times

    I can’t seem to get it to work using the same button. Thanks in advance!

    August 14, 2014 at 6:12 am
  • joynal

    1st switch working but 2nd switch not working. here is code

    void main()
    {
    TRISD.F0 = 1; //Configure 1st bit of PORTD as input
    TRISB.F0 = 0; //Configure 1st bit of PORTB as output
    PORTB.F0 = 0; //LED OFF
    do
    {
    // LED 1
    if(PORTD.F0 == 0) //If the switch is pressed
    {
    Delay_ms(10); //Switch Debounce
    if(PORTD.F0 == 0)//If the switch is still pressed
    {
    PORTB.F0 = !PORTB.F0; // LED out port
    }
    }
    Delay_ms(10); // LED Delay

    // LED 2

    if(PORTD.F1 == 0) //If the switch is pressed
    {
    Delay_ms(10); //Switch Debounce
    if(PORTD.F1 == 0)//If the switch is still pressed
    {
    PORTB.F1 = !PORTB.F1; // LED out port
    }
    }
    Delay_ms(10); // LED Delay

    }while(1);

    }

    August 14, 2014 at 12:36 pm
  • Ligo George

    Try this :

    void main()
    {
    TRISD.F0 = 1; //Configure 1st bit of PORTD as input
    TRISD.F1 = 1;
    TRISB.F0 = 0; //Configure 1st bit of PORTB as output
    TRISB.F1 = 0;

    PORTB.F0 = 0; //LED OFF

    do
    {
    // LED 1
    if(PORTD.F0 == 0) //If the switch is pressed
    {
    Delay_ms(10); //Switch Debounce
    if(PORTD.F0 == 0)//If the switch is still pressed
    {
    PORTB.F0 = !PORTB.F0; // LED out port
    }
    }
    Delay_ms(10); // LED Delay

    // LED 2

    if(PORTD.F1 == 0) //If the switch is pressed
    {
    Delay_ms(10); //Switch Debounce
    if(PORTD.F1 == 0)//If the switch is still pressed
    {
    PORTB.F1 = !PORTB.F1; // LED out port
    }
    }
    Delay_ms(10); // LED Delay

    }while(1);
    }

    August 14, 2014 at 6:28 pm
  • Ligo George

    Try like this …
    void main()
    {
    char cnt;
    cnt = 0;

    while(1)
    {
    if(switch pressed)
    cnt++;
    if(cnt == 1)
    {
    //action here
    }
    else if(cnt == 2)
    {
    //action here
    }
    }
    }

    August 14, 2014 at 6:33 pm
  • joynal

    Thank u Ligo George

    How to coding for battery with 4 led, when battery 12v 1st led on other off, after battery 11v than 2nd led on other led off, again battery 10v 3rd led on and other led off, again battery 9v 4led on other led off,

    August 17, 2014 at 2:19 pm
  • Ligo George

    Hello, you can use PIC’s ADC for that..

    You asking questions which are out of our scope…. This commenting system is only intended to ask doubts related to above article….
    If you wan’t premium support you need to hire one of our engineers. ..

    August 18, 2014 at 12:19 pm
  • joynal

    how can i premium support..

    August 18, 2014 at 4:19 pm
  • Ligo George

    just drop a mail to [email protected] or use Contact Us Page..

    August 18, 2014 at 4:29 pm
  • joynal

    Thank u,
    How can i do coding when button press 1st led on, again press 2nd led on 1st led off, again press 3rd led on 1st and 2nd led off,

    August 18, 2014 at 5:02 pm
  • Joshua Quizon Icabales

    hello! can i have an idea about the program for this situtation;
    when the button is pressed, the port connected to relay1 will go high, then when the button is released the the port connected to relay2 will go high while the port in relay1 will turn low and after the buttton is still release the port will go off.
    can you help me with this?

    thank you!

    August 31, 2014 at 8:27 pm
  • Ligo George

    Try like this :
    if(switch pressed)
    Relay 1 ON
    while(switch pressed); // LOOP HERE
    Relay 1 OFF
    Relay 2 ON

    Better solution is to use PORTB On Change Interrupt.

    September 1, 2014 at 4:47 pm
  • Joshua Quizon Icabales

    thank you but my concern is when the button is released then the relay1 is off and the relay2 is on what do you think about this? thank you for your reply

    September 1, 2014 at 8:56 pm
  • Joshua Quizon Icabales

    and hoe do I use the PORTB on change interrupt? sorry for questions I’m a beginner in using PIC thank you for the concern

    September 1, 2014 at 9:21 pm
  • Ligo George

    Try this article : http://electrosome.com/hc-sr04-ultrasonic-sensor-pic/
    it uses PORTB On Change Interrupt.

    September 1, 2014 at 10:38 pm
  • Ligo George

    Yes, you can do it by simply writing to PORT register.

    September 1, 2014 at 10:39 pm
  • Joshua Quizon Icabales

    can you explain further it can i see i sample code thank you ligo 🙂

    September 2, 2014 at 12:47 pm
  • Ligo George

    Visit this page : http://electrosome.com/hc-sr04-ultrasonic-sensor-pic/
    There is a sample code.

    September 3, 2014 at 11:31 am
  • Ligo George

    like this :

    while(switch pressed); //Waits for switch release
    Relay 1 OFF;
    Relay 2 ON

    September 3, 2014 at 11:33 am
  • Joshua Quizon Icabales

    void main()
    {
    TRISC.F6 = 1; //Configure 1st bit of PORTD as input
    TRISB.F6 = 0; //Configure 1st bit of PORTB as output
    PORTB.F6 = 0; //LED OFF
    do
    {
    if(PORTC.F6== 0) //If the switch is pressed
    {
    Delay_ms(100); //Switch Debounce
    if(PORTC.F6 == 0)//If the switch is still pressed
    {
    PORTB.F6 = 1; //LED ON
    Delay_ms(1000); //1 Second Delay
    PORTB.F6 = 0; //LED OFF

    }
    }
    }while(1);
    }

    when i write this the led is already on and when i push the button it turns off what is wrong with my program

    September 4, 2014 at 5:51 pm
  • Ligo George

    Try changing 0 to 1 :

    if(PORTC.F6== 1) //If the switch is pressed
    {
    Delay_ms(100); //Switch Debounce
    if(PORTC.F6 == 1)//If the switch is still pressed

    September 4, 2014 at 10:22 pm
  • ahsan

    Ligo Thankyou so much, its very help full to me.

    October 26, 2014 at 2:04 am
  • ahsan

    Why we using while(1) ?

    October 26, 2014 at 2:12 am
  • Ligo George

    while(1) >> while(TRUE)…. makes INFINITE LOOP… It will run the program as far the power is given to microcontroller..

    October 26, 2014 at 10:09 am
  • ahsan

    okay now i understand thank you so much .

    October 27, 2014 at 2:11 am
  • Ligo George

    🙂

    October 27, 2014 at 6:33 pm
  • aarthisree

    hi. i want to do a task. if push button is pressed once the led wants to turn on. again if the switch is pressed the led wants to turn off. plz help me

    November 7, 2014 at 12:51 pm
  • Ligo George

    void main()
    {
    TRISD.F0 = 1; //Configure 1st bit of PORTD as input
    TRISB.F0 = 0; //Configure 1st bit of PORTB as output
    PORTB.F0 = 0; //LED OFF
    do
    {
    if(PORTD.F0 == 0) //If the switch is pressed
    {
    Delay_ms(100); //Switch Debounce
    if(PORTD.F0 == 0)//If the switch is still pressed
    {
    PORTB.F0 = !PORTB.F0;
    }
    }
    }while(1);
    }

    November 7, 2014 at 1:14 pm
  • Rakib

    Hi,

    How can I coding,
    when 1st switch press 1st led on and 2nd led off. again 1st switch press 1st led off and 2nd led on.

    When 2nd switch press 1st led on and 3rd led off. again 2nd switch press 1st led off and 3rd led on.

    November 27, 2014 at 3:23 pm
  • padmanaban

    hi! what is the program when i press abutton,how much i press button and display in lcd!!

    November 29, 2014 at 5:06 pm
  • padmanaban

    how to count the number of input that is switch using every 1s and display it in lcd!!

    November 29, 2014 at 5:48 pm
  • Ligo George

    Just use a variable to count it… and you can convert that variable in to string using IntToStr() mikroc function…. and you can print it on the LCD

    December 2, 2014 at 9:08 am
  • Ligo George

    You may use timer module of PIC or.. you can use a variable for checking time which is incremented on every iteration of the loop..

    December 2, 2014 at 9:09 am
  • ANEESH M

    thankssssssss

    December 2, 2014 at 10:29 am
  • Ahsan Ahmed Siddiqui

    This code is running perfectly in proteus, but with same code when i try in real hardware the led start blinking itself without button pushed, please help me if there is bother with ADC ? i am using pic16f877a

    December 3, 2014 at 3:02 am
  • padmanaban

    thanks!can u give some logic for this?

    December 4, 2014 at 10:59 am
  • Ligo George

    It will work without any problem if you are using above code and circuit..

    December 5, 2014 at 11:22 am
  • Ligo George

    Sorry, please use our forums ( https://electrosome.com/forums )if you need support outside the scope of above tutorial

    December 5, 2014 at 11:27 am
  • padmanabam

    thank u!

    December 5, 2014 at 9:05 pm
  • chirag patel

    hi !!!

    how to connect more than 50 leds with pic micro controler and ac power ??????????

    please help me.

    thanks.

    December 10, 2014 at 2:36 pm
  • chirag patel

    hi

    could u plz tell me in c language as i do programming in mp lab…c language.

    thanks

    December 10, 2014 at 2:37 pm
  • chirag patel

    hi sorry to disturb u …

    i need help with this program.

    it doesnt work on hardware..
    ///////////////////////////////////////////////////////////////////////////
    #include

    #define _XTAL_FREQ 4000000

    void main()

    {

    TRISD=0b00000000;

    TRISB=0xf0;

    PORTD=0b00000000;

    INTEDG=0;

    ANSEL=0x00;

    ANSELH=0x00;

    RBIE=1;

    GIE=1;

    IOCB=0xff;

    while(1)

    {

    //PORTD=0b11111111;

    }

    }

    void static interrupt isr()

    {

    char c;

    if(RBIF==1)

    {

    RBIE=0;

    GIE=0;

    //c=PORTB;

    {

    PORTD=!PORTD;

    }

    GIE=1;

    RBIF=0;

    INTF==0;

    }

    }

    December 10, 2014 at 2:47 pm
  • Ligo George

    You can use a transistor driver to drive 50 LEDs from mains.. You may also use a capacitor based transformer less power supply for PIC..

    December 12, 2014 at 6:17 pm
  • Ligo George

    int main()
    {
    TRISB0 = 0; //RB0 as Output PIN
    TRISD0 = 1; //RD0 as Input PIN

    RB0 = 0; //LED Off

    while(1)
    {
    if(RD0 == 0) //If Switch Pressed
    {
    RB0 = !RB0;
    __delay_ms(300);
    }
    }
    return 0;
    }

    December 12, 2014 at 6:23 pm
  • Ligo George

    Which compiler you are using ? If your doubt is not related to above article.. please open a new topic in our forums.. ( https://electrosome.com/forums )

    December 12, 2014 at 6:24 pm
  • iqwanwahab

    sir,,
    can you please show me a coding for 8 led and 4 push button,,
    and the led will only light up when push 2 button,,
    hope you can help me as soon as possible,,
    need this for my assignment,,

    December 17, 2014 at 3:30 pm
  • Shivendra Kr. Sahu

    hi i want to do on-off the led one by one If the switch is unpressed.its i call Task_1. it work fine .those are

    as

    void Task_1()
    {
    if(PORTD.F0 == 1)//If the switch is unpressed . its free runing code
    {
    PORTB.F0 = 1; //LED ON
    Delay_ms(1000); //1 Second Delay
    PORTB.F0 = 0; //LED OFF

    PORTB.F1 = 1; //LED ON
    Delay_ms(1000); //1 Second Delay
    PORTB.F1 = 0; //LED OFF

    PORTB.F2 = 1; //LED ON
    Delay_ms(1000); //1 Second Delay
    PORTB.F2 = 0; //LED OFF

    PORTB.F3 = 1; //LED ON
    Delay_ms(1000); //1 Second Delay
    PORTB.F3 = 0; //LED OFF
    }}

    now i want to modified code as, if switch is pressed 1 time PORTB.F0 = 1; LED on-off till 20sec , if agen

    pressed PORTB.F1 = 1; LED on-off till 25sec ,if agen pressed PORTB.F2 = 1; LED on-off till 30sec , if agen

    pressed PORTB.F3 = 1; LED on-off till 25sec . Switch Debounce time is 100 ms , its i call Task_2
    if switch is pressed more then Switch Debounce time , mcu extit Task_2
    so now code is

    void Task_2()
    {
    if(PORTD.F0 == 0) //If the switch is pressed 1 time
    {
    Delay_ms(100); //Switch Debounce
    if(PORTD.F0 == 0)//If the switch is still pressed
    {
    PORTB.F0 = 1; //LED ON
    Delay_ms(1000); //1 Second Delay
    PORTB.F0 = 0; //LED OFF
    }
    }}
    for 2nd time whats need ? ?
    Can you please help….?

    December 21, 2014 at 9:06 pm
  • Ligo George

    Please use our forums : https://electrosome.com/forums for asking doubts outside the scope of above article. Also elaborate your question.

    December 22, 2014 at 12:22 pm
  • Ligo George

    You can use a variable to count the switch pressing.

    December 22, 2014 at 12:35 pm
  • Shivendra Kr. Sahu

    pl give example pl .

    December 22, 2014 at 6:23 pm
  • chirag patel

    hi.
    i am using Proteus 8, hi tech compiler.

    thanks.

    December 27, 2014 at 2:02 pm
  • Ligo George

    You can use the same code in Proteus 8.

    December 28, 2014 at 2:34 pm
  • chirag patel

    i tried mine codes but not succeed.and i want to use portb as input and want to use interrupt service routine.

    thanks.

    December 31, 2014 at 4:11 pm
  • Ligo George

    The codes given in this article will work on Proteus without any problem. But when using in hardware you should make sure that configuration bits are correct.

    January 3, 2015 at 8:42 am
  • chirag patel

    r u talking about mine code or ur code??????

    January 5, 2015 at 12:14 pm
  • fatima

    can any one tell the logic used in the main function?i mean plz explain if u understand

    /* Interfacing 4-digit 7-segment display

    This program implements time-multiplexed 4-digit 7-segment

    display interfacing of type common anode. PORT A (pin 2 to 5) and

    PORT B are used for digit selection and segement lines, respectively.

    Author M Tahir

    Date Nov 25, 2013

    */

    #define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108))

    #define GPIO_PORTA_DATA_R (*((volatile unsigned long *)0x400043FC))

    #define GPIO_PORTB_DATA_R (*((volatile unsigned long *)0x400053FC))

    #define GPIO_PORTF_DATA_R (*((volatile unsigned long *)0x40025040))

    #define GPIO_PORTA_DIR_R (*((volatile unsigned long *)0x40004400))

    #define GPIO_PORTB_DIR_R (*((volatile unsigned long *)0x40005400))

    #define GPIO_PORTA_DEN_R (*((volatile unsigned long *)0x4000451C))

    #define GPIO_PORTB_DEN_R (*((volatile unsigned long *)0x4000551C))

    #define GPIO_PORTF_DEN_R (*((volatile unsigned long *)0x4002551C))

    #define GPIO_PORTF_PUR_R (*((volatile unsigned long *)0x40025510))

    #define SYSCTL_RCGC2_GPIOABF 0x00000023 //Port A,B,F Clock Gating Control

    #define PORT_A_PINS 0x3C // Port A pins 2,3,4 and 5

    #define DEALY_COUNT 10000

    void Delay(unsigned long counter);

    void Port_Init(void);

    // Global variable declarations

    unsigned char Lookup_7Seg_Disp[10] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};

    unsigned char i, count=0, flag=0;

    int main()

    {

    // Initialize the ports

    Port_Init();

    while(1)

    {

    for(i = 1; i <= 4; i++)

    {

    GPIO_PORTB_DATA_R = Lookup_7Seg_Disp[i+count];

    // Select the digit on Port A

    GPIO_PORTA_DATA_R = (PORT_A_PINS – (1 < 5)

    count = 0;

    Delay(DEALY_COUNT);

    }

    }

    else

    {

    flag = 0;

    }

    }

    }

    // Intialization routine for setting up the required ports

    void Port_Init()

    {

    // Enable the clock for port A, B and F

    SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOABF;

    // Configure Port A and B direction and digital enable functions

    GPIO_PORTB_DIR_R = 0xFF;

    GPIO_PORTB_DEN_R = 0xFF;

    GPIO_PORTA_DIR_R = PORT_A_PINS;

    GPIO_PORTA_DEN_R = PORT_A_PINS;

    // Configure port F as digital input with pullup

    GPIO_PORTF_DEN_R |= 0x10; // enable digital I/O on PF4

    GPIO_PORTF_PUR_R |= 0x10; // enable weak pull-up on PF4

    // Make port A and B logic high so all the LEDs are OFF.

    GPIO_PORTA_DATA_R = PORT_A_PINS;

    GPIO_PORTB_DATA_R = 0xFF;

    }

    // This function implements the delay

    void Delay(unsigned long counter)

    {

    unsigned long i = 0;

    for(i=0; i< counter; i++);

    }

    January 7, 2015 at 6:36 pm
  • Ligo George

    Please use our forums (https://electrosome.com/forums) for asking doubts which are not related to above topic.

    January 10, 2015 at 9:08 am
  • chirag patel

    is there any errors in my codes as i put it above???

    please check my codes.

    thanks.

    January 10, 2015 at 11:53 am
  • chirag patel

    which type of configuration bits i got to set ???

    thanks.

    January 11, 2015 at 4:17 am
  • namratha

    Hi Ligo George..,

    I need your help am working on pic16f877a and using XC8 complier. Have interfaced pushbutton if I press the button value should increment.. its working fine but one more condition – if I keep press and hold down for long time fastely value should increment how to do this I dont no am trying but not achieved. Below is my button code

    void main()
    {
    int oldstate;
    oldstate = 0;

    while(1)
    {
    if(RA4 == 1) // switch

    {

    oldstate = 1; // update flag

    }

    if(oldstate &&(RA4 == 0))

    {

    val++;

    if(val > 10)

    {

    val = 1;

    }

    oldstate = 0; // update flag

    }
    }

    January 27, 2015 at 3:07 pm
  • Ligo George

    I hope that your problem is already solved here :
    https://electrosome.com/topic/pic16f877a-push-button/

    January 27, 2015 at 5:34 pm
  • Mohan Aitha

    can i have program using four switches to display four different status on lcd pic 16f877a

    February 10, 2015 at 8:53 pm
  • Ligo George

    Sorry we don’t have such program…
    You should program it yourself.. Commenting section is to ask doubts about above article..
    Please use our forums : https://electrosome.com/forums for asking doubts not related to above article.

    February 17, 2015 at 8:11 am
  • abdelmalek

    iam sorry but could you help me
    i want to turn on a led by pressing on a switch and wont turn off till a minute is passed or the by pressing on the same switch

    March 8, 2015 at 10:31 am
  • Ligo George

    try like this :
    t = 0;
    if(SWITCH PRESSED)
    {
    LED = ON;
    while(t < constant)
    {
    if(SWITCH PRESSED)
    {
    LED = OFF;
    break;
    }
    DELAY;
    t++;
    }
    }

    March 11, 2015 at 10:16 am
  • Nyaro

    Hello sir i tried this and i got the led off when the switch is released. When the switch is pressed the led is on and when released its off. But actually what I want is to make the led on on single press and release and the state must be saved and off only when there is another switch press. So can you give me some idea. I have tried many things but not working. Thanks for the help

    March 19, 2015 at 6:02 am
  • Ligo George

    try like this :
    if(SWITCH PRESS)
    {
    LED = !LED;
    }

    March 23, 2015 at 3:08 pm
  • dheeraj

    same thing by ccs copmpiler..
    and thnks in advance

    May 20, 2015 at 4:31 pm
  • Ozan

    It does not work. LED turns on even if the button is not pressed!

    June 10, 2015 at 10:46 pm
  • Ozan

    Sorry, it works but I could not understand why LED turns on when the press is not pressed even if the code tells the inverse?

    June 10, 2015 at 11:11 pm
  • Ligo George

    The above project is 100% working.

    June 16, 2015 at 9:41 pm
  • Ligo George

    It will work without any problems.

    June 16, 2015 at 9:43 pm
  • haseeb

    hello sir i need to switch on led only when a switch is released after press…

    June 22, 2015 at 9:53 pm
  • Ligo George

    It is very simple. You can do it yourself if you understand above program.

    July 1, 2015 at 7:08 pm
  • Mthendeleka Somkhele

    Sir, im trying to implement a logic where button1 when pressed sets a pin x to 1 and clears pin y to 0. button 2 when pressed sets pin y to 1 and clears pin x. i can get one setting but the other button cannot set its pin , it only clears the other pin. plz help. heres my code.

    void main() {
    TRISD.F6=1;
    TRISD.F2=0;
    TRISD.F5=1;
    TRISD.F3=0;
    TRISD.F4=1;
    TRISC.F4=0;

    while(1){
    if(PORTD.F6==0 ) //If the switch is pressed
    {
    Delay_ms(100); //Switch Debounce
    if(PORTD.F6==0)//If the switch is still pressed
    {
    PORTD.F2 = 1; //LED ON
    PORTD.F3=0; // turn off others
    }
    }
    if(PORTD.F5==0 ) //If the switch is pressed
    {
    Delay_ms(100); //Switch Debounce
    if(PORTD.F5==0)//If the switch is still pressed
    {
    PORTD.F2=0; // turn off others
    PORTD.F3 = 1; //LED ON

    }
    }
    }
    }

    October 25, 2015 at 6:46 pm
  • Ligo George

    I think the program is correct.

    November 21, 2015 at 1:52 pm
  • padmaj

    How to

    December 14, 2015 at 8:55 am
  • padmaj

    How to control blinking speed of led using analogue channel of pic micro

    December 14, 2015 at 8:56 am
  • Ligo George

    You an use variable delay functions in MikroC. Just connect a potentiometer and read its value using ADC.

    December 19, 2015 at 3:57 pm
  • prathap gangadhar

    hello sir , i need your help, i need if i press switch (switch1), LED should be On. If again press and hold same switch(switch1),other LED should be On state, please its urgent , can you send me the code please sir

    February 8, 2016 at 3:47 pm
  • Aien Ika Rahman

    how to select different location of city by using switch button in pic16f877A microC?
    anyone can help me please??

    May 26, 2016 at 6:24 pm
  • Aien Ika Rahman

    May i know how to make PIC16F877A coding to select different location of city?

    May 26, 2016 at 6:39 pm
  • Ligo George

    Sorry, I don’t understand your project.
    Please use our forums ( https://electrosome.com/forums ) for asking such questions.

    June 2, 2016 at 6:42 pm
  • Bunheng

    Excuse me.
    How could I write a code of an example below:
    -single click PB0 => PD0=1 /LED1 ON/
    -double click PB0 => PD1=1 /LED2 ON/
    Thanks in advanced.

    July 20, 2016 at 12:02 am
  • Ligo George

    You may use interrupt feature or just a simple counter.

    August 27, 2016 at 1:11 pm
  • Aravind NA

    hi sir
    i want to do on the led when switch is pushed.
    if the same switch is again pushed then led will be turned off

    for that ques u replied like
    led = off
    if(switch on)
    {
    led = !led (! = not)
    }
    can pls tell me exact code..

    September 11, 2016 at 2:48 pm
  • Ligo George

    You can easily do it if you understand above tutorial.

    September 14, 2016 at 12:37 pm
  • alibaba

    sir, i want to do control two led with use one push button only.
    can u help me?

    December 15, 2016 at 9:17 pm
  • Ligo George

    Kindly describe your applicaiton.
    Please use our forums ( https://electrosome.com/forums/ ) for asking doubts outside the scope of above article.

    December 17, 2016 at 2:22 pm
  • ELIAS GOMES

    Whe user TRISD.F0
    INSTEAD OF TRISD.RD0?

    January 6, 2017 at 12:55 am
  • John Kaup

    Hi Ligo
    I loaded your schematic into proteus with the code and couldn’t get the simulation to run until I replaced the pull-up resistor with a simple diode.

    I would like to use your example on a machine. It works like this:
    1. I have to push 2 buttons together to get an input to the micro controller
    2. Air cylinder 1 must move for a set time and then stop.
    3 Air cylinder 2 must move for a set time and then stop.
    4. Air cylinder 1 must continue.
    5. Air cylinder 3 must move for a set time and then stop.
    6. Air cylinder 1 must return to home position.

    Am I able to do this or is there a better way?

    thankyou in advance
    John

    August 3, 2017 at 3:39 pm
  • Ahmed

    Hi, I have watched your tutorial on “Using Push Button Switch with PIC Microcontroller” and I am trying to make something similar, that is, output voltage “3.3V” if input is bigger than 1V using PIC24F16KL402. But initially I want to implement simple output voltage to turn an LED on. I’ve tried:

    #include
    #include
    #include “trial.h”
    #include

    void main(void) {
    TRISB = 0b00000000;
    while(1){
    PORTBbits.RB3 = 1;
    }
    }

    But nothing is happening. I’m using mplab with pickit 3, everything seems to be fine, the code compiles and gets downloaded to the pic but not working.
    Help! Thanks!

    August 9, 2017 at 8:25 pm
  • haziq syahmi

    can help me draw PIC18F4550 AND CODE
    MY QUESTION
    exercise
    Sw1 = led1 on,buz on
    sw2 = led2 on,buz off

    February 16, 2018 at 2:22 pm
  • adam

    Hi sir..I want to interface 5 leds with 1 push button..plz help me!mail me

    February 22, 2018 at 11:17 am
  • Adam Ubaid

    SIR.. PLZ HELP..
    I have measured volt+out volt on lcd but ,,i want to measure one button …if i press the button ,then if(PORTA.RA3 == 1) //If the switch is pressed
    {
    Lcd_Out(2,1,”OVERLOAD “);
    overload should be showed on lcd,,
    //
    my program
    float inputread,inputread2, inputmeasured,inputmeasured2, in2,in;
    // Lcd module connections start
    sbit LCD_RS at RC7_bit;
    sbit LCD_EN at RD4_bit;
    sbit LCD_D4 at RD5_bit;
    sbit LCD_D5 at RD6_bit;
    sbit LCD_D6 at RD7_bit;
    sbit LCD_D7 at RB3_bit;

    sbit LCD_RS_Direction at TRISC7_bit;
    sbit LCD_EN_Direction at TRISD4_bit;
    sbit LCD_D4_Direction at TRISD5_bit;
    sbit LCD_D5_Direction at TRISD6_bit;
    sbit LCD_D6_Direction at TRISD7_bit;
    sbit LCD_D7_Direction at TRISB3_bit;

    char txt[9]; // declare a char array
    char txt2[11]; // declare a char array
    void main(){
    TRISA.RA1=1; // PORTA is input
    TRISA.RA2=1; // PORTA is input
    TRISA.RA3=1;
    TRISB=0; // PORTC is Output
    TRISC=0; // PORTC is Output
    ADC_Init(); // Initialize ADC module with default settings
    Lcd_Init(); // Initialize Lcd
    Lcd_Cmd(_LCD_CLEAR); // Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
    Lcd_Out(1,7,”Welcome”); // dsiaplay the welcome logo on the LCD screen at position ROW=1 COLUMN=1
    delay_ms(100); // keep displaying logo for 5s
    Lcd_Cmd(_LCD_CLEAR); // Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
    Lcd_Out(1,3,”Adam Ubaid”);
    delay_ms(100); // keep displaying logo for 5s
    while(1){

    inputread = ADC_Read(1);
    inputread2 = ADC_Read(2);// Read analog value from channel 0
    delay_ms(5);

    inputmeasured=(inputread*4.88)/1000; // converting to value
    in = (inputmeasured/0.005);
    inputmeasured2=(inputread2*4.88)/1000; // converting to value
    in2 = (inputmeasured2/50000.0);
    floatToStr(in,txt);
    floatToStr(in2,txt2);

    Lcd_Out(3,1,”PV Volts”);
    lcd_out(3,12,txt);
    Lcd_Out(4,1,”OUTPUT V “);
    lcd_out(4,12,txt2);
    if(PORTA.RA3 == 1) //If the switch is pressed
    {
    Lcd_Out(2,1,”OVERLOAD “);

    }
    } }

    April 10, 2018 at 2:02 pm
  • Ligo George

    Better try some other PORTS. For PORTA you need to configure it as digital input first.

    Don’t forget to configure TRIS register for setting whether a pin is Input/Output.

    April 21, 2018 at 12:21 pm
  • Ratnesh Kumar

    hii sir, i want to program led as,
    when i pressed 1st button then 1st led on.
    when i pressed 2nd button then 1st led off,without realising the 1st button.

    please sir tell me.

    June 17, 2018 at 1:24 pm
  • Stalin AR

    Above tutorial is not working properly. I was not able to control LED by switch , LED is glowing like blinking. can you please write the code for that?

    September 28, 2018 at 4:56 pm
  • saugata chatterjee

    (LED) = (~LED);
    __delay_ms(100);

    December 30, 2018 at 9:56 pm

Leave a Reply

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