2 part question about LED behaviour and digitalRead

Hi all,

I'm just starting out but am kind of confused by some basic behaviour i'm seeing with the Arduino Uno board I have. I'm sure it's fairly elementary to others.

My first sketch is this:

void setup() {
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
pinMode(8, INPUT);
pinMode(7, INPUT);

}

void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(8) == HIGH) {
  digitalWrite(LED_BUILTIN, HIGH);
} else if (digitalRead(7) == HIGH){
  digitalWrite(LED_BUILTIN, LOW);
}
}

I have a single jumper wire coming from 3.3v. When i connect it to pin 8 I want the builtin led to light up. When i connect it to pin 7 i want it to go dark again.

So how come when i move the jumper wire from pin 8 to 7 it only loosely obeys my logic? I mean, it takes a few seconds to switch the LED on or off, doesn't do it immediately.

My second but related sketch is:

void setup() {
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
pinMode(8, INPUT);

}

void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(8) == HIGH) {
  digitalWrite(LED_BUILTIN, HIGH);
} else {
  digitalWrite(LED_BUILTIN, LOW);
}
}

Again i plug the jumper wire from 3.3v into pin 8 and the LED turns on. But when i unplug it from 8 it takes around 5 seconds to turn off. How come it isn't immediate?

Thanks

Please read the forum guide in the sticky post at the top of the forum section. This will tell you how to post code correctly so that emojis do not appear in your code!

But it sounds like your inputs are floating, which will cause unexpected behaviour. Do you have pull-down resistors on the input pins? Why are you using a jumper wire from 3.3V? Uno uses 5V logic, so 3.3V will probably read OK as HIGH, but why take the risk?

The behaviour seems the same using the 5v. I have no resistors involved. So do i need to try a 10k resistor on pin 8 (if testing just the second sketch)?

Noted about the posting of code.

Many thanks!

Noted? Best if you could fix the post please.

Yes, I suspect 10K pull-downs on all inputs that your sketch reads will make the circuit behave. Or use the internal pull-ups and connect your jumper wire to ground instead of 5V. If you do that, change your code to look for LOW rather than HIGH.

“PS (8) should read as (eight) - emojis have sabotaged me!“

No, you have sabotaged yourself.

Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

FYI

After some research I found that when you initialise an input pin on Arduino you can also connect its built in pull_up resistor (there is no pull_down option).

So I changed my code to the below and moved the jumper wire to come from GND rather than 5v. This now solves the 'floating' input issue I was experiencing.

As i understand it this has pulled up the value on that pin so it reads HIGH when no ground is connected. So I have flipped my if statement to achieve the desired result. The LED now responds instantly when the wire from GND is connected or disconnected.

void setup() {
  // put your setup code here, to run once:
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  pinMode(8, INPUT_PULLUP);

}

void loop() {
  if(digitalRead(8) == HIGH) {
    digitalWrite(LED_BUILTIN, LOW);
  } else {
    digitalWrite(LED_BUILTIN, HIGH);
  }
}

Well done (and +1 karma for fixing the code tags in your post.)

Slowreader87:
there is no pull_down option

Actually there is INPUT_PULLDOWN, but only on types of Arduino with more modern chips, for example Arduino Zero/M0, MKR1000, Nano 33 IoT, ESP8266/32. But in general you are right, none of the common/traditional Arduino, for example based on AVR chips like Uno, have that feature.

To make your code more portable between Arduino types, and for safety reasons (e.g. avoiding accidental short-circuits) stick to INPUT_PULLUP if you have the choice.

That’s good to know. Thanks

PaulRB:
To make your code more portable between Arduino types, and for safety reasons (e.g. avoiding accidental short-circuits) stick to INPUT_PULLUP if you have the choice.

The reason regarding safety is that by having all switches go to ground, you avoid taking the 5 V supply from the microcontroller out to the switches where it would be at some risk of shorting to ground - not at all desirable even if unlikely. :astonished:

It just makes sense. If switches connect to ground the worst a short circuit can do is to appear as if the switch is closed.