MBC Controller × Pybricks built-in blocks

Read the Controller with Pybricks built-in blocks, no library

On a SPIKE hub the Controller shows up as a LEGO color sensor, so Pybricks' own color sensor blocks can read it: two numbers, reflection() and ambient(). The Controller packs values into each; split them with math blocks and use them. Work down the steps on the left and watch the blocks grow on the right; the block pictures are exported from Pybricks Code itself.

How this differs from Pybricks blocks (with the library)
  • This page: nothing to download or import, just open Pybricks; but it gives only 11 keys and 3 stick directions (no RY), and you split them yourself with // and %.
  • With the library: import MBC_gamepad.py first, but it gives all 16 keys, the sticks, pad connection and battery on both PS2 and PS4, one block per function.

Check first: Pybricks block coding needs a license code or a Patreon membership. Without one, write the MicroPython directly; it is exactly the same (switch the right panel to "MicroPython").

1

set up: create the color sensor and pick the port

No library: under the yellow "set up" put Pybricks' own color sensor block (ColorSensor), pick the port the Controller is on, and name it pad.

Which receiver do you have?

Which port is the Controller on?
  1. In Pybricks Code press "+" to add a file and choose blocks.
  2. From "Sensors" drag the color sensor setup block (sensor icon) under "set up", rename it pad (any English name works) and pick the port above.
2

program: read reflection() and ambient()

Under "program" put "repeat while true", print pad reflected light (reflection) and pad ambient light (ambient) inside, then "wait 100 ms". Reading reflection switches the sensor to SPIKE mode 5 and ambient to mode 7 automatically; the Controller puts one set of numbers in each:

The simulator reads now:

On Pybricks 4.1 reflection() carries one decimal, so make it whole (0–2047, never negative); ambient() keeps one decimal: LX, LY before the point, RX in the first decimal.

3

Split with math blocks and use it

Store the reading in a variable with "set to" from Variables: r = reflection, a = ambient (as is). Then split it with // (whole part of ÷) and % (remainder) from Math, and print or test the values. Click a function and the blocks and output change to its example. Dashed ones are for the other receiver (clicking one switches it for you).

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

Every function has a set of blocks to copy (name pad, port C). "Try it" jumps to step 3, and the blocks and output on the right change to that example.

For more values use Pybricks blocks with the library or MicroPython.

Good to know

  1. Only 2 numbers. reflection() and ambient() hold one each; everything else is out of reach (listed under the step 2 table). For those use Pybricks blocks with the library.
  2. Pybricks 4.1 readings carry decimals: reflection() has one decimal (e.g. 81.3), so make it whole first (floor if ≥ 0, ceiling if negative) before // and %; ambient() keeps only one decimal, so split the integer part and the first decimal separately (floor, round(a × 10) % 10).
  3. Pybricks 4.1 cannot read the right stick up/down (RY): ambient() keeps one decimal. For RY, L2, R2, L3, R3 and START use Pybricks blocks with the library.
  4. Pad not connected: no keys (0) and sticks in the middle (55.5); the built-in blocks cannot tell "not connected" from "nothing pressed".
  5. Sticks give only 1–9: not −100 to 100; subtract 5 and multiply by 25 yourself.
  6. Do not use the "color" block or color names. The Controller sends numbers: store them in variables and compare numbers. Unplugging the sensor makes the built-in blocks raise an error and stop the program (the library version does not).
  7. Behind the blocks is MicroPython. The code pane on the right of Pybricks Code shows the program the blocks make; it is the same as "MicroPython" on this page.