Arduino Tutorial - Buttons (English)

in #hardware6 years ago

THUMBNAILE.png

This tutorial is for advanced beginners

Hello everyone, after a long while I didn't post anything, its time to get back to the routine.

In today's tutorial, I will teach you how to use buttons on Arduino.

I know that some of you are not familiar with Arduino, so I recommend you to go on arduino.cc website and start learning this fantastic language.

ABOUT ARDUINO

" Arduino is an open-source electronics (raised, flat supporting surface) based on easy-to-use hardware and software. Arduino boards can read inputs - light on a sensor, a finger on a button, or a Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing something online. You can tell your board what to do by sending a set of instructions to the microcontroller on the board. To do so you use the Arduino programming language (based on Wiring), and the Arduino Software (IDE), based on Processing. "

information about Arduino, was brought from arduino.cc website

LETS LEARN HOW TO USE BUTTONS ON ARDUINO

Products:
  1. Arduino Uno board
    arduino-2168193_640.png

Be aware of the fact that youll need this cable either (must have):

There are several kinds of cables, but in this tutorial I am going to use this one.

  1. Cabling board
    product_1347_18718_0x0.jpg

  2. Cables
    2714x635702289521216133_L.jpg

  3. Button

f4f3025f-03c0-4e0a-94d0-5eefae72b4d9.png
this button has 2 legs, most of the buttons in the market have 4. In this tutorial you'll understand how to wire a button with 2 legs, but it is the same with 4 legs.

  1. Resistor

ea81dd92-5e0c-4e5e-bab5-b55793d934e9.jpg

the resistor colors are not matters in THIS VERY TUTORIAL

  1. Arduino Software (could get from arduino.cc)

OK, SO AFTER WE BROUGHT ALL THESE PRODUCTS

Lets start putting the button in his place

0f4eab3d-40d7-46a3-83e8-cc2c5fff66c6.jpg

#WARNING

Do not put this two legs on the same row because it will not work.

Now lets cable the cables like so:

73e847dd-4d86-42f5-b1e4-49fd4a001c7f.jpg

red is +, black is -. (Doesnt realy matters, just remember this).

60e07771-d072-4f24-9e76-f8c4ff683827.jpg

Ok, so the red cable is connected to the 5V (default), place on the Arduino board.
And the black one, is connected to the ground (GND).

Now take your resistor

and put it like so:

ddd50443-8843-4c50-88e8-c8b4ce3954b0.jpg

As you can see, your resistor is now connected to the RED row, (+), which means it gets 5Volts.
We connected the resistor to the left leg of the button.

WARNING

Put the bigger leg on the right, and the smaller on the left. And remember it. It is important.

Let's put some cables on !

Put them like so:
d9b7a984-5362-4c7c-a2e5-416daba31a2e.jpg

cd47d884-4c5d-47cc-ab62-2a8e5b779fd0.jpg

The orange one is connected to 'PORT D', hall number 7.
The blue cable, is connected to the black ROW (-), which means its connected to the ground.

Thats wonderfull! You finally can go and CODE IT.

The coding part is a bit harder, but I will put my own source code here, for you to compare.

I recommend code your own sketch, before continuing.

GOOD! Lets start.

First thing youll see is:

Ok, I recommend first to define the '7' pin as a button.

#define button 7
this line of code comes before every void, function, or integers you wrote.
This line of code is actually defining 7 as button. They are the same thing now.
You will understand later on.

void setup() {
// put your setup code here, to run once:
DDRD = 0xFF;
Serial.begin(9600);
pinMode(button, INPUT);
}

This is your default settings, it will run only 1 time. (We dont need it to run more then that).

The void loop, is going to run reapeatedly (non-stop).
I hope you learned IF-Else statemantes, if you didnt so learn it because it is important for this project.

First of all, we define a new integer.

but = short for a button

void loop() {
// put your main code here, to run repeatedly:
int but = digitalRead(button);
if (but == HIGH) {
Serial.println(" PUSH IT ! JUST DO IT ! ");
} else {
Serial.println(" PLS STOP ! YOU ARE PUSHING TOO HARD. ");
}
}

AND THERE YOU GO!

Why should I use a button? What can I use it for?

Buttons are very important in Arduino. For example, drinking machine, can be also be working by buttons.
Buttons are awesome, how you learned something.

#GOISRAEL #GOARDUINO #WELOVEHARDWARE #EZPZ

Sort:  

Very nice guide, arduino is an awesome toy :D

Thank you very much for your reply. I really appreciate this.