mechanical key/How it works/05. Pressed reads HIGH
How it works · 05 of 9

Pressed reads HIGH

A 10 kΩ resistor from BUTTON to GND holds the pin LOW while the key is up; the switch pulls it to VCC when the key goes down. So a press reads HIGH. The page this replaced said LOW, and its sketch was backwards.

Up is LOW

With the key up, the switch is open and BUTTON is connected to one thing on the board: R7, 10 kΩ, to GND. No current flows through it, so there is no voltage across it, and BUTTON sits at GND: 0 V, LOW.

Without that resistor BUTTON would be joined to nothing while the key is up. A pin like that floats: it picks up whatever is nearby and reads HIGH and LOW at random. The resistor is what makes "up" a definite answer. It is called a pull-down because it pulls the line down to GND when nothing else is driving it.

Down is HIGH

Pressed reads HIGH
Your board
The key
The sketch calls pressed
BUTTON
3.30 V
Through 10 kΩ
0.33 mA
Sketch is
right
The switch joins BUTTON to VCC, so the pin reads HIGH at 3.30 V. The 10 kΩ is still there and takes 0.33 mA while you hold the key; that is the whole cost of a press.

Press the key and the switch joins BUTTON to VCC. The resistor is still there, but a switch is a far stronger connection than 10 kΩ, so BUTTON sits at VCC: HIGH. The only thing that changes is a small current through R7, VCC divided by 10 kΩ, for as long as you hold the key.

So in a sketch:

pinMode(BUTTON_PIN, INPUT);        // the block has its own pull-down
if (digitalRead(BUTTON_PIN) == HIGH) {
  // the key is down
}

The old page had it the other way

The page this handbook replaces said the key "outputs LOW when pressed", and its sketch set INPUT_PULLUP and treated LOW as pressed. On this board that is backwards: it reports a press whenever the key is up. Pick "LOW (the old page)" in the figure to watch it happen. Many buttons are wired the pull-up way, which is why the mistake is so common; this one, like the TK04, is not.

When it does not work

Should the pin be INPUT or INPUT_PULLUP?

INPUT. The block already has its resistor, pulling down. INPUT_PULLUP adds the chip's own resistor pulling the other way, so with the key up the two fight and BUTTON sits somewhat above 0 V instead of at it. It usually still reads LOW, but it is two resistors doing one job badly.

It reads HIGH even with nothing pressed.

Check that the BUTTON wire is not on a 3V3 or 5V pin, and that VCC and BUTTON are not swapped. Then check the sketch's test: a sketch written for a pull-up board treats LOW as pressed, and on this block that reads every idle moment as a press.

What does the key draw while it is held?

VCC across 10 kΩ: 0.5 mA at 5 V, 0.33 mA at 3.3 V, for as long as the key is down, and nothing while it is up. It is the only current the key half of the board ever takes.

Where this goes next

What the library sends down the WS2812 wire, and why green goes first.

A colour in 24 bits →

Edit this page — content/books/mechanical-key/pressed-reads-high.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

Mechanical Key and LED

Loading discussions…

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum →