It's easy to use the ANO Rotary Encoder to I2C adapter with Python or CircuitPython, and the Adafruit_CircuitPython_seesaw module. This module allows you to easily write Python code that reads encoder position (relative to the starting position) and the five button presses (up, down, left, right and select).
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'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.
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
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 John Furcean # SPDX-License-Identifier: MIT """I2C ANO rotary encoder simple test example.""" import board from adafruit_seesaw import seesaw, rotaryio, digitalio # For use with the STEMMA connector on QT Py RP2040 # import busio # i2c = busio.I2C(board.SCL1, board.SDA1) # seesaw = seesaw.Seesaw(i2c, 0x49) i2c = board.I2C() # uses board.SCL and board.SDA # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller seesaw = seesaw.Seesaw(i2c, addr=0x49) seesaw_product = (seesaw.get_version() >> 16) & 0xFFFF print(f"Found product {seesaw_product}") if seesaw_product != 5740: print("Wrong firmware loaded? Expected 5740") seesaw.pin_mode(1, seesaw.INPUT_PULLUP) seesaw.pin_mode(2, seesaw.INPUT_PULLUP) seesaw.pin_mode(3, seesaw.INPUT_PULLUP) seesaw.pin_mode(4, seesaw.INPUT_PULLUP) seesaw.pin_mode(5, seesaw.INPUT_PULLUP) select = digitalio.DigitalIO(seesaw, 1) select_held = False up = digitalio.DigitalIO(seesaw, 2) up_held = False left = digitalio.DigitalIO(seesaw, 3) left_held = False down = digitalio.DigitalIO(seesaw, 4) down_held = False right = digitalio.DigitalIO(seesaw, 5) right_held = False encoder = rotaryio.IncrementalEncoder(seesaw) last_position = None buttons = [select, up, left, down, right] button_names = ["Select", "Up", "Left", "Down", "Right"] button_states = [select_held, up_held, left_held, down_held, right_held] while True: position = encoder.position if position != last_position: last_position = position print(f"Position: {position}") for b in range(5): if not buttons[b].value and button_states[b] is False: button_states[b] = True print(f"{button_names[b]} button pressed") if buttons[b].value and button_states[b] is True: button_states[b] = False print(f"{button_names[b]} button released")
In the simple test example, the code confirms that you have connected the ANO rotary encoder to I2C adapter by checking the seesaw firmware for the product ID number. After that, in the loop, the rotary encoder position is printed to the serial console. Additionally, if a button is pressed or released that is also printed to the serial console.
Text editor powered by tinymce.