Hi
I purchased these buttons thinking that they would be wired like a simple switch with a pullup resistor but I discovered that they have resistors already installed. It has a normally open wire and a normally closed wire. When I hook it to an Arduino using the example button script, and attach one of the NO or NC wires to pin 2 it sometimes works and sometimes does not.
Can anyone give me some tips about how to wire this thing up? Usually I just use a 10k Ohm resistor and I am good to go but apparently this switch is over my head.
This is the code I am using:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
Serial.begin(9600);
// 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);
delay(50);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
Serial.println("HIGH");
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
//Serial.println("LOW");
}
}