slim joystick/The stick/04. The key under the stick
The stick · 04 of 11

The key under the stick

Push the stick straight down and a switch under it joins KEY to 3V3. A 10 kΩ resistor on the board, R1, holds KEY at 0 V the rest of the time. So KEY reads LOW at rest and HIGH while clicked, the opposite of most buttons, and the pin is INPUT, never INPUT_PULLUP.

A switch and a pull-down

The stick is also a button. Push it straight down, without tilting, and it clicks: a small switch under the housing closes and joins KEY to 3V3. Let it up and the switch opens.

An open switch leaves KEY connected to nothing, and a pin connected to nothing reads whatever the air around it suggests. So the board adds R1, a 10 kΩ resistor from KEY to GND at the bottom left, by the stick's legs. With the switch open R1 holds KEY at 0 V; with it closed the switch wins, because it is a direct wire to 3V3 and R1 is 10 kΩ.

The key under the stick
The stick
pinMode
Your board
KEY at
0.00 V
Reads
LOW
Pull-down
10 kΩ, R1
Let go, the switch is open and R1 holds KEY at 0 V: LOW. That is a pull-down, the opposite of the pull-up most buttons use, so a click is LOW to HIGH, not HIGH to LOW. INPUT is right: the board already has its resistor.

A resistor that holds a line at 0 V until something lifts it is a pull-down. Most buttons are wired the other way, with a pull-up and a switch to GND, so they read HIGH at rest and LOW when pressed. KEY is LOW at rest and HIGH while clicked. Test for HIGH.

INPUT, not INPUT_PULLUP

Because the board already has its resistor, the pin needs none of its own:

pinMode(KEY_PIN, INPUT);        // R1 on the board pulls it down

INPUT_PULLUP switches on a resistor of tens of kilohms inside the chip, from the pin up to the chip's supply. Against R1 it makes a divider, and at rest KEY no longer sits at 0 V but some way up towards the line where LOW becomes HIGH. It will usually still read LOW, but with less margin than it should have, and on an Uno the pull-up goes to 5 V, so every click also feeds a little current from 5 V into the board's 3V3. Neither is worth it.

3.3 V on an Uno is still HIGH

On an Uno, KEY's HIGH is 3.3 V from the header, not 5 V. The Uno's chip, an ATmega328P, counts anything above 0.6 × its 5 V supply, 3 V, as HIGH, so 3.3 V clears it. It is closer to the line than a 5 V signal would be, which is one more reason to leave the pull-up off.

It bounces

The switch is a pair of metal contacts, and metal contacts bounce: for a moment after they meet they open and close several times. A loop that reads KEY thousands of times a second sees each bounce as a click. Count a click when KEY goes from LOW to HIGH, and look again only after a pause; the push button's book shows the pattern.

When it does not work

KEY reads HIGH when I am not touching it.

Check the sketch's pinMode. This board already has its own pull-down, R1, so the pin should be INPUT. If KEY is not wired at all, or wired to the wrong pin, an INPUT floats and can read anything; count from the square pad and check the pin number.

My sketch sees the click the wrong way round.

Most button examples use INPUT_PULLUP and treat LOW as pressed. This board is the other way: LOW at rest, HIGH while the stick is pushed down. Test for HIGH.

One click counts as two or three.

The switch's contacts bounce for a moment as they close and open, and a fast loop sees each bounce. Count a click only when KEY goes from LOW to HIGH and then wait some milliseconds before looking again, or read it once per frame the way the steering sketch does.

Tilting the stick sometimes clicks it.

Pushing hard at the end of the travel can press the switch too. Tilt with a lighter thumb, or ignore KEY while either axis is far off centre if your project cannot tell the two apart.

Where this goes next

How six buttons share one analog pin.

A ladder of resistors

Edit this page — content/books/slim-joystick/the-key-under-the-stick.mdx

Community

Questions about this product

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

Ask a question ↗

Slim Joystick

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