Using Push Button Switch with 8051 and Keil C – AT89C51

Push Button Switch

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

  • upputuri ramesh

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

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

  • pritesh

    led is not blink only on

  • kuttAPPAN

    tool chain is not installed,,,wot to do

  • sachin

    led is not blinking in ISIS proteus.

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

  • 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 ..

  • 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.

  • divya

    sir INPUT HIGH IMPEDANCE means ????

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

  • 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

  • There is a maximum limit for integer i.

  • Karan Bansal

    how can i make different patterns of led?

  • 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

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

  • 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

  • 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;
    }

  • 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

  • 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

  • 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

  • anand

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

  • 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.

  • Yes, you can do it like that.

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