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

  • This program is interms of microprocessor program or C language type…. It’s looks like C language….It is also same in MPMC( microprocessor and microcontroller)

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

  • 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

  • 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

  • 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

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

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

  • 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

  • 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

  • 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

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

  • 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

  • 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

  • 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

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

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


  • >