It's easy to use the Mini I2C STEMMA QT Gamepad with Python or CircuitPython, and the Adafruit_CircuitPython_seesaw module. This module allows you to easily write Python code that reads the joystick analog values and the six button presses (X, Y, A, B, Start, and Select).
You can use the Gamepad with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.
CircuitPython Microcontroller Wiring
First wire up the Gamepad to your board, exactly as shown below.
The following is the Gamepad connected to a Feather RP2040 using STEMMA QT:
Simply connect a STEMMA QT cable from the STEMMA QT connector on the Feather to the STEMMA QT connector on the Gamepad.
The following is the Gamepad connected to a Feather RP2040 using a solderless breadboard:
- Board STEMMA 3V to Gamepad VIN (red wire)
- Board STEMMA GND to Gamepad GND (black wire)
- Board STEMMA SCL to Gamepad SCL (yellow wire)
- Board STEMMA SDA to Gamepad SDA (blue wire)
Python Computer Wiring
Since there's dozens of Linux computers/boards you can use we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported.
The following is a Raspberry Pi wired with I2C using the Gamepad STEMMA connector:
Using a STEMMA QT to socket cable plugged into the STEMMA QT connector on the Gamepad, connect the wires to the Pi as follows:
- Red wire to Pi 3V
- Black wire to Pi GND
- Yellow wire to Pi SCL
- Blue wire to Pi SDA
The following is a Raspberry Pi wired with I2C using a solderless breadboard:
- Pi 3V to Gamepad VIN (red wire)
- Pi GND to Gamepad GND (black wire)
- Pi SCL to Gamepad SCL (yellow wire)
- Pi SDA to Gamepad SDA (blue wire)
The Pi OS default I2C baudrate is 100 kHz, but the Gamepad works best at 400 kHz.
Modify the i2c_arm_baudrate variable in the /boot/config.txt file to look like this:
i2c_arm_baudrate=400000
Python Installation of seesaw Library
You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!
Once that's done, from your command line run the following command:
pip3 install adafruit-circuitpython-seesaw
If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!
CircuitPython Usage
To use with CircuitPython, you need to first install the Adafruit_CircuitPython_seesaw library, and its dependencies, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY/lib folder should contain the following folders and file:
- adafruit_bus_device/
- adafruit_seesaw/
- adafruit_pixelbuf.mpy
Python Usage
Once you have the library pip3
installed on your computer, copy or download the following example to your computer, and run the following, replacing code.py with whatever you named the file:
python3 code.py
Example Code
If running CircuitPython: Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
If running Python: The console output will appear wherever you are running Python.
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-FileCopyrightText: 2023 Kattni Rembor for Adafruit Industries # SPDX-License-Identifier: MIT import time import board from micropython import const from adafruit_seesaw.seesaw import Seesaw BUTTON_X = const(6) BUTTON_Y = const(2) BUTTON_A = const(5) BUTTON_B = const(1) BUTTON_SELECT = const(0) BUTTON_START = const(16) button_mask = const( (1 << BUTTON_X) | (1 << BUTTON_Y) | (1 << BUTTON_A) | (1 << BUTTON_B) | (1 << BUTTON_SELECT) | (1 << BUTTON_START) ) i2c_bus = board.STEMMA_I2C() # The built-in STEMMA QT connector on the microcontroller # i2c_bus = board.I2C() # Uses board.SCL and board.SDA. Use with breadboard. seesaw = Seesaw(i2c_bus, addr=0x50) seesaw.pin_mode_bulk(button_mask, seesaw.INPUT_PULLUP) last_x = 0 last_y = 0 while True: x = 1023 - seesaw.analog_read(14) y = 1023 - seesaw.analog_read(15) if (abs(x - last_x) > 3) or (abs(y - last_y) > 3): print(x, y) last_x = x last_y = y buttons = seesaw.digital_read_bulk(button_mask) if not buttons & (1 << BUTTON_X): print("Button x pressed") if not buttons & (1 << BUTTON_Y): print("Button Y pressed") if not buttons & (1 << BUTTON_A): print("Button A pressed") if not buttons & (1 << BUTTON_B): print("Button B pressed") if not buttons & (1 << BUTTON_SELECT): print("Button Select pressed") if not buttons & (1 << BUTTON_START): print("Button Start pressed") time.sleep(0.01)
Reading Joystick Values
The joystick is made up of two potentiometers at a 90 degree angle to each other, creating an x and y axis across it, each of which produce analog values ranging from 0 to 1023. To read the values, you use the seesaw firmware analog_read
and provide it the seesaw firmware pin to which each axis is connected. The x axis is on pin 14, and the y axis is on pin 15.
Due to the physical orientation of the joystick on the gamepad, it is necessary to reverse the analog values so that when x is to the left or y is down, both report 0
, and when x is to the right or y is up, both report 1023
. This is done by subtracting both axis' values from 1023 when reading the value.
x = 1023 - seesaw.analog_read(14) y = 1023 - seesaw.analog_read(15)
The values printed to the serial console are printed within an if
block. This code is used to only print the values when they have changed more than 3. This avoids constant printing in the event of noise or vibration of the gamepad itself. That way, it only prints when you are actively and deliberately moving the joystick around.
if (abs(x - last_x) > 3) or (abs(y - last_y) > 3): print(x, y) last_x = x last_y = y
Reading Button Presses
The button variables are created and set to be equal to the pin to which they are connected in the seesaw firmware (as shown on the Pinouts page). For example, the X button is connected to pin 6 in the seesaw firmware, and therefore BUTTON_X = const(6)
.
BUTTON_X = const(6) BUTTON_Y = const(2) BUTTON_A = const(5) BUTTON_B = const(1) BUTTON_SELECT = const(0) BUTTON_START = const(16)
Then the button mask is created. This is what tells the seesaw on the Gamepad which pins are of interest, e.g. only the pins connected to each of the six buttons.
button_mask = const( (1 << BUTTON_X) | (1 << BUTTON_Y) | (1 << BUTTON_A) | (1 << BUTTON_B) | (1 << BUTTON_SELECT) | (1 << BUTTON_START) )
The button mask is then used in two places. It is first used to set all the buttons up as inputs with pullups.
seesaw.pin_mode_bulk(button_mask, seesaw.INPUT_PULLUP)
It is then used inside the loop to begin looking for button presses.
buttons = seesaw.digital_read_bulk(button_mask)
The remainder of the loop checks for button presses, one at a time, with an if
block for each. For any button that is pressed (can be more than one) a print statement is output.
if not buttons & (1 << BUTTON_X): print("Button x pressed") if not buttons & (1 << BUTTON_Y): print("Button Y pressed") if not buttons & (1 << BUTTON_A): print("Button A pressed") if not buttons & (1 << BUTTON_B): print("Button B pressed") if not buttons & (1 << BUTTON_SELECT): print("Button Select pressed") if not buttons & (1 << BUTTON_START): print("Button Start pressed")
Using the Gamepad Demo
This example prints the x and y analog joystick values to the serial console, as well as the button presses. Try moving the joystick to see the x and y values change, and press the buttons to see the button name.
Text editor powered by tinymce.