# MicroPython or Circuit for Pico RGB Rotary Breakout

**URL:** https://forums.pimoroni.com/t/micropython-or-circuit-for-pico-rgb-rotary-breakout/17164
**Category:** Support
**Created:** [May 19, 2021, 9:49am UTC](https://forums.pimoroni.com/t/micropython-or-circuit-for-pico-rgb-rotary-breakout/17164 "2021-05-19T09:49:26Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![hackgeek](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/hackgeek/32/7914_2.png) [@hackgeek](https://forums.pimoroni.com/u/hackgeek)
#### Post date: [May 19, 2021, 9:49am UTC](https://forums.pimoroni.com/t/micropython-or-circuit-for-pico-rgb-rotary-breakout/17164/1 "2021-05-19T09:49:26Z")

</div>

Hi,

Has anyone got the rgb breakout rotary encoder working with micropython or circuitpython on a rpi pico?

([RGB Encoder Breakout – Pimoroni](https://shop.pimoroni.com/products/rgb-encoder-breakout))

I’m having trouble trying to port the ioe example code to pico.

---

<div class="post-metadata">

### Author: ![alphanumeric](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/alphanumeric/32/2729_2.png) [@alphanumeric](https://forums.pimoroni.com/u/alphanumeric)
#### Post date: [May 19, 2021, 10:39am UTC](https://forums.pimoroni.com/t/micropython-or-circuit-for-pico-rgb-rotary-breakout/17164/2 "2021-05-19T10:39:30Z")

</div>

I saw this list as to what’s supported in the Pimoroni Micro Python uf2 file  
I see RGB Encoder Breakout in the list. And it appears to be the one you have.

- [1.3" SPI Colour Round LCD (240x240) Breakout](https://shop.pimoroni.com/products/1-3-spi-colour-round-lcd-240x240-breakout)
- [1.3" SPI Colour Square LCD (240x240) Breakout](https://shop.pimoroni.com/products/1-3-spi-colour-lcd-240x240-breakout)
- [0.96" SPI Colour LCD (160x80) Breakout](https://shop.pimoroni.com/0-96-spi-colour-lcd-160x80-breakout)
- [LED Dot Matrix Breakout](https://shop.pimoroni.com/products/led-dot-matrix-breakout)
- [LTR-559 Light & Proximity Sensor Breakout](https://shop.pimoroni.com/products/ltr-559-light-proximity-sensor-breakout)
- [11x7 LED Matrix Breakout](https://shop.pimoroni.com/products/11x7-led-matrix-breakout)
- [5x5 RGB Matrix Breakout](https://shop.pimoroni.com/products/5x5-rgb-matrix-breakout)
- [SGP30 Air Quality Sensor Breakout](https://shop.pimoroni.com/products/sgp30-air-quality-sensor-breakout)
- [Trackball Breakout](https://shop.pimoroni.com/products/trackball-breakout)
- [AS7262 6-channel Spectral Sensor (Spectrometer) Breakout](https://shop.pimoroni.com/products/as7262-6-channel-spectral-sensor-spectrometer-breakout)
- [RGB Encoder Breakout](https://shop.pimoroni.com/products/rgb-encoder-breakout)
- [RGB Potentiometer Breakout](https://shop.pimoroni.com/products/rgb-potentiometer-breakout)
- [IO Expander Breakout](https://shop.pimoroni.com/products/io-expander)
- [MICS6814 3-in-1 Gas Sensor Breakout (CO, NO2, NH3)](https://shop.pimoroni.com/products/mics6814-gas-sensor-breakout)
- [MSA301 3DoF Motion Sensor Breakout](https://shop.pimoroni.com/products/msa301-3dof-motion-sensor-breakout)
- [RV3028 Real-Time Clock (RTC) Breakout](https://shop.pimoroni.com/products/rv3028-real-time-clock-rtc-breakout)

---

<div class="post-metadata">

### Author: ![alphanumeric](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/alphanumeric/32/2729_2.png) [@alphanumeric](https://forums.pimoroni.com/u/alphanumeric)
#### Post date: [May 19, 2021, 10:44am UTC](https://forums.pimoroni.com/t/micropython-or-circuit-for-pico-rgb-rotary-breakout/17164/3 "2021-05-19T10:44:21Z")

</div>

I do believe this is the Micro Python Pico stuff.  
[pimoroni-pico/micropython/examples at main · pimoroni/pimoroni-pico · GitHub](https://github.com/pimoroni/pimoroni-pico/tree/main/micropython/examples)

[pimoroni-pico/micropython/examples/breakout\_encoder at main · pimoroni/pimoroni-pico · GitHub](https://github.com/pimoroni/pimoroni-pico/tree/main/micropython/examples/breakout_encoder)

---

<div class="post-metadata">

### Author: ![hackgeek](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/hackgeek/32/7914_2.png) [@hackgeek](https://forums.pimoroni.com/u/hackgeek)
#### Post date: [May 19, 2021, 2:18pm UTC](https://forums.pimoroni.com/t/micropython-or-circuit-for-pico-rgb-rotary-breakout/17164/4 "2021-05-19T14:18:00Z")

</div>

Many thanks, my google-fu was weak

---

<div class="post-metadata">

### Author: ![hackgeek](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/hackgeek/32/7914_2.png) [@hackgeek](https://forums.pimoroni.com/u/hackgeek)
#### Post date: [May 19, 2021, 3:07pm UTC](https://forums.pimoroni.com/t/micropython-or-circuit-for-pico-rgb-rotary-breakout/17164/5 "2021-05-19T15:07:51Z")

</div>

I initially had circuit python on the pico and it detected the encoder breakout board on pins 11 & 12 during an i2c scan.

I flashed the pimoroni micropython, and loaded the demo from the examples linked above

```auto
from pimoroni_i2c import PimoroniI2C
from breakout_encoder import BreakoutEncoder

PINS_BREAKOUT_GARDEN = {"sda": 11, "scl": 12}
#PINS_PICO_EXPLORER = {"sda": 20, "scl": 21}

steps_per_rev = 24

i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN)
enc = BreakoutEncoder(i2c)

enc.set_brightness(1.0)
# enc.set_direction(BreakoutEncoder.DIRECTION_CCW) # Uncomment this to flip the direction

# From CPython Lib/colorsys.py
def hsv_to_rgb(h, s, v):
    if s == 0.0:
        return v, v, v
    i = int(h * 6.0)
    f = (h * 6.0) - i
    p = v * (1.0 - s)
    q = v * (1.0 - s * f)
    t = v * (1.0 - s * (1.0 - f))
    i = i % 6
    if i == 0:
        return v, t, p
    if i == 1:
        return q, v, p
    if i == 2:
        return p, v, t
    if i == 3:
        return p, q, v
    if i == 4:
        return t, p, v
    if i == 5:
        return v, p, q

def count_changed(count):
    print("Count: ", count, sep="")
    h = ((count % steps_per_rev) * 360.0) / steps_per_rev # Convert the count to a colour hue
    r, g, b = [int(255 * c) for c in hsv_to_rgb(h / 360.0, 1.0, 1.0)] # rainbow magic
    enc.set_led(r, g, b)

count = 0

count_changed(count)
enc.clear_interrupt_flag()

while True:
    if enc.get_interrupt_flag():
        count = enc.read()
        enc.clear_interrupt_flag()

        while count < 0:
            count += steps_per_rev

        count_changed(count)

```

However, I get a “ValueError: bad SDA pin”.  
If I try GP pin numbering (8, 9) I get “breakout not found” instead, which indicates the board is being detected on 11 & 12. Is it my soldering skills?

---

<div class="post-metadata">

### Author: ![hackgeek](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/hackgeek/32/7914_2.png) [@hackgeek](https://forums.pimoroni.com/u/hackgeek)
#### Post date: [May 19, 2021, 3:17pm UTC](https://forums.pimoroni.com/t/micropython-or-circuit-for-pico-rgb-rotary-breakout/17164/6 "2021-05-19T15:17:51Z")

</div>

A simple reset fixed it - not my day today

---

<div class="post-metadata">

### Author: ![alphanumeric](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/alphanumeric/32/2729_2.png) [@alphanumeric](https://forums.pimoroni.com/u/alphanumeric)
#### Post date: [May 19, 2021, 4:59pm UTC](https://forums.pimoroni.com/t/micropython-or-circuit-for-pico-rgb-rotary-breakout/17164/7 "2021-05-19T16:59:04Z")

</div>

Stuff happens. The links to that stuff could be better IMHO. If you look hard enough you will find it though. I went to the Pico Breakout Garden shop page where it lists all the breakouts, a link to Pimorni’s Micro Python stuff is there if you scroll down far enough.

---

<div class="post-metadata">

### Author: ![Darrencuk](https://avatars.discourse-cdn.com/v4/letter/d/e56c9b/32.png) [@Darrencuk](https://forums.pimoroni.com/u/Darrencuk)
#### Post date: [June 26, 2023, 1:46pm UTC](https://forums.pimoroni.com/t/micropython-or-circuit-for-pico-rgb-rotary-breakout/17164/8 "2023-06-26T13:46:43Z")

</div>

I don’t suppose anyone has managed to get this to work with Circuit Python? I would love to incorporate it into my Macro Pad that’s currently using a normal rotary encoder using Stemma.

Thanks!

Darren.

---

<div class="post-metadata">

### Author: ![murray](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/murray/32/9789_2.png) [@murray](https://forums.pimoroni.com/u/murray)
#### Post date: [October 9, 2023, 8:08pm UTC](https://forums.pimoroni.com/t/micropython-or-circuit-for-pico-rgb-rotary-breakout/17164/9 "2023-10-09T20:08:10Z")

</div>

I’m trying to do something similar. Got it working fine in MicroPython and regular Python, but I’m having trouble reverse engineering the MP or Python libraries to create a CircuitPython library I can use on the Keybow PMK image. Have you had any luck yet?

---

<div class="post-metadata">

### Author: ![bablokb](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@bablokb](https://forums.pimoroni.com/u/bablokb)
#### Post date: [October 10, 2023, 10:04am UTC](https://forums.pimoroni.com/t/micropython-or-circuit-for-pico-rgb-rotary-breakout/17164/10 "2023-10-10T10:04:18Z")

</div>

What is your exact problem when you try to reverse engineer the Python-libraries? I ported some of the Pimoroni (Pi-) Python-libs to CircuitPython and most of the work was to write simple wrappers to the low-level I2C (or SPI) methods. The IOExpander for example has a lot of `i2c_writeXX` methods that do the low-level I2C-communication and these have to be converted to the way CircuitPython speaks to the I2C bus. Same holds true of course for I2C bus/device creation.

---

<div class="post-metadata">

### Author: ![murray](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/murray/32/9789_2.png) [@murray](https://forums.pimoroni.com/u/murray)
#### Post date: [December 3, 2023, 6:19pm UTC](https://forums.pimoroni.com/t/micropython-or-circuit-for-pico-rgb-rotary-breakout/17164/11 "2023-12-03T18:19:37Z")

</div>

Yes that’s exactly it. I’m still learning with I2C and so haven’t figured out how to port the low-level methods yet. I got as far replicating the `smbus2` module used by IOExpander with this: [pimoroni/circuitpython\_adapter: A CircuitPython translation layer (github.com)](https://github.com/pimoroni/circuitpython_adapter).

This module doesn’t replicate the `i2c_msg` class though and CircuitPython doesn’t have the `uctypes` library I think I need to replicate it myself, so I’ve hit a bit of a blocker just now. Any guidance would be wonderful! Could I use the CircuitPython `i2ctarget` as a substitute for `smbus2` with a suitable adapter in between it and IOExpander?

---

<div class="post-metadata">

### Author: ![bablokb](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@bablokb](https://forums.pimoroni.com/u/bablokb)
#### Post date: [December 4, 2023, 6:58am UTC](https://forums.pimoroni.com/t/micropython-or-circuit-for-pico-rgb-rotary-breakout/17164/12 "2023-12-04T06:58:17Z")

</div>

In fact I found a CircuitPython-driver for the IOExpander at github, but I did not test it yet: [GitHub - mathcampbell/IOExpanderCircuitPython: CircuitPytthon port of the Pimoroni IO Expander Breakout library](https://github.com/mathcampbell/IOExpanderCircuitPython). Maybe you want to give it a try.

---

<div class="post-metadata">

### Author: ![pddring](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@pddring](https://forums.pimoroni.com/u/pddring)
#### Post date: [January 27, 2024, 4:41pm UTC](https://forums.pimoroni.com/t/micropython-or-circuit-for-pico-rgb-rotary-breakout/17164/13 "2024-01-27T16:41:14Z")

</div>

> [@murray](#):
>
> This module doesn’t replicate the `i2c_msg` class though and CircuitPython doesn’t have the `uctypes` library I think I need to replicate it myself, so I’ve hit a bit of a blocker just now. Any guidance would be wonderful! Could I use the CircuitPython `i2ctarget` as a substitute for `smbus2` with a suitable adapter in between it and IOExpander?

I’ve had a go at porting the io expander module to circuitpython in order to use a RGB encoder breakout on a Raspberry Pi Pico. It works to read the encoder value and set the RGB levels. Code here: [GitHub - pddring/ioe-circuitpython: CircuitPython port of the library for the Nuvoton MS51 Pimoroni IO Expander Breakout](https://github.com/pddring/ioe-circuitpython)

---

<div class="post-metadata">

### Author: ![murray](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/murray/32/9789_2.png) [@murray](https://forums.pimoroni.com/u/murray)
#### Post date: [February 2, 2024, 3:32pm UTC](https://forums.pimoroni.com/t/micropython-or-circuit-for-pico-rgb-rotary-breakout/17164/14 "2024-02-02T15:32:47Z")

</div>

Brilliant, thanks for sharing. I’ll have a go at integrating this with my setup and let you know how I get on.
