Categories: Firmware Guide-PIC18

Push button interfacing with PIC Microcontroller

Push button switches

Push buttons are basic input device in an embedded system seen in very simple to highly complex systems. They are the basic mechanical on-off buttons which act as control devices. It short circuits the line when it is pressed and opens when it is not pressed. This chapter deals with the technicalities of push button interfacing with PIC microcontroller. Image of a push button switch is given below.

Before going to the details of interfacing pushbutton switches with the PIC, we shall discuss in brief about the types of switches. In general, switches are classified as mechanical switches and electrical/electronic switches. On the basis of holding the current through it, switches are classified as late switch and momentary switch. Mechanical switches are classified into five basically. They are spst, spdt, dpst, dpdt and 2p6t switches. Push button switches are coming under the category of momentary operation switches along with toggle switches. If in a push button switch, the input is given by pressing the push button inward, in a toggle switch, the input is given by toggling a lever from one position to another.

Electronic devices such as transistors, MOSFETs, and relays can be acted as switches and they fall under the category of electrical/electronic switches.

Push button interfacing with PIC microcontroller

Basic interfacing circuit of a push button switch is given below.

Push button interfacing – Microcontroller sourcing the current

Here, the switch is directly connected to the microcontroller through a resistor to limit the sourcing current. Sourcing current is the current which passes to the microcontroller when the switch is closed (ON state). There is maximum sourcing current defined for different microcontrollers. Use the datasheet to find it out. The current passing through this circuit should be less than the maximum current.

Here we’ve taken an example sinking current of 10 mA.
I = 5/10mA = 500 ohms

Firmware

Here, the circuit is connected to RB0 pin of the microcontroller. We need to make this pin in input mode. The firmware is given below.

TRISBbits.TRISB0 = 1;               // Set RB0 as input

Now keep reading the port for a HIGH. Whenever there is HIGH, that means the switch is pressed.

do{
    status = PORTBbits.RB0;         // Read the pin
    delay_ms(10);                   // Introduce a delay between each read
}while(status);                     // Keep reading till a HIGH

// Switch Pressed, Do something for showing off

Switch Debouncing

The spikes get generated while a metal comes in touch with another. These spikes can be misread as different switch presses while we’ve pressed only once. In order to avoid this type of error, we need a filtering mechanism. Most common idea is to introduce a small delay and scan again for the switch status. If it is HIGH, then we can safely assume the switch is pressed.

delay_ms(100);                      // a 100ms delay
status = PORTBbits.RB0;             // Read the port pin
if (status)
{
    // Switch Pressed, Do something for showing off
}

To filter bounces, we can also use a hardware filter circuit which filters high-frequency signals, i.e., a low pass filter. Since we are are using a microcontroller, it is better to use a software filter as we said in above section.

Advanced circuits

In our previous design, the microcontroller was sinking the current which is not an efficient design. Instead, we can use a pull-up or pull-down resistor to avoid this condition.

Pull-up mode

For the pull-up mode, the switch will be in active low configuration. That is, the state of the pin when the push button not pressed is HIGH, we need to check for a LOW to check the switch press event.

Push button interfacing in pull-up mode

Below is the sample firmware for checking the keypress event. This part also includes debouncing.

do{
    status = PORTBbits.RB0;         // Read the pin
    delay_ms(10);                   // Introduce a delay between each read
}while(!status);                    // keep reading till a LOW
delay_ms(100);                      // Switchpress detected  - debouncing delay
status = PORTBbits.RB0;             // read again
if (!status)                        // check the pin status
{
    // Switch Pressed, Do something for showing off
}

Pull-down mode

In pull-down mode, the current flows to the current when the switch is pressed. In open state, the pin is connected to the ground via a resistor. In this mode, the output acts as an active high.

Push button interfacing in pull-down mode

Below is the sample firmware for scanning the push button in pull-down mode. It also includes the debouncing part.

do{
    status = PORTBbits.RB0;         // Read the pin
    delay_ms(10);                   // Introduce a delay between each read
}while(status);                    // keep reading till a HIGH
delay_ms(100);                      // Switchpress detected  - debouncing delay
status = PORTBbits.RB0;             // read again
if (status)                        // check the pin status
{
    // Switch Pressed, Do something for showing off
}

Key debouncing – Hardware filters

We can filter the bounces using filter circuits. The most basic circuit is given below, in this debounce filter is connected in pull-down mode. The capacitor filters spikes and smoothens mechanical switch bounce.

Push button interfacing in pull-down with debounce circuit

The circuit is not a perfect replacement to the software debouncing because it provides filtering only in open to closed transition. It will still bounce during closed to open transition because the capacitor shows a short across it. Using the debouncing circuit with software will be a better option.

 

Share