It's easy to use the PC Joystick to seesaw adapter with Python or CircuitPython, and the Adafruit_CircuitPython_seesaw module. This module allows you to easily write Python code that reads the movement of the joystick and button inputs over I2C.
You can use this adapter 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 an I2C adapter to your board exactly as follows. The following is the adapter wired to a Feather RP2040 using the STEMMA connector:
- Board STEMMA 3V to adapter VIN (red wire)
- Board STEMMA GND to adapter GND (black wire)
- Board STEMMA SCL to adapter SCL (yellow wire)
- Board STEMMA SDA to adapter SDA (blue wire)
The following is the adapter wired to a Feather RP2040 using a solderless breadboard:
- Board 3V to adapter VIN (red wire)
- Board GND to adapter GND (black wire)
- Board SCL to adapter SCL (yellow wire)
- Board SDA to adapter SDA (blue wire)
Python Computer Wiring
Since there are 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.
Here's the Raspberry Pi wired with I2C using the STEMMA connector:
- Pi 3V to adapter VIN (red wire)
- Pi GND to adapter GND (black wire)
- Pi SCL to adapter SCL (yellow wire)
- Pi SDA to adapter SDA (blue wire)
Here's the Raspberry Pi wired with I2C using a solderless breadboard:
- Pi 3V to adapter VIN (red wire)
- Pi GND to adapter GND (black wire)
- Pi SCL to adapter SCL (yellow wire)
- Pi SDA to adapter SDA (blue wire)
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: 2023 Limor Fried for Adafruit Industries # # SPDX-License-Identifier: MIT import time import board from micropython import const from adafruit_seesaw.seesaw import Seesaw BUTTON_1 = const(3) BUTTON_2 = const(13) BUTTON_3 = const(2) BUTTON_4 = const(14) JOY1_X = const(1) JOY1_Y = const(15) JOY2_X = const(0) JOY2_Y = const(16) button_mask = const( (1 << BUTTON_1) | (1 << BUTTON_2) | (1 << BUTTON_3) | (1 << BUTTON_4) ) 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=0x49) seesaw.pin_mode_bulk(button_mask, seesaw.INPUT_PULLUP) last_x = 0 last_y = 0 x = 0 y = 0 while True: # These joysticks are really jittery so let's take 4 samples of each axis for i in range(4): x += seesaw.analog_read(JOY1_X) y += seesaw.analog_read(JOY1_Y) # take average reading x /= 4 y /= 4 # PC joysticks aren't true voltage divider because we have a fixed 10K # we dont know the normalized value so we're just going to give you # the result in 'Kohms' for easier printing x = 1024 / x - 1 y = 1024 / y - 1 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_1): print("Button 1 pressed") if not buttons & (1 << BUTTON_2): print("Button 2 pressed") if not buttons & (1 << BUTTON_3): print("Button 3 pressed") if not buttons & (1 << BUTTON_4): print("Button 4 pressed") time.sleep(0.01)
Plug in a PC joystick to the 15 pin connector socket port on the adapter. Then, start up the example code. The serial console will print out whenever a button is pressed or the joystick is moved.
The loop uses some math to take an average reading of the two analog pins for the joystick potentiometers since they can be really jittery. This provides a more stable reading from the controller.
Text editor powered by tinymce.