Digitalread not set back with button

Hello,

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 ALL depends on how you have BUTTON wired. Is it grounded when not presses or is it +5 when not pressed?

well, it is on +5 if open through the resistor

Circuit: 3V3->10 k resistor->D5->button->GND

should this be

GND->10k->D5->button->3V3 instead ?

Either should work, or you can use the internal pull-up resistor.

D5 == GPIO14 which was no pull-up/down resistors on the wemos board, I believe.

Attach some bright, clear, close-up photos of the circuit so we can check your wiring.

Sorry, just found the bug in the system, the VCC wire was too lose on the breadboard. :roll_eyes:
Thanks to you all for the quick assistance anyway!

You need one of these:

https://create.arduino.cc/projecthub/madmark2150/perf-panel-paired-power-plug-piggyback-for-mega-2560-67d604

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

FYI
Suggest you wire switches as S3 is wired.

Make software changes looking for a LOW on switch push/closure.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.