Code for DIY NES Game Pad to Arduino to iPad Adapter

As promised, here’s the code to connect your NES game pad to an Arduino via USB to an iPad emulating an iCade controller (whew!).

// NES to USB iCade emulation to iPad
// Paul Rickards
// rickards@gmail.com
// January 15, 2012
// Tested with Arduino 1.0 IDE
// Add V-USB for Arduino to your Libraries folder
// http://code.google.com/p/vusb-for-arduino/
//
// Changes I made to the library (usbconfig.h):
// * changed USB_CFG_DMINUS_BIT to 3
// * commented out defintions of USB_CFG_PULLUP_IOPORTNAME and USB_CFG_PULLUP_BIT
//
// Arduino pin 2 = USB+
// Arduino pin 3 = USB-
// Don't forget the small circuit (3 resistors and 2 diodes) needed to connect
// USB to the Arduino. See the library for more info.
//
// Other controllers are possible. I first designed it for use with an Atari/C64 joystick
// The pins needed to be debounced (the bounce.h library will not work with the V-USB stack)
// and I needed more buttons on the joystick so I scrapped the idea.

#include "UsbKeyboard.h"

// Define NES pins
int latch=8;
int clock=9;
int datin=10;

bool t = 1, prev_nes[8] = { 1,1,1,1,1,1,1,1 };

// iCade key mapping http://www.ionaudio.com/downloads/iCade_Dev_Resource_v1.3.pdf
// NES controller mapping      A      B    select start   up    down   left   right
const int key_press[8]   = { KEY_O, KEY_L, KEY_Y, KEY_H, KEY_W, KEY_X, KEY_A, KEY_D };
const int key_release[8] = { KEY_G, KEY_V, KEY_T, KEY_R, KEY_E, KEY_Z, KEY_Q, KEY_C };

void setup() {
  // Setup NES controller
  pinMode(latch,OUTPUT);
  pinMode(clock,OUTPUT);
  pinMode(datin,INPUT);
  digitalWrite(latch,HIGH);
  digitalWrite(clock,HIGH);
}

void loop() {
  UsbKeyboard.update();

  digitalWrite(latch,LOW);
  digitalWrite(clock,LOW);
  digitalWrite(latch,HIGH);
  delayMicroseconds(2);
  digitalWrite(latch,LOW);

  for (int i=0; i<8; i++) {
    t = digitalRead(datin);
    if (prev_nes[i] != t) { // button changed
      if (t) UsbKeyboard.sendKeyStroke(key_release[i]);
      else   UsbKeyboard.sendKeyStroke(key_press[i]);
      prev_nes[i] = t;
    }
    digitalWrite(clock,LOW);
    delayMicroseconds(2);
    digitalWrite(clock,HIGH);
    delayMicroseconds(2);
  } 
}

8 thoughts on “Code for DIY NES Game Pad to Arduino to iPad Adapter

  1. Permalink  ⋅ Reply

    Stef.

    January 17, 2012 at 8:17am

    Hi there!
    I’m pretty new to electronics and am currently realizing my first project w. Audrino, so i’m pretty unexperienced.
    Is there a chance you would upload a pic of the soldered from front and back so i can try to re-build it or understand what is connected where? 🙂 Would really appreciate!
    BR Stef.

  2. Permalink  ⋅ Reply

    Matthew Farrow

    January 29, 2012 at 11:40pm

    Am I right in thinking that you could interface this to an SNES gamepad by adding more repeats to the for loop to read the extra bits from its shift registers?

    Any interest in a small PCB using the surface mount (TQFP) version of the ATMEGA328 chip? It would be small enough to stash inside the NES or SNES controller and replace the cord with a USB cable. It’d mean hacking the controller itself but you could lose one of the dongles in this setup. You’d only need the iPad CCK and the controller, not too bad and still pretty portable.

    • Permalink  ⋅ Reply

      admin

      January 30, 2012 at 12:03am

      I believe so but I don’t have one to test it. It looks like there’s two extra buttons. I’m sure the code exists somewhere.

      A smaller self-contained version of the NES game pad that only has USB on it would be sweet.

  3. Permalink  ⋅ Reply

    Matthew Farrow

    January 30, 2012 at 9:36am

    I’m pretty sure that the SNES pad has two shift registers cascaded together with their data and clock lines tied. You toggle the latch and shift out eight bits, then eight more, which makes the “near” register shift out its eight bits then the “far” register shifts its bits through the first register.

    More info on the SNES / NES setup for those interested: http://code.google.com/p/nespad/

    http://www.thehelloworld.info/moreInfo1.php

  4. Permalink  ⋅ Reply

    dandee

    October 2, 2012 at 8:54am

    Hey! Great job and thanks for sharing. I wonder if this could be done with an arduino mini as well. My dream would be to put all the electronic inside the joystick itself, as carrying aditional boxes and cables along with the ipad is kind of against the idea of portability. 😉 As far as I can see the mini has all its needs.

  5. Permalink  ⋅ Reply

    sirbull

    January 19, 2013 at 6:37pm

    If you use a Arduino Leonardo, you won’t need to emulate the keypad presses as you would with the Uno.
    Do you know what kind of changes I need to do to make this work with the Arduino leonardo?

    • Permalink  ⋅ Reply

      admin

      January 19, 2013 at 10:44pm

      I’m not sure, I don’t have the Leonardo. It may just be as simple as removing the V-USB library and using the Leronardo’s built in functions. If you give it a try, be sure to let us know if it works.

Leave a Reply

Your email will not be published. Name and Email fields are required.