Now that you’re familiar with the working of the ports of 8051 let’s put the knowledge into practice by interfacing a switch with an 8051 microcontroller. Our main aim in this tutorial to control an LED using a push-button switch interfaced to an 8051.
A push-button is a type of momentary switch which when pressed completes a circuit, and when released, the button returns to its original position and breaks or interrupts a circuit. Let’s start by getting ourselves familiar with all the components that are required.
Contents
Components Required
- 8051 microcontroller – AT89C51/AT89S51/52 or similar variants.
- Oscillator crystal – 12Mhz
- Capacitors – 22pF x 2, 10µF x 1
- Resistors – 10kΩ x 2, 500Ω x 1
- Pushbutton switch
- LED x 1
Connection of an electrical switch
The switch is an input device. When interfaced with microcontroller, it can be used to control other peripherals connected to the microcontroller. It basically “makes” the electrical circuit by establishing the flow of current. Or “breaks” it by interrupting the flow of current.
A switch, when not in use, has no definite value associated with it. Correspondingly, the pin of the uC that it is attached to has no definitive value either. It is said to be floating between 0 or 1. Hence a resistor is connected between the switch and VCC or GND. To assign a specific value to it once and for all.
Based on the position where the resistor is placed, it either acts as a Pull-up or a Pull-down resistor, i.e., it either provides zero or a finite resistance. Also, the value of this resistance is calculated, keeping in mind that any switch can handle currents in milli-amperes. For a supply voltage of 5V connected to the 8051 microcontrollers, the current drawn when a resistor of 10k is used is 5/10k = 5mA, which is perfect.
Positive Logic
When the resistor is connected to the ground, it is called a pull-down resistor.
- When the switch is OFF (not pressed), the input to pin P2.0 is a LOW pulse (0).
- When the switch is ON (pressed), the input to pin P2.0 is a HIGH pulse (1).
This particular logic is called a positive logic.
Negative Logic
When the resistor is connected to the VCC, it is called a pull-up resistor.
- When the switch is OFF (not pressed), the input to pin P2.0 is a HIGH pulse (1).
- When the switch is ON (pressed), the input to pin P2.0 is a LOW pulse (0).
This particular logic is called a positive logic, and we’re going to interface the switch using a negative logic with a 10k ohm pull-up resistor.
Now how does a switch work when connected to the ports of 8051?
By default, the output of ports of 8051 is a HIGH pulse. If we consider port 0, we can say that by default, P0 = 0xFF is set. When the switch is in open state pin P0.1 (to which the switch is connected) is directly connected to the +5V power supply through a pull-up resistor, but as soon as the switch is turned ON (pressed), the circuit is complete, and the pin P0.1 is directly connected to the ground, and its input is LOW now. The port 0 of the 8051 microcontrollers does not consist of external pullup resistors. Hence we are using a 10k resistor.
Therefore, we program the microcontroller in such a way that, when the pin P0.1 is used as an input,
- HIGH ( = 1), then the LED should remain OFF.
- LOW ( = 0), then the LED should turn ON.
So, to configure ports of 8051 as an input port, P0 = 0xFF; command should be used or just make the pin you’re using as an input pin by using sbit switch_pin = P0^1
(define a variable) and switch_pin = 1
command.
Circuit diagram to interface a switch with 8051
- Step 1: If you’re using Proteus or and other simulation software or even hardware, select the AT89C51 or AT89S51 microcontroller or any other compatible variant.
- Step 2: Connect a 12 MHz oscillator between pin 18 and 19.
- Step 3: Connect two capacitors of 22pF, with one terminal on either side of the oscillator and the other terminal to ground, as shown below.
- Step 4: Set Pin 31, i.e., EA pin to HIGH by connecting it to the +5V DC source.
- Step 5: Now, to make the RESET circuit, connect Pin 9 (RST) to +5V through a capacitor of 10µF and connect the same pin to +0V (GND) through a 10kΩ resistor or a potentiometer.
- Step 6: Connect the pin P2.0 to the cathode of the LED, and the anode of LED to +5V power supply through a resistor of 500 ohms, such that it turns on when a LOW pulse is provided to its cathode.
- Step 7: Connect one end of the push button to ground, and the other end is connected to two points, one to pin P0.1 and the other to +5V supply through a pull-up resistor of 10k ohms.
That’s all you need to do to control an LED connected at one port using a pushbutton connected at another port.
C Code to interface a switch with 8051 microcontrollers
We start by including the predefined register of the 8051 microcontroller. Next, we define variables for the LED as well as the switch to later use it in our main function.
#include<reg51.h> sbit LED_pin = P2^0; // LED is connected to pin P2.0 sbit switch_pin = P0^1; // switch is connected to pin P0.1 void main (void) // Null return type { switch_pin = 1; // To make switch_pin an input pin LED_pin = 1; // Initialize LED in OFF state while (1) // Run forever until power is cut off { if(switch_pin == 0 ) // If switch pressed once (pressed and released) { LED_pin = 0; // Turn ON the LED } else // If switch is not pressed { LED_pin = 1; // Keep the LED in OFF state } } }
Switch Debouncing
Whenever a physical switch is interfaced with a digital circuit, there is always is a requirement of debouncing the signal. What is switch debouncing? When a switch button is pressed, there is always a chance that it does not exactly make contact in one swift motion. There might always be a slight ‘bounce’. This phenomenon occurs again when the switch is released. This could cause multiple switch “presses” and can cause some errors in your project.
Is there a way to remove this? Yes, definitely. We can either create a physical circuit with capacitors to remove switch debouncing. OR we can remove it in the program. Let’s design a switch debouncing program because it’s easier and doesn’t require additional circuitry. The steps you need to follow are:
- Step 1: Monitor the status of the pins connected to the pushbutton switch.
- Step 2: When there is a change in the status of the pin, i.e. from HIGH to LOW or LOW to HIGH, then create a delay of few milliseconds.
- Step 3: Check the status again, and if the pin hasn’t yet changed its state, then the input to pin if LOW, which implies the LED glows.
C Code to interface a switch using Switch Debouncing method
#include<reg51.h> //Value of Delay #define DEBOUNCE_VALUE 240 // Depends on the push button switch //Switch Status #define SWITCH_PRESSED 1 #define SWITCH_BOUNCE 0 // Connections sbit LED_pin = P2^0; // LED is connected to pin P2.0 sbit switch_pin =P0^1; // switch is connected to pin P0.1 // Function declarations int Switch_Debounce(void); void DebounceDelay(void); // Main program int main(void) { LED_pin = 0; //configuring as output pin switch_pin = 1; //Configuring as input pin while(1) // Run this loop forever until power is cutoff { if(SWITCH_PRESSED == Switch_Debounce()) //Check the switch status { LED_pin = 1; // Turn ON the LED } else { LED_pin = 0; // Turn OFF the LED } } } //Function to check the status of Switch int Switch_Debounce(void) { int status = SWITCH_BOUNCE; if(Switch == 0) // If the switch is pressed { DebounceDelay(); //Wait time more then bouncing period if(Switch == 0) // Check the status of switch again { status = SWITCH_PRESSED; } } return status ; } //Function provides a delay to prevent the bouncing phenomenon void DebounceDelay(void) { int i = 0; for(i=0; i < DEBOUNCE_VALUE; i++) }
We hope that you were able to complete this 8051 project in a breeze. Check out our free 8051 course or the sidebar for more 8051 related projects. If you have any queries regarding this project, let us know in the comments below.