Read a 2-axis analog joystick’s X position, Y position, and button press, and print all three to the Shell.
A joystick module is really two rotary potentiometers mounted at 90° to each other, plus a push button underneath, all on one PCB.

| Pin | Function |
|---|---|
| GND | Ground |
| +5V | Power |
| VRX | X-axis analog output |
| VRY | Y-axis analog output |
| SW | Push-button digital output (Z axis) |
Moving the stick along the X or Y axis turns one of the internal potentiometers, producing an analog voltage on VRX or VRY — so reading position requires the ADC, just like Read the Voltage of a Potentiometer. Pressing the stick down (the Z axis) closes a simple switch, which is a digital signal — read with a plain GPIO pin, the same as a push button.


Disconnect all power before building the circuit. Reconnect once verified.

Connections:

File: 03_sensors/code/Joystick.py
from machine import ADC,Pin
import time
xVal=ADC(Pin(14))
yVal=ADC(Pin(13))
zVal=Pin(12,Pin.IN,Pin.PULL_UP)
xVal.atten(ADC.ATTN_11DB)
yVal.atten(ADC.ATTN_11DB)
xVal.width(ADC.WIDTH_12BIT)
yVal.width(ADC.WIDTH_12BIT)
try:
while True:
print("X,Y,Z:",xVal.read(),",",yVal.read(),",",zVal.value())
time.sleep(1)
except:
xVal.deinit()
yVal.deinit()
03_sensors/code/.Joystick.py.xVal=ADC(Pin(14))
yVal=ADC(Pin(13))
zVal=Pin(12,Pin.IN,Pin.PULL_UP)
X and Y are analog (ADC), since they come from potentiometers. Z is a plain digital input with the internal pull-up enabled, since it’s just a button — pressing it pulls GPIO12 LOW, same logic as Button & LED.
print("X,Y,Z:",xVal.read(),",",yVal.read(),",",zVal.value())
xVal.read() and yVal.read() return 0–4095 ADC values; zVal.value() returns 0 (pressed) or 1 (released).
See Class ADC for the full API reference.
Adapted from Python_Tutorial.pdf Project 13.1