Using Push Button Switch with PIC Microcontroller – CCS C

Push Button Switch

Using Push Button Switch with PIC Microcontroller – CCS C

I hope that you already go through our first tutorial of CCS C Compiler, Getting Started with PIC Microcontroller – CCS C Compiler. In that tutorial we learn how to use an output pin of PIC Microcontroller by blinking an LED with a delay of 1 second. In this tutorial we will learn how to read the status of an input pin and to make decisions according to its state. For the demonstration of its working we are connecting an LED and a Micro Switch to PIC 16F877A microcontroller. Pin connected to LED is configured as an output and pin connected to switch is configured as input. We will program in such a way that when the switch is pressed, LED will glow for 2 seconds.

Connecting Switch to a Microcontroller

PULL-UP and PULL-DOWN Resistors

PULL-UP and PULL-DOWN Resistors

Circuit Diagram

Using Push Button Switch - PIC Microcontroller

Using Push Button Switch – PIC Microcontroller

VDD and VSS of PIC Microcontroller is connected to 5V and GND respectively to provide necessary power for its operation. 8MHz crystal will provide clock for the operation of the microcontroller and 22pF capacitors will stabilize the oscillations produced by the crystal. Pin RD0 (PIN 19) is configured as an input pin to which switch is connected and Pin RB0 (PIN 33) is configured as an output pin to which LED is connected. A 10KΩ resistor is used along with switch to ensure that the pin RD0 is at Logic HIGH (VDD) state when the switch is not pressed. Whenever the switch is pressed pin RD0 becomes Logic LOW (VSS) state. A 470Ω resistor is used to limit the current through the LED.

CCS C Code

#include <main.h>

#use delay (clock=8000000)

void main()
  output_low(PIN_B0);     //LED OFF
  output_float(PIN_D0);   //Set RD0 as Input Pin
                          //OR set_tris_x(0b00000001)
    if(input_state(PIN_D0) == 0)
      output_high(PIN_B0); //LED ON
      delay_ms(2000);      //2 Second Delay
      output_low(PIN_B0);  //LED OFF

If you are not familiar with functions used above, please read our first tutorial Getting Started with PIC Microcontroller – CCS C Compiler.


You can download the entire project files here…
Using Push Button Switch

Buy Here

  • Sale
    Crystal OscillatorCrystal Oscillator

    Crystal Oscillator

    0 out of 5

    A Crystal Oscillator is an electronic oscillator which is commonly used to provide clock signals to Microcontrollers and Microprocessors. It creates electrical signals with a particular frequency having very high stability. It takes the advantage of vibrating crystal of piezoelectric material to generate precise frequency of oscillations.

    10.00 8.00
  • -31%
    PIC 16F877APIC 16F877A

    PIC 16F877A Microcontroller

    0 out of 5

    This powerful (200 nanosecond instruction execution) yet easy-to-program (only 35 single word instructions) CMOS FLASH-based 8-bit microcontroller packs Microchip's powerful PIC architecture into an 40- or 44-pin package and is upwards compatible with the PIC16C5X, PIC12CXXX and PIC16C7X devices. The PIC16F877A features 256 bytes of EEPROM data memory, self programming, an ICD, 2 Comparators, 8 channels of 10-bit Analog-to-Digital (A/D) converter, 2 capture/compare/PWM functions, the synchronous serial port can be configured as either 3-wire Serial Peripheral Interface (SPI™) or the 2-wire Inter-Integrated Circuit (I²C™) bus and a Universal Asynchronous Receiver Transmitter (USART). All of these features make it ideal for more advanced level A/D applications in automotive, industrial, appliances and consumer applications.

    200.00 139.00
  • -21%
    Out of stock
    USB PIC ProgrammerPICKit2 Clone

    USB PIC Programmer PICkit 2

    0 out of 5

    The PICkit 2 programmer is a development tool with a easy to use interface for programming and debugging Microchip’s flash families of micro-controllers. The windows programming interface supports all commonly available PIC microcontrollers. With Microchip’s powerful MPLAB Integrated Development Environment (IDE) the PICkit 2 enables in circuit debugging on most PIC microcontrollers.

    1,170.00 929.00

Share this post