five-direction joystick/How it works/06. Telling them apart
How it works · 06 of 10

Telling them apart

Six levels on one pin: five pushes and nothing. A sketch draws a line halfway between each pair of neighbours and asks which lines a reading is above. In thousandths of VCC the lines are 722, 359, 227, 146 and 56. The closest pair, up and the press, are 72 counts apart on an Uno and 233 mV apart at 3.3 V; 1 % resistors move a level by a few counts at most.

Six levels, five lines

Telling them apart
Your board
Closest pair
up, press: 72 apart
In volts
354 mV apart
1 % resistors move a level
25 mV at most
The Uno measures against its own supply, which is VCC, so a level is the same fraction of 1023 whatever that supply really is: 1023, 455, 279, 186, 114 and 0. Up and the press are the closest pair, 72 counts apart; a line halfway leaves 36 counts either side.

SIGNAL can sit at six levels, and a sketch has to say which. The simple way is to draw a line halfway between each pair of neighbours. A reading above a line and below the next one up is that push.

In thousandths of VCC:

Aboveand belowis
722right
359722down
227359left
146227press
56146up
56nothing

The sketches carry exactly this list, highest first, and stop at the first line the reading is above:

const int LIMIT[] = {722, 359, 227, 146, 56};

One number for every board

An Uno and a Pico count from 0 to 1023 against their own supply, which is VCC, so the count over 1023 is already a fraction of VCC. An ESP32 and an ESP32-S3 count to 4095 against a reference inside the chip; there the sketch asks analogReadMilliVolts for calibrated millivolts and divides by 3300. Either way the sketch ends up with thousandths of VCC, and one list of lines fits all four.

The ESP32's ADC reads nothing below about 0.1 V and tops out near 3.1 V. Neither end matters here: up, the lowest push, is 0.37 V, and right's full 3.3 V reads as the top of the scale, far over its line at 722.

How much room there is

The closest pair is up and the press: a ninth and two elevenths of VCC, 0.071 of it apart. That is 72 counts on an Uno and 233 mV on a 3.3 V board, and the line halfway leaves half of it either side.

The resistors are 1 % parts. Worked through every combination of all six at the ends of their tolerance, no level moves by more than about half a percent of VCC: 16 mV at 3.3 V, 5 counts on an Uno. Noise on a breadboard is a few counts more. Both are small against 36 counts of room, which is why no calibration step is needed.

When it does not work

Up and the press get mixed up.

They are the closest pair, a ninth and two elevenths of VCC. Check that VCC is on your board's logic voltage and that the sketch's lines are 146 between them; then print the raw reading while you hold each. On an ESP32, check the sketch divides millivolts by 3300 and VCC is on 3V3, not 5V.

My numbers are a few counts off the book's.

Expected. The resistors are 1 %, which can move a level by up to about half a percent of VCC, and an ADC has a few counts of noise. The lines sit halfway between levels, 36 counts from either side on an Uno, so a few counts change nothing.

Why thousandths and not raw counts?

So one table works on every board. An Uno and a Pico count to 1023 against their own supply, an ESP32 counts to 4095 against its own reference. Turned into thousandths of VCC, the same five lines fit all four.

Where this goes next

What SIGNAL reads when a thumb closes two contacts.

Two at once →

Edit this page — content/books/five-direction-joystick/telling-them-apart.mdx

Community

Questions about this product

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

Ask a question ↗

Five-Direction 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 →