RGB Rotary Encoder in CircuitPython for Raspberry Pico
So in my latest project I wanted to get an RGB rotary encoder for my Pico running CircuitPython. The reason for using CircuitPython is that I am creating a specific piece of hardware in which the library I cannot (or dare not) re-write. I want to make a Human Interface Device (HID) using the Raspberry Pi Pico and the only solid library I can find is the one by Adafruit. This fundamental decision means that any hardware with which I intend to interface then requires that I use libraries in CircuitPython, or C++.
Getting to know the hardware
It’s really important to read the datasheet for the electronics with which you are going to be playing. While this one is not great, it at least provides clues about the hardware that allow users to know a few things. This hardware includes:
In my mind, these seemed to be the list in order from easiest to hardest… I was wrong. Having played with common cathode RGB led’s I was left utterly confused. It took me the better part of a day to figure it out. The most annoying part is an absolute lack of a pin out. While it it seemingly obvious to others, it’s not obvious to me.
I know the wiring elements on the right are not necessarily part of a pinout diagram, however it was these elements of the data sheet that REALLY helped me understand that it was common anode as opposed common cathode. I based the pinout on the data sheet and other information I have found, like the breakout board, which allows for two types of rotary encoder.
I have separated out the components and code so that if you only need one element, you can skip ahead to that one.
Raspberry Pico CircuitPython RGB LED common anode
The Raspberry Pico board has three Analog to digital converters, meaning that if we are going to run dimming on the LED’s (on any other pins), then we’re going to have to use pulse width modulation (PWM). There is a reference note in the Adafruit CircuitPython Pico documentation (page 82) that if you have trouble, change the pin you’re using. I had to fiddle around a bit.
Wiring
Code
Raspberry Pico CircuitPython SPST Switch
Due the RGB LED being shared anode, the switch needs to be pulled down when reading. It also requires power to be connected. A Single Pole Single Throw (SPST) switch has only one input and only one output terminal.
Wiring
Code
Key event management can be a lot more complex, but a simple digital read is good enough to tell if the switch is open or closed. In a future post I’ll elaborate on my approach in my repo.
I’ll be putting these into separate classes so that the pins are variable and that it can be reused elsewhere. You can find the core project here.
Final thoughts
I am not great at figuring out the wiring of stuff, and I have not had the most pleasant experience with the specialist communities. I have found their toxic communication style only serves their ego rather than education. Please feel free to pleasantly offer criticism and corrections. I’m very eager to learn and grow. One of the things that comes to my mind is that there should probably be some resistors on the LED pins.
Comments
No comments found for this article.
Join the discussion for this article on github. Comments appear on this page instantly.
Comments
No comments found for this article.
Join the discussion for this article on github. Comments appear on this page instantly.