8051 push button with led – Main task
Microcontroller used in the project is 89c52. You can also use any other 8051 series microcontroller’s in your push button and led diy project like 89c51 or 89s51.
How led is interfaced with microcontroller?
- When microcontroller pin is made low(from code). Microcontroller pin becomes ground and current start flowing from 5 volt source to ground making led to switch on.
- When microcontroller pin is made high(in code). Microcontroller pin becomes high and current will find no way to flow and led switches off.
You can also change the polarity of the circuit by connecting led anode to microcontroller and cathode to external ground. In that configuration microcontroller will supply current to glow led. Which is not good. Their is a very big chance of microcontroller to reset due to power shortage. Hence the below led microcontroller configuration is recommended to utilize in circuits involving led and microcontroller. In below configuration external source will supply current to led.
How Push button is interfaced with microcontroller?
- When button is open(not pressed) ground will appear on microcontroller pin. Their is no path for 5 volt to circulate.
- When button is pressed current starts flowing through the circuit and 5 volts will appear on the microcontroller pin.
If the microcontroller pin is declared input then ground and 5 volts transitions at microcontroller pin by pressing and releasing the push button can be read. You can also change the polarity of the circuit(push button with ground and resistor to 5 volts) same like led circuit discussed above. The recommended circuit is below.
How push button and led works with 8051 microcontroller?
On every button press we want 8051 microcontroller to glow the led. Microcontroller will glow led by him self after processing the input information(button press). Since microcontroller switches led by its own so led is an output. We must declare led an output. I will discuss it later in code.
Push button and led with 89c51 microcontroller – Project code
Coming to the code i first included the necessary header file reg52.h. If you are using 89c52 microcontroller in your project and you are using keil as software tool to write, compile your microcontroller code and generating hex file of code then you have to include this header file in your project directory. It contains necessary configuration files of 8051 microcontroller. If you are using 89c51 or 89s51 the library is same with slightly change which is reg51.h.
- Port 1 pin 0 is declared as button, means i am using this bit(microcontroller pin) for my button. Declaring statement is sbit button=P1^0;
- Port 1 pin 1 is declared as led, means i am connecting my led to this pin of microcontroller. Declaring statement is sbit led=P1^1;
Now we can use these pins with their names in code. In the main function the statement P1=0x01; declares the button as input and led as output. 0x01 is a hexadecimal command. If we translate it to binary it becomes 00000001. This command is written to 8051 microcontroller Port-1. Which declares button as input and led as output. How come?
Initially writing ‘1′ to 8051 microcontroller pin declares it input and ‘0‘ declares it output. If we translate the above statement according to port 1 pins. It comes out
Led and button with 8051 microcontroller – Project circuit diagram
A continuous while loop in code is used to continuously run the logic declared in it. According to the login when ever button is high led glows else led is off. Belowis the circuit diagram of the project.
Filed Under: 8051 Microcontroller, Microcontroller Projects