How to Interface Switch with 8051

switch Interfacing with 8051 Primer

Tags: interfacing switch with 8051, 8051 switch interfacing,8051 switch programming,c program for switch interface using 8051,
Overall rating
8051 Primer Board

The 8051 Primer board is specifically designed to help students to master the required skills in the area of embedded systems. The kit is designed in such way that all the possible features of the microcontroller will be easily used by the students. The kit supports in system programming (ISP) which is done through serial port.

NXP’s 8051 (AT89V51RD2), 8051 Primer Kit is proposed to smooth the progress of developing and debugging of various designs encompassing of speed 8-bit Microcontrollers.


A switch is an electrical component that can break an electrical circuit, interrupting the current or diverting it from one conductor to another. A switch may be directly manipulated by a human as a control signal to a system, or to control power flow in a circuit.

Interfacing Switch

Fig. 1 shows how to interface the switch to microcontroller. A simple switch has an open state and closed state. However, a microcontroller needs to see a definite high or low voltage level at a digital input. A switch requires a pull-up or pull-down resistor to produce a definite high or low voltage when it is open or closed. A resistor placed between a digital input and the supply voltage is called a "pull-up" resistor because it normally pulls the pin's voltage up to the supply.



Fig. 1 Interfacing switch to Microcontroller

Interfacing Switch with 8051

We now want to control the LED by using switches in 8051 Primer Board. It works by turning ON a LED & then turning it OFF when switch is going to LOW or HIGH.

The 8051 Primer board has eight numbers of point LEDs, connected with I/O Port lines (P0.0 – P0.7) to make port pins high. Eight switches, connected with I/O port lines (P2.0 – P2.7) are used to control eight LEDs.

Pin Assignment with 8051


 Circuit Diagram to Interface Switch with 8051


Source Code

The Interfacing switch with 8051 program is very simple and straight forward, that controls led by using switches when it going LOW or HIGH.

C Program to switch functions using 8051


Title : Program to Blink LEDs controlling by switches






#include<reg51.h>                                                                  //Define 8051 Registers


#define SW  P2                                                             //Define Switch to Port2

#define Led  P0                                                            //Define Led to Port0



//   Main Function


void main()                                                 


            P2=0xff;                                                               //Port-2 as input to FFh

            P0=0x00;                                                            //Port-0 as output to 00h

                        while(1)                                                    //Loop Forever


                                    Led = ~SW;                     //Assign the Switch value to Led







Join the World's Largest Technical Community

we respect your privacy.