# buttonstate == LOW vs buttonstate == HIGH

**URL:** <https://forum.arduino.cc/t/buttonstate-low-vs-buttonstate-high/159607>\
**Category:** Programming\
**Created:** [April 29, 2013, 1:59am UTC](https://forum.arduino.cc/t/buttonstate-low-vs-buttonstate-high/159607 "2013-04-29T01:59:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 29, 2013, 1:59am UTC](https://forum.arduino.cc/t/buttonstate-low-vs-buttonstate-high/159607/1 "2013-04-29T01:59:24Z")

</div>

How come in the arduino examples, when a button state is high, it means that the button is being pressed. But when I do my own programming, it's the other way around, and I always set it to low?

```cpp
 if (buttonState == LOW) {     
    // turn LED on:    
digitalWrite(red, LOW);
  digitalWrite(green, LOW);
  digitalWrite(yellow, LOW);
  delay(1000);
  digitalWrite(red, HIGH);
  delay(500);
  digitalWrite(red, LOW);
  digitalWrite(green, HIGH);
  delay(100);
  digitalWrite(green, LOW);
  digitalWrite(yellow, HIGH);
  delay(1000);
  digitalWrite(yellow, LOW);
  }

```

just a quick example. and it works like that. when I put it as high, the lights flash until I press the button.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 29, 2013, 2:03am UTC](https://forum.arduino.cc/t/buttonstate-low-vs-buttonstate-high/159607/2 "2013-04-29T02:03:29Z")

</div>

how have you wired up the button? 5v \> button \> pin?

don't forget to add a pull down resistor (10k from gnd to the digital pin side will do it)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 29, 2013, 2:12am UTC](https://forum.arduino.cc/t/buttonstate-low-vs-buttonstate-high/159607/3 "2013-04-29T02:12:07Z")

</div>

it's on a breadboard and it's a 2 pin push button. one pin is in a 5 volt power rail, and then a resistor is connected to the ground rail, and then in the same row as the button, which is then in the same row as a pin of the chip... sorry if it's confusing.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 29, 2013, 3:24am UTC](https://forum.arduino.cc/t/buttonstate-low-vs-buttonstate-high/159607/4 "2013-04-29T03:24:38Z")

</div>

try a picture or draw the circuit...

---

<div class="post-metadata">

**Author:** ![Sembazuru](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sembazuru/32/215710_2.png) [@Sembazuru](https://forum.arduino.cc/u/Sembazuru)\
**Post date:** [April 29, 2013, 4:49am UTC](https://forum.arduino.cc/t/buttonstate-low-vs-buttonstate-high/159607/6 "2013-04-29T04:49:42Z")

</div>

> Delta\_G:  
> Buttons can be connected either to be active low or active high where being pressed makes the button give low or high. Either way works, but you have to take care to note which way you have it in the code. Active low buttons are much easier to use because the Arduino has built in pull-up resistors. Active high buttons are easier for beginners to understand for some unknown reason. So the tutorials get written using active high buttons even though you will usually use active low if you know what's up.

Active high signals are probably easier for beginners to think about because one has to think of active low signals have to be thought of kinda backwards. This is because HIGH == TRUE and if statements are looking to act on a TRUE. It is a little more complex to think about having one's if statements act on a !LOW. (Other programming syntaxes would have this as ~LOW, or /LOW, or just simply Not LOW.)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 29, 2013, 5:27am UTC](https://forum.arduino.cc/t/buttonstate-low-vs-buttonstate-high/159607/7 "2013-04-29T05:27:07Z")

</div>

> Sembazuru:  
> This is because HIGH == TRUE and if statements are looking to act on a TRUE. It is a little more complex to think about having one's if statements act on a !LOW. (Other programming syntaxes would have this as ~LOW, or /LOW, or just simply Not LOW.)

Just adding one more statement: ~LOW would equal -1, !LOW would equal 1. So don't use ~LOW in this case. ~ is the bit operator NOT, and ! is the logical NOT.

---

<div class="post-metadata">

**Author:** ![Sembazuru](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sembazuru/32/215710_2.png) [@Sembazuru](https://forum.arduino.cc/u/Sembazuru)\
**Post date:** [April 29, 2013, 8:14pm UTC](https://forum.arduino.cc/t/buttonstate-low-vs-buttonstate-high/159607/8 "2013-04-29T20:14:10Z")

</div>

> dkl65:
> 
> > Sembazuru:  
> > This is because HIGH == TRUE and if statements are looking to act on a TRUE. It is a little more complex to think about having one's if statements act on a !LOW. (Other programming syntaxes would have this as ~LOW, or /LOW, or just simply Not LOW.)
> 
> Just adding one more statement: ~LOW would equal -1, !LOW would equal 1. So don't use ~LOW in this case. ~ is the bit operator NOT, and ! is the logical NOT.

True, in the context of C and it's derived languages (of which the Arduino is a part), ~LOW would be inappropriate. But as I said "Other programming syntaxes". I've seen other programming languages that use the tilde for boolean NOT. (Probably who ever thought up that syntax was probably thinking that a letter with the tilde accent looks close enough to a bar above the letter, especially with the limited character set of original ASCII...)

Edit: Actually let me restate... Other languages don't differentiate between boolean NOT and bit NOT. If the boolean datatype was actually 1 bit, there probably wouldn't be the need for ! as not, but that is neither here nor there. For this thread, one should only consider !HIGH == LOW. 😉

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 8:11pm UTC](https://forum.arduino.cc/t/buttonstate-low-vs-buttonstate-high/159607/9 "2021-05-05T20:11:19Z")

</div>

Closed on May 5, 2021, 8:11pm UTC


