A pushbutton (or push button) is a simple switch mechanism for controlling some aspect of a machine or a process. Buttons are typically made out of hard material, usually plastic or metal. The surface is usually flat or shaped to accommodate the human finger or hand, so as to be easily depressed or pushed. Pushbuttons are most often used to complete an electrical circuit when pressed, allowing current to flow through it, and are released to interrupt the circuit. They are common components in electronic devices, machinery, and various control systems.
Pin Number | Description |
---|---|
1 | Normally Open (NO) |
2 | Common (COM) |
// Define the pin connected to the pushbutton
const int buttonPin = 2;
// Variable for storing the pushbutton status
int buttonState = 0;
void setup() {
// Initialize the pushbutton pin as an input
pinMode(buttonPin, INPUT);
}
void loop(){
// Read the state of the pushbutton value
buttonState = digitalRead(buttonPin);
// Check if the pushbutton is pressed.
// If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// Turn on the LED
} else {
// Turn off the LED
}
}
Q: Can I use a pull-up resistor instead of a pull-down?
Q: How do I know if my button is momentary or latching?
Q: What is the purpose of debouncing a button?