Give orders to Arduino with a push button

Updated: May 5, 2020



Know it

The micro-push button is a simple tactile button that connects and disconnects a circuit. Think of an ON/OFF button. You can also connect multiple buttons to your Arduino so the user can select between different options.

Wire it

We are going to allow the LED to turn on and off using the button. We need to complete the circuit by having:

  • Breadboard and Jumper wires

  • 220Ω resistor

  • 10KΩ Ohm resistor

  • 5mm LED

  • Arduino board and USB connector



Connections:


Power:

  • Arduino pin GND Breadboard GND column

  • Arduino pin 5V Breadboard 5V column


LED:

  • Breadboard GND column LED Short leg

  • Arduino pin 13 Resistor (220Ω) LED long leg


Push-Button:

  • Arduino pin 12 Button top left leg

  • Breadboard 5V column button bottom right leg

  • Breadboard GND column Resistor (10K) button bottom left leg




Code it

We are going to control turn ON/OFF an LED using the push button using this code.

Try it out! Copy the code below:

/*
  Micro Push Button
  
  Tutorial link: https://www.learn.voltaat.com/post/pushbutton
  This sketch was written by Voltaat Store
  
  This is an Arduino Sketch that will turn on the LED when u press the
  push button, and turn off when u release the push button.
  
  Components Needed:
  1. Push Button.............. x 1
  2. 10k ohm Resistors........ x 1
  3. 220 ohm Resistors........ x 1
  3. 5mm Led.................. x 1
  
 Connections:
  
  Power:
  - Arduino pin GND → Breadboard GND column 
  - Arduino pin 5V → Breadboard 5V column 
  
  LED:
  - Breadboard GND column  → LED Short leg
  - Arduino pin 13 → Resistor (220Ω) → LED long leg
  
  Push-Button:
  - Arduino pin 12 → Button top left leg 
  - Breadboard 5V column → button bottom right leg
  - Breadboard GND column → Resistor (10K) → button bottom left leg
*/

int pushButton = 12;  // pin number where button is connected to 12 
int led = 13;         // pin number where led is connected to 13 
int buttonState;      // status of button (high or low)

// This routine runs only once upon reset
void setup() 
{
   pinMode(led, OUTPUT); // Initialize 13 pin as output 
   pinMode(pushButton, INPUT); // Initialize 12 pin as input
} 

// This routine loops forever
void loop() 
{
  buttonState = digitalRead(pushButton);   // read the state of the pushbutton
  
  // check if the pushbutton is pressed. If it is, the buttonState will be HIGH
  
  if (buttonState == HIGH)
  {
   digitalWrite(led, HIGH);  // Turn ON the LED
  } 
  
  else 
  {
   digitalWrite(led, LOW);   // Turn OFF the LED
  } 
  
}

 

Learn

Arduino Starter kit

Contact us

Doha, Qatar

Company

© 2020, Voltaat Store for Trading