Complete beginner: simple button and resistors / why are HIGH/LOW declared as int

Hello everyone.

I looked around in the forums but there is still something I don't understand.

I have some background with programming but I'm a complete beginner with electronics.

Question 1:

Doing various tutorials so far I've used the simple little 4-pins button (the one in the starter kit) in various ways to control some basic on/off stuff. Every time I was required to put a 10k resistor in the circuit. I have a very basic understanding of Ohm's law, and I assumed that 10k Ohms were required for the same safety reasons they are used in LEDs. But apparently this is not the case, because I found other exercises in which the button is directly connected to the 5V and GND jumpers without any resistor. I haven't been able to find any explanation for this difference.

Does the simple button require a resistor or not? What might be the reason why it is there in some exercises and not in others?

(if you lack context, consider that these are extremely basic exercises like "turn on the onboard led when button is down", "print to console when button is down" and stuff like that.)

Question 2:

In the exercises so far I see HIGH and LOW states declared as "int". But digging further it looks like they are just placeholder names for 1 and 0. It'd seem more appropriate (and cheaper memory-wise) to have them as bools, but maybe there's a reason they are always declared as int?

Thank you :slight_smile:

If it is a desire to short the power supply to ground then some resistor is required with a simple mechanical momentary push button. Some codes may be using the internal pull up/down resistors.

I use the return values from the GPIO pin sate readings as bool's. Lazy coders?

It doesn't require an external resistor if you use the built-in pull-ups, but yes, you do need a resistor (pullup or pull-down)to stop the pin floating with an open switch.

This is not an introductory tutorial- please use the flag icon to attract the attention of a mod, and ask them to move it.

That's a good question because they are pre-defined as type bool! I would have expected re-defining them to cause an error, or at least a warning...

It's OK to copy a boolean value into an int (int X = HIGH;) but you should have a good reason for doing so. This is called "casting" or "typecasting".

You can also copy an int value to a type bool but the result will be 1 for all non-zero values.

Resistors are used for many different purposes and sometimes the value is not that critical....

On a pushbutton or switch they are used as "pull-up" or "pull-down" resistors. (The ATmega chip actually has built-in pull-up resistors, about 20K I think, that can be optionally enabled.)
Unconnected inputs (like when the switch/button is off) are "floating" and undefined... The voltage is unpredictable and they can read high or low.

A pull-up resistor will pull the input high when the switch is off. The input has very-high impedance so almost no current flows, and almost any resistor value will work (1K to 10K is common). The button/switch is tied to ground so when the button is pressed the input forced to ground (low) and current flows through the resistor.

LEDs are a bit "odd" because (like all diodes) they are non-linear, which means their resistance changes with the voltage. Ohm's Law is a law of nature and it's always true but we can't use it directly on the LED because we don't know it's resistance...

The resistor and LED form a Voltage Divider where one resistor is replaced by the LED. If we are applying 5V and the LED is spec'd at 2V we know there will be 3V across the resistor.

We also know that the same current flows through the LED & series resistor so knowing we'll have 3V across the resistor, we can use Ohm's Law to calculate the required resistance for the current we want.

1 Like

[Citation needed]

Thank you everyone for the precious explanations.

So HIGH/LOW for all intents and purposes may as well be declared as bools. I was just fearing there could be some reason not immediately apparent for having those ints, but apparently there is not. I will proceed to make it a habit of declaring them as bools regardless of what the tutorials use. Overall it doesn't change much, but it seems more appropriate and consistent with what those values actually are.

Regarding the resistors... I thank you all for the help you offered, but I'm realizing I still need to learn so much more about electronics to fully understand what was explained :frowning:

Anyway this new Arduino world looks amazing and I can't wait to learn more. Thank you all again, see you around!

No. I can't see how you figure that.
If, for example, a switch is wired to an input pin with a pullup, then LOW is the state of the switch being closed.

@DVDdoug - I'm still waiting... {drums fingers impatiently}

They are constants defined in the IDE itself.

You do not declare or define them in code. And as such, it does not matter what they are. What "exercises" would these be?

Well, you did not find a button "directly connected to the 5V and GND", it connects to one or the other but best practice by far is to connect it to an input and ground and use a pull-up either in the Arduino itself or externally to 5 V.

Then I completely misunderstood :sweat_smile:

Alright I'll try to explain myself better. I tried to log variables with HIGH/LOW values and they always print either 0 or 1. I then tried to declare those exact variables as bools and it still logged only 0 or 1 (as expected) and there weren't any errors that I could notice.

So why would I declare a, say, "ledState" variable, as an int? Couldn't I just declare it as bool and save 1 byte?

Of course my concern is not so much saving that 1 byte as much as understanding the internal workings of this new platform. So put it differently: when storing HIGH/LOW values, what are the pros and cons of doing it in an int variable or in a bool variable?

My current knowledge and understanding only allow me to go as far as to say "well, I expect only 2 possible values and a bool allocates less memory so let's just use a bool". Is this a wrong way to think in this environment? And why would an int be better?

Thank you for your patience :see_no_evil:


Declare it as byte. :+1:

1 Like

As in arduino an int is 2 byte declaring as a bool might very well save 1 byte.
But why not declare as a bool? Even if it does not save memory? It makes clear that you expect only 2 possible states (true/false LOW/HIGH 0/1).

"Pushbuttons often generate spurious open/close transitions when pressed, due to mechanical and physical issues: these transitions may be read as multiple presses in a very short time fooling the program". (in

So, that´s what it makes the resistor in this case. It works as:

  1. a pull-down (if connected to GND). Arduino will read LOW untill the button is pressed; OR

  2. a pull-up (if connected to +5V). Arduino will read HIGH untill the button is pressed.

You should consider reading about INPUT-PULLUP in which could make you get rid of the physical resistor in this case (be carefull because as said before, resistors have other applications too).

I wrote this: Buttons and other electro-mechanical inputs (introduction) to explain why you need resistors with buttons, and to show some ways of reading buttons.

I was frustrated with folk new to electronics not having a single reference that explains why the resistors are needed and why you need to pay attention to bounce.

Generally, making use of what you happen to know about how something is implemented is a terrible idea. It's a great way to break your code when what you relied on changes.

In the case of digitalRead, such an issue is vanishingly likely to occur, but it is a bad habit to acquire.

Don't use HIGH/LOW. Use 1 and 0 instead. Makes more sense and takes less time to type.

I use

#define ledON 1
#define ledOFF 0

completely unambiguous


Historical accident as digitalWrite was declared to take uint8_t, and digitalRead to return int, probably inherited from the Wiring codebase.

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