Sunday 17 August 2014

Reading key switches by eliminating bouncing problems

Here is a tutorial for beginners to know about key interfaces and bouncing problems.....

The image below shows the key switch that I use. They are called as mini push buttons / mini tactile switches. These are not like our home switches that are used to turn On or OFF electrical appliances. These tactile switches gets shorted while pressed and becomes open while released.
You may wonder why there are four pins (Since only two is enough – one for input and one for output). The pin are interconnected as given in the picture,
 
You can find out yourself the interconnected pins by using the Short Circuit test in a Multimeter.

A simple circuit using switch:



The LED will glow as long as the switch is pressed. As soon as you release the switch, the LED turns OFF.
 

Key interface to microcontroller:

 

In the above circuit, if the switch is in open state, then you will receive HIGH / Logic ‘1’ at the microcontroller input. Hence the default input to the microcontroller from the switch would be logic ‘1’. If the switch is pressed, then it gets shorted to the ground and the entirre current flows to the ground. Hence you will receive a logic ‘0’ at the microcontroller input. Thus microcontroller will read either logic ‘1’ or ‘0’ according to switch state.
Here is a simple key interface with GR-sakura (Arduino Compatible) to turn ON anf OFF the onboard LED. The controller will read ‘1’ at its input as default. If it reads a ‘0’ (occurs once the switch is pressed), then the state of the LED is changed.

Code:



#include<rxduino.h>
int main()
{
                int state=0;
                pinMode(7,INPUT);
                pinMode(PIN_LED0,OUTPUT);
                while(1)
                {
                                if(digitalRead(7)==0)
                                {
                                                state=!state;
                                                digitalWrite(PIN_LED0,i);
                                }
                }
}
                But the LED will turn On and Off rapidly because of a problem called “Bouncing”.

Bouncing Problems :

 
One problem that we face while interfacing key switches with microcontrollers is the switch bouncing i.e., the output will turn on and off repeatedly, for a short period of time (in milli seconds).
Example – if you press ‘1’, you may read ‘111111’ rather than ‘1’.

In circuit 1, once the switch is pressed the LED will turn ON and OFF (bounce) for a few milli seconds and then be turned ON until the switch is released. This switch bouncing also happens in our electrical switches in home. Since the bouncing lasts only for certain milli seconds, they are not noticed by human eyes. But with microcontrollers, the situation is different. It will read every bounced state (ON and OFF) as valid input. That’s why you will read as ‘1111111’ if you press 1. The bouncing time varies from one switch type to another.

This bouncing can be taken care in software by introducing a certain delay in the code. Each switch bouncing takes place for around 100 ms (approximation). Hence, once the switch is pressed, if you read that switch again after that 100 ms bouncing time, the problem can be solved.

The normal code for reading switch is,

While(1)
{
                if(digitalRead(7)==0)
                          {
                                                state=!state;
                                                digitalWrite(PIN_LED0,i);
                       }
}

Type 1:

The above code can be changed for debouncing as

While(1)
{
              if(digitalRead(7)==0)   // checks whether switch pressed
                                {
                                        delay(100);  // wait for 100 milli second
                                         if(digitalRead(7)==0)  // agains checks whether switch pressed
                                         {
                                                             state=!state;
                                                            digitalWrite(PIN_LED0,i);
                                        }
                            }
}

The feature of above code is that if you long press the switch, the LED will turn ON and OFF repeatedly for every 100 ms (Usage – for scrolling purposes). The delay 100 ms is only an approximate value. If you didn’t get the correct output, try with different delays (switch bouncing time varies with different switches). For me, 200ms proves to be a right one.

Type 2:

But if you want to change the state of LED only once even if you long press the key, follow the below code.

While(1)
{
            if(digitalRead(7)==0)   // checks whether switch pressed
              {
                       delay(100);  // wait for 100 milli second
                       if(digitalRead(7)==1)  // checks whether switch is released
                       {
                                         state=!state;
                                         digitalWrite(PIN_LED0,i);}
}
}

In this code, the controller first checks whether the key is pressed. After the bouncing delay, the controller checks whether the pressed key is released. If the key is released, the following code gets executed. If the key is not released, the controller waits until the key is released and then executes the following code i.e., the LED changes its state only once for every key press, even if it is long pressed.

By these two techniques, the debouncing problem with key interfaces can be eliminated.

Hope it’s useful for beginners……..!!!!!!!

No comments:

Post a Comment