(Reference) Buttons

Push Buttons

various types of switches

Wiring

Screenshot 2024-07-03 at 8.34.08 PM

Operation (Normally Open Button)

Placement

switch on breadboard

Unpressed / Open Circuit

switch on breadboard

  • In the picture, each set of pins “vertically across” from each other are always connected (purple and blue)
  • Each set of pins “horizontally next to” each other are not connected until the button is presed

Pressed / Closed Circuit

  • When button is pressed, all four pins are connected (yellow pins)

switch on breadboard

Values

  • Open / unpressed is HIGH
  • Closed / pressed is LOW

Code

const int PIN_BUTTON = D2;

void setup() {
   pinMode(PIN_BUTTON, INPUT);  //configure
   Serial.begin(9600);
}

void loop() {
	//read button state
   int buttonVal = digitalRead(PIN_BUTTON); 
   
    if (buttonVal == HIGH) {
     Serial.write("Button not pressed");
   }
   else {
     Serial.write("Button pressed");
   }
 }

Credit

Updated: