❌ ❌ ❌ ❌ ❌ ❌ ✅

PlayStation Controller

controller
class PlayStationController(joystick_deadzone=10)

Use a Sony® PlayStation® controller as a sensor in your projects to control them remotely.

This has been tested on the DUALSHOCK®4 wireless controller for PlayStation®4 and the DualSense™ wireless controller for PlayStation®5, but similar gamepads may also work.

Pair the controller in the hub menu first. When your program runs, it uses the controller that is currently connected.

For tips on connectivity and pairing, see below.

Added in version 4.1.

Parameters:

joystick_deadzone (Number, %) – Joystick deadzone (0 to 100). Values below this threshold in both axes will be reported as 0 to prevent stick drift.

Raises:

OSError – If the controller is not connected.

name() → str | None

Gets the Bluetooth name of the connected controller.

Returns:

Bluetooth name of the controller, or None if it is not known.

Raises:

OSError – If the controller is not connected.

controllercenteris pressed
buttons.pressed() → set[Button]

Checks which buttons are currently pressed.

Returns:

Set of pressed buttons.

Buttons include:

  • Button.A (cross), Button.B (circle), Button.X (square), and Button.Y (triangle).

  • Button.UP, Button.DOWN, Button.LEFT, Button.RIGHT (direction pad). At most two of these can be pressed at the same time.

  • Button.LB and Button.RB (L1 and R1).

  • Button.LJ and Button.RJ (L3 and R3, pressing the joysticks).

  • Button.VIEW (share or create), Button.MENU (options), Button.GUIDE (the PlayStation logo), and Button.UPLOAD (pressing the touchpad).

Note

The buttons are currently reported using the names of the equivalent buttons on the Xbox controller, as listed above. This will change in a future version.

controllerleft joystick horizontal
controllerleft joystick vertical
joystick_left() → tuple[int, int]: %

Gets the left joystick position as percentages between -100% and 100%. The center position is (0, 0). A square deadzone is applied: if both axes are within the deadzone, both are reported as 0.

Returns:

Tuple of X (horizontal) and Y (vertical) position.

Raises:

OSError – If the controller is not connected.

controllerright joystick horizontal
controllerright joystick vertical
joystick_right() → tuple[int, int]: %

Gets the right joystick position as percentages between -100% and 100%. The center position is (0, 0). A square deadzone is applied: if both axes are within the deadzone, both are reported as 0.

Returns:

Tuple of X (horizontal) and Y (vertical) position.

Raises:

OSError – If the controller is not connected.

controllerleft trigger
controllerright trigger
triggers() → tuple[int, int]: %

Gets the left (L2) and right (R2) trigger positions as percentages between 0% and 100%.

Returns:

Tuple of left and right trigger positions.

Raises:

OSError – If the controller is not connected.

controllerdirection pad
dpad() → int

Gets the direction-pad value. 1 is up, 2 is up-right, 3 is right, 4 is down-right, 5 is down, 6 is down-left, 7 is left, 8 is up-left, and 0 is not pressed.

This is essentially the same as reading the state of the Button.UP, Button.RIGHT, Button.DOWN, and Button.LEFT buttons, but this method conveniently returns a number that indicates a direction.

Returns:

Direction-pad position, indicating a direction.

Raises:

OSError – If the controller is not connected.

PlayStation Controller Pairing Instructions

The first time you use a controller with a hub, you will need to pair them using the hub menu:

  1. On the hub, go to the settings tab and choose Add new gamepad.

  2. Put the controller in pairing mode: press and hold the share (or create) button and the PlayStation button at the same time until the light starts flashing rapidly.

  3. Select the controller on the hub when it appears in the list.

When the connection is successful, the light on the controller stops flashing. You can now start your program. Only one controller can be connected at a time.

Repeat Connections

If you keep using the same controller with the same hub, you can simply press the PlayStation button on the controller the next time and it will connect to the hub automatically.

If you connect the controller to your console or to another device in the meantime, you will need to pair it with the hub again as described above.