Simple Push Button Setup

Introduction

(momentary button) or switches are used to connect two points in an electrical circuit when pressed. This can be used for many things including controls of a prototype, simple on/off switches, user interfaces, etc.

In this guide, you will learn how to integrate- and program a to read a message when the button is pushed.

Before continuing this guide it is recommended that you have read- or have basic knowledge about .

Supplies

Circuit

The circuit created in TinkerCAD
Connect three wires to the board. Two of these wires should connect the 5 volts (5V) and ground (GND) to one side of the button. Now insert a 220 Ohm in the connection between the button and the wire that goes to ground. The third button should connect one of the legs of the other side to one of the pins on the board. This will be the wire that reads if the button is pushed or not.

Code

// Print message when the push button is pressed
// This code will make the console print out a message when
// the button is being pressed

// constants will not change value
// a pin is assigned to the button
const int buttonPin = 2;

// variables will change
// a variable is assigned to read the status of the button
int buttonState = 0;

void setup() {
  Serial.begin(9600);
  // initialize the push button as an input
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read state of button value. If the read is 'HIGH' the
  // button is pushed. If not the value will be 'LOW'
  buttonState = digitalRead(buttonPin);

  // check if the button is pressed.
  if (buttonState == HIGH) {
    // print a message to the console
    Serial.print("Arduino rock!\n");
  }
  // Adds one second delay to the program
  delay(1000);
}

Further Work

Now that you have completed this tutorial on a simple system, try the guide on that combines what you have learned here about push buttons with LEDs.