A pushbutton is a fundamental electronic component that operates as a simple switch to control an electrical circuit. It is a momentary switch mechanism that, when pressed, either completes or interrupts a circuit. Pushbuttons are ubiquitous in electronic devices, serving as inputs for a wide range of applications, from simple household gadgets to complex industrial machinery.
Pin Number | Description |
---|---|
1 | Terminal 1 (Contact) |
2 | Terminal 2 (Contact) |
Note: Some pushbuttons may have more than two pins for additional features such as built-in LEDs or multiple contacts.
// Define the pushbutton pin
const int buttonPin = 2;
// Variable for reading 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 (assuming an LED is connected to pin 13)
digitalWrite(13, HIGH);
} else {
// Turn off the LED
digitalWrite(13, LOW);
}
}
Note: The above code assumes the use of an internal pull-up resistor. If an external pull-down resistor is used, the pinMode
should be set to INPUT
instead of INPUT_PULLUP
, and the logic would be inverted.