When use INPUT_PULLUP

Hello! In my project I want to use 2 push buttons, very standard, when is pressed and realeased something will happen (standard toggle).
The majority of tutorials uses 5v and ground to detect high (or low depending how you connect) when pressed: https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button

However there are the ones which uses INPUT_PULLUP, happens the same thing, but only connects to ground without any resistor: https://www.arduino.cc/en/Tutorial/BuiltInExamples/InputPullupSerial

I understand the concept, but I don't know the benefits and downsides or if doesn't make much difference between those two. I feel like the first one is more "correct", but I'm not sure. Which one is better to use?

Less wiring and components. (good)
Minimising the chance of shorting a pin to +Ve when it’s not intended.(bad)

You can use one pin for multiple roles. If the inputs are parallel, there is no conflict - no matter how many are closed.
Think about the same idea with open-collector outputs, they can be ganged up until stray capacitance becomes a problem.

Does this help: Buttons and other electro-mechanical inputs (introduction) - #2 by PerryBebbington - General Electronics - Arduino Forum ?
Scroll down to Why is it usual to pull up rather than down?
Or better still read the whole tutorial.

I feel like the first one is more "correct"

Yes ,"active high" seems more logical. I'd guess that's the main reason the Digital Read Serial Example uses a pull-down.

But the electronics & software don't care and it's super-easy to reverse the logic in software... It's just as easy to make an LED turn-on when the input goes low as it is to do the opposite.

If I'm just using a switch into the Arduino I'll use the built-in pull-ups, simply because it's easier. (The comments in my code can remind me how the logic works.)

There may be different circuitry where a pull-down is easier but even when there is no built-in resistor I see pull-ups used more than 90% of the time in real-world circuits/products.

Open-collector and open-drain outputs require a pull-up. (The Arduino outputs are driven high or low and they can source or sink current so they don't need a pull-up or pull-down resistor.)

To me, active LOW seems natural, because I push the button. So pull-up makes sense.

guarda-chuva:
I understand the concept, but I don't know the benefits and downsides

Benefit: Less external components you need to wire up for you switches. Also the thing in Perry's post.
Downsides: None.

I feel like the first one is more "correct", but I'm not sure. Which one is better to use?

It's exactly as correct as always driving on the right-hand side of the road. :slight_smile:

Which is to say, it is not something that is objectively correct, but merely an arbitrary convention that you are establishing in your head. Similarly, there is nothing about a particular voltage level that inherently associates it with ON or OFF. In fact, a massively huge number of chips use the "active low" convention for their inputs and outputs.

Sometimes different circuits will use different conventions. One project I made is a persistence of vision display that uses an active low Hall sensor (output goes LOW when the magnet is close) and LEDs wired to be active high (turn on when the output is HIGH). Just like you define names for pin numbers, you can define names for levels and other things you need help keeping track of, like this:

const uint8_t HallEffectPin = 10;
const uint8_t HallActiveLevel = LOW;
const uint8_t LEDPin[] = {2, 3, 4, 5, 6, 7, 8};
const uint8_t LEDPins = sizeof(LEDPin) / sizeof(LEDPin[0]);
const uint8_t LEDActiveLevel = HIGH;

So when I want to check if my Hall sensor has a magnet near it, digitalRead(HallEffectPin)==HallActiveLevel is much more informative to read than digitalRead(HallEffectPin)==LOW. And if I want to turn off an LED, I don't have to look up which way they're connected, I can just do digitalWrite(LEDPin[pin], !LEDActiveLevel). With this coding practice, you can easily use either convention in your code, and even use multiple conventions for different attached circuits when necessary.

lesept:
To me, active LOW seems natural, because I push the button. So pull-up makes sense.

This is not the right way to think about it either. It is best to decouple the two concepts in your mind so that you are comfortable using either convention when necessary.

Not relevant in hobby context but if a resistor costs $0.01 (just an example) and you do a bulk production of a million units, an internal pull-up (or pull-down) will save you $10,000.

Jiggy-Ninja:
It's exactly as correct as always driving on the right-hand side of the road. :slight_smile:

Oops

Now if you'd said "It's exactly as correct as always driving on the right side of the road", that may have been slightly less contentious. :smiley:

TheMemberFormerlyKnownAsAWOL:
Oops

Now if you'd said "It's exactly as correct as always driving on the right side of the road", that may have been slightly less contentious. :smiley:

It seems you missed my point. I deliberately added -hand to my sentence to avoid any ambiguity.

Jiggy-Ninja:
It seems you missed my point. I deliberately added -hand to my sentence to avoid any ambiguity.

But if I always drove on the right-hand side of the road, I'd collide with people driving on the right (aka correct) side of the road (which is left, obviously), just like 30%+ of the planet's population.

TheMemberFormerlyKnownAsAWOL:
But if I always drove on the right-hand side of the road, I'd collide with people driving on the right (aka correct) side of the road (which is left, obviously), just like 30%+ of the planet's population.

That's...exactly the point I was making.