Engineers Garage

  • Projects and Tutorials
    • Circuit Design
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • EE Design News
    • DIY Reviews
    • Guest Post
    • Sponsored Content
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • Video
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

Push Button and Led interfaced with 8051 Microcontroller: Keil uvision

April 24, 2019 By EG Projects

Here is a simple tutorial on how to interface a push button with 8051 microcontroller. In this tutorial i will teach you how to switch led on/off with push button press using 89c51 microcontroller. Push button with led is a beginners tutorial, who are getting started with 8051(89c51,89c52) microcontroller and keil uvision ide. Keil uvision ide is software tool by keil used to program variety of microcontrollers. In this tutorial we are using it for writing and compiling code for our 89c51 microcontroller.  

8051 push button with led – Main task

When push button is pressed by user the led connected with 89c51 microcontroller will glow. When the button is released led switches off. 
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?

One end(cathode or anode) of led is connected to microcontroller pin and the other with power rail. In the below picture led cathode is connected to microcontroller directly and anode is connected to +5 volts source in series with a resistor. Resistor is used to limit the current. How the below circuit works.

  • 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.   

Led with microcontroller

Led with microcontroller

How Push button is interfaced with microcontroller?

Same like led, push button is interfaced with microcontroller. One leg of button is connected to microcontroller and other to power source. Now

  • 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.

Push button interfaced with microcontroller

Push button interfaced with microcontroller

How push button and led works with 8051 microcontroller?

Since we want 8051 microcontroller to respond to button press, so push button is input to 89c51 microcontroller. We are physically giving(inputing) some thing to microcontroller externally hence push button is giving an input to 8051 microcontroller. So push button must be declared as input. I will discuss it in project code.  

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

when 8051 microcontroller initially boots or powered up its ports are default initialized as output. So when we need any input we have to initialize a particular port or pin of 8051 microcontroller  as input. I our case our led is output and button is input so we must declare their corresponding interface pins on 8051 microcontroller as input in 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.  

  1.  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;
  2.  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 

8051 microcontroller port1 pins declaration

8051 microcontroller port1 pins declaration

Led and button with 8051 microcontroller – Project circuit diagram

In the main function my first statement is P1=0x01. This statement is actually a hexa-decimal value that is assigned to port 1.Since hexa-decimal number in binary is four bit number(like F=1111,0=0000,1=0001). So the statement 0x01 first two digits 0x means that the number is in hexa-decimal form and the last two digits 01 is the number which is in hexa-decimal form. This number in binary is equivalent to 01=00000001 so this 8-bit binary number is assigned to port 1 bits. Which makes pin 0 of port 1 as input pin and rest of the 7 pins are acting as output. Our button is connected to port 1 pin 0 which is declared as input by the statement P1=0x01 so now we can use port 1 pin 0 as input from the button.

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. Below is the circuit diagram of the project.

Push button and led with 89c51 microcontroller

Push button and led with 89c51 microcontroller

Picture

You can also connect more than one button and led with the 8051 microcontroller and make a special blinking pattern of them. Other projects involving leds and microcontroller are below.

Blinking leds pattern with 8051 microcontroller

Fading led with 89c51 microcontroller

How to blink an led with 8051 microcontroller?

  Button and led code

Related Articles Read More >

Mathematical equations executed by an MCU
Clap sensor using STM32 MCU
Electro nonmechanical relays characteristics and how-to DIY
How to build an object-following robot using Arduino

Featured Tutorials

  • Remote Controlling Multiple Loads Using RF Module (Part 2/23)
  • Increasing Range of RF Module by Increasing Transmission Power (Part 3/23)
  • Increasing range of RF module by using antenna (Part 4/23)
  • Increasing the Range of RF Module by Using Antenna and Increasing Transmission Power (Part 5/23)
  • Controlling Multiple RF Receivers by Universal RF Transmitter (Part 6/23)
  • Multiple RF Receivers Controlled by a Universal RF transmitter based on Address Pin Configuration (Part 7/23)

Stay Up To Date

Newsletter Signup

EE Training Center Classrooms

“ee

“ee

“ee

“ee

“ee

Recent Articles

  • IAR Systems announces IEC and ISO certifications of its RISC-V development tools
  • Mathematical equations executed by an MCU
  • Infineon’s new Wi-Fi 6 brings reliable connectivity to smart homes
  • Toshiba releases compact LDO regulators for stabilizing DC power lines
  • Clap sensor using STM32 MCU
...

RSS EDABOARD.com Discussions

  • How can I explan this simple PWM creator Circuit
  • Can I use a Virtual clock instead of using a create_clock?
  • MCLR as digital input is resetting the device
  • Symmetrical Fully Differential Amplifier Layout
  • What is the DC voltage rating of this Y2 capacitor?

RSS Electro-Tech-Online.com Discussions

  • NTC flakey
  • Diode that only allows continuity at 13.5v?
  • Can two N-Channel MOSFETs be connected in series to perform a "AND" function?
  • Cycle Indicator Lights
  • #MEASURING OUTPUT VOLTAGE# Ideal laboratory power supply - YOURS requirements
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2021 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Circuit Design
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • EE Design News
    • DIY Reviews
    • Guest Post
    • Sponsored Content
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • Video
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering