Posts

Interfacing PS3 controllers to Arduino via wired USB

ps3 usb code

ps3 usb code

This is a short announcement about a new feature added to USB Host Shield 2.0 Library. Kristian Lauszus from TKJ Electronics developed and contributed code for interfacing to several Sony PS3 game controllers and has been working on refining it for several months. Two days ago he posted a pull request on GitHub with the following comment:

I don’t know if you could post it on your blog too, so people will know that it now also supports the controllers via USB?

Since the beginning, Kristian concentrated on Bluetooth interface for the controllers. This is the preferred way, however, the standard wired USB interface is as good as Bluetooth dongle plus it is 100% compatible. Take a look at this example showing how to initialize PS3 controller and access its buttons, joysticks and accelerometers.

Kudos to Kristian for this nice piece of code!

Oleg.

42 comments to Interfacing PS3 controllers to Arduino via wired USB

  • Matej

    Is it possible to detect button hold? Thanx 😉

  • Matej

    Thank you again Lauszus 😉

  • Nero

    Can we connect the ps3 controller to arduino wirelessly. If yes how can we do that and what do we need to add

  • Angel

    I am creating an RC spy car. I have the car but no control can I adapt an arduino microcontroller to the ps3 dual shock 3 and use it to control the rc car? Do I need to take the original board from the car and replace it with the arduino or do i still have to use the orignal board?

  • Adrian

    Hi, I’m a student who wants to make a remote controlled quadricopter PS3. I’m looking for information on how to control the PS3 controller in arduino and turn on some LEDs. Not that I have to do I am very novice at this … Would greatly appreciate if you give me some explanation, you saw the PS3 library. Could you guide me please to configure some command? Connect this to arduino and hence will try to turn an LED for each button.

  • TerribleNews

    I looked on the github page and couldn’t find an answer, so I’m hoping you can help me: if you plugged a USB hub into the host shield, would it be possible to talk to multiple PS3 controllers? Also, will the controllers charge while plugged into the host shield?

  • pippo

    Hi.

    Great work!

    I’m dreaming of the day when I could control my quadcopter with a ps3 controller, and not having to use an expensive 500$ Futaba system.

    Ideally, we should be able to connect our PS3 controller to an arduino device, witch will send the commands further trough 2.4Ghz wireless system to a receiver mounted on the quad.

    How complicated do you think this is?

  • pippo

    Hi again, Lauszus. I looked around, Xbee would seem like a nice way to go (around 2km outdoor with the 2.4Ghz version, much longer for the 900Hz ones). There are already quads being controlled with them, but from PC (even some with PS or Xbox connected to them).

    So I’m thinking PS3 via USB or BT -> Arduino -> Xbee transmitter ======> quad Xbee receiver -> Flight Controller.

    What do you think?

  • jake

    Hey how would I use a Servo with the ps3BT library? like for controlling it with a joystick? I love this project and am very excited to use the library but I cant find any tutorials so this would be a big help thanks.

    • You could just do something like this:

      #include < PS3BT.h >
      #include < Servo.h >
      
      USB Usb;
      BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
      PS3BT PS3(&Btd); // This will just create the instance
      
      Servo servo;
      
      void setup() {
        Serial.begin(115200);
        while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
        if (Usb.Init() == -1) {
          Serial.print(F("\r\nOSC did not start"));
          while (1); //halt
        }
        servo.attach(9); // Use pin 9, as signal pin
        Serial.print(F("\r\nPS3 Bluetooth Library Started"));
      }
      void loop() {
        Usb.Task();
      
        if (PS3.PS3Connected)
          servo.write(map(PS3.getAnalogHat(LeftHatY), 0, 255, 0, 180));
        else
          servo.write(90); // Set to middle position
      }
      

      Reference: http://arduino.cc/en/reference/servo and http://arduino.cc/en/reference/map.

  • jake

    Well this has been a TREMENDOUS help. Thanks so much for this (and how fast you replied also). I cant tell you how much of a help this is.

  • Jolean

    Can you please tell me how to convert buttonClicks and buttonPresses to Commands PS3USB OR PS3BT.

  • Navirtual

    hi, my english is very basic (i’m from LA, CHILE), it’s possible conect a PS3 Dualshock 3 with Arduino without USB Host Shield, i have a module of HC-06 Bluetooth, i can create comunnication between hc-06 and dualshock 3?

    Thanks for the help!!!

    • you’d have to use a BT module which supports HID; I’m not sure they even exist.

    • No that will not be possible with any HC-06 Bluetooth module I know of. Also note that the PS3 controller also needs a special command before it will even send out any data. This is the reason why I used the USB Host shield, as it allowed me to control the behavior of a Bluetooth dongle just how I wanted. Furthermore in order to pair with the PS3 controller you need to set the Bluetooth address of the dongle via USB. This is another reason why you will have to use the USB Host shield, unless you pair with it using a custom application on your computer.

  • Cekstuffertz

    is it possible to control a dc motor with a motorshield? And if yes, how?

    Cekstuffertz

  • cem

    can ı use wifi dongle on usb host shield

  • CreteRobotics

    Do you have any information on using two ps3 controllers with the same arduino?

  • Mark

    I have found some problems with delays in control response PS3 to control a brushless motor. When I press a button to increase the motor speed, for example, the response takes more than 5 seconds to occur. Do you have any idea what might be happening? Thanks for the help!

  • nilesh

    I am trying to start and control speed of a bldc motor using PS3 L2 button
    here’s the part of code :

    //Analog button values can be read from almost all buttons
    if(PS3.getAnalogButton(L2_ANALOG) > 0 || PS3.getAnalogButton(R2_ANALOG) > 0) {
    Serial.print(F(“\r\nL2: “));
    Serial.print(PS3.getAnalogButton(L2_ANALOG));
    pos = PS3.getAnalogButton(L2_ANALOG);
    pos = map(pos, 0, 255,1000,2000);
    esc.writeMicroseconds(pos);

    Serial.print(F(“\tR2: “));
    Serial.print(PS3.getAnalogButton(R2_ANALOG));
    }

    but when i press the L2 button nothing happens … plz help !!

  • jack threlfall

    Hi, I’m making an animatronic alien mask for a film and I want to control the servos in it with a ps3 controller & an arduino uno. What would be the best way to do this? I don’t want to use a dongle, I just want to plug the controller into the usb shield and go from there.

    Also, is it possible to create combinations of servo movement and assign said movements to one button? e.g. Angry face – squint eyes and flare lips- by pressing triangle.

    Thanks for any help, its much appreciated

    Jack

  • Sachin

    Lauszus,

    I went through comments section here found that ps3 cannot communicate with hc 06.

    Hc 06 is a slave bt. But hc 05 can act as master. Can it be connected to hc 05 provided hc05 supports Bluetooth 2.0 with EDR.

    if it cannot be connected that way, can ps3 be connected to Bluetooth module connected to usb-to-ttl connector? If it can be connected connected, is there a library for it? If not, could you provide me some docs on what handshakes are happening between bt and ps3 so that I can write a library myself?

    Thanks
    Sachin

  • Mikjail

    Hi, its necessary a Arduino host shield in order to use this code? I have an arduino uno standalone and i pass the code through FTDI converter and i need to control some brushless motors without buying this host shield

  • Is it possible to use this code, but with the wires soldered to the GPIO headers? In other words, cut the end off of a USB PS3 controller and solder the 5v, ground, positive signal, and negative signal wires to the appropriate pins.

  • Sherlock

    I purchased a Thrustmaster T-Flight Stick X Flight Stick (PC and PS3). I bought the USB shield 2.0 host for Arduino. I plugged it onto the Arduino and loaded the library example for PS3 (that came bundled with the libraries). It printed ‘PS3 library started’ or something like that but did not print anything when I clicked or moved the joystick. I don’t think that the joystick was recognized? How do I trouble shoot this? There is no error message in the serial monitor…There are many rows of pins on the top of the USB host shield but no instructions to connect any of them for different applications (or maybe I just did not find it)?

    Thanks so much for the help,

  • AllanBering

    How to solve this?

    C:\Program Files\Arduino\libraries\USBHostShield\PS3BT.cpp: In member function ‘float PS3BT::getAngle(AngleEnum)’:
    C:\Program Files\Arduino\libraries\USBHostShield\PS3BT.cpp:107: error: ‘atan2f’ was not declared in this scope
    C:\Program Files\Arduino\libraries\USBHostShield\PS3BT.cpp:109: error: ‘atan2f’ was not declared in this scope

  • AllanBering

    I used ver1.0.5-r2 and simply compiled the example: PS3BT

  • LordSythrus

    I keep getting this when I try to upload the example:

    PS3USB:15: error: ‘USB’ does not name a type
    PS3USB:17: error: ‘PS3USB’ does not name a type
    PS3USB.ino: In function ‘void setup()’:
    PS3USB:28: error: ‘Usb’ was not declared in this scope
    PS3USB.ino: In function ‘void loop()’:
    PS3USB:35: error: ‘Usb’ was not declared in this scope
    PS3USB:37: error: ‘PS3’ was not declared in this scope
    PS3USB:38: error: ‘LeftHatX’ was not declared in this scope
    PS3USB:38: error: ‘LeftHatY’ was not declared in this scope
    PS3USB:38: error: ‘RightHatX’ was not declared in this scope
    PS3USB:38: error: ‘RightHatY’ was not declared in this scope
    PS3USB:51: error: ‘L2’ was not declared in this scope
    PS3USB:51: error: ‘R2’ was not declared in this scope
    PS3USB:59: error: ‘PS’ was not declared in this scope
    PS3USB:62: error: ‘TRIANGLE’ was not declared in this scope
    PS3USB:64: error: ‘CIRCLE’ was not declared in this scope
    PS3USB:66: error: ‘CROSS’ was not declared in this scope
    PS3USB:68: error: ‘SQUARE’ was not declared in this scope
    PS3USB:71: error: ‘UP’ was not declared in this scope
    PS3USB:74: error: ‘LED4’ was not declared in this scope
    PS3USB:76: error: ‘RIGHT’ was not declared in this scope
    PS3USB:79: error: ‘LED1’ was not declared in this scope
    PS3USB:81: error: ‘DOWN’ was not declared in this scope
    PS3USB:84: error: ‘LED2’ was not declared in this scope
    PS3USB:86: error: ‘LEFT’ was not declared in this scope
    PS3USB:89: error: ‘LED3’ was not declared in this scope
    PS3USB:92: error: ‘L1’ was not declared in this scope
    PS3USB:94: error: ‘L3’ was not declared in this scope
    PS3USB:96: error: ‘R1’ was not declared in this scope
    PS3USB:98: error: ‘R3’ was not declared in this scope
    PS3USB:101: error: ‘SELECT’ was not declared in this scope
    PS3USB:105: error: ‘START’ was not declared in this scope
    PS3USB:111: error: ‘Pitch’ was not declared in this scope
    PS3USB:113: error: ‘Roll’ was not declared in this scope
    PS3USB:119: error: ‘Off’ was not declared in this scope
    PS3USB:122: error: ‘Red’ was not declared in this scope
    PS3USB:125: error: ‘Green’ was not declared in this scope
    PS3USB:128: error: ‘Blue’ was not declared in this scope
    PS3USB:131: error: ‘Yellow’ was not declared in this scope
    PS3USB:134: error: ‘Lightblue’ was not declared in this scope
    PS3USB:137: error: ‘Purple’ was not declared in this scope
    PS3USB:140: error: ‘White’ was not declared in this scope

    Can anyone help me?

  • Jack

    Hello sir.I modified my old rc car in order to work with PS3 controller.It works very well expect for the backwards.

    When i press right, it turns right
    When i press left, it turns left
    When i press Forward, it goes forward
    When i press backwards, doesn’t respond at all.

    What i am doing Wrong.Please help me.I need your help, sir.

    My code is this:
    #include

    USB Usb;
    BTD Btd(&Usb);
    PS3BT PS3(&Btd);

    int const pwm_drive = 6;
    int const dir_drive = 7;
    int const pwm_steer = 5;
    int const dir_steer = 4;

    void setup() {

    Serial.begin(115200);
    #if !defined(__MIPSEL__)
    while (!Serial); // Wait for serial port to connect – used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
    #endif
    if (Usb.Init() == -1) {
    Serial.print(F(“\r\nOSC did not start”));
    while (1); //halt
    }
    Serial.print(F(“\r\nPS3 Bluetooth Library Started”));
    pinMode(dir_drive, OUTPUT);
    pinMode(dir_steer, OUTPUT);
    pinMode(pwm_steer, OUTPUT);
    pinMode(pwm_drive, OUTPUT);
    }

    void loop() {
    Usb.Task();

    if (PS3.getButtonClick(RIGHT)) {
    Serial.print(F(“\r\nRight”));
    PS3.moveSetBulb(Red);
    digitalWrite(dir_steer, LOW);
    analogWrite(pwm_steer, 255);
    PS3.moveSetBulb(Off);
    digitalWrite(dir_steer, 0);
    analogWrite(pwm_steer, 0);
    }
    if (PS3.getButtonClick(LEFT)) {
    Serial.print(F(“\r\nLeft”));
    PS3.moveSetBulb(Green);
    digitalWrite(dir_steer, HIGH);
    analogWrite(pwm_steer, 255);
    PS3.moveSetBulb(Off);
    digitalWrite(dir_steer, 0);
    analogWrite(pwm_steer, 0);
    }
    if (PS3.getButtonPress(CROSS)) {
    digitalWrite(dir_drive, HIGH);
    analogWrite(pwm_drive, 255);
    }
    if (PS3.getButtonPress(TRIANGLE)) {
    digitalWrite(dir_drive, LOW);
    analogWrite(pwm_drive, 255);

    }

    else {

    digitalWrite(dir_drive, 0);
    digitalWrite(dir_steer, 0);
    analogWrite(pwm_steer, 0);
    analogWrite(pwm_drive, 0);
    }

    if (PS3.getButtonClick(PS)) {
    PS3.disconnect();
    }
    }