Skip to main content

How to use a Push Button with Arduino

How to use a Push Button with Arduino

Hello Guys, I hope you all are doing well. My Name is Vivek Garg and I welcome you all to my Blog Mostly Programing. If you're new here I will suggest you check out my YouTube channel @mostlyprograming. I post 3 New Videos every week, Let's Code Tuesdays, Innovative Thursdays, and Geeky Saturdays. 
Go! Go! Go! Check it out and if you like the content you can maybe give me a subscription or a comment or a like (YOU DECIDE!).

In today's tutorial, we are going to see how a push-button works. I know that may sound quite easy to some of you but believe me, it's not as easy as it seems to be (especially for the Newcomers). 
So, Today we will be seeing 
  • What is a push-button?
  • How it works?
  • What different configurations can it work in?
  • How to interface one with an Arduino?
  • Also, How to read its status using a software program?
  • How do we control an LED using a Push Button?
Let's get started with the Tutorial.
Fig. 1 shows a 4 terminal push-button (these are the type you will generally use). And Fig. 2 shows the 4 terminals of the push-button.

Fig. 1 Push Button 

Fig. 2 4 Terminals of Push Button

What is a Push Button and How it Works?

A push switch (or often called as a button) is a momentary switch that causes a temporary change in the state of an electrical circuit when the button is physically Pressed. A 'push to make' button allows electricity to flow between its two contacts when the button is pressed and when the button is released, the circuit is broken, and then no electricity or current flows between its two contacts.
Fig. 3 shows a basic electric circuit where a 'Push to make' switch is used.


Fig. 3 A basic Push to Make Circuit  (Switch is Released)

In the circuit shown in Fig. 3 the LED stays off until the SW (Switch /Push Button) is in the Open state i.e. it is not pressed. As soon as the button is pressed as shown in Fig. 4 the LED lights up as the circuit is complete now i.e. the current now flows in the circuit.


Fig .4 A basic Push to Make Circuit  (Switch is Pressed to make the circuit complete)

Now, you must be thinking that there are 4 terminals of a push-button so how do we connect them.
So, we use only two terminals of the push button either 1a and 2a or 1b or 2b (as shown in Fig. 2). Also, when we use a push button to complete a circuit we can't simply place it between two open contacts. The push-button must be in either one of the two configurations discussed below:

Fig. 5 Pulled Up and Pulled Down Configuration of a push-button 

Let's understand the working of each type of configuration.

Pulled Up Configuration: 

In the pulled up configuration, one terminal of the push-button (say 1a) is connected to +5volts through a 10 KOhm Resistor and the other terminal of push-button (say 2a) id connected to ground. Vout is taken from the wire joining 2a terminal of push-button and the resistor. 
Case 1: When Switch is not Pressed:
When the switch is not Pressed, there is no contact between the two terminals therefore Vout is +5volts as the lower circuit where the switch is connected is open. The +5volts travels through the resistor and reaches Vout.
Case 2: When Switch is Pressed: 
The Lower Circuit is complete now and the ground i.e. (0 volts) travels through the switch contact to the Vout as there is Lower resistance in the lower circuit as compared to the upper circuit where a 10KOhm resistor is connected.

From the above 2 cases, we can observe that when a switch is in the pulled up configuration, it is normally High and when we press the switch it goes Low. Therefore, when we are using a switch in the Pulled up configuration we must program our Arduino to check when the push-button goes Low.

Pulled down Configuration: 

In the pulled down configuration, one terminal of the push-button (say 1a) is connected to ground through a 10 KOhm Resistor and the other terminal of push-button (say 2a) id connected to +5volts. Vout is taken from the wire joining 1a terminal of push-button and the resistor. 
Case 1: When Switch is not Pressed:
When the switch is not Pressed, there is no contact between the two terminals therefore Vout is  0 volts as the upper circuit where the switch is connected is open. Thus the 0volts i.e ground travels through the resistor and reaches Vout.
Case 2: When Switch is Pressed: 
The Upper Circuit is complete now and the Power supply i.e. (+5 volts) travels through the switch contact to the Vout as there is Lower resistance in the upper circuit as compared to the Lower circuit where a 10KOhm resistor is connected.

From the above 2 cases, we can observe that when a switch is in the pulled down configuration, it is normally LOW and when we press the switch it goes HIGH. Therefore, when we are using a switch in the Pulled down configuration we must program our Arduino to check when the push-button goes HIGH.

Project 1: Control an LED using a Push Button and Arduino

Components Required:
  1. Arduino Uno
  2. Push Button
  3. Bread Board
  4. LED
  5. 10 KOhm Resistor
  6. 220 Ohm Resistor
  7. Jumper Wires
Wiring Diagram:


Fig. 6 Circuit Diagram
Push Button is used in Pulled down configuration and the Vout node is connected to the A0 pin of Arduino UNO (refer to Fig. 5).
Cathode of LED is directly grounded and Anode is connected to the A1 pin of Arduino through a 220 Ohm Resistor.
That's all the Connections.
Let's see how the system is going to work.
When the button would be pressed the LED would Light up and as soon as the button would be released the LED would go down.

Note: The Concept of Debouncing would be updated soon.

So, let's write a program for it now.

Arduino Code:


Also, You can download the code from here:
PushButton.ino

YouTube Video:



Also, If you have any queries, feel free to comment down below and I will try my best to solve, or you can directly DM me at my social media accounts.

Twitter    


Content Creator: Vivek Garg

Comments

Popular posts from this blog

JoyStick HW-504 Interfacing with Arduino

JoyStick HW-504 Interfacing with Arduino Hello Guys, I hope you all are doing well. My Name is Vivek Garg and I welcome you all to my Blog Mostly Programing. If you're new here I will suggest you to check out my YouTube channel  @mostlyprograming . I post 3 New Videos every week, Let's Code Tuesdays, Innovative Thursdays, and Geeky Saturdays.  Go! Go! Go! Check it out and if you like the content you can maybe give me a subscription or a comment or a like (YOU DECIDE!). So, In Today's Tutorial, We are going to see How a PS2 Thumb Joy Stick Works? and How we can use one with an Arduino. Fig. 1 HW - 504 Thumb JoyStick Module When we hear the word joystick, the first thing that strikes our mind is Game Controllers (Like the one shown in Fig. 2). Mainly we use them for playing Games but we can do a lot of fun stuff with them in Electronics. For instance Controlling Robo Cars, Controlling Barrier Gates, etc. Fig. 2 Game Controller Hardware Overview The

Flappy Bird Game with Arduino Uno

Flappy Bird Game with Arduino Uno Hello Guys, I hope you all are doing well. My Name is Vivek Garg and I welcome you all to my Blog Mostly Programing. If you're new here I will suggest you to check out my YouTube channel  @mostlyprograming . I post 3 New Videos every week, Let's Code Tuesdays, Innovative Thursdays, and Geeky Saturdays.  Go! Go! Go! Check it out and if you like the content you can maybe give me a subscription or a comment or a like (YOU DECIDE!). So, Today we are gonna build a fun game with Arduino. This game is called The Flappy Bird game, I bet you must have played this game in your childhood. And if not you can make one today and play. Components Required 1 x BreadBoard 1 x Arduino Uno Board 1 x I2C LCD Display (16x2) 1 x 10 KOhm Resistor 1 x Push Button Jumper Wires USB A/B Cable Circuit Diagram Fig. 1 Circuit Diagram Connections: SCL(LCD) ---> A5/SCL Pin SDA(LCD) ---> A4/ SDA Pin Vcc (LCD) ---> +5Volts Gnd