It's easy to use the I2C Quad Rotary Encoder breakout with Python or CircuitPython, and the Adafruit_CircuitPython_seesaw module. This module allows you to easily write Python code that reads each encoder position (relative to the starting position) and the four button presses on each encoder.
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 breakout VIN (red wire)
- Board STEMMA GND to breakout GND (black wire)
- Board STEMMA SCL to breakout SCL (yellow wire)
- Board STEMMA SDA to breakout SDA (blue wire)
The following is the adapter wired to a Feather RP2040 using a solderless breadboard:
- Board 3V to breakout VIN (red wire)
- Board GND to breakout GND (black wire)
- Board SCL to breakout SCL (yellow wire)
- Board SDA to breakout 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 breakout VIN (red wire)
- Pi GND to breakout GND (black wire)
- Pi SCL to breakout SCL (yellow wire)
- Pi SDA to breakout SDA (blue wire)
Here's the Raspberry Pi wired with I2C using a solderless breadboard:
- Pi 3V to breakout VIN (red wire)
- Pi GND to breakout GND (black wire)
- Pi SCL to breakout SCL (yellow wire)
- Pi SDA to breakout 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 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """Quad I2C rotary encoder NeoPixel color picker example.""" import board from rainbowio import colorwheel import digitalio import adafruit_seesaw.seesaw import adafruit_seesaw.neopixel import adafruit_seesaw.rotaryio import adafruit_seesaw.digitalio # For boards/chips that don't handle clock-stretching well, try running I2C at 50KHz # import busio # i2c = busio.I2C(board.SCL, board.SDA, frequency=50000) # For using the built-in STEMMA QT connector on a microcontroller i2c = board.STEMMA_I2C() seesaw = adafruit_seesaw.seesaw.Seesaw(i2c, 0x49) encoders = [adafruit_seesaw.rotaryio.IncrementalEncoder(seesaw, n) for n in range(4)] switches = [adafruit_seesaw.digitalio.DigitalIO(seesaw, pin) for pin in (12, 14, 17, 9)] for switch in switches: switch.switch_to_input(digitalio.Pull.UP) # input & pullup! # four neopixels per PCB pixels = adafruit_seesaw.neopixel.NeoPixel(seesaw, 18, 4) pixels.brightness = 0.5 last_positions = [-1, -1, -1, -1] colors = [0, 0, 0, 0] # start at red while True: # negate the position to make clockwise rotation positive positions = [encoder.position for encoder in encoders] print(positions) for n, rotary_pos in enumerate(positions): if rotary_pos != last_positions[n]: if switches[n].value: # Change the LED color if switch is not pressed if ( rotary_pos > last_positions[n] ): # Advance forward through the colorwheel. colors[n] += 8 else: colors[n] -= 8 # Advance backward through the colorwheel. colors[n] = (colors[n] + 256) % 256 # wrap around to 0-256 # Set last position to current position after evaluating print(f"Rotary #{n}: {rotary_pos}") last_positions[n] = rotary_pos # if switch is pressed, light up white, otherwise use the stored color if not switches[n].value: pixels[n] = 0xFFFFFF else: pixels[n] = colorwheel(colors[n])
In the example, each rotary encoder position is printed to the serial console as it changes. As the encoder position changes, the NeoPixel underneath advances through the rainbow. If you press a rotary encoder button, the NeoPixel underneath turns white.
I2C Clock Stretching
For boards that don't handle clock-stretching well, like Raspberry Pi, you may want to reduce the I2C clock speed to 50KHz by following the directions in this guide.
Then, at the beginning of the example code, uncomment the busio I2C instantiation and comment out the STEMMA_I2C()
instantiation.
# For boards/chips that don't handle clock-stretching well, try running I2C at 50KHz import busio i2c = busio.I2C(board.SCL, board.SDA, frequency=50000) # For using the built-in STEMMA QT connector on a microcontroller # i2c = board.STEMMA_I2C() seesaw = adafruit_seesaw.seesaw.Seesaw(i2c, 0x49)
For more information on I2C clock stretching and Raspberry Pi, check out this Learn Guide.
Text editor powered by tinymce.