import joystick
The joystick module reads common two-axis analog joystick boards with VRx, VRy, and an optional push switch. The native firmware samples two ADC-capable GPIOs, auto-centers the stick on begin, and returns both raw ADC values and normalized axis values from -1.0 through 1.0. Use ``joystick.Joystick(...)`` to create independent joystick objects when a board has more than one stick. Use ADC1 pins when your script also uses WiFi, especially on ESP32-class boards, because ADC2 can time out while WiFi is active.
Quick example
import joystick
import system
left = joystick.Joystick(x_pin=34, y_pin=35, button_pin=32, deadzone=0.10)
right = joystick.Joystick(x_pin=36, y_pin=39)
def poll_stick():
state = left.read()
if state["ok"]:
print(state["direction"], state["x"], state["y"], state["pressed"])
system.schedule("stick", 50, 50, -1, poll_stick)
import joystick
stick = joystick.Joystick(34, 35)
stick.calibrate()
print(stick.direction())
importjoystick
Analog joystick module.
The joystick module reads common two-axis analog joystick boards with VRx, VRy, and an optional push switch. The native firmware samples two ADC-capable GPIOs, auto-centers the stick on begin, and returns both raw ADC values and normalized axis values from -1.0 through 1.0. Use ``joystick.Joystick(...)`` to create independent joystick objects when a board has more than one stick. Use ADC1 pins when your script also uses WiFi, especially on ESP32-class boards, because ADC2 can time out while WiFi is active.
joystick.Joystick(x_pin: int, y_pin: int, button_pin: int = None, deadzone: float = None, samples: int = None, invert_x: bool = None, invert_y: bool = None, button_active_low: bool = None, attenuation: int = None) -> joystick_device
joystick.Joystick(x_pin: int, y_pin: int, button_pin: int = None, deadzone: float = None, samples: int = None, invert_x: bool = None, invert_y: bool = None, button_active_low: bool = None, attenuation: int = None) -> joystick_deviceCreate an independent analog joystick object.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
x_pin |
int |
positional or keyword | Yes | ADC-capable GPIO connected to the joystick X/VRx output. |
y_pin |
int |
positional or keyword | Yes | ADC-capable GPIO connected to the joystick Y/VRy output. |
button_pin |
int |
positional or keyword | No | Optional GPIO connected to the push switch. Defaults to no button. |
deadzone |
float |
positional or keyword | No | Optional normalized deadzone from 0.0 through 0.95. Defaults to 0.08. |
samples |
int |
positional or keyword | No | Optional number of ADC samples per read. Defaults to 8. |
invert_x |
bool |
positional or keyword | No | Optional boolean to reverse the X axis. |
invert_y |
bool |
positional or keyword | No | Optional boolean to reverse the Y axis. |
button_active_low |
bool |
positional or keyword | No | Optional boolean. Defaults to True for common joystick boards where the switch connects to ground when pressed. |
attenuation |
int |
positional or keyword | No | Optional ADC attenuation in dB. Use 0, 2, 6, or 12. Defaults to 12. |
joystick_device joystick_device object.
joystick.Joystick().calibrate(samples=64) -> dict
joystick.Joystick().calibrate(samples=64) -> dictSet the current stick position as center.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
samples |
int |
positional or keyword | No | Optional value. Defaults to 64. |
joystick.Joystick().read(samples=None) -> dict
joystick.Joystick().read(samples=None) -> dictRead normalized axes, direction, and button.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
samples |
int |
positional or keyword | No | Optional value. Defaults to None. |
joystick.Joystick().raw(samples=None) -> dict
joystick.Joystick().raw(samples=None) -> dictRead raw ADC values.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
samples |
int |
positional or keyword | No | Optional value. Defaults to None. |
joystick.Joystick().x(samples=None) -> float
joystick.Joystick().x(samples=None) -> floatRead normalized X position.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
samples |
int |
positional or keyword | No | Optional value. Defaults to None. |
joystick.Joystick().y(samples=None) -> float
joystick.Joystick().y(samples=None) -> floatRead normalized Y position.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
samples |
int |
positional or keyword | No | Optional value. Defaults to None. |
joystick.Joystick().direction(samples=None) -> str
joystick.Joystick().direction(samples=None) -> strRead direction text.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
samples |
int |
positional or keyword | No | Optional value. Defaults to None. |
joystick.Joystick().pressed() -> bool
joystick.Joystick().pressed() -> boolRead the optional push switch.
joystick.Joystick().deadzone(value=None) -> float
joystick.Joystick().deadzone(value=None) -> floatGet or set this stick's deadzone.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
value |
any |
positional or keyword | No | Optional value. Defaults to None. |
joystick.Joystick().info() -> dict
joystick.Joystick().info() -> dictReturn this stick's configuration and state.
joystick.Joystick().close() -> bool
joystick.Joystick().close() -> boolClose this joystick object.
joystick.begin(x_pin: int, y_pin: int, button_pin: int = None, deadzone: float = None, samples: int = None, invert_x: bool = None, invert_y: bool = None, button_active_low: bool = None, attenuation: int = None) -> bool
joystick.begin(x_pin: int, y_pin: int, button_pin: int = None, deadzone: float = None, samples: int = None, invert_x: bool = None, invert_y: bool = None, button_active_low: bool = None, attenuation: int = None) -> boolInitialize an analog joystick.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
x_pin |
int |
positional or keyword | Yes | ADC-capable GPIO connected to the joystick X/VRx output. |
y_pin |
int |
positional or keyword | Yes | ADC-capable GPIO connected to the joystick Y/VRy output. |
button_pin |
int |
positional or keyword | No | Optional GPIO connected to the push switch. Defaults to no button. |
deadzone |
float |
positional or keyword | No | Optional normalized deadzone from 0.0 through 0.95. Defaults to 0.08. |
samples |
int |
positional or keyword | No | Optional number of ADC samples per read. Defaults to 8. |
invert_x |
bool |
positional or keyword | No | Optional boolean to reverse the X axis. |
invert_y |
bool |
positional or keyword | No | Optional boolean to reverse the Y axis. |
button_active_low |
bool |
positional or keyword | No | Optional boolean. Defaults to True for common joystick boards where the switch connects to ground when pressed. |
attenuation |
int |
positional or keyword | No | Optional ADC attenuation in dB. Use 0, 2, 6, or 12. Defaults to 12. |
bool True when both axis pins resolve to ADC channels and the optional button is configured.
joystick.calibrate(samples: int = None) -> dict
joystick.calibrate(samples: int = None) -> dictSet the current stick position as center.
Keep the joystick released while calling this method. The module averages a short window and stores the result as ``center_x`` and ``center_y``.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
samples |
int |
positional or keyword | No | Optional number of ADC samples to average. Defaults to 64. |
dict Dict with ``ok``, ``x_pin``, ``y_pin``, stored ``center_x``, ``center_y``, and ``samples``.
joystick.read(samples: int = None) -> dict
joystick.read(samples: int = None) -> dictRead joystick axis, direction, and button state.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
samples |
int |
positional or keyword | No | Optional number of ADC samples for this read only. |
dict Dict with ``ok``. When ``ok`` is true it includes ``x_pin``, ``y_pin``, ``button_pin``, ``raw_x``, ``raw_y``, ``center_x``, ``center_y``, normalized ``x`` and ``y`` values, ``magnitude``, ``angle``, ``direction``, ``pressed``, ``deadzone``, and ``samples``. Direction is one of ``center``, ``up``, ``down``, ``left``, ``right``, ``up_left``, ``up_right``, ``down_left``, or ``down_right``. When ``ok`` is false it includes ``error`` and ``hint``.
joystick.raw(samples: int = None) -> dict
joystick.raw(samples: int = None) -> dictRead raw ADC values from both joystick axes.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
samples |
int |
positional or keyword | No | Optional number of ADC samples for this read only. |
dict Dict with ``ok``, ``raw_x``, ``raw_y``, ``pressed``, and ``samples``.
joystick.x(samples: int = None) -> float
joystick.x(samples: int = None) -> floatRead normalized X-axis position.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
samples |
int |
positional or keyword | No | Optional number of ADC samples for this read only. |
float Float from -1.0 through 1.0 after center calibration and deadzone.
joystick.y(samples: int = None) -> float
joystick.y(samples: int = None) -> floatRead normalized Y-axis position.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
samples |
int |
positional or keyword | No | Optional number of ADC samples for this read only. |
float Float from -1.0 through 1.0 after center calibration and deadzone.
joystick.direction(samples: int = None) -> str
joystick.direction(samples: int = None) -> strRead joystick direction as text.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
samples |
int |
positional or keyword | No | Optional number of ADC samples for this read only. |
str String direction such as ``center``, ``left``, ``right``, ``up``, ``down``, or a diagonal direction.
joystick.pressed() -> bool
joystick.pressed() -> boolRead the optional joystick push switch.
bool True when the configured button pin is pressed.
joystick.deadzone(value: float = None) -> float
joystick.deadzone(value: float = None) -> floatGet or set the normalized joystick deadzone.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
value |
float |
positional or keyword | No | Optional deadzone from 0.0 through 0.95. |
float Float current deadzone.
joystick.info() -> dict
joystick.info() -> dictReturn joystick module state.
dict Dict with initialization state, calibration state, pins, centers, deadzone, sampling, inversion flags, button polarity, ADC units, ADC channels, attenuation, last error, and hint.
joystick.reset() -> bool
joystick.reset() -> boolRelease ADC state and clear joystick configuration.
bool True when module state is cleared.