Push Button as Input and Led as Output with Pic Microcontroller

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 resistor is attached in series to a push button, other leg of push button is attached to +5v signal. 

Led with Pic Microcontroller

Led with Pic 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

Current Project / Post can also be found using:

  • microcontroller playing with push button inputs and outputs
  • pic push button assembly
  • sample led circuit with microcontroller

Leave a Comment

= 3 + 4

Read previous post:

The serial controlled 50 Segment Bar-Graph Display built using CAT4016 IC. Very useful projects for development Bar-Graph based, Temperature Meter,...

Scroll to top