In this tutorial, we will see how we could interface a simple switch to micrcontroller. Yes, it is indeed very
simple, later in this tutorial we will do some logical operations with LEDs and Switches.

Switch Basics

If we directly connect a switch to one of the microcontroller port pins, the pin floats when the switch is open.
Meaning, it is not at a fixed logic level 0v or 5v. Hence we pull the pin up through a resistor to 5v(LOGIC 1),
as shown in the figure 1. Now, whenever the switch is pressed, it grounds(LOGIC 0) the port pin.
Now we could go ahead and interface it to the microcontroller, but we would rather interface 8 switches and 8
LED's to two ports as shown in the schematic below.


read the switch status and display on LEDs.  A DIP (dual in line package) Switch. reset and power connections for the micrcontroller. array of 8 switches is connected to PORT3 AND 8 LEDs to PORT2. configure PORT3 (switches) as inputs and PORT2 (LEDs) as outputs. The code for the project is listed below: 2 .Figure 2: Schematic Diagram As we can see the schematic has basic circuits for oscillator. You could also use 8 discrete resistors as well. it is array of 8 resistors in a single pack(SIP). It is as good as connecting 8 pullup resistors as shown in figure 1.  Observe the RR1 component. Read Switches and Display Status on LEDs This a very simple one. Experiments 1.

0 . Read Switches and Display Status on LEDs #include<reg51.h> #define SWITCHES P3 #define LEDS P2 void main() { unsigned char switchInput.5 Group B P3.4 P3.7 P3. /* Get the SWITCH combination */ LEDS= switchInput. 1. The LEDS and SWITCHES as defined and assigned to P2 and P3 respectively. /*Configure SWITCHES as Input */ while(1) { switchInput = SWITCHES.6 Operation 3 P3.1 P3. we will divide switches into three groups as shown in table 1.7 and P3.2 Table 2: P3.7 Group A P3.3 P3.6 are Operators.6 P3.  SWITCHES(P3 is made input)  The switches are read and status is displayed on LEDS continously. SWITCHES = 0xff. /* Dispaly the SWITCH I/P on LEDS */ } } 2. Switches connected to P3. Table:1 Operation P3.they are selected as shown in table 2 to perform bit-wise logical operations on Group A and Group B. Logical Operations with LEDs and Switches For this experiment.

result. case 0x01: result = groupA | groupB. /*extract the groupB I/P(D2- D0) */ switch(operation) /*perform operations as per table*/ { case 0x00: result = groupA & groupB.D7) */ groupA = ((SWITCHES >> 3) & 0x07). /*Configure SWITCHES as Input */ while(1) { operation = ((SWITCHES >> 6) & 0x03). /* Perform AND operation*/ break. /*extract the operation(D6.h> #define SWITCHES P3 #define LEDS P2 void main() { unsigned char operation.groupB. SWITCHES = 0xff.groupA.0 0 AND 0 1 OR 1 0 XOR 1 1 INVERT(group A) The code for the second experiment is listed below: #include<reg51. /* Perform OR operation*/ 4 . /*extract the groupA I/P(D5- D3) */ groupB = (SWITCHES & 0x07).

break. case 0x02: result = groupA ^ groupB. /* Perform XOR operation*/ break.zip 5 . default: result = 0x00. } LEDS= result. /* Finally dispaly the result on the LEDS */ } } Switches_and_LEDs. break. case 0x03: result = (~groupA) & 0x07. /*Invert value of groupA*/ break.