What Are We Making?

This is a simple project that is designed to get you started with more complex Arduino builds in the future. We won’t be giving our buttons a job, but this means that you can choose what you would like them to do for yourself using one of our other handy DIY Arduino guides.

What Do You Need?

You only need a handful of parts to complete this project, many of which you will already have in your DIY parts collection.

4 x Push-button switches 4 x 10kΩ resistors 1 x Any type of Arduino board (we’re using a Leonardo) PVC/silicone coated wire

Wiring Multiple Push-Buttons With an Arduino

The wiring for this project is simple, but you have to be careful to avoid getting your wires tangled as you make each connection, as each button requires two different wires.

Starting simple, it makes sense to wire one push-button with its positive leg connected to the 5V pin on our Arduino, and the button’s ground leg connected to both GND and Digital Pin 2 on our Arduino board. A resistor must be connected between the button’s ground leg and the GND pin on the Arduino.

With one button wired up, it’s time to add the others. Each button needs its own Digital Pin; we picked 2, 3, 4, and 5 for the four buttons that we are using, but any of the Digital Pins will work. Now that your Arduino is all wired up, it is time to start working on the code.

Programming Multiple Push-Buttons With an Arduino

The code for the project is nice and simple, without the need for any class libraries or other complicated programming.

Assigning Buttons to Pins

For the first step, we need to assign our buttons to the different pins on our Arduino board. Seeing as we used Digital Pins 2, 3, 4, and 5, these are the pins we will declare with our code. This should be placed at the top of your Arduino project, before any of the functions.

Setting Up the Buttons

As with most Arduino projects, we will be using a function that will run once at the beginning of the program, called void setup(). First, we will start our serial connection with a baud rate of 57600, followed by the initialization of our buttons. This is all we need in our void setup() function.

Detecting Button Presses

This next stage is more complex than the others, as we will be creating our own function that will be dealing with a variable from the main loop function. To start, we need to declare our function with an integer variable like the code below.

Following this, we need to assign the variable we are using and create an if statement to detect when each button is pressed. This function can only check on button at a time, using the variable it gets from the main loop function so that it knows which button the check. Our if statement checks the state of the button using the built-in digitalRead function.

Code added to the if statement will run when a button is pressed, while code in the else statement will only run when a button isn’t pressed.

Building the Main Loop

Finally, as the last piece of code you need to add to your program, it’s time to build the void loop() function. We just need four lines of code: one for each of the buttons that we have attached to our Arduino. These lines call the function we created in the previous step with the pin number of each button.

The Finished Code

Once you have all of this code in place, your finished program should look like the code below. We’ve added comments to each line to make it easier to understand, but we encourage you to look up and read about anything that you don’t recognize. This can be a great way to expand your coding knowledge.

Success: Wiring and Programming Multiple Push-Buttons With an Arduino

This is an easy project to get started with when you want to learn about hardware and software for Arduinos. You can easily expand on what you have made with additional components, giving you the chance to explore a wealth of exciting ideas and create things that make you feel proud.