You are on page 1of 1

Connecting push buttons to microcontroller.

There are two ways to connect push button to microcontroller.


1. Pull-UP
2. Pull-DOWN
When push-ON switch connected in pull-down method as in fi.2! microcontroller pin is connected
to round le"el trouh resistor #$oic %&'( before switch is pressed. When switch pressed!
microcontroller pin is connected to )cc #$oic %1'(. When switch is released loic le"el oes bac*
to $oic %&'.
Fig.2 Push button connected as Pull-Down

When push-ON switch connected in pull-up method as in fi +! microcontroller pin is connected to
)cc throuh resistor #loic %1'( before switch is pressed. When switch pressed! microcontroller pin
is connected to ,round #$oic %&'(. When switch is released loic le"el oes bac* to $oic %1'.
Switch De-bouncing.
Operation of push button switch is simple- it either ma*es connection or brea*s connection between
its two terminals. .ut wait! there is problem. When switch is pressed there is short period of time
where oscillation occurs. Oscillation is random and usually less than 1&m/ for eneral P0.
mounted push button switches *nown as micro switches. This problem occurs because mechanical
contacts of switchs are not perfect and nature of electricity. Different speed of pushin switch is also
affects this problem. This phenomenon is called switch de-bouncin.
1f we inore this de-bouncin when de"elopin proram for microcontroller! proram may beha"e
in une2pected way or! sinle button press may produce more than one output pulse. To o"ercome
this iissue we introduce small delay in our proram. 3or e2ample! when we detect switch press
#loic chane! say & to 1( in proram! we wait small time #usually about 1&m/( and aain chec*
status of switch! now if we encounter loic 1 aain this means loic status of switch is chaned
from & to 1! which is called switch pressed. 4bo"e fiures 2 and + shows timin diarams of
switch pressin. Note the time periods t1 to t2! t+ to t5 are bouncin. This is what you see if you
e2amine switch press in oscilloscope.
This type delay method of de-bouncin has some disad"antaes. 6ainly the delay time of 1&m/ or
so iis idlin time for 60U and for some applications this time is too much and unacceptable.
6uch better way to do debouchin is usin interrupts. 3or e2ample we use 1&m/ timer interrupt
which will branch our proram to switch scan routine where we will 7uic*ly scan status of all of our
switches and record them in array or "ariables. Then after another 1&u/ same thin will happen.
The idea here is simple. 3or e"ery 1&ms time! each push button state will be chec*ed and compared
to the pre"ious states recorded. This will unco"er risin and fallin edes of sinal. 1f there is no
chane compared to pre"ious state of one switch! it is ob"ious! that switch is not pressed #or not
chaned( its status. 1n case of chane from & to 1! risin ede detected and chane from 1 to &!
fallin ede detected. 1f ne2t + or 5 chec*s do not detect any chane in new loic state! we are
certain that switch is pressed #or released(.
3ollowin e2ample shows simple circuit which has two push button switches and when pressed
proram will turn on two $8Ds respecti"ely.

You might also like