MBC PS2 / PS4 Controller receiver × EV3 Pybricks

Read the Controller receiver on the EV3 with MicroPython in three steps

MicroPython on the EV3 always goes in this order. First import. Then create an object and give the receiver a name. Last, use that name to read values. First pick Pybricks v4 or EV3 MicroPython v2 above. Then follow the steps on the left, and the program on the right grows with you. Click the buttons on the gamepad simulator and drag the sticks to see what the program prints.

Your software:
1

Import the library

The first thing your program does is tell Pybricks: "I want to use the Controller library". Without this line, the hub does not know what MBC_GAMEPAD means, and you get a NameError.

Pick one library. Both work the same way. Only the import line 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. Open code.pybricks.com in Chrome. Follow the steps on the site to put the Pybricks firmware on the EV3 (the release version already includes the EV3 firmware).
  3. Press "Import a file" in the file list on the left. Import the library into the same project.
  4. Do not rename the file, and do not put it in a sub-folder.
  5. Go back to your main program. Write the lines below at the very top (marked ① in the program on the right).
2

Create an object: name your receiver

A robot can have several sensors, so you "create an object". It tells the program which port the receiver is on, and gives it a name. Every time you read a value, you use this name.

Which port is the receiver on?

Turn on multitasking?

The 1 in the program is the port number: EV3 1~4 = S1~S4. You can also write Port.S1, but then you also need from pybricks.parameters import Port.

3

Use a function

You always write "pad.function_name()": the name, a dot, then the function. To read buttons or sticks, start every loop with pad.update(). It reads the gamepad once, so the functions after it get new values. Click a function, and the program 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.

An example for every function

Full examples


        

!


        

!


        

!

Good to know

  1. Read the gamepad once at the start of every loop. Pybricks v4: buttons(), pressed(), any_pressed(), stick() and stick_centered() all read the snapshot taken by the last update(). Forget update() and nothing changes when you press a button. EV3 MicroPython v2: read once per loop with data = name.read('IR-SEEK'), then take values out of data.
  2. Unplugging works differently in the two programs. Pybricks v4 with the MBC library: when the receiver cannot be read, you get the last value it read (or None if it has never been read). The computer prints one warning line, and the program keeps running. If you unplug while driving with a stick, the robot keeps driving with the last stick position. To check the cable, use sensor_ok(). EV3 MicroPython v2 with Ev3devSensor: you get an OSError and the program stops. Plug it back in and run the program again. Tick "Unplug the sensor" under the gamepad simulator to see both.
  3. On the EV3, changing mode takes about 1.1 s. Buttons and sticks are all mode 1 (the update() group). connected() is mode 3. kind(), device_id() and firmware_version() are mode 5: read them once, outside the loop. Modes 3 and 5 need receiver firmware v4.0 or later.
  4. Stick numbers: stick() in the library gives 1 to 9 (5 = centre). Reading mode 1 directly gives 0 to 99 (50 = centre). A tiny push makes the number jump, so treat values near the centre (4 to 6, or 40 to 60) as "not pushed". Direction: right and up make the number bigger. When the gamepad is not connected, all buttons are 0 and the sticks are in the centre.
  5. What the EV3 cannot read or do: battery(), buttons_psx() and pressed_psx() are SPIKE only. Rumble and the light bar of a PS4 gamepad are "commands sent to the receiver", and the EV3 sensor port can only read, not send, so they do not work. The PS button and the touchpad cannot be read. Write button names as capital-letter strings, for example pressed('CROSS'). On PS4 / PS5, SHARE = 'SELECT' and OPTIONS = 'START'.