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
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:
| Above | and below | is |
|---|---|---|
| 722 | right | |
| 359 | 722 | down |
| 227 | 359 | left |
| 146 | 227 | press |
| 56 | 146 | up |
| 56 | nothing |
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
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.
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.
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.
Edit this page — content/books/five-direction-joystick/telling-them-apart.mdx
Questions about this product
See what other owners have asked, and read their solutions.
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.