In this tutorial, we will show you how to control an LED using a switch with the PIC16F877A microcontroller. The PIC16F877A is a microcontroller similar in performance and feature of the ATmega328P microcontroller used in popular Arduino board. This is a simple yet fundamental project in embedded systems and microcontroller programming, perfect for beginners looking to understand how digital inputs and outputs work.
Components Needed:
- PIC16F877A Microcontroller
- Push Button (Switch)
- LED
- 220Ω Resistor (for the LED)
- Pull-up Resistor (10kΩ) for the switch
- Breadboard and Jumper Wires
- Power Supply (5V)
Understanding the Code
Here is the code we’ll use to control the LED based on the input from a switch.
// Switch Example
#define _XTAL_FREQ 8000000 // Defining clock frequency for delay functions
#include <xc.h> // Include header for PIC Microcontroller
// BEGIN CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage Programming Disabled
#pragma config CPD = OFF // Data EEPROM Memory Code Protection Disabled
#pragma config WRT = OFF // Write protection disabled
#pragma config CP = OFF // Flash Program Memory Code Protection Disabled
//END CONFIG
void main(void) {
// Configure I/O pins
TRISB1 = 0; // Set RB1 as Output PIN for the LED
TRISB0 = 1; // Set RB0 as Input PIN for the switch
// Infinite loop
while (1) {
// Check if switch (connected to RB0) is pressed
if (RB0 == 0) {
RB1 = 1; // Turn ON the LED (connected to RB1)
} else {
RB1 = 0; // Turn OFF the LED
}
}
}
How It Works
This code monitors a switch connected to PORTB pin RB0 and controls an LED connected to PORTB pin RB1. Here’s a step-by-step breakdown of how the code operates:
1. Configuration Bits
The configuration bits are set at the beginning of the code to define important system parameters. These include the oscillator selection, watchdog timer, power-up timer, and brown-out reset options.
For instance, in this code:
- We use a High-Speed (HS) Oscillator.
- The Watchdog Timer (WDT) is enabled, which is important for resetting the microcontroller in case of a system crash.
- Low-Voltage Programming is disabled to free up the RB3 pin for regular digital I/O use.
2. Pin Setup
TRISB1 = 0
: This configures RB1 as an output pin, meaning the microcontroller will control the state (HIGH/LOW) of this pin to turn the LED on and off.TRISB0 = 1
: This configures RB0 as an input pin, meaning the microcontroller will monitor the state of this pin to detect the button press.
3. Switch Control Logic
In the while
loop, the program continuously checks the state of RB0. The button is connected to this pin with a pull-up resistor. When the button is not pressed, the pin reads a logic high (1). When the button is pressed, the pin reads a logic low (0).
if (RB0 == 0)
: If the button is pressed, the microcontroller sets RB1 to high (RB1 = 1
), turning on the LED.else
: If the button is not pressed, the microcontroller sets RB1 to low (RB1 = 0
), turning off the LED.
This way, pressing the switch toggles the LED.
Circuit Diagram
The PIC16F877A pin diagram is shown below.Here is the basic setup for the circuit:
LED Circuit:
- Connect the positive leg (anode) of the LED to RB1 (pin 34).
- Connect the negative leg (cathode) of the LED to a 220Ω resistor, and then to ground.
Switch Circuit:
- One leg of the switch is connected to RB0 (pin 33) with a 10kΩ pull-up resistor to 5V.
- The other leg of the switch is connected to ground.
Power:
- Provide a 5V regulated power supply to the PIC16F877A and ensure the ground is common across all components.
Conclusion
This project demonstrates how to use a switch to control an LED using a PIC16F877A microcontroller. The concept is simple but forms the foundation of digital input-output handling in microcontroller projects. You can expand on this by adding more inputs and outputs, or by implementing more advanced functionality such as controlling multiple LEDs, using interrupts, or debouncing the switch more effectively.
Related Topics
# Arduino Push Button LED On/Off Tutorial
# Push Button controlling LED - Programming Arduino using Matlab