Git Product home page Git Product logo

i2c-linux-gamepad-using-ads1015-ads1115-and-mcp23017's Introduction

I2C Analog/Digital Gamepad for the Raspberry Pi

Overview

This project allows you to create a gamepad using two I2C modules, suitable for a variety of games on the Raspberry Pi. The gamepad created can handle both analog and digital input, giving you a full-fledged gaming experience. This project is perfect for gaming enthusiasts looking for a fun and engaging DIY project, educators teaching technology or programming, and anyone interested in learning more about the interaction between hardware and software in game controllers.

The ADS1015 or ADS1115 handle analog input for the joystick, and the MCP23017 handles digital input for up to 16 buttons. By the end of this guide, you'll have a functional gamepad that you can use with your favorite games on your Raspberry Pi.

Prerequisites

Hardware:

  • A Raspberry Pi or similar board (the install script was only tested on a Raspberry Pi)
  • ADS1015 or ADS1115 I2C module
  • MCP23017 I2C module

Software:

  • GCC Compiler
  • Jstest to verify function
  • I2C Tools for advanced troubleshooting

Hardware Setup

diagram

The wiring and setup of your Raspberry Pi for the gamepad is broken down into two parts:

  1. Wiring the ADS1015 or ADS1115 Analog-to-Digital Converter (ADC). Both the ADS1015 and ADS1115 use the same I2C communication protocol to read analog values. You can wire each chip to the Pi in the following way:
  • ADS1x15 VDD to Raspberry Pi 3.3V
  • ADS1x15 GND to Raspberry Pi GND
  • ADS1x15 SCL to Raspberry Pi SCL
  • ADS1x15 SDA to Raspberry Pi SDA​
  • ADS1x15 A0 to the X axis of your left joystick
  • ADS1x15 A1 to the Y axis of your left joystick
  • ADS1x15 A2 to the X axis of your right joystick
  • ADS1x15 A3 to the Y axis of your right joystick
  • Joystick VDD to Raspberry Pi 3.3V
  • Joystick GND to Raspberry Pi GND
  1. Wiring the MCP23017 Digital Input. The MCP23017 handles digital input for up to 16 buttons. The wiring of the MCP23017 to the Raspberry Pi is as follows:
  • MCP23017 VDD to Raspberry Pi 3.3V
  • MCP23017 GND to Raspberry Pi GND
  • MCP23017 SCL to Raspberry Pi SCL
  • MCP23017 SDA to Raspberry Pi SDA​
  • You can change the address of the chip by hooking each of the 3 address pins to either ground or 3.3v. The driver expects all of these pins to be connected to GND, which the module should have done already, meaning that you can leave the pins alone.
  • GPB0 – GPB7 and GPA0 – GPA7 to your pushbuttons. The GPIOs are pulled up to 3.3v by the module, so you should connect each pushbutton to ground.

Software Setup

Before proceeding with the software installation, ensure that you have the necessary software installed on your Raspberry Pi, including the GCC Compiler.

If you haven't installed GCC Compiler, you can do so with the following command:

sudo apt-get install -y gcc

Using the Setup Script

Installation is done by copying setup.sh, gamepad.c, scan.c, and gamepad.service to a folder on your Raspberry Pi (or other Linux device) and running sudo bash setup.sh. The driver will compile and I2C will get configured, and then the script will ask whether you want to load the driver at startup.

Manual Compilation and Running

If you prefer to compile and run the driver manually without using the setup script, follow these steps:

  1. Compile the Gamepad Driver: Navigate to the directory containing the gamepad.c file and compile it using the GCC Compiler with the following command:

    gcc -O3 -o gamepad gamepad.c
    

    This will create an executable file named gamepad.

  2. Run the Gamepad Driver: You can run the gamepad driver with the following command:

    sudo ./gamepad
    

    Note: The driver needs to be run with root privileges to access the I2C interface.

  3. Set the Gamepad Driver to Run at Startup: If you want the gamepad driver to run automatically at startup, you can add it to the system's service. First, open the gamepad.service file and make sure the path to the gamepad executable is correct. Then, copy the gamepad.service file to the /etc/systemd/system/ directory and enable it with the following commands:

    sudo cp gamepad.service /etc/systemd/system/
    sudo systemctl enable gamepad.service
    sudo systemctl start gamepad.service
    

    This will set the gamepad driver to start automatically at boot.

Usage Instructions

Testing the Gamepad

Before you start playing, you might want to test whether the gamepad is working as expected. You can do this using a tool like jstest.

To use jstest, you can use the following command:

jstest /dev/input/js0

If jstest is not installed, you can install it with:

sudo apt-get install -y joystick

The command will display the status of your joystick and buttons. It assumes that the othermod gamepad is the first gamepad. If your device is not at js0, you may need to try other numbers such as js1, etc.

You can list your devices with the following command:

ls /dev/input/js*

Using the Gamepad in Games

The gamepad should be detected in the same way that USB gamepads are detected. To use the gamepad, go to the game settings and select the othermod gamepad as your input device.

Troubleshooting

If you encounter any issues while using the gamepad, verify that the MCP23017 and ADS1x15 I2C modules are correctly connected.

Other common issues and their solutions are:

  • Gamepad not recognized: Try rebooting your Raspberry Pi. If the problem persists, ensure that the driver is being loaded at startup.
  • Buttons/joystick not responsive: Double-check your wiring. If a button or joystick axis is not working, it may be due to a loose connection.

You can also use the I2C Tools for advanced troubleshooting (sudo apt install -y i2c-tools).

Contributing

License

ADS1015 MCP23017

video guide on othermod.com

i2c-linux-gamepad-using-ads1015-ads1115-and-mcp23017's People

Contributors

othermod avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

i2c-linux-gamepad-using-ads1015-ads1115-and-mcp23017's Issues

Right Thumbstick Doesn't Get Detected On Startup

I am using a pi 4 b and the same MCP23017 & ADS1015 as you did in the guide and video, the issue isn't when I first run the command sudo ./gamepad -j 2 & as when I run it for the first time all buttons and both joysticks work, but once I run setup.sh and reboot only one joystick will work and when I do jstest there are only 2 axes unless I do sudo ./gamepad -j 2 & I'm not sure what the issue is as I have done this about 6 times now with having it start on boot both with setup.sh and the code in the readme file which I believe is the manual process. I appreciate any help you can provide.

Haydn.

Switching i2c from 1 to 0

Hi

Would it be possible to switch the i2c bus to i2c0? The pi sugar i am using uses i2c1.

Do i just have to change the statement
#define I2C_BUS "/dev/i2c-1"
#define I2C_BUS "/dev/i2c-0"
In gamepad.c?

Changing to a ADS1115 instead of ADS1015

Hi,

How hard would it be to change the code for the ADS1015 and make it the ADS1115. I'm thinking possibly just a find and replace 1015 with 1115 should do the trick?

Any thoughts on this?

How can I change the i2c address?

Hi,
I'm trying to use your code with a PCF8591 (for test) and later a ADS1015.
For now it is not working and I suspect that might be because the i2c address of the PCF8591 is 0x48.
Is there a way to change that?

Do I need 2 ADS1115

Hi,

Can I use the ads1115 and just tweak the code and if so can I just use the one module for 2 joysticks?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.