Rotary encoder on Raspberry Pi's GPIO

Hi folks,

I’ve built a raspberry pi 4B + 7" touchscreen system based on the tutorial by timewasternl: Building a standalone DDJ-400 with a Raspberry Pi and Mixxx - DJ TechTools

In addition, I integrated a KY-040 rotary encoder (see picture below) which I plan to use for changing parameters of the GUI such as the zoom level, etc.

The rotary encoder is connected to the RPi’s GPIO and I was able to test it successfully using a little Python script I found on the Internet.

Now I want to integrate this rotary encoder to Mixxx’s mapping system. What’s the best way to proceed?
I guess I need to write a backend (a Linux driver?) which emulates a MIDI controller? Does the Mixxx’s controller system only work for external hardware plugged on USB ports?

(disclaimer, this “mapping” will be quite specific as it’s only for Raspberry Pi, only for this type of rotary encoder, and only on Linux)

nice idea!
Integration would depend on how you want to use the knob, i. e. will you have modifier keys (like Shift, Ctrl on the keyboard) to add more layers.
I guess you could also make it send keystrokes (GPIO > some script > emulate keystrokes), or make it be detected as HID (keyboard, mouse) in the first place.
but MIDI would probably be the best choice in terms of Mixxx integration as editing and swapping mappings for the knob works like for any other controller.

1 Like

for the MIDI emulation part, there are definitely plenty of tools that can send MIDI notes on Linux, virtual midi as keyword etc.

Thanks a lot for the swift answer! I will explore the MIDI emulation part as well as the other solutions you suggested.

You can create a virtual midi device with rtmidi. It’s available as a python library and to create a virtual midi device you don’t need more then these lines:

import rtmidi

NAME = "TestMidiDevice"
def main():

        virtualMidiOutput = rtmidi.MidiOut()

        buf = [0x11, 0x1, 127]
if __name__ == "__main__":

I use this for a project called serial2midi which basically makes a usb serial device available as a virtual midi device. It works very stable and is fast enough for djing.


Great, thanks for the tip.
I have written a small proof of concept based on the mido library using the rtmidi backend (Backends — Mido 1.2.10 documentation). It works quite well so far.

1 Like

I had some problems with mido that i don’t remember anymore, but if it works for you, great! Have fun and enjoy the endless possibilieties with foss music tools :slight_smile: