Saturday, July 21, 2007

NES controller to arduino

Couldn't sleep this morning. Instead, i connected a NES controller that was lying around to my Arduino. The NES controller is actually digital, so it only uses three digital pins on the Arduino (plus positive and ground voltage pins) to read the eight button states individually. The code below has been tested and working. This is all thanks to this handy chapter on videogame hardware. Thanks!

I can't think of anything overly useful for this, apart from gutting mechanically defective NES controllers for their 4021 shift registers... but hey, I'm tired. I'm sure there is something cool that can be done with it.

Here is the pinout of the 4021:
P0 to P7 are the parallel digital inputs.
Latch is PL.
Clock is CP.
Serial Out is 07.

Anyway, here are the pinouts of the NES controller (lead plug on controller, not the socket on the console):
The state of the controller is fed out in the following order: A button, B button, select, start, up, down, left, right. Because the code shifts each bit to the left once it has been received, the first bit becomes the most significant bit. The format of the data byte is as follows:

bit 7 = A
bit 6 = B
bit 5 = select
bit 4 = start
bit 3 = up
bit 2 = down
bit 1 = left
bit 0 = right


[expand / collapse code]

8 comments:

Anonymous said...

hey, that link to the videogame hardware manual should be http://www.parallax.com/dl/docs/prod/prop/Hydra-Ch6All-v1.0.pdf

just sayin..

Sebastian Tomczak said...

hey, i fixed the link, thanks for picking up the error. Cheers,
Seb Tomczak

Anonymous said...

I'm having trouble getting this program to work. I get a lot eights back from the console and the software locks up but that might be another problem I've got (namely I'm using an old laptop to run Arduino.) Anyway the reason for my responce is that the compiler has an issue with the line that indicates controller_data = controller_data << style= ......yada yada yada...
I don't know why... it says style is unrecognized. I also am not quite sure that you syntax is propper on that line or maybe it's just displayed strangley either way I can't get the program to run well.

I do have one question. Is the result of this program to have the button pushed displayed on the terminal console within Arduino? If not how would you get it to do that?

Anonymous said...

I'm having trouble getting this program to work. I get a lot eights back from the console and the software locks up but that might be another problem I've got (namely I'm using an old laptop to run Arduino.) Anyway the reason for my responce is that the compiler has an issue with the line that indicates controller_data = controller_data << style= ......yada yada yada...
I don't know why... it says style is unrecognized. I also am not quite sure that you syntax is propper on that line or maybe it's just displayed strangley either way I can't get the program to run well.

I do have one question. Is the result of this program to have the button pushed displayed on the terminal console within Arduino? If not how would you get it to do that?

Sebastian Tomczak said...

Apologies for the delay in response; i have been on holidays.

This is an error of trying to auto-convert arduino code to html. apologies. I will try an correct as soon as possible. Probably today.

The result of the program is to get the current state of the NES pad as single-bit flags in an 8 bit byte. A high state (1) in a bit position indicates that a button is pressed at that point in time.

bit 7 = A
bit 6 = B
bit 5 = select
bit 4 = start
bit 3 = up
bit 2 = down
bit 1 = left
bit 0 = right

So for example, if i read back
11000000, then A and B are both pressed.

Sebastian Tomczak said...

hey, i updated the code. the line should read:

controller_data = controller_data << 1;
controller_data = controller_data + digitalRead(datin);

This makes much more sense!

;-)

Anonymous said...

Works like a charm for me. Anon, are you sure you have the serial monitor set to 57600?

Anonymous said...

How would you write a program to make a 00010000 value turn on a LED?