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 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 may be 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 ohm.

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 assert 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 assert 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 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 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

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 few millisecond (wait time depend 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.

 

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.