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

Anton

Updated on:

MINDSTORMS EV3 Racecar with PS4 controller

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.

Tools
  • Windows, macOS or Linux computer with Microsoft Visual Studio Code
Materials
  • LEGO MINDSTORMS EV3 brick
  • 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

Like this article? Help us make more!

Your support matters

We appreciate your support on Patreon and YouTube! Small things count.

Become a Patron

Don't miss a thing

Subscribe below to get an email when we publish a new article.

Share this with someone who needs to know

25 thoughts on “How to connect a PS4 Dualshock 4 controller to your MINDSTORMS EV3 brick with Bluetooth”

  1. i get the below error when i press F5

    Starting: brickrun –directory=”/home/robot/ps4_ev3″ “/home/robot/ps4_ev3/main.py”
    ———-
    Traceback (most recent call last):
    File “/home/robot/ps4_ev3/main.py”, line 37
    SyntaxError: invalid syntax
    ———-
    Exited with error code 1.

    Reply
  2. Python code written for PS3 pad is working well with PS4 pad (only changed to event4), but Python code written for PS4 doens’t work with PS4 pad 🙁 it’s continuously spinning the A port motor and nothing else 🙁

    Reply
    • The PS3 version is in Python3 and uses multithreading. The PS4 version is written in MicroPython (Pybricks) and uses a tight loop control for reading events and setting speeds. Make sure you have the latest ev3dev version for this to work well. If you continue to have problems, let me know. I will rebuild the car and try to help and locate the issue.

      Reply
    • i thought the same. A-port motor is spinning and nothing else. But: A port motor is the: steer_motor = Motor(Port.A) (on line14). And the steer motor is calibrating it self (on line 20). It runs until it hits the end of the steering distance and then the ev3 knows where the center is. Very good programming but only works with exakt the same model as anton build.

      Reply
  3. Hi, great article. I have paired the pad with the brick. When I try to connect I am getting below error message:
    GDBUS.Error:org.bluez.Error.Failed: Input/output error

    Have you seen such error messages as well? Do you know how I can fix that?

    Many thanks.
    Rudi

    Reply
    • Yeah, I get that from time to time. Rebooting and turning things on and off is my strategy. Resetting the controller with a paperclip helps too.

      Reply
  4. Hey Anton, thanks for the guide and the code. I had a lot of fun with my son putting the car together.

    I am quite new to lego mindstorms and this is actually our first project. Unfortunatelly the steering in my car seems broken. The angle doesn’t return consistently to the center when I let go of the stick. It moves a couple degees to the left every time I try to steer the car. And after 3-4 steerings the tires are all the way to the left and won’t move anymore. If I change line 80 from steer_motor.track_target(left) to steer_motor.dc(left), I am able to steer the car without any problems (but I guest this wasn’t the idea behind using track_target).
    I suspect the sensory in my steering motor is broken. But before I order a replacement I wanted to ask if you observed something similar and this is maybe just a calibration problem?

    Thanks

    Reply
    • Did you know you can calibrate the steering with the shoulder buttons? Have you tried swapping the motor out for a different motor? Do the steering wheels auto-center when you start the program? Did you build the exact model from the article?

      Reply
  5. I am getting the error code:

    Traceback (most recent call last):
    File “/home/robot/ev3ps4/main.py”, line 48, in
    OSError: [Errno 2] ENOENT

    in the line there is in_file = open(infile_path, “rb”)

    Reply
    • In theory you can. But I haven’t tried. I think the button id’s and button mapping will be different. If only I had the time to own a PS5…

      Reply
  6. Hi Anton,
    I do not seem to be able to connect the ps4 controller to the ev3 brick.
    I start the scan, see it appear, select pair, then get the black bluetooth icon for a few seconds, but then it goes off. The light on the ps4 controller keeps flashing and never gets blue.
    I have tried restarting and reflashing the card many times.
    Would you have any ideas of how to get me unstuck please?
    Thank you for your time.

    Reply
    • The black icon actually stays on for about 20 seconds and then an error pops up: ‘timeout was reached’

      Reply
  7. I thought I would show what I managed to get working using this tutorial as a starting point. I have taken Danny Benedettelli’s Transforming robot — https://robotics.benedettelli.com/transformer-retail/ and connected it to my PS4 controller. I changed your drive settings to use DriveBase. My only difficulty has been trying to figure out what code settings to use for all the buttons. The code snipet below shows what I have got working.
    ”’
    if ev_type == 3:
    if code == 17 and value == 0: #left set of up or down buttons, I cannot separate out the the four
    buttons from each other
    ev3.speaker.play_file(SoundFile.SPEED_UP)
    Robot_Down()
    elif code == 0:
    left = scale(value, (0,255), (40, -40))
    elif code == 4: # Righ stick vertical
    forward = scale(value, (0,255), (100,-100))

    if ev_type == 1 and code == 304 and value == 1: #this is for the right symbol buttons, but like the
    other set of buttons I cannot separate the four of
    them so that I can control other functions
    ev3.speaker.beep()
    Robot_Up()

    robot.drive(forward, left)

    ”’
    I did use the print this line to puzzle out some of the codes, but it changes very fast and is not always consistent even if only pressing one button.

    Reply

Leave a Reply

Item added to cart.
0 items - 0.00