MBC PS2 / PS4 Controller receiver × MATRIX Mini R4 (MATRIXblock)

Read the PS2 / PS4 Controller receiver with MATRIXblock in three steps

In MATRIXblock, the software for the MATRIX Mini R4, the PS2 / PS4 Controller receiver shows up as a stock "colour sensor" (I2C address 0x22), so the built-in blocks can read it and no library is needed. First put Color Sensor Initialize in Setup of "Mini Begin", then a wait in Loop, and finally the colour sensor reading blocks in the loop. Follow the steps on the left and the blocks on the right build up; these pictures are drawn by MATRIXblock's own block code, so they look the same as in the software.

MATRIXblock is the free block software from MATRIX (installed in C:\matrixblock-r4\ by default). Buy the MATRIX edition of the sensor and connect it to I2C1 or I2C2 with the stock 4-pin sensor cable.

1

Set up: which port the PS2 / PS4 Controller receiver uses

The first thing in the program is a "Color Sensor ○ Initialize" block inside "Setup" of the blue "Mini Begin" block, so the MATRIX starts talking to the receiver.

Which port is the sensor plugged into?
  1. Open MATRIXblock. A new project already has a blue "Mini Begin" block (if not, drag one from the "Mini Core" category).
  2. Click the "Sensing" category and find "Color Sensor I2C1 Initialize" (not "Color Sensor V3").
  3. Put it inside "Setup" and pick I2C1 in the dropdown, the same port the sensor cable is plugged into.
2

Put a wait in the loop

Blocks inside "Loop" repeat forever. First add "wait 100 millisecond" so it reads every 0.1 seconds and the Information Window does not scroll too fast.

  1. Drag "wait 1000 millisecond" from "Control" into "Loop" and change the number to 100.

Leave "UART: On" and "Baud: 9600" on "Mini Begin" as they are: the examples use "USB Serial Print … with New Line" to show values in the Information Window (set the Information Window to 9600 too).

3

Use the reading blocks

The "Color Sensor I2C1 Red" block in "Sensing" has 7 usable slots in its dropdown: Red, Green, Blue, Cyan, Magenta, Yellow and Key. Each slot holds one value from the receiver; the cards below tell you what each slot is and how to work it out. Pick the same port as in step 1 (I2C1). Click a function and the blocks and output change to its example; the example goes inside "Loop", above the wait.

Simulated gamepad: click the buttons, drag the sticks and watch the output change

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 0 to 99, 50 = centre, and the number gets bigger to the right and up. PS2 and PS4 receivers give exactly the same seven slots on MATRIX.

An example for every reading

14 examples you can copy block by block (port I2C1); they go inside "Loop", above the wait. Press "Try it" to jump to step 3; the blocks and output on the right change to that example.

Things to know

  1. Use the seven slots only: in the "Color Sensor" dropdown pick Red, Green, Blue, Cyan, Magenta, Yellow or Key (R, G, B, C, M, Y, K), each 0 to 255. Do not pick ColorID, Hue, Saturation or Value (the controller works those out from Red, Green and Blue, so they mean nothing for MBC products), and do not use the "Color Sensor V3" blocks (they are for another MATRIX sensor and cannot read the PS2 / PS4 Controller receiver).
  2. Unplugging does not stop the program: when the MATRIX library cannot reach the sensor, all seven slots read 255 and the program keeps running. Use the "Is it plugged in?" card to check; plug it back in and it works again.
  3. PS2 and PS4 read the same way: the seven slots mean exactly the same for both receivers. MATRIXblock cannot use the PS4-only rumble, light bar or battery level; use Arduino, mBlock or micro:bit for those.
  4. Connect the gamepad first: a PS2 pad connects by itself when you switch it on (the receiver's green light goes off when connected; the pad's ANALOG light must be on). A PS4 / PS5 pad must be paired the first time: while the receiver's green light flashes fast, hold SHARE + PS (PS4) or Create + PS (PS5).
  5. Many buttons at once: the numbers add up, so to check one button, take it out with "/", "floor" and "mod 2" (see the "Is ▲ pressed?" card).