Interfacing of switch and led using the 8051

Input and output devices are the important components of embedded system, we cannot imagine any embedded device without the input and output device, switch and led are the basic example of input and output device, so it is very important to understand the interfacing of the switch and led.

Here we learn how to control a led using an SPST (single pole single throw) switch. Led and the switch is the basic example of input and output device, before moving toward the interfacing of led and switch with 8051 micro-controllers,I want to introduce you guys to the led and switch because if you have no idea about the led and switch then you have faced a lot of problem in your project. You can know more about LED blinking program in C here.

LIGHT EMITTING DIODE (LED)

Led comes in various colour, its colour depends on its semiconductor material. Led have two leads one is the cathode and another one is the anode.We can easily identify the cathode and anode to see the length of leads, the length of cathode leads is lesser than the length of anode but sometimes they come in equal size.

When the length of both leads cathode and anode are equal in the size that time we can identify the anode and cathode to see their filament, the cathode has broader filament than the anode.

Led

Connection of Led

It is important to remember never connected the led directly with Vcc (output voltage which comes from directly 7805 ). If you connected the Led directly with the Vcc then maybe your led burnout.

So always connect the led using the resistance, if you need good brightness then you can select the value of resistance between 100 to 150-ohm either for medium brightness, you can select 300 ohms.

connection of led

Electrical Switch

The switch is a basic input device, use to control the operation of any output device using the microcontroller or control unit. It basically breaks the electrical circuit and interrupts the flow of current.

electrical switch

Connection of electrical Switch

In-circuit Pull-up and Pull-down resistor use to convert infinite or zero resistance into the digital signal, on the basis of pull-up and pull-down resistor, we can interface the switch in two-way, but most important point need to remember the value of pull-up and pull-down resistor depends on the microcontroller.

Positive Logic:  In this connection, we use a pull-down resistor connected to ground. When we pressed the switch then logic asserts high and when we disconnect the switch logic assert low.

positive logic

Negative Logic: In this connection, we use a pull-up resistor connected to Vcc. When we pressed the switch then logic asserts low and when we disconnect the switch logic assert high.

negative logic

Note: We faced the problem with the mechanical switch when we pressed the switch then it oscillates. It’s called bouncing of the switch, its varies according to the switch. We can resolve the bouncing problem with the help of hardware or software. In software, If we give the delay of a few milliseconds between the time, when we read the status of the switch then we resolved the switch bouncing problem.

Algorithm to control the led using the switch (SPST)

  • The microcontroller pin connected to the led makes the output.
  • The microcontroller pin connected to the switch makes the input.
  • Continuous monitor the status of the switch, if the switch is pressed then led pin status high either make it low.

Interfacing of led and switch with 8051 microcontrollers

In the below circuit, I have connected an SPST switch with P1.1 and a Led with P2.1. I am using the here negative logic circuit to connect the switch to the microcontroller.

Interfacing switch and led



Sample program to describe the interfacing of led and switch with 8051 microcontrollers

 

#include<reg51.h>

sbit Led  = P2^1;    //pin connected to toggle Led
sbit Switch =P1^1;  //Pin connected to toggle led


int main()
{
    Led  = 0; //configuring as output pin
    Switch = 1; //Configuring as input pin
    while(1) //Continuous monitor the status of the switch.
    {

        if(Switch == 0)
        {
            Led =1; //Led On
        }
        else
        {
            Led =0; //Led Off
        }
    }
    return 0;
}

 

 

Basic code example for switch debouncing in C

When we write a code to control the led light using the push button switch then we faced generally switch bouncing issues. In this example, I am describing a basic example to remove the switch bouncing issues from your led project circuit.

Steps to remove the switch bouncing issues

  • We have to monitor the status of pins connected to button switch.
  • If the status of pins changes then waits for a few milliseconds (wait time depends on the switch).
  • Again check the status of pins connected to push button switch if still it in the changes state then make the led pins high to blink the led light.
#include<reg51.h>

//Value of Delay
#define DEBOUNCE_VALUE  240

//Switch Status
#define SWITCH_PRESSED    1
#define SWITCH_BOUNCE     0

//LED STATUS
#define LED_ON           1
#define LED_OFF          0


// Connection
sbit Led = P2^1;    //pin connected to toggle Led
sbit Switch =P1^1;  //Pin connected to toggle led


//Function provides a delay to prevent from switch bouncing
void DebounceDelay(void)
{
    int iTimeDelay = 0;
    for(iTimeDelay=0; iTimeDelay < DEBOUNCE_VALUE; iTimeDelay++)
    {
    }

}


//Function to check the status of Switch
int CheckSwitchDebounce(void)
{
    int iRetValue =  SWITCH_BOUNCE;

    if(Switch == 0)
    {
        DebounceDelay(); //Wait time more then bouncing period

        if(Switch == 0)
        {
            iRetValue = SWITCH_PRESSED;
        }

    }

    return iRetValue ;
}



//Program start from here
int main(void)
{
    Led = 0; //configuring as output pin
    Switch = 1; //Configuring as input pin

    while(1)  //Super loop to continuously monitor the status of the switch
    {
        if(SWITCH_PRESSED == CheckSwitchDebounce()) //Check the switch status
        {
            Led = LED_ON;     //Led On
        }
        else
        {
            Led = LED_OFF;    //Led off
        }
    }
}

 

 

Your opinion matters

Here I have tried to discuss a lot of points regarding the switch and led but I would like to know your opinion on the connection of led and switch. So please don’t forget to write a comment in the comment box.

 




18 comments

  1. I am a beginner…but plan on learning more about embedded C programming… Any recommendations/advice… ?

    By the way quite helpful….thanks :).

Leave a Reply

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