Minggu, April 28, 2013

Controlling Led With Push Button Using Arduino

Assalamu ‘alaikum friends..., every body is healthy? Alhamdulillah, still live life with full spirit... : ) Don’t make self be sad, because since we was child has explained “Sad is not be used... “ hehe... : )

Still realted to Arduino, now we want to try controlling object (in this case led as object) with use an input, at this time will be used as input is a push button

Basic circuit is exact same with controlling led with Arduino, just we add a push button, push button is also familiar with tactile switch, push button will be used now is push button with 4 pins, not known whatever there is available push button with 2 pins or not, but even if 4 pins from outside, actually only 2 pins cause there are 2 pins is connected mutually. Scheme of push button is like picture below:

Pin 1 is connected to pin 2, pin 3 is connected to pin 4. Yeah, if we take apart a push button, like that seem approsimately. When push button is pressed then pin 1-2 will be connected to pin 3-4.

For push button circuit, one pin of push button, suppose pin 1-2 is connected to voltage source 5 Volt of power pin, then pin 3-4 is connected to parallel circuit of input of digital pin and ground of power pin.

Circuit of controlling led normally on Arduino approsimately as follows:

Picture 1
Explanation:
  1. One of digital pin (begin from 0 to 13, at this example we use digital pin 13)
  2. Resistor
  3. Led
  4. Ground of digital pin (next to digital pin 13)

As while push button circuit on Arduino as follows:

Picture 2
  1. Power pin 5 Volt
  2. Resistor
  3. Push button
  4. One of digital pin (at this example we will use digital pin 2)
  5. Ground of power pin

Digital pin on picture 1 and digital pin on picture 2 are not allowed same, with other word led circuit and push button circuit use different digital pin (many digital pin is availabe, there are 14...)

We will use Red Led diffused 5 mm, then resistor value will be needed is 150 Ω. See its explanation on this article!

For push button, based on Tactile Switch datasheet, its resistance value is 100 mΩ, as while voltage output from Arduino is 5 Volt, then current value flows on tactile switch (push button) and arduino is:

Based on Tactile Switch datasheet, contact resistance is safe to be flowed by current 100 mA, it means 50 A is not safe for Arduino. For that needed a resistor so that current flows on Tavtile Switch and Arduino compatible with current value allowed.

Suppose, current will flow is 20 mA for Tactile Switch and Arduino so that be safe for both, then resistance value will be needed is:

So got resistance value approsimately 250 Ω is safe if we use on tactile switch circuit.

Circuit and its result friends can see on video at last section in this post. Sahabat confident, all friends can string it easy... : )

The sketch are follows:

int ledPin = 13;
int inPin = 2;
int val = 0;

void setup {
  pinMode(ledPin, OUTPUT);
  pinMode(inPin, INPUT);
}

void loop() {
  val = digitalRead(inPin);
  if (val == HIGH) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}


digitalRead() is code to read value of a spesific digital pin, will have value HIGH or LOW, its sintax is:
digitalRead(pin);

At this example, digitalRead() code read a object is connected to digital pin number 2, if that object is on connected position then digitalRead(inPin) will have HIGH value and if that object is not on connected position then digitalRead(inPin) will have LOW value.

When push button is pressed then object will be connected from voltage source 5 Volt forward to digital pin number 2 dan forward to ground of power pin then digitalRead(inPin) will have HIGH value. On sketch if val have HIGH value then Led will ON.

And when push button is not pressed then object will not be connected, current has broken off between both of push button pin then digitalRead(inPin) will have LOW value. On sketch if val is not have HIGH value then Led will OFF

First, val is defined have 0 value, why is defined 0 value? Hmm, not also known friends, if be guided of its source, to storage value will be read of digitalread(), for a while just accept that, like this its prorequirement.

For if... then... else... code, like easy to be understood, because same with understanding if... then... else... formula on microsoft office excel and others programming language...

Way to string up and its result can be seen on video follows:



Above is sketch to contolling Led where Led will ON if push button is pressed adn will ON if push button is not pressed, how if we want to make instead? Led will ON if push button is not pressed and will OFF if push button is pressed? The answer is easy, we just change little of sketch code so that the sketch will be follows:

int ledPin = 13;
int inPin = 2;
int val = 0;

void setup {
  pinMode(ledPin, OUTPUT);
  pinMode(inPin, INPUT);
}

void loop() {
  val = digitalRead(inPin);
  if (val == LOW) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}


Please correction if there is something wrong, and don’t hesitate for asking if there something is understood less...

Hopefully useful...

Assalamu ‘alaikum : )

0 komentar: