This article explains effective method in order to sense the button presses in battery powered project using microcontroller. For the sake of discussion we use PIC 18F46J50 microcontroller from microchip; however this technique can be used for most of the other microcontrollers with hardware requirements given below. For this technique to work with microcontroller, it should have at least one pin with edge interrupt detect and analogue to digital functionality. Firstly let’s discuss why this technique is useful for battery powered projects. In most of the battery powered projects, conserving the energy is of paramount importance. In this regard we may have to employ radical hardware or software techniques to make the project as efficient as possible so it will run long time using batteries. Let’s see how we would normally connect 4 push buttons to microcontroller. Obviously we would use 4 digital inputs of the microcontroller to sense voltage going low or high when button is pressed. This is shown in two figures below.
The above are two most common and simplest ways of connecting four push buttons to microcontroller. However if you are concerned about energy and resource efficient design they are not the best. Both of the circuit require microcontrollers to have four digital inputs enabled, which draws small but significant current from battery powered projects. In case if we can reduce the number of input microcontroller uses it would reduce the power use by the circuit comparatively. Let us look at technique where we can reduce the number of digital inputs to one. Consider the following circuit with still four push buttons but with one digital input. For the time being the resistor values will look like random but I will explain why did we choose these values as we go on.
Here we got only four push buttons with one input. So how does the microcontroller know which push buttons is pressed? Now we employ a radical software technique to full fill this. First of all we can connect the single input to edge detecting interrupt pin of the micro. By doing this we can keep the micro sleep all the time but allow it to wake up if any of the push button is pressed since every push will pull high the voltage at the input. In case of PIC18F46J50 we can connect this input to pin 8 or RB0/INT0 where we can detect low to high change and fire interrupt to wake the pic from sleep. As discussed earlier RB0 pin also has analogue to digital conversion option hence choosing this pin fulfils the hardware requirements discussed earlier.
Ok now PIC knows a button has been pressed how will it know which button was pressed? The resistors of the one input circuit is chosen so by pressing each buttons gives a distinctive voltage input. We sense this voltage input to determine which button has been pressed. In PIC 18F46J50 we can see pin 8 being an interrupt also has the capability to measure analogue voltages. We use ADC capability to measure input to know which button has been pressed. So to summarised above technique I give below steps.
- Set the particular input(hardware dependent so you must refer the datasheet)to a interrupt and wait in sleep mode until a button is pressed
- A button is pressed and interrupt occurs and wakes up the pic
- As soon as pic wakes up from sleep it disables the interrupt and turn the interrupt pin to ADC input
- It then measures the voltage at the input to determine which button is pressed using ADC
- Once the pic knows which push button is pressed it can do task assigned for that button
- Go to step 1 again