A pushbutton is a simple and fundamental component in electronics, serving as a mechanical switch. It operates by making or breaking an electrical connection when pressed. Pushbuttons are ubiquitous in electronic devices, used for a variety of applications such as initiating actions, inputting commands, and controlling operations.
Pin Number | Description |
---|---|
1 | Normally Open (NO) |
2 | Common (COM) |
3 | Normally Closed (NC) |
// Define the pin connected to the pushbutton
const int buttonPin = 2;
// Define the pin for the LED
const int ledPin = 13;
// Variable for storing the pushbutton status
int buttonState = 0;
void setup() {
// Initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
// 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
digitalWrite(ledPin, HIGH);
} else {
// Turn off the LED
digitalWrite(ledPin, LOW);
}
}
Q: Can I use a pushbutton with a microcontroller like an Arduino? A: Yes, pushbuttons are commonly used with microcontrollers. Ensure you use a pull-up or pull-down resistor to define the input state.
Q: How do I know if my pushbutton is momentary or latching? A: A momentary pushbutton will only maintain contact while being pressed, whereas a latching pushbutton will stay in its last state until pressed again.
Q: What is the purpose of the NC terminal? A: The NC terminal is used when you want the circuit to be normally closed and open the circuit when the button is pressed.