How to Wire and Program Multiple Push-Buttons With an Arduino

2023-02-15 15:38:09 By : Ms. Jinshi Tian

Learn how to connect multiple push-buttons to an Arduino and check for them being pressed in a program.

Push-buttons are incredibly common in the DIY Arduino space, with countless projects across the web relying on these small switches to activate their code. But how exactly can you wire and program more than one push-button to work with your Arduino? Let’s find out.

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.

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

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.

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

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.

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.

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.

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.

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.

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.

Samuel is a UK-based technology writer with a passion for all things DIY. Having started businesses in the fields of web development and 3D printing, along with working as a writer for many years, Samuel offers a unique insight into the world of technology. Focusing mainly on DIY tech projects, he loves nothing more than sharing fun and exciting ideas that you can try at home. Outside of work, Samuel can usually be found cycling, playing PC video games, or desperately attempting to communicate with his pet crab.