Read the buttons’ states – A function reads from the controller, which buttons are currently pressed

Help You parse the data – Using simple constants, processing the returned data is peanuts for You.

Not only that the library is incredible easy to use, You can also easily get to know the structure of an Arduino library, how these things are developed and what’s happening in the background. All the code is documented, the Commits on GitHub are also full of useful details. If you’re interested, dive in – it’s easy!

To ensure you have the super-duper latest code, grab it from GitHub. Just use the git clone command from above on your OS of choice. However, if you don’t have the git program installed, feel free to download the repository as a ZIP. But note: When downloading the repo as a zip, you have to rename the extracted folder SNESpaduino-master (remove the -master)!

If you’re new to libraries, the official Arduino Library reference explains how you can install one. When You have successfully imported the library into the IDE, go over to connect your gamepad.

Connect the SNES Gamepad to an Arduino

The SNES Gamepad has 5 contacts: +5V, GND, Latch, Clock, Data. Here is the pin configuration of the connector (from left to right, starting with the group of 4 contacts). Also take a look at the picture, where pin-numbers as well as color coding is noted:

Nr

1

2

3

4

5

6

7

Use

+5V

Clock

Latch

Data

N/C

N/C

GND

Color

White

Yellow

Orange

Red

—

—

Brown

Pin configuration and color codes of the SNES gamepad.

Connect the gamepad’s GND / 5V to your Arduino’s Ground / +5V. Latch, Clock and Data go to any In/Out pin. Even analog ones. Over the Clock and Latch lines, the Arduino will send data, 5V/GND (HIGH/LOW), to tell the gamepad we’d like to receive the current state of all buttons. Over the Data line, our Arduino will receive that data.

In my examples, I used the pins 19, 20 and 21 of my Arduino Mega, but as said, you may use any In/Out Pin.

Test library and gamepad

Once the library is installed and the gamepad connected to the Arduino, it’s time to test things. The library includes 2 example sketches. Those are available under File > Examples > SNESpaduino. First, load the SNESpaduinoPrintToSerial sketch, compile it and load it onto the Arduino. The project’s code is documented, so you should be able to figure out what it does. If not, leave me a comment ;)

Using the library works like this: First, an instance of SNESpaduino is created, handing over the 3 pin numbers for Latch, Clock and Data lines to the constructor. Next, you can use the function getButtons() which will return the current state of all 12 Buttons as a 16 digit integer. Each bit in that integer represents a button: Is this bit 0 / LOW / False, the button is currently being pressed. So if a bit is 1 / HIGH / True, that means the according button is currently NOT being pressed.

This is due to the controller’s hardware design. Because this might be confusing, getButtons() will by default return the opposite (negative) bit pattern. This way, it’s easier to use If-Statements to check which buttons are being pressed (and seems more intuitive). If you need to have the “raw”, non-inverted output from the controller, call getButtons(boolean return_inverted) with the parameter false: getButtons(false);

Parsing data

The getButtons() function returns a uint16_t. As mentioned above, the bits in this integer represent which buttons are being pressed. To work with the data, here is a table to determine which bit represents which button:

Nr.

12

11

10

9

8

7

6

5

4

3

2

1

Button

R

L

X

A

RIGHT

LEFT

DOWN

UP

START

SELECT

Y

B

Value

2048

1024

512

256

128

64

32

16

8

4

2

1

HEX

0x800

0x400

0x200

0x100

0x80

0x40

0x20

0x10

0x08

0x04

0x02

0x01

So, if only A is being pressed, getButtons(); returns the value 1111000100000000

IMPORTANT: The last 4 bits (Most Significant Bits, MSB, left) must be ignored! getButtons() ony affects the first 12 bits (Least Significant Bits, LSB, right). So the remaining 4 bits will never be touched, they will always return as 1 (or non-inverting: 0).

Now, using logic operations and a bitmask, individual bits can easily be checked. For this purpose, SNESpaduino.h contains constants (preprocessor directives) BTN_*. These values are the same as the ones above in the table. If you wanted to check if The A button is being pressed, simply apply the logic AND function to the buttons’ states and the bitmask BTN_A. If this statement (button_states & BTN_A) is true, button A is in fact being pressed. See the 2 examples shipped with the library, it’s really simple.

I’d recommend modifying SNESpaduinoPrintToSerial – you can easily get used to the library’s usage while writing a simple function like… Check each individual button if it’s being pressed (and write that to Serial, just like in the example).

Contact

Please, feel free to contact me for any further questions, ideas, pull requests and so on. Write a comment or an eMail, or twitter @tacticalcode

All code (and this tutorial) is being published under CC-BY-SA license. So Share, Remix and Hack on!

This is the perfect website for everyone who wants
to understand this topic. You realize a whole lot its
almost hard to argue with you (not that I
personally will need to…HaHa). You definitely put a fresh spin on a topic which has been discussed for years.
Excellent stuff, just great!

Excellent article. Please be a little bit more specific about the pin order for the SNESpaduino object. Using the example provided the order is: SNESpaduino pad(PIN_LATCH, PIN_CLOCK, PIN_DATA). It was a gotcha for me eheh.

Im getting the same thing as you, i found that how the arduino interprets a button press is inverted. so if youre not pressing a button, it thinks you are. If you are pressing a button, it thinks youre not. This is regardless of whether or not you invert getButtons via getButtons(false), which puzzles me.

Hey there!
At the moment I don’t have any SNES pads here, so I can’t fiddle with the actual hardware. Does it make any difference when you call getButtons(false); ? (Line 29 in SNESpaduinoPrintToSerial example)
Are your wires attached to the correct pins? Which model of SNES pad did you use? (I only tested the PAL/EU Version)
Just to make sure: Delete %USERPROFILE%\Documents\Arduino\libraries\SNESpadunio
and extract the git version (https://github.com/TacticalCode/SNESpaduino/archive/master.zip) to the libraries folder (rename to delete the “-master” in the folder name). Load, Compile and Flash the SNESpadunioPrintToSerial example and watch the serial monitor.
The last time I checked, everything worked fine with my Arduino Mega R3 and original SNES pad.

Thanks so much for the quick response, i reinstalled the librtary and i tested the code with getButtons(false) and still no luck. It would seem as though i have a different controller, the 5 wires on my controller are soldered directly to the board. my best guess is that is the problem.

Bad news is that the creators library ONLY WORKS WITH PAL/EU CONTROLLERS.
After encountering problems that i discussed in my previous comments, i went out and bought a PAL controller to see if it would work. It does.

If you have a non-PAL SNES controller, your serial monitor will be spammed as though everything is being pressed at once OR the only input button that will work is B, and when you press it it will trigger every button press.

GOOD NEWS IS THAT IF YOU DONT HAVE A PAL CONTROLLER THERE IS A SOLUTION.
The solution is in the form of another library by a different person, which can be found herehttps://code.google.com/p/nespad/

This code is not as nice and neat as the OP’s, but it gets the job done. (You should thank me as finding it took some digging)

Because the linked library is so old, you will have to go into the snespad.cpp file and change the “#include Wprogram.h” to #include Arduino.h”

You’ll probably never see this comment but I figured it was worth a shot. I’m running into the exact issue you described, and I tried the library that you linked to and it doesn’t solve the problem for me. Do you know what the difference is between PAL and non-PAL controllers? I can’t find any documentation online.

I’am running into same problem as Joe above. The other link is not helping either. I tried get all data from gamepad but it does not help, it looks like pressing buttons don’t change any bit. I assume maybe something with clock/strobe is not working correctly.

I will add that this gamepad is not made by Nintentdo, it’s Nintendo compatibile, had someone gone through similar problem?

Anyone hoping to buy their first car, or replace an existing one can be forgiven for being a
little confused about the range of credit solutions available to finance the
vehicle. Is there any reason why any company should offer a car loan to anybody
with a poor credit record. It is because having your own car is most
likely more beneficial to you in so many ways than not
having one.