MBC PS2 / PS4 Controller receiver × LEGO SPIKE App

Read the PS2 / PS4 Controller receiver with the LEGO SPIKE App in three steps

In the official LEGO SPIKE App, the Controller receiver shows up as a Color Sensor. So you do not import a library: you read it with the Color Sensor blocks and then work out the buttons and sticks from the numbers. 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 the web version of the official App (spike.legoeducation.com), so they look the same as in the App.

1

Start: when program starts + forever

The gamepad must be read again and again. First put the yellow "when program starts", attach "forever" under it, and put "wait 0.1 seconds" inside, so it reads every 0.1 s.

  1. Open the official LEGO SPIKE App, make a new project and choose "Word Blocks".
  2. From "Events", drag out "when program starts"; from "Control", drag "forever" and attach it underneath.
  3. Drag one "wait 1 seconds" into forever and change the number to 0.1.
2

Meet the four value blocks and pick the right port

The official App reads the Controller receiver with four Color Sensor blocks. Each block starts with a port dropdown; pick the port the receiver is plugged into. You must work out the buttons and sticks from the numbers you read:

Which port is the receiver plugged into?
Official blockWhat it readsHow to work it out
reflected light (in Sensors)D-pad + shape buttonsUp 1, Right 2, Down 4, Left 8, △ 16, ○ 32, ✕ 64, □ 128; buttons pressed together add up (0 ~ 255)
color (in Sensors)Shoulder buttons + function buttonsL2 1, R2 2, L1 4, R1 8, SELECT (PS4: SHARE) 16, L3 32, R3 64, START (PS4: OPTIONS) -128; buttons pressed together add up
raw red (in the "More Sensors" extension)Left stick: X×10 + Y55 when released. Up-down (Y) = the remainder after dividing by 10; left-right (X) = the whole part after dividing by 10; both are 1 ~ 9
raw green (in the "More Sensors" extension)Right stick: X×10 + YWork it out the same way as raw red

To use raw red and raw green, first press the extension button at the bottom left of the App and add "More Sensors". This page does not use "raw blue".

3

Store it in a variable and use it

In "Variables", make a new variable (for example "buttons" or "leftY"). Use "set … to …" to store the value you read (or worked out) in it; this gives the value a name. After that, use the variable to show it or to check it. To check one button: take the whole part (floor) of (variable ÷ that button's number), then see if the remainder after dividing by 2 is 1. Click a function and the blocks and result change to its example.

Gamepad simulator: click the buttons, drag the sticks, and watch the result 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). The line underneath shows the numbers the four official blocks read.

Block examples for every function

Every function the official App can read has a set of blocks you can copy (port C). Press "Try it" to jump to step 3.

What the official App cannot read: the official App reads only the "combined data" of the receiver, so it cannot read gamepad connected, gamepad battery, receiver type, sensor ID, firmware version or sensor plugged in properly. For these, use Pybricks (the hub needs the Pybricks firmware). If you only want to pick "which button" directly and read the sticks as 1 ~ 9 without working anything out, use the MBC SPIKE Blocks website.

Tips

  1. In the official App, use the Color Sensor blocks. To the official App the Controller receiver is simply a Color Sensor, so you do not install anything else. The receiver must have the SPIKE firmware.
  2. This page is for the Education version (SPIKE 3). The home version (Robot Inventor) reads the "color" block differently, so the examples on this page do not work there.
  3. To check one button, always work it out. Use "the whole part of (value ÷ the button's number), and the remainder after dividing by 2 = 1". Do not use comparison blocks like "is color red?"; always use the number from "color". START (PS4: OPTIONS) is a negative number, -128, in the color block, and the same calculation still works.
  4. When you use a stick as a switch, use 7 or more and 3 or less. With a small push, left and down become 4 almost right away, but right and up need about half a push to become 6, so the two sides are not equally sensitive. Treat 4 to 6 as "centre".
  5. Direction: push a stick right or up and the number gets bigger (9 all the way); push left or down and it gets smaller (1 all the way); released is 5. When the gamepad is not connected, all buttons are 0 and the sticks are centred (raw red and raw green read 55).