Friday, 3 April 2015

Arduino Tips & Tricks

Part1: Using buttons in a Reliable way.


Buttons are an integral part of any system. Although now we are more into touch panels and touch screens but still they are there in slightly older systems and may be their count will drop down considerably but they will remain part of newer systems too. So it is good to understand them.

There are two key issues that arise while dealing with buttons.
1. Switch debounce. This is a issue that arise when you press a switch to open it or close it. Spurious signals or sometimes termed as spikes arise when contacts are made or broken. This results in false detection of state.
2. Non usage of pull-ups or pull downs. This  also leads to a state which is un-deterministic.

So these are the problems that I am going to discuss and the measures you should take to eradicate these issues.If you couldn't understand some pieces of what i wrote just don’t worry keep reading and you will get it after you read the examples below.

Example 1:
In this example we will read the state of pin 5 on arduino and will toggle the state of LED on pin 13 according to the state of pin 5. The circuit diagram is as below.


 Here as you see a button is connected to pin 5 and there is a resistor of 20k. This resistor is technically called a pull down resistor. When the button is pressed circuit is completed and  there is a voltage drop across the 20k resistor and this voltage drop should be more than 2.5V. If it is so the state of pin 5 will be read as HIGH by the controller otherwise the state will be read as LOW and you can use this information to switch on or off any LED or anything else. If we donot use the 20k resistor then when the button is not pressed the state of pin 5 will be undeterministic  but as there is a resistance here which pulls down the state of pin 5 to ground when button is not pressed so the indeterministic state is avoided. 

Below is a code that turns the LED on and off based on the state of pin 5
const int ledPin = 13; // choose the pin for the LED     ///#D80E49
const int inputPin = 5; // choose the input pin (for a pushbutton)
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
}
void loop(){
int val = digitalRead(inputPin); // read input value
if (val == HIGH) // check if the input is HIGH
{
digitalWrite(ledPin, HIGH); // turn LED on if switch is pressed
}
else
{
digitalWrite(ledPin, LOW); // turn LED off
}
}

Here you saw the that a pulldown resistor( or a pullup resistor which is connected to Vcc instead of gnd) is very necessary. Run the above code without this resistor and you can yourself see the difference it creates. 


USING INTERNAL PULL UPS

There are internal pull up resistors available inside the the microcontroller on any arduino board. They can be activated if you want to and in that case you will not have to add an external resistor. To activate it you have to make the state of pin set as input to HIGH in the setup function of arduino.


CODE:
Look at the line  6.
const int ledPin = 13; // output pin for the LED
const int inputPin = 5; // input pin for the switch
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);
digitalWrite(inputPin,HIGH); // turn on internal pull-up on the inputPin
}
void loop(){
int val = digitalRead(inputPin); // read input value
if (val == HIGH) // check if the input is HIGH
{
digitalWrite(ledPin, HIGH); // turn LED OFF
}
else
{
digitalWrite(ledPin, LOW); // turn LED ON
}
}
so that solves for this time .

No comments:

Post a Comment