Adding a button requires a little more wiring and coding than adding an LED. The button uses its own methods when programming and you will also have to utilize if-statements to read what the button is doing.
This guide will be useful for anyone trying to add user input to a project, instead of the board doing everything by itself.
In this guide, I have included an LED that is connected to the button, to actually show that the button is working. If your button does something else (which it probably does), feel free to skip the LED setup steps.

Goal:

This guide will show you how to set up an Arduino Uno board to make it react to a button being pressed, essentially giving a button some functionality.

Prerequisites:

Before you can start integrating a button into your project you will need a couple of things first

  • An Arduino Uno Board and the USB printer cable (duh)
  • One Push Button
  • One 10k Ohm Resistor (The baby blue one) + One extra for the LED
  • Three Jumper Wires + Two extra for the LED
  • One 5mm Standard LED
  • One Breadboard
  • A Computer with an internet connection
  • The Arduino IDE (Where you code)
Computer and USB Printer Cable are not present in this picture

The Procedure:

Step 1 (Setup):

The first thing that we need to do is to clear our table or another working area. When you are ready place the board on your table and get all of the extra accessories ready.
If you have worked with an Arduino before, you will most likely already have the Arduino IDE, but if you do not have it go to the Arduino website, download it and set it up.

Step 2 (Wiring):

When everything is ready we can start putting everything together and write the program that will be used to make the button actually function.
First Configure the button, LED, resistors, and jumper wires as is shown in the picture below:

Step 3 (PC Connection):

When the board has been configured you can plug the USB cable into the board and into your computer, to power the board and to transfer the program you will be writing.

Step 4 (Programming the LED):

Now that your computer and board are connected you can start programming. Open the Arduino IDE, and follow any directions on the screen. When you get to the Arduino IDE start page press FILE, then press NEW.
We will be programming the LED first, so we can see how the button will work with it, let’s go.
The first line of code will be placed before any of them, and its purpose is to tell the board where the wire that is hooked up with the LED is located. For this program, I will be using Digital Port 12, but you can use whichever one you want.
int LED = 12;
The second line of code will be placed inside the void setup() function, which means the code will run when the board is turned on. The code will be used to indicate what signal the wire should be sending, which in this case is an OUTPUT signal.
pinMode(LED, OUTPUT);
Your code should look something like this:

Step 5 (Programming the button):

Now that the LED has been set up it is time for the meat of this tutorial the button.
The first line of code will be written right after the first line you wrote in this program, AKA on line 2. This will tell the board where the wire that is hooked up to the button is located. For this program, I will be using Digital Port 4, but you can use whichever one you want.
int BUTTON = 4;
The other lines of code will be an if-else-statement inside the void loop() function, which means the code will continue to run as long as the Arduino is powered on. This code will be used to check if the button is being pressed or not, and then execute the command that turns on the LED.
The code will be used to turn on the LED, by setting the LED setting to HIGH, which is ON, and LOW is OFF. Furthermore, I will use the digitalRead() function to determine if the button is being pressed, which will set it to HIGH.
if (digitalRead(BUTTON)==HIGH) {
digitalWrite(LED, HIGH);
}
else {
digitalWrite(LED, LOW);
}

This is how the program should look. If you want the program I have created a GitHub Repository, where you can download it (or copy-paste it).

Step 6 (Finale):

Now that you have the program ready press CTRL + S, to save the program, name it, and find a place to store it on your computer.
When the program is saved it is time to send it to the board. This is done simply by pressing the “Upload” button, which is the one with a little arrow that points to the right.
Now you can press the small button and the LED will light up and when you let go of the button it will turn off again.

Conclusion

Thank you for reading this article, and if you read my last Arduino Uno article, I hope you have learned something new, and that this was hopefully a useful step on your Arduino journey.
If you have any questions feel free to put them in the comments below this article :).
Thank you so much for visiting JavaLite, it is incredibly humbling, if you have any feedback feel free to comment below too.

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *