Switches having a delay

I am working on a project where I'm trying to use a dip switch but for some reason I'm coming across a strange problem. turning the switch on works perfectly fine but when turning the switch off there is a delay of usually a few seconds. I have connected the dip switch from the 5V live on an Arduino Uno through a resistor then through the switch and finally back to a digital read pin. is there something wrong with that because I tried writing a separate code just to read the switch and I'm coming across the same thing.

for this graph I flipped the switch back at 340 exactly and the reading finally fell at 367.

the code was

void setup() {
  Serial.begin(9600);
  pinMode(3, INPUT);

}

void loop() {
  
  Serial.println(digitalRead(3));
  delay(500);

}

I'm sorry if this is a dumb error, I'm really new to Arduino and I have no idea where to look to fix this.

Try:

pinMode(3, INPUT_PULLUP);


FYI


thank you so much, that works perfectly. I really appreciate the help.

Wiring switches as per S3 in the schematic is preferred as no external resistor is needed.

that makes a lot more sense. this had been bugging me for days now.

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