Using Push Button Switch with 8051 and Keil C – AT89C51

Using Push Button Switch with 8051 and Keil C – AT89C51

This tutorial is for beginners in the field of microcontroller. In this case the microcontroller is AT89C51, a reprogrammable derivative of 8051. This purpose of this tutorial is to familiarize with the use of push button switch with the microcontroller. It will be useful whenever a decision is to be made according to the press of a switch. If you are not yet started with 8051 with Keil C please refer this tutorial Blinking Led using 8051 and Keil C.

Circuit Diagram

LED and Switch Interfacing with AT89C51 8051 Microcontroller

LED and Switch Interfacing with AT89C51 8051 Microcontroller

Push button switch is connected to the first bit of PORT 0 (P0.0) which is configured as an input pin. Which is connected to a pull up resistor as there is NO INTERNAL PULL UP RESISTORS FOR PORT P0. Thus P0.0 pin is at Vcc potential when the switch is not pressed. When the switch is pressed this pin P0.0 will be grounded. The LED is connected to the first bit of PORT 2 (P2.2) and a resistor is connected in series with it to limit the current.

KEIL C Program

#include<reg52.h> /* special function register declarations */
/* for the intended 8051 derivative */

sbit LED_pin = P2^0; //Defining LED PIN
sbit switch_pin = P0^0; //Defining Switch PIN

void Delay(int); //Function prototype declaration

void main (void)
{
  switch_pin = 1; // Making Switch PIN input
  LED_pin=1; //LED off initially

  while(1) //infinite loop
  {
    if(switch_pin == 0 ) //If switch pressed
    {
      LED_pin = 0; //LED ON
      Delay(2000); //Delay
      LED_pin = 1; //LED OFF
    }
  }
}

void Delay(int k)
{
  int j;
  int i;
  for(i=0;i<k;i++)
  {
    for(j=0;j<100;j++)
    {
    }
  }
}

The program is self explanatory. So if you have any doubts please comment below.

Tip : You can do above project without 10KΩ PULL UP resistor by connecting switch to any other port.


Download Here

You can download entire keil c project here.

Share this post

Comments (31)

  • upputuri ramesh

    download is disabled..file not found y???

    November 11, 2013 at 4:16 pm
  • Ligo George

    The download problem is solved….. It was a problem caused during our server migration…
    Sorry for the inconvenience caused..

    November 14, 2013 at 7:39 am
  • pritesh

    led is not blink only on

    February 27, 2014 at 1:09 am
  • kuttAPPAN

    tool chain is not installed,,,wot to do

    August 26, 2014 at 10:28 pm
  • sachin

    led is not blinking in ISIS proteus.

    March 19, 2015 at 11:21 am
  • Ligo George

    It is working fine.. .I already checked in proteus.

    March 23, 2015 at 3:08 pm
  • divya

    switch_pin = 1; // Making Switch PIN input
    LED_pin=1; //LED off initially

    here how we can tel to compiler that led is used as output..My doubt is if i want to use switch as input ,i have to give sw=1.similarly if i want led as output we have to give led=0; am i correct sir..pls help me..how to determine whether it is input or output or high logic/low logic..,Because in pic TRIS & PORT are there to avoid confusion..here only port is there ..

    June 9, 2015 at 4:19 pm
  • Ligo George

    Hi,
    8051 is a primitive microcontroller. There is not 2 registers like PIC MCU in which each pins have 3 states (HIGH, LOW, INPUT-HIGH-IMPEDANCE).

    Here in 8085 each pin has only 2 states (LOW or INPUT-HIGH-IMPEDANCE) in which LOW state is used for output operation and other for input.

    June 16, 2015 at 9:33 pm
  • divya

    sir INPUT HIGH IMPEDANCE means ????

    June 18, 2015 at 6:44 pm
  • Ligo George

    All inputs should be in HIGH impedance state.. otherwise it may LOAD the input signal.

    July 1, 2015 at 6:58 pm
  • divya

    Actually i m having doubt on delay function..For delay i use for loop as
    void delay()
    {
    int i;
    for(i=0;i<30000;i++);
    }
    But if i give more than 30000 in loop ,the delay is more & i have to wait for five min or even more.
    But according to ur delay loop,it coming correctly by giving diff values of delay..
    Correct me if i m wrong sir??????????
    in ur pgm delay(2000) is there so loop will be executed as 2000 time 100(ie 2000*100=200000)
    So if i put directly as for(i=0;i<200000;i++),the delay is not coming sir..it taking so much of time..pls clarify

    August 5, 2015 at 2:38 pm
  • Ligo George

    There is a maximum limit for integer i.

    August 14, 2015 at 9:34 pm
  • Karan Bansal

    how can i make different patterns of led?

    September 15, 2015 at 10:31 pm
  • apurva patel

    hey if i want to swutch on relay from pressing push button and again it need to off when i press that push button again how it is possible i want to use more than 2 relay

    October 31, 2015 at 4:58 pm
  • Ligo George

    You can use the following logic.
    …….
    RELAY = OFF:
    …..
    if(SWITCH PRESSED)
    {
    RELAY = !RELAY; //TOGGLE or COMPLIMENT
    }
    ….

    November 21, 2015 at 1:55 pm
  • prathap gangadhar

    hello sir, i need a help, i need if i press the switch(switch1) LED should be ON, and if i press and hold the same switch(switch1), other LED should be ON, Please send me the code sir

    February 8, 2016 at 3:35 pm
  • Ligo George

    You can try something like this.

    if(SWITCH PRESSED)
    {
    while(SWITCH PRESSED)
    {
    CNT++;
    SMALL DELAY
    }
    if(CNT > SOME VALUE)
    LED = 0;
    else
    LED = 1;
    CNT = 0;
    }

    February 14, 2016 at 7:11 pm
  • S.RAJKUMAR

    Using Push Button Switch with 8051 and Keil C – AT89C51

    THIS IS WORKING LIKE I ASK
    PLS TELL FRIENDS

    PUSH TO ON PUSH TO OFF
    POWER CUT AND COME BACK
    OLD STATUS ITS COME
    ITS ON ITS ON
    ITS OFF ITS OFF

    April 11, 2016 at 3:26 pm
  • sandeep mishra

    hello sir ,
    i am beginner in embedded system . i want to make a hardware to on – off led by pressing a key at all pin of a port means 8 led by 1 switch and 8 led by 8 switch
    .can u guide me in c code

    April 14, 2016 at 12:35 pm
  • anand

    hello programmers ,my program is led on/off with same button in a port first four pins taken as i/p and next four pins taken as o/p.when i pressed 1st sw 1st led must be on.but in my code first four leds are blinking. please send sample code

    April 27, 2016 at 4:49 pm
  • anand

    same problem what you saidin previous.please send sample code.

    April 27, 2016 at 4:54 pm
  • Vinay Bakle

    can i use laser and photo transistor/dode in place of switch and a motor as output in place of LED. my project working laser falls on photo transistor, at that time motor remains off. if laser light is obstructed to fall on photo transistor motor will ON.

    November 15, 2016 at 3:50 pm
  • Ligo George

    Yes, you can do it like that.

    Kindly use our forums ( https://electrosome.com/forums/ ) for asking doubts outside teh scope of above article.

    November 27, 2016 at 2:37 pm
  • sandip patel

    using push button switch 8051.
    one time push switch relay is on and than i push same switch relay is off.
    how to write code,please help me.

    February 20, 2017 at 10:49 pm
  • ravi ranjan shah

    hello I want to use the same program for 5 leds so can u please help me!!!

    February 23, 2017 at 8:59 pm
  • jayaramulu T

    Hi,
    This is Jayaram. I need your help Sir.
    I a using Push button Switch and one led sir. when switch is pressed first time led should be on until the second time switch is pressed.
    when we press the switch for second time led should be blink.
    plz help me

    April 2, 2017 at 11:28 am
  • Kovinthan Jeyaraj

    Please let me know, if we use LDR to light the LED, what is the “c”code for 89C52 micro controller ..
    For
    1. Auto density control
    2. On/off

    May 31, 2017 at 4:07 pm
  • vikash

    hello sir
    i want to made a device which having​ delay control to push button.
    like when i want to led blink for 1 seconds .push 1 time ..when i pushed 5 times delay create 5 seconds

    June 9, 2017 at 6:58 pm
  • Vikash Kumar

    and delay create parmanent…when i again restart device delay cant be changed

    June 9, 2017 at 7:02 pm
  • lol

    how i want tu torn ON LED for 0.5 sec when switch is pressed

    August 15, 2017 at 2:16 pm
  • Chinmay Kolhatkar

    I wants to turn a led on when a switch is pressed for about one sec.

    October 15, 2017 at 4:03 pm

Leave a Reply

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