Analog Joystick on RetroPie Using an I2C Module (Guide)
Re: Analog Joystick
Hey guys. I promised an update once I had it, so here it is.
I asked a while back whether anyone knew a good way to get a joystick working in RetroPie, using an interface such as I2C or SPI. You guys attempted to help, but there wasn't a lot of useful info out there. I've made a solution.
The module I'm using to get analog input is this one. That link will take you to eBay, to a module containing a chip called ADS1015. You can also get it on my site if you want, but homeboy gets a cut. It provides 4 12-bit analog inputs over the I2C interface. I won't get too deep into the wiring details, since you guys can probably figure it out. Basically SDA goes to SDA (for I2C-0 BCM pin 0, physical pin 27), SCL goes to SCL (for I2C-0, BCM pin 1, physical pin 28), VCC (power) goes to a 3.3v pin, GND goes to a GND pin, and ADDR (address) goes to a GND pin. The other 4 pins are for the analog input, but only channels 0 and 1 are needed for a joystick. You're also going to be using one pin on the Raspberry Pi as a button during setup, and GPIO 27 (physical pin 13) is preset as this pin. When the time comes, you'll need to run a jumper from this pin to a GND pin to simulate pressing a button on the joystick. If your joystick actually has a button then you'll just run the joystick button to this pin permanently.
Here's where the guide gets incredibly complicated (just kidding, I did a ton of work so you don't have to). I'm starting with a fresh image of RetroPie on the SD card, so if you have problems you should do the same. I've got all this set up for an offline installation since not everyone has internet on their Pi. You'll need to go to Github and download and extract the zip file. Open the zipped folder and copy the contents to your RetroPie SD card. You'll need to overwrite the config.txt on your SD card and make sure that all the other folders copy correctly. A couple really important files will copy to the overlays folder.
Boot up your system with a keyboard, let RetroPie do it's thing (again, assuming fresh install), and do the controller configuration. Use your keyboard to do the configuration initially, it'll make the next part easier.
After the keyboard is configured as input, press F4 to exit RetroPie. Then type the following commands.
cd /boot/uinput/ (then press enter)
sudo python setup.py install (press enter)
sudo python joystick.py & (press enter)
jstest /dev/input/js0 (press enter)
Then wiggle the joystick around and make sure it is picked up.
press CTRL and C on the keyboard to exit
Type the following to set the joystick software to load on bootup (I'm assuming you're still in /boot/examples):
sudo bash install.sh (press enter)
exit (press enter, and you'll be taken back to RetroPie)
Use the keyboard to restart the controller configuration. It should already say there is a controller attached. Press the joystick button (or simulate pressing the button by connecting GPIO 27 to GND for a few seconds), and the configuration will start. Use the keyboard to press down until you reach the one called "LEFT ANALOG UP", and this is where you do that motion on the joystick to get it to detect. Then go through the other three and use the keyboard to arrow down and hit OK. You're done.
Some games won't pick up the joystick automatically, so you'll possibly have to go into retroarch and configure some things. I'm not getting into that part because I'm exhausted from typing. Love you guys.
I can't seem to get the pi to define js0 in /dev/input/
When I run joystick.py, I'm receiving this error:
Traceback (most recent call last):
File "joystick.py", line 58, in <module>
an0 = int(open('/sys/class/hwmon/hwmon0/device/in4_input').read())
IOError: [Errno 121] Remote I/O error
My first thought was I wired the ADS1015 incorrectly, but I'm able to pick up the signal when I run Adafruit's troubleshooting python script so I know it's connected properly.
adamspc last edited by
Did you add the overlays to the overlays folder on the SD card, and the overlay to config.txt?
Also, if you did do that already, what GPIO pins are you soldering the I2C pins to?
It was the I2C GPIO connections, I initially had them running to GPIO 2 (SDA) and GPIO 3 (SCL). I have since switched them to physical pins 27 and 28, respectively. Looks like I'm able to pickup on the joystick voltages now. Thanks for providing this write-up, saved me a lot of headache.
No problem. This can be modified to use any of the I2C pins (I2C-0, I2C-1, or I2C-2), and each set of them is available in a couple locations. I went a little off the beaten path by using I2C-0 because it was needed for my project.
Also, I went with the overlays to reduce CPU usage substantially. There's a lot of room for improvement by using built-in joystick drivers, but that's over my head at the moment.
Im having a very similar issue with Remote I/O Error. I can confirm that the I2c module is correctly wired as i can receive analog data readings from i2c-1 using Adafruits' python library (https://github.com/adafruit/Adafruit_Python_ADS1x15). I used i2c-1 to make sure i had it wired properly, then reinstalled Retropie, copied over files as indicated (config.txt + folders to /boot), switched the i2c pins back to i2c-0 and ran the instructions above but still getting the error. Not sure what im missing?? Do you think it could be the latest version of Retropie given this post is almost 6 months old?
adamspc last edited by
Shouldn't be the updates causing it. I'm using the same method on 4.4 without issue. Most likely it's an issue with the connection to the I2C-0 bus, or the bus not being enabled,
It's time for me to update this anyway, so I'll go ahead and add I2C-1 compatibility over the next couple days.
@adamspc Thank you. I did manage to get it working in the end, but unfortunately i had to revert back to the baked-in ADS1115/ADS1015 overlays to get the inputs to read correctly. For anyone who is having the same issues its worth having a read of this article: http://www.jumpnowtek.com/rpi/Using-ads1115-ADCs-with-Raspberry-Pis.html