How to connect a PS4 Dualshock 4 controller to your MINDSTORMS EV3 brick with Bluetooth

| | ,

The EV3 brick has a regular Bluetooth chip inside, the PS4 controller too. In this article, I’ll show you how you can connect the two to build remote-controlled robots. The example I’ll be using is a race car with a proportional steering mechanism. I will provide a python code, a step by step guide and building instructions.

The PS4 controller is a ubiquitous device. Many kids and teens have a few. Let’s get creative and use the controller for something else than virtual running and gunning. 

Connecting the controller to the brick

The default LEGO software does not allow full access to hardware in your EV3 brick. The good news is that you can boot your brick with different software – a Debian Linux distribution – that gives full access to the hardware. We have to thank the people behind ev3dev and Pybricks for that. It also allows you to program the brick in Python. How to download this other firmware and boot your brick with it is the subject of a different article. To keep this article simple, I will assume you have succeeded in doing that.

Time Needed : 5 minutes

How to connect a Sony PS4 Dualshock 4 controller to a LEGO MINDSTORMS EV3 Intelligent Brick.

  1. Boot the brick

    Use the official latest ev3dev SD card image.

  2. Go to 'Wireless and Networks'

    Use the centre button on your EV3 brick to select.
    ev3dev wireless and networks

  3. Go to 'Bluetooth'

    Use the centre button on your EV3 brick to select.

  4. Enable 'Powered'

    A filled square means that it's selected. Use the centre button.

  5. Enable 'Visible'

    A filled square means that it's selected. Use the centre button. The brick is now visible to other Bluetooth devices.

  6. Select 'Start scan'

    The brick starts looking for Bluetooth devices in pairing mode.

  7. Put the PS4 controller into pairing mode

    Press the Playstation and the Share buttons at the same time until the white light in front of the controller starts blinking.pair ps4 controller

  8. Select 'Wireless Controller'

    It should appear in the list after a few seconds.

  9. Select 'pair' or 'connect'

    The first time you do this, the left button says: 'Pair'. I made this screenshot later.

  10. Verify the connection

    Check if the light on the PS4 controller has turned blue. On the EV3 brick, a black Bluetooth icon should be visible.

  • Windows, macOS or Linux computer with Microsoft Visual Studio Code
  • Original Sony PS4 controller
  • MicroSD card with LEGO Education Python

Building the race car

The drift car is a fun model to control with the gamepad. You can find the building instructions on my Patreon site. I have made two versions: a 31313 one-kit model and a model that uses the wheels from the Mercedes Benz Arocs and an added large motor. The links below give you access to digital downloads of PDF building instructions for both models.

Programming the EV3 brick to respond to the PS4 controller

Now that the controller is connected, we have to read joystick events from it and control motors. In Debian Linux, this is as simple as reading a file. The file contains byte data and is not human legible. We have to unpack the data with a C struct. Python makes that easy for us. The program below opens the right file and reads gamepad events from it in a continuous loop. When you connect joystick events to motors, you have to keep in mind that the loop doesn’t run with regular intervals. It blocks at the read statement until there is a new gamepad event. The blocking means you can not run motor control feedback loops in it. Luckily for us, Pybricks has a track_target() method that does all the control magic in the background.

What’s next?

This should be enough for a lot of fun while driving a race car with proportional steering control. The steering is not perfect, however, because there’s no differential. How would you program an electronic differential? And wouldn’t it be cool to have a video stream from the car? Stay tuned for the expanded version of the race car. You can support me on Patreon to get notified, subscribe to my Youtube channel or like my page on Facebook. I would also appreciate it if you’d browse my T-shirt designs!

MINDSTORMS EV3 Racecar with PS4 controller
MINDSTORMS EV3 Racecar with PS4 controller
MINDSTORMS EV3 Racecar with PS4 controller
MINDSTORMS EV3 Racecar with PS4 controller

2019 in retrospect for Anton’s Mindstorms

Building and Coding Voice-controlled EV3 race cars with optional FPV


Leave a Comment

%d bloggers like this: