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
- board
- Wires
Circuit
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.