A pushbutton, also known as a push switch or momentary button, is a simple yet essential component in many electronic circuits. It operates as a momentary switch that, when pressed, creates an electrical connection and, when released, breaks the connection. Pushbuttons are widely used for user input, allowing users to interact with electronic devices by sending signals to perform various tasks such as turning on a light, starting a process, or inputting commands.
Pin Number | Description |
---|---|
1 | Common terminal (C) |
2 | Normally open (NO) |
Note: Some pushbuttons may have more than two pins for additional functionality or stability when mounted on a board.
// Define the pin connected to the pushbutton
const int buttonPin = 2;
// Variable to store the state of the button
int buttonState = 0;
void setup() {
// Initialize the button pin as an input
pinMode(buttonPin, INPUT);
}
void loop() {
// Read the state of the pushbutton
buttonState = digitalRead(buttonPin);
// Check if the button is pressed (assuming the other end of the button is connected to ground)
if (buttonState == HIGH) {
// The button is pressed
// Perform an action here
} else {
// The button is not pressed
}
}
Note: The above code assumes the use of a pull-down resistor. If using a pull-up resistor, the logic will be inverted.
Q: Can I use a pushbutton without a resistor?
Q: How do I know if my button is normally open or normally closed?
Q: What is debouncing and why is it important?