Friday, February 17, 2012

Wireless Reprogrammable PS2 Controller (Part I)

Awesome Sauce Wixel & Arduino/Atmega Based PlayStation 2 Project Controller

pic1m
pic9s pic10s pic11s

The Reason

Ok, so I was at my girlfriend’s family’s place on some recent holiday and I was being asked to show off what my robot ABE could do. I did the fully automated things by turning him on and doing a few little pre-programmed responsive actions (covering his “eyes”, covering both IR sensors etc1) but I quickly realised that in order to show off his full bag of tricks, I would have to send him a few commands (or at least requests). At the time, the only way to do that was to take my other Wixel unit, plug it into my computer and send it some serial love. I had no computer setup there, nor my other Wixel.

The Design

At first I had intended to add two interrupt buttons to ABE and I even studied Schmitt circuitry to that end. Then, I had a better idea - I would add an infrared remote to him. While I was looking into that option, I had the best idea yet. Why should I waste even more power on extra components when ABE already has inbuilt wireless comms via the Wixel? Why don’t I just build a Wixel-enabled remote control?

The Better Design

To begin with, I ordered an LCD from Little Bird Electronics along with a few other parts for various new ideas. I had intended to use some old contact pads I had laying around for the buttons but I couldn’t come up with a design that I liked. It had to be light, portable and I had to want to use it.

After a bit of pondering, I remembered that I had an extra PS2 controller in a box doing nothing except tangling my other cabling up. Surely someone had already thought of this before? Google time!

PS2 Plus Arduino?

Feasiblity Study

I looked around for an Arduino library to communicate with a PS2 controller and I found it right here. Thanks to Bill Porter, I had my answer. Not only was it possible but much of the work and testing was already done. Music to my ears, and a BIG thanks to Bill.

What I wanted

I wanted a compact, portable replacement for communicating with my robot via the Serial Monitor. This was my general requirements list:

It must…

  1. be compact and portable… (duh)
  2. allow multiple user inputs to the robot
  3. allow human-readable output from the robot
  4. look professional and slick so I will want to play with it (oooh shiny!)
  5. not interfere with the ability to wirelessly reprogram ABE

What I ended up making

I ended up making a comfortable, portable replacement for communicating with any of my projects that include a Wixel

It…

  1. is pretty compact and portable (at least hand-held) and is fairly light
  2. allows multiple user input to the robot via:
    • 10 digital buttons (all with changed, down and up event notifications)
    • 4 analog pressure-sensitive buttons
    • 2 biaxial analog stick controllers (with two additional digital buttons underneath them)
    • a microprocessor-driven LCD-Menu interface (that I’m writing AFTER this post)
  3. allows 32 5x8 pixel backlit LCD characters over 2 lines for human-readable output and interaction
  4. looks professional (the Sony part… my bit looks nice enough :)
  5. allows pass-through reprogramming of ABE when the controller is plugged into a USB port using the standard Arduino IDE

Sweet - I’ve totally decimated my requirements list (that’s a good thing)!

Oh, and as an added “I wonder if I can….” bonus, I’ve also managed to design it in such a way that if ABE is plugged into the computer USB port, the controller itself can be wirelessly reprogrammed (without having to remove the chip). Woot!

What does that mean?

Well, it means I can remote control ABE and any other project that I’m making that includes a Wixel.

It means I can make stand-alone programs for the controller (and I have a few sneaky projects in mind) and its 2.4GHz transceiver (Wixel) - I could even theoretically reprogram ABE in-the-field.

It means - I’ve made myself a new toy to play with. :)

pic14m pic6m

The LCD Bracket

To make a slick-looking controller, you’ve got to have slick-looking curves. Because I still have a fair bit of the stuff and because it’s durable, mouldable (if you’re really careful with a blowtorch or hot air machine) and strong, I decided on using more perspex. I wanted the bracket to tilt the LCD screen slightly towards the user (me) for good visibility and comfort. I also wanted not to mess up the beautiful aesthetics of Sony’s controller.

I scribbled up the design (above) on my whiteboard one afternoon and measured it carefully onto a piece a day or so later. To cut it, I used a fine-grade hacksaw and to sculpt it, I used a pocket blowtorch (very carefully) and a padded vice. I beveled the edges afterwards with some hand-files and wet-and-dry.

Bracket Parts List
  • 3mm x 6mm (M3) Screws
  • 10mm Hex Spacers
  • 90mm x 3mm x 120mm Sheet of Perspex

pic7m pic8m

After I’d drilled all the bracket holes, I also drilled holes in the PS2 controller’s lower shell. I had to be extremely careful to keep my M3 screwheads away from the interior circuitry but there was enough clearance for them to co-exist peacefully. Holes in the perspex were threaded using an M3 tap tool, so no extra nuts were needed (keeps the minimalist aesthetic).

pic2m

PS2 Controller Wiring

The wire pinout provided at Bill’s site was a little different from the one I had, but it was noted that the colours were succeptable to frequent change.

pic3m

Bill’s Site Pin/Wire layout:
  1. Brown = data
  2. Orange = command out
  3. Grey = dualshock power
  4. Black = ground
  5. Red = power
  6. Yellow = attention
  7. Blue = clock
  8. White = ?
  9. Green = ack
My Pin/Wire layout:
  1. Brown = data
  2. Orange = command out
  3. Purple = dualshock power
  4. Black, Grey (both) = ground
  5. Pink = power
  6. Yellow = attention
  7. Blue = clock
  8. N/C (shield and bare wire also both N/C)
  9. Green = ack

pic4m

PS2 + Arduino

In order to link up the PS2 controller to the Arduino, I needed Bill Porter’s library called PS2X.

Download PS2X from Github

Once I had wired it all up as explained in the very top of the code (see the breadboard diagram below for the cheatsheet), I turned it on but it didn’t seem to work straight away.

Not to worry!

Bill has an absolutely brilliant section on his site that’s dedicated to troubleshooting the library. What a guy!

Bill’s Brilliant Troubleshooting Guide

It turned out that all I needed was a single resistor to act as a pullup for the controller. Simple as that.

pic5m

PS2 + Arduino + LCD

After I’d linked up the controller to the Arduino (and yes, mucked about with it for a good half-hour), I added in the LCD. This was probably the easiest part. The wiring is only 4 pins (power, ground, SCL, SDA) and the sample code in the DFRobot-provided library gave me all I needed to find-and-replace all of the Serial.print calls in Bill’s demo with lcd.print.

Download the DFRobot LCD Library for Arduino from DFRobot

…more fiddling and mucking about ensued…

pic12m

PS2 + Arduino + LCD + Wixel

For the home stretch (a few days later - I’m a busy guy), I added the Wixel into the mix. The following links came very much in handy:

For this part, (ABE has a Wixel Shield) I wanted the circuit to function similarly to the Wixel Shield. That way, I would be able to program both ABE and the controller remotely and still allow full communication between the two (without buying or powering more hardware). What followed was a simple (remember, this is version 1.0) design that was mostly voltage dividers2 and pullup resistors.

pic13

The Circuit

I’m going to write this in point form so that it reads simply. It’s not that complex a circuit, though. Here’s how it works:

  1. The Arduino powers everything from its 5V pin
  2. All grounds are connected (as they should be)
  3. The PS2 controller pins (shown on board left to right, pin1 to pin8) connect to Arduino digital pins, power and ground
    (pins 3 and 8 are not connected)
  4. The LCD pins connect to the I2C bus (SCL,SDA), power and ground
  5. The Wixel takes its power directly from the Arduino 5V pin (it has its own regulator)
  6. Arduino Transmission (TX) lines are voltage-divided down to about 3.3V for the Wixel’s RX inputs
  7. The Wixel Transmission (TX) lines are connected directly to the Arduino (because 3.3V is still considered HIGH by 5V logic)
  8. Various pullup resistors were required to keep the Arduino/Wixel logic in certain states at certain times (especially during program-loading)
  9. The single transistor is part of a quick on-board digital inverter (NOT gate). When the Wixel wants to reset the Arduino (when programming), it sets P0_0 to HIGH. However, to reset the Arduino, the RESET pin has to be pulled LOW. This is a very simplified version of the connection shown on the Wixel Shield schematic (but it works well).

N.B. I can’t remember if I put the right resistance values into Fritzing before I generated the breadboard graphic.

Stay tuned for the next post. I’ll add a schematic with values and details of the next steps, and if required, I’ll post some more explanation about the inverter, pullup resistors and logic conversion. By then, I should have some code to post too.


  • ABE is (at the time of writing) programmed to start in different "modes" depending on the condition of some of his sensors at time of startup. If an object of greater than 31°C is less than 10cm away from his sonar sensors (i.e. a human hand over his eyes), he will start up in standby mode. If both of his IR sensors detect obstacles on startup, he will start to sing a tune.

  • The Wixel runs at 3.3V whereas the Arduino runs at 5V. I bought a logic converter for the job but it was actually simpler to construct the circuit from basic components as I have.

  • 6 comments:

    1. Hi...

      I am starting out to do the same as you. Any chance of sharing the code?

      Thanks,
      Madhu.

      ReplyDelete
      Replies
      1. Hi Madhu,

        The current form of the code isn't in sketch form, as I've moved it over to pure C/C++ using Eclipse. There are notes above, however, that you can follow to create your own code version. The PS2X library and the appropriate LCD library for your device are a good start. If you're interested, drop me a line on Google+. I'm often helping out with people's projects there.

        Adam.

        Delete
    2. Hi,

      I have done everything but i have no luck...The PS2 Controller goes on but it is not detected. Could you help me out

      ReplyDelete
    3. a perfect project to kill time with.
      thank you.

      ReplyDelete