MBC PS2 / PS4 Controller receiver × Pybricks blocks

Read the PS2 / PS4 Controller receiver with Pybricks blocks in three steps

Blocks follow the same order as MicroPython: first import the library under "set up", then create an object and give the receiver a name, and finally use that name under "program" to call a function. Work through the steps on the left and the blocks on the right grow with you. These block pictures were built and exported straight from Pybricks Code, so they look the same as on your screen.

Check first: the block mode in Pybricks needs a licence code or a Patreon membership. Without one, you can write MicroPython instead; it works exactly the same way (switch the right side to "Matching MicroPython" to see it).

1

Import the library

The first thing in a block program is a red "from … import …" block under the yellow "set up". It tells Pybricks to use the Controller library.

Pick one library (the blocks are exactly the same; only the name after "from" is different):

  1. Download the library (save the file and keep its name): MBC_gamepad.py (only this sensor) or MBC_sensors.py (the all-in-one library). You picked MBC_gamepad.py.
  2. In Pybricks Code, press "Import a file" in the file area on the left and import it into the same project. Do not rename the file.
  3. Press "+" to add a new file and choose blocks.
  4. Drag the red "from … import …" block from the block list, attach it under "set up", and pick the two names.
2

Create an object: give the receiver a name

Drag a red "… is a …" block and attach it under the import block. On the far left is the name you give the receiver; after it come the port and whether to use multitasking. After that, every time you read a button or a stick, you pick this name.

Which port is the receiver plugged into?

Give it a name (in Pybricks you can use any English name; here are three examples):

Turn on multitasking? (the green true / false on the block)

The port box takes a number: SPIKE 1~6 = A~F, so 3 is port C.

3

Use the functions

Put red function blocks under "program": pick the name pad in the first box, await in the middle, and the function in the last box. To read buttons or sticks, the first block in the loop is "pad … update" (the kind of function block that stacks). It reads the gamepad once, so the blocks after it get new values. Button and stick names go in the extra box on the right of the function block (press ">" to open it), for example CROSS or LY, in capital letters. Click a function and the blocks and output change to its example.

Gamepad simulator: click the buttons, drag the sticks, and watch the output on the right

The simulator is only a sketch: click a button once to hold it, click it again to let go. Drag the dot on a stick to move it (it stays where you let go; press "Release all" to centre it). Sticks go from 1 to 9, 5 = centre, and the number gets bigger to the right and up. The PS4 gamepad battery is fixed at 80 in this demo.

Block examples for every function

14 functions, each with a set of blocks you can copy (name pad, multitasking true). Press "Try it" to jump to step 3; the blocks and output on the right change to that example.

Tips

  1. Import the library file into the project first. If MBC_gamepad.py is not imported, you cannot pick MBC_GAMEPAD in the red blocks.
  2. await and call must match multitasking: multitasking true → pick await; multitasking false → pick call. If you mix them, the program fails on the hub.
  3. Put update at the start of every loop. buttons, pressed, any_pressed, stick, stick_centered, buttons_psx and pressed_psx all read the snapshot taken by the last update. Without update, the values stay the way they were the first time. connected, battery, kind, device_id and firmware_version do not need update.
  4. Behind the blocks is MicroPython. The code pane on the right of Pybricks Code is the program the blocks make, the same as "Matching MicroPython" on this page (the multitasking version has one extra line, await wait(0), which Pybricks adds by itself).
  5. Unplugging does not stop the program: when the receiver cannot be read, you get the last value it read; use the sensor_ok function to check the connection. (The warning line is in Chinese and English.) Push a stick right or up and the number gets bigger (1 to 9, 5 = centre); when you use a stick as a switch, use 7 or more and 3 or less, so both sides feel the same. On PS4, SHARE = SELECT and OPTIONS = START.