Introduction: How to Use a Push Button Switch With Arduino


This is an Instructable that tells you how to connect a 4 pin push button switch with the Arduino.
The contraption allows an LED to be switched on when the push button is pressed.

Step 1: What You Need:

You need:

A push button switch
Arduino
1 Resistor (any value), I used a 220 ohm one.
2 Jumper cables(breadboard jumpers)
1 Breadboard (to make it easy)
LED

Step 2:

1. Put the switch in the breadboard and put an LED with the longer end into pin 13 and shorter end to the Gnd of the Arduino.
 

Step 3:

Put the resistor with one end in +5 V and the other end connected with one of the terminals of the switch.
Connect the other corresponding terminal to Gnd. The corresponding terminal is usually on the same side as the first one.

Step 4:

Connect the first terminal (the one with the resistor) to pin 2 on the Arduino and load the program:

int d=2;   // to store on or off value
void setup()
{pinMode(2,INPUT);
pinMode(13,OUTPUT);

}
void loop()
{
d=digitalRead(2);
if(d==0)
{digitalWrite(13,HIGH);}
else
{digitalWrite(13,LOW);}
}

Step 5: You Are Done!!



Now just press the switch and the LED will light up!

Comments

author
ajitchahal made it! (author)2017-04-18

Thanks :)

Push_or_reed_bb.pngPush_or_Reed_button.inoPush_or_reed_schem.png
author
VitaliyMysechko (author)2016-06-05

Hi!
Thanks for the nice example.

Why I have "1" on the input D2 while the button is not pressed (Serial monitor from Arduino IDE shows it )? It seems that input is inverted - it shoud have "0" when it is no current or "1" when we have it (button pressed) - but I have oposite situation!

On the other side - HIGH means "1" for me and we have LED bright with HIGH flag for the output D13....So it is normall behaviour...

Please explain!

author
vermasunny (author)2016-06-03

I dnt have sufficient knowledge abt electronics so I want to know why Connect the first terminal (the one with the resistor) to pin 2 on the Arduino ? tell me anyone

author
evdschepop (author)2016-05-19

Thanks!!

author
mirzaa11 (author)2016-04-14

Push putton can be of 6 terminal then how it will be connected

author
thijs12345 (author)2016-01-27

cool. thanks, it is a good help for one of my big projects.

author
BorisP12 made it! (author)2015-12-17

How did you make you LED change colors like that?

IMG_1165.JPG
author
Firebolts (author)2015-11-27

Hey i completed this but have a question. I just got the Arduino UNO starter pack so i have a small breadboard and button. It did not always answer to my button and sometimes went out. Is there a way to do it on a smaller scale.

author
ironheartbj18 (author)2015-07-05

There is insufficient the code details not sure how you get the change from blue to green. it does not make any sense if high will change to blue change blue or red. it got me suspect. what kind of you were using the led.

author
sidgupta (author)ironheartbj182015-07-11

Dude. That's just a color changing LED. There's no programming needed to change color. It does that on it's own.
If you'd used a blue/green/red LED instead it wouldn't have changed color.

author
Adhanala (author)2015-05-28

Thanks, I wanted to know how can i light the led up for longer than few seconds.

author
sidgupta (author)Adhanala2015-07-11

Hi!
Sorry for the late reply.
Add a small delay. If you want to keep it on for ten seconds then put the delay as 10000 (10,000 milliseconds)
Example:
void loop()

{
d=digitalRead(2);
if(d==0)
{digitalWrite(13,HIGH);delay(10000);}
else
{digitalWrite(13,LOW);}
}

author
Ryanj2000 (author)2013-12-09

Thanks, I am making a project involving buttons and leds. This helped a lot.

About This Instructable

174,522views

45favorites

License:

Bio: Love simple robotics, electronics, sensors, arduino projects and generally building, modifying and opening up stuff
More by sidgupta:Make A Simple Bipedal Humanoid Robot (Servo Walking Robot)How to make ChaasHow to use a push button switch with arduino
Add instructable to: