Use an Arduino With an N64 Controller

93K146117

Intro: Use an Arduino With an N64 Controller

There are tutorials out there for using an NES controller with an Arduino, but using the more complicated N64 controller and its analog joystick has a definite appeal. If you have an Arduino around and don't want to buy an Adaptoid, this instructable will make it possible to play emulated games in Project 64 with your Ardunio and an N64 controller.

Is this hard? / Will it damage my stuff?
This will not modify your controller in any way, and the wiring is extremely simple, so if you do everything carefully there should be no risk to your controller, and you can unhook it at any time to use with a N64 console.

What you will need:
Arduino - $30
Processing 1.0 - free
Arduino Software - free
3 pieces of wire - free (I hope)
USB cable


STEP 1: Wiring the Controller

The first thing you need to do is connect your Arduino to the controller
The controller only uses three leads: +3.3V, signal, and ground.
Looking directly at the plug, ground is farthest left, signal is in the middle, and +3.3V is on the right. Using the wire, connect ground and +3.3V to the respective pins on the Arduino, and connect the signal lead to the Digital 2 pin on the Ardunio.

NOTE: If you have other code on your Arduino, you should disconnect the controller and upload the new code from the next page to the Arduino before powering it up with the controller attached.

Connect the Arduino
Connect the USB cable and the Arduino will have power.

STEP 2: Unpack and Run the Code

This code was written by me, with parts of the N64_Arduino file based on assembly code written by Andrew Brown.

ZIP Archives:
The two Zip files below contain the code needed to run the Arduino and then to interpret the data it sends to the computer. The N64_Arduino file needs to be compiled in the Arduino IDE, and the N64_Controller runs in Processing 1.0.

N64_Arduino
This PDE file should upload to your Arduino and run without a hitch if you have everything connected properly. It simply queries the N64 controller for data on the buttons and Analog stick and sends it back to the computer over the serial port. It is easy enough to modify, for example, you could use the methods from this file to query a controller and use the data to run an Arduino robot instead of transmitting it back to the computer.

N64_Controller
This is a Processing 1.0 project that takes the data transmitted by the Arduino and converts it into keyboard presses that you can map to an emulator like Project 64. You might need to change the line String portName = Serial.list()[1]; to match the your Arduino, it should be either Serial.list()[0]; Serial.list()[1]; or Serial.list()[2];

EDIT: Add
"import java.awt.Robot;"
"import java.awt.AWTException;"
"import java.awt.event.InputEvent;"
to the code if you are using Processing 1.1

N64_Controller_mouse
This is the same as N64_Controller, except that the analog stick controls your mouse, not the arrow keys. A and B are right and left click, respectively. To activate the mouse, press the start button on your controller.

STEP 3: Set Up Project 64

Before you can use the controller, the Arduino needs to be connected and running the code you downloaded in the last step, and Processing 1.0 needs to be open with the N64_Controller program running.

Test it out in Notepad, pressing the A button should type an A, B should type a B, etc.

So now you have a working controller (hopefully) and you want to play some games.

Downl0ad PJ 64
http://www.pj64-emu.com/downloads/

Set Key Mappings
Start PJ 64 and open the settings menu first (Ctrl+T). Change the input controller to N-Rage's direct input. Open the "Configure Controller Plugin" menu and set the mappings using the controller.

Start Playing!
You should be all set to go now! Download some ROMs and start enjoying your homebrew N64 adapter.

STEP 4: Arduino Code in Depth

The N64 Protocol
The bits sent to and from the N64 controller on the one wire interface are encoded in 4 µs wide pulses. A '0' is 3 µs low and 1 µs high. A '1' is 1 µs low and 3 µs high.

The Arduino code in the methods N64_send or N64_receive use very carefully timed assembly code written by Andrew Brown to bit-bang the data line to communicate with the controller. nop blocks are used to wait the appropriate amounts of µs before polling the line of sending data.

On startup, 0x00 is sent to the controller, and then after that the only command used is 0x01 to query for the controller's status.

Data Encoding
When the data is received after a 0x01, it arrives as 16 bits of button information and 16 bits of analog joystick information. The data would look like 44000000000000400044440044000444.

The format of the bits is: A, B, Z, Start, Dup, Ddown, Dleft, Dright, 0, 0, L, R, Cup, Cdown, Cleft, Cright + 16 bits of analog stick position.

The method translate_raw_data() goes through the 32 bits, inserting them into the struct N64_status. The first 16 bits are simple 1 or 0, but the last 16 are translated into an integer approximately in the range (-80, 80) by

    for (i=0; i<8; i++) {
        N64_status.stick_x |= N64_raw_dump[16+i] ? (0x80 >> i) : 0;
    }

After the data is in this simple form, it is easy to do whatever you want with it. In this case, it is simply sent over the serial port in the loop() method as a string of binary data with two integers for the x and y values.

The data sent over the serial port might look like: 0400000000000400 63 -67 which would mean that two buttons were pressed and the control stick was at 63,-67.

STEP 5: References

Andrew Brown's project to create a gamecube to N64 adapter with the Arduino was invaluable while coding for this project: http://www.cs.duke.edu/~brownan/n642gc.html

Helpful schematics can be found here: http://www.raphnet.net/electronique/gc_n64_usb/index_en.php

Information on the N64 controllers proprietary protocol can be found here: http://www.mixdown.ca/n64dev/

112 Comments

Great post!

With the new version of Arduion-1.0 i did have to include the "import" stuff & as well take care of RXTX-2.2pre1 / RXTX-2.2pre2 (see below)


Minor Issue:
Has anyone made a patch yet for the controller's sensitivity? With a lot of games this is a HUGE problem.

Overall, great job throwing together the documentation


Fix the RXTX errors:
http://rxtx.qbang.org/wiki/index.php/Download
Hey xenoic, which version do I need to download? And do I need the source or binary?
Currently they are only offering "RxTx-2.2 pre2". So this should help you out.

just pull back the latest RxTx 2.2 preview version & you should be good
I am trying to use this with an esp32 and a battery. Any thoughts? I am a nooooooob with electronics and coding. I can put the stuff together and fumble through if someone points me in the right direction...
How do you open the z64 files on the A64? (Arduino 64)
I've noticed that if you want precise movements with the Analog Stick in an emulator like Project 64 the N64_Controller_Mouse won't work. So I fixed this issue replacing Processing with FreePie and now the stick works marvelously. Here you'll find my solution: https://github.com/mlacosta/n64analogstick
Can you help me get this to work with switch?

I can't get this working, im using an arduino mega 2560 with uploaded , pin 2, 3.3v and gnd. I added import java.awt.Robot;

import java.awt.AWTException;

import java.awt.event.InputEvent;

import java.awt.event.KeyEvent;

I had to change my serial.list()[1] to 0 for coms port 3

i am not getting any output from the program from ether controller

I know this is an old project, but it is still awesome. Tried it out with processing 2.2.1 which seems to work fine. Just include the import already listed in the description and this import java.awt.event.KeyEvent; --- So total additional import is

import java.awt.Robot;

import java.awt.AWTException;

import java.awt.event.InputEvent;

import java.awt.event.KeyEvent;

Thank you for this instructable, I'm finally able to play again majora's mask as it should! (after some adjustment now that it is version 3 of processing :) )

hello, and Great instructable! very easy to follow. however, i have one question. i get an error that says

Error, disabling serialEvent() for COM3

null

any help? it is greatly appreciated, and keep up the good work!

Hi, I'm trying to get this to run on Ubuntu 14.04.

I seem to only be able to get processing 3 to run natively and I'm running processing 1.5.1 out of WINE.

When I try to run the processing code I get this error message and I have no idea how to make heads or tails of it, any help with getting this up and running would be really appreciated! I mean to link it up to pure data and play some music with my controller! (also just Jet Force Gemini)

woops, forgot to include the error message- here's some info

Error inside Serial.<init>0

(with the line N64 Connection = new Serial(this, portName, 115200) highlighted)

the whole error message is much longer and I'll type it up soon- can't seem to copy paste...

i suppose I'll type the whole thing up if you let me know if you can help.

The first line is:

gnu.io.PortInUseException; Unkonwn Application

I have actually built on of the v-usb based adapters from raphnet.net I part of the way though porting his implementation into something more arduino friendly. I saw this link and spent a little time today to see how well it works. The code isn't very pretty but it does work. I modified it to read 2 n64 controllers at the same time and created a HID usb descriptor for v-usb for it to show up as 2 gamepads. No host software needed. Just a note, 2 of your links from above are dead.

Hopefully I can find the time to spin off a continuing instructable that shows how much more power you can get out of this set up.

Thanks for sharing

Has anyone got this to work with a RetroPie? http://blog.petrockblock.com/retropie/

Im trying to use custom pads that send keypresses like the makey makey to control it.
how do i do it plz help been stuck on this for weeks
More Comments