Arduino Buttons Part 1 : Arduino Push Button : Arduino 9th Lesson of Arduino Tutorial

This is the Arduino ninth lesson of Arduino tutorial. In this post, we are going to talk about how to work with input devices. In all pervious lessons, we have leaned about only output devices. But in this lesson, we can learn about input devices. So, most basic input device is the switch. We are going to learn how to work with push button in this post. Keep reading and stay connected. Let’s get started.

Push Button

In this topic, we are going to turn on LEDs parallelly and single.  So, if you don’t know how push button works, here is the push button pinout and structure.


Push button works when you pressed it. When you release it, other wire will begin to working. That’s the special with push buttons. This is the circuit diagram for creating push button led on/off. LED is out output way. Below is the source code and simulation. You can press the start simulation buttons and run the simulation. See it yourself before getting Arduino and try it practically.


In this code, we initialize out 8th and 9th pins as outputs and out 2nd pin as input. 8th and 9th pin is connected to LEDS and 2nd pin is connected to push button. Then in the void setup, we put if condition.

If condition means weather conation is true or false. Imagine you have to go to shop in the evening. You decided to go to shop in the evening, if there isn’t any raining outside. So, raining is the condition. If it rains in the evening, you won’t go to shop. But if it doesn’t rain in the evening, you will definitely go to the shop. That’s the simple if condition when you can experience in your life. Then we can have a look at else. Else represents if condition is false, then the other option. If it rains in the evening, you decided to that I won’t go to the shop and you began to sleep. That’s the else option. But if it doesn’t rain the evening, you’ll go to the shop and you won’t sleep. That’s the else side of if condition. You can put if condition without else. No problem with that. But if condition is needed for else condition.

Ok, then we have to learn about digital read function. Digital read is the function is the option that you can get a pin digital reading. Digital reading is always in two states. One is 0 and other is 1. It defined in the Arduino as HIGH or LOW. So, you can check weather input is high or low. Note, that you can’t get sensor readings by this digital read function. This digital read function is only for getting button or some high low readings.

So, we put variable named “state” and in the second line of void loop, we put if condition and get the value that, if stated is HIGH (“state == HIGH”). Inside the if condition we but result as blink 8th pin. In the else part put blink at 9th pin LED.

I think you have good knowledge about Arduino Push button controlling. In next post, we can learn about Arduino toggle switch controlling.

Post a Comment

If you have any doubt, Let us know

Previous Post Next Post