Push button interfacing with PIC Microcontroller

///Push button interfacing with PIC Microcontroller
  • Push button interfacing with PIC Microcontroller

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.

Push button switch

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 with PIC - sourcing microcontroller

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.

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

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.

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 - Pull-up mode

Push button interfacing in pull-up mode

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

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 - pull-down mode

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.

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 - debounce circuit

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.

 

DON’T MISS OUT UPDATES!
Be the first to get latest updates and exclusive content straight to your email inbox.
Stay Updated
Subscribe, it is quick.
We hate spam too. Give it a try, you can unsubscribe anytime.
Spread the love, share this

Leave A Comment

DON’T MISS OUT UPDATES!
Be the first to get latest updates and exclusive content straight to your email inbox.
Stay Updated
Subscribe, it is quick.
We hate spam too. Give it a try, you can unsubscribe anytime.
close-link
25% OFF - OpenLabPro Course
Use Coupon: CRS25UL

 
PIC Microcontroller Primer
Want to learn indepth programming, peripheral interfacing and driver development?
close-link
Launched: IoT online Course - 25% Off when you complete your order today.
Checkout Now
close-image

Subscribe To our Email Newsletter

Be the first to get latest updates and exclusive content straight to your email inbox.
Send me Tutorials and Guides
close-link