dual axis joystick/The stick/05. The click and two lights
The stick · 05 of 10

The click and two lights

Press the stick straight down and a switch joins SW to VCC. R10, a resistor on the board, holds SW at 0 V the rest of the time. So SW reads LOW at rest and HIGH while pressed, the pin is INPUT, never INPUT_PULLUP, and the blue SW light shows each click with no sketch at all.

A switch and a pull-down

The stick is also a button. Push it straight down, without tilting, and it clicks: the switch on the frame closes and joins SW to VCC. Let it up and the switch opens.

An open switch would leave SW connected to nothing, and a pin connected to nothing reads whatever the air around it suggests. So the board adds R10, the top part in the column up the left edge, from SW to GND. With the switch open R10 holds SW at 0 V; with it closed the switch wins, because it is a direct wire to VCC and R10 is a resistor.

The click and two lights
The stick
pinMode
Your board
SW at
0.00 V
Reads
LOW
R10
5.1 kΩ
Let go, the switch is open and R10 holds SW at 0 V: LOW, and the SW light is dark. A pull-down, the opposite of the pull-up most buttons use, so a click is LOW to HIGH. INPUT is right: the board already has its resistor. The red PWR light stays on either way.

Press the stick in the figure and follow the current: through the switch, down R10 to GND, and through R9 and the SW light, which comes on. Let go, and SW falls to 0 V and the light goes out. The PWR light, on its own resistor from VCC, stays on either way.

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. SW is LOW at rest and HIGH while pressed; the back of the board prints SWITCH ACTIVE HIGH and PULL-DOWN BY RESISTOR to say so.

INPUT, not INPUT_PULLUP

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

pinMode(SW_PIN, INPUT);         // R10 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 R10 it makes a divider, and at rest SW sits a little above 0 V instead of on it. It still reads LOW, so nothing looks wrong, but it buys nothing and spends margin.

Two lights

PWR is R1 and a red LED in series from VCC to GND. It is on whenever VCC and GND are connected the right way round, sketch or no sketch. Dark means no power, or VCC and GND swapped.

SW is R9 and a blue LED in series from SW to GND, so it lights only while SW is at VCC: while the stick is pressed. It is the quickest way to split a fault in two. Light on and the sketch disagrees: look at the wire and the code. Light off: look at the stick and the supply.

What the resistors cost

R1, R9 and R10 are all 5.1 kΩ. While the stick is held, R10 takes VCC over 5.1 kΩ: about 0.65 mA on 3V3 and about 0.98 mA on 5V. It comes from VCC through the switch, never from your board's pin.

Each light takes what is left of VCC after its LED's own drop, over 5.1 kΩ. A blue LED drops more than a red one: about 2.6 V against about 1.8 V at these tiny currents, lower than the 2.6–3.1 V and 1.8–2.4 V the datasheets give at their test currents. Worked out, not measured, that is about 0.29 mA for the red PWR light on 3V3 and 0.63 mA on 5V, and about 0.14 mA for the blue SW light on 3V3 and 0.47 mA on 5V. The PWR light draws it all the time; the SW light only while the stick is held.

So on a 3.3 V board the blue SW light is dim, a glow that is easier to see in shade. That is the resistor and the blue LED's higher drop, not a fault: the switch is closing and SW is at VCC just the same.

When it does not work

SW reads HIGH when I am not touching it.

Check the SW light first. If it is dark, the switch is open and R10 is holding the line LOW, so the pin reading HIGH is not on SW, or not wired at all, or has a pull-up switched on. Set it to INPUT and count from the square pad.

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 pressed, as the back of the board says. Test for HIGH.

The SW light comes on but the sketch sees nothing.

Then the board is doing its part: the switch closed and SW went to VCC. The fault is between the header and the sketch. Check which pin SW is wired to against the number in the sketch.

The blue SW light is barely visible on my 3.3 V board.

Normal. It gets about 0.14 mA there, against about 0.47 mA on 5 V, because a blue LED drops about 2.6 V of the 3.3 V and the 5.1 kΩ resistor sets the rest. Look at it in shade, or trust the serial monitor: if SW prints 1 while pressed, the switch is fine.

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 SW while either axis is far off centre if your project cannot tell the two apart.

Where this goes next

Five wires and a sketch that prints X, Y and SW.

The first read

Edit this page — content/books/dual-axis-joystick/the-click-and-two-lights.mdx

Community

Questions about this product

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

Ask a question ↗

Dual Axis 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