Engineers Garage

  • Projects and Tutorials
    • Circuit Design
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Components
  • Contributions
  • Articles
    • EG Blogs
    • Insight
    • Invention Stories
    • How to
  • What Is
  • Forums
    • EG Forum Archive
    • EDABoard.com
    • Electro-Tech-Online
  • 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
  • News
    • EE Design News
    • DIY Reviews
    • Guest Post
    • Sponsored Content
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Classrooms
    • Grid Infrastructure
    • Aerospace & Defense
    • Building Automation
    • Power Delivery
    • Factory Automation
    • Motor Drives
    • Medical Technology
  • Video

Push Button as Input and Led as Output with Pic Microcontroller

July 4, 2019 By EG Projects

This is a simple tutorial/project on how to interface a transistor (2n2222), led and push button with Pic microcontroller. In the project i am going to switch on and off(blink) an led with the help of a push button. Since transistors are used at outputs we can drive heavy loads with the same circuit. Pic16f877 microcontroller is used in the project. Port-B of Pic16f877 is used as output port. Port-B of pic16f877 is an 8-bit port. 8 transistors are connected to the general purpose input/output pins (gpio’s) of this port. Transistors bases are connected directly to the pins of port-b. Led’s are connected to emitter side of transistor in series to a 560 ohm resistor. Collector side of each transistor is supplied an external 5 volt signal.
An external 20 Mhz crystal is used to supply clock to microcontroller. Crystal is attached to microcontroller in parallel to two 33 pf capacitors. Push button is attached to Port-D Pin#7. 1 K ohm resistor is attached in series to a push button, other leg of push button is attached to +5v signal. 
Push Button and Led with PIC16f877a microcontroller

Push Button and Led with PIC16f877a microcontroller

Code is written using MPLAB-IDE and High Tech C compiler is used to compile code and generate Intel hex file of the code. First the necessary header file htc.h is included in the code. This header file is necessary to be included in every code that is written in MPLAB-IDE and uses High Tech C compiler to compile the code. If you didn’t include this header file in your code the htc(high tech c compiler) compiler will be unable to recognize the code. This header file tells the htc compiler that this is his code and he has to compile it. 

_XTAL_FREQ 20e6 statement is specifying the crystal frequency that is attached to the microcontroller. I am Using 20 Mhz external crystal with the controller so i specified 20e6(Equivalent to 20e6=20,000,000).

Statement #define Input RD7 is defining Port-D Pin#7 as Input. Now we can access Port-D Pin#7 with the name ‘Input’. TRISB=0x00; Statement is declaring Port-B as Output. TRISD=0x80; Statement is declaring Port-D Pin#7 as Input. (0x80=1000 00000

Pic Microcontroller Port as Input/Output

Declaring Pic Microcontroller Port as Input/Output

Switching on and off(blink) an led logic is placed in while(1) loop. When ever we press the push button, Current passes through the button towards Port-D Pin#7 and microcontroller reads it as logic-1. As a result Port-B outputs 0 and transistors base receives no current due to which their is no current flow between collector and emitter and led becomes off. Now when ever button is released no current flows through the circuit and Port-D Pin#7 reads it as logic-0, this outputs 1 at Port-B and base of transistors receives some current. Since base receives some current so the channel is on now. Current start flowing through collector to emitter and led becomes on. 

Push button to switch off led and release button to switch on led. You can also reverse the logic. Just change the statement if(Input==0) to if(Input==1). This will on led when Push button is pressed and off led when released. Pushing button and releasing it continuously will cause your led seem blinking.

Driving heavy Loads(using Transistor) with the same configuration given above

You can also drive heavy loads with the same circuit, just remove led and attach a heavy load like Toy dc motor. Use a transistor which can drive heavy loads. 2n2222 supports 40 volt and 800 mA between collector and emitter. You can drive a load which consumes 40 volt and 800 mA using the circuit given above. 
​
Some tutorials on driving heavy loads with microcontrollers are below.

How to drive heavy loads with microcontrollers?

Nodemcu heavy loads WiFi driven

Arduino uno driving heavy loads

Stm32 driving heavy loads

Download the Project Code, Folder contains the Hex file as well all the project files. Project is made in MPLAB. You need MPLAB software installed own your PC(Personal Computer) to open the project.
Led Push button with Pic(Download)

Related Articles Read More >

Microcontroller Project: STM32 low power modes analysis
Decimal counter designed in vhdl
Display custom urdu language characters on 16×2 lcd using pic16f877a microcontroller
Arduino home security system using Sim900 Gsm module, Pir motion detector and magnetic door contact switch

Stay Up To Date

Newsletter Signup

Popular Posts

Basic Electronics 21 – How to select an inductor
Serial data received from pc and displayed on 16×2 lcd using 8051(89c51,89c52) microcontroller UART port
NxN(8×1 , 8×2 , 10×2 , 16×1 , 16×2 , 16×4 , 20×2 , 20×4 ,24×2 , 30×2 , 32×2 , 40×2) Character lcd working, Pinout and description
Displaying ASCII Characters on 16×2 lcd using 8051(89c51,89c52) Microcontroller

EE Training Center Classrooms

“ee

“ee

Recent Articles

  • RPi Python Programming 05 – Introduction to Python
  • Renesas’s RL78 prototyping board simplifies IoT endpoint equipment
  • Google co-founders step down from parent company
  • RPi Python programming 04 – Setting up Raspberry Pi Linux computer
  • NXP broadens the LPC5500 Arm Cortex -M33 microcontroller series
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 © 2019 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
  • Contributions
  • Articles
    • EG Blogs
    • Insight
    • Invention Stories
    • How to
  • What Is
  • Forums
    • EG Forum Archive
    • EDABoard.com
    • Electro-Tech-Online
  • 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
  • News
    • EE Design News
    • DIY Reviews
    • Guest Post
    • Sponsored Content
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Classrooms
    • Grid Infrastructure
    • Aerospace & Defense
    • Building Automation
    • Power Delivery
    • Factory Automation
    • Motor Drives
    • Medical Technology
  • Video