I am feeling really dumb with this question as this should be so simple.
Trying to get a reed switch triggering something on a D1 Mini. So I just thought this should just work like a simple button and tried this code:
int led = BUILTIN_LED;
int button = D5;
int temp = 0;
void setup() {
Serial.begin(115200);
pinMode(led, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
temp = digitalRead(button);
Serial.println(temp);
if (temp == HIGH) {
digitalWrite(led, HIGH);
Serial.println("Button open");
delay(1000);
}
else {
digitalWrite(led, LOW);
Serial.println("Button pressed");
delay(1000);
}
}
However once triggered / pushed the temp = digitalRead(button); doesn´t get set back to open, I even tried to force a set back after the delays but it doesn´t happen. Then I thought the reed got stuck but the same behaviour happens when I just use a wire as button.
Circuit: 3V3->10 k resistor->D5->button->GND
it's more conventional to wire the switch between the pin and ground, configure the pin as INPUT_PULLUP to enable the internal pullup and check for LOW.
one of the reasons the pin can be configured with an internal pullup