stickers_logo_text

Arduino Tutorial 03: Buttons & Functions

Hello !!!!!!!!! & Welcome back guys in our 3rd tutorial of Arduino tutorial series. Hope you guys following our previous tutorials.  In this tutorial we are going to talk about “Buttons”. In our daily life we have already seen many applications of buttons, like in our home, offices, car, mobile phones etc. So, button seems to be a very useful device in electronics and our daily life. Today, we are going to see how you can interface buttons with your Arduino Uno R3 Board.

So let’s get started

Requirements

  • 1 x Arduino Uno R3 Board
  • 1 x Laptop or pc
  • 1 x Bread board full size
  • 1 x Led(Red,5mm or 3mm)
  • 1 x Push Button
  • 1 x 1KΩ Resistor
  • 1 x 10KΩ Resistor
  • 1 x USB cable
  • Jumper wires

Connection Diagram

tutorial03

 

Source Code

/* ========================================================
       Project : Button
        Author : Jordan
       Created : 20th Nov 2015
   Description : Program for Led Control using Button 
============================================================
*/
int buttonPin = 8;
int led = 13;
void setup() {
 // put your setup code here, to run once:
 pinMode(buttonPin, INPUT);
 pinMode(led, OUTPUT);
}

void loop() {
 // put your main code here, to run repeatedly:
 if(digitalRead(buttonPin) == HIGH)
 {
 digitalWrite(led, HIGH);
 }
 else
 {
 digitalWrite(led, LOW);
 }
}

So, in the above mention code, as you can see when you press and hold the button led is glowing and when you release the button led is “off”. This is the simplest trick which you can do with the button.

Let’s do some thing else, Now we are going to perform another operation. When you press the button led “turn on” and when you again click the button led will “turn off”. So, let’s do some real coding. In this experiment we are also going to learn how to use functions in arduino programming.

Source Code

/* ========================================================
       Project : Button
        Author : Jordan
       Created : 20th Nov 2015
   Description : Program for Led Switchig using Button 
============================================================
*/
int buttonPin = 8;
int led = 13;
boolean preButton = LOW;
boolean Button = LOW;
boolean ledOn = false;
void setup()
{
 pinMode(buttonPin, INPUT);
 pinMode(led, OUTPUT);
}
boolean debounce(boolean last)
{
 boolean current = digitalRead(buttonPin);
 if (last != current)
 {
 delay(5);
 current = digitalRead(buttonPin);
 }
 return current;
}
void loop()
{
 Button = debounce(preButton);
 if (preButton == LOW && Button == HIGH)
 {
 ledOn = !ledOn;
 }
 preButton = Button;
 
 digitalWrite(led, ledOn);
}

Now, It’s time to test the code. Upload the above mention code in your arduino uno r3 board. Rest of the hardware connections are same.

Now, press the button and this time you will find something interesting. This time when you press the button you will see led is “ON” and hold it’s state. When you again press the button led will “OFF”.

!!!!!!!!!!!!!!!!!!!!Enjoy!!!!!!!!!!!!!!!!!!

Hope you guys like our new tutorial.

We will do some coooool!!!!!!!!  stuff in our next tutorial with Arduino uno R3.

comments powered by Disqus

how can we help you?

Please leave your contact details, Our customer relationship manager will assist you.

get in contact
Be the Campus Ambassador of ELAN IIT Hyderabad!