Midi Library setup for Output only? Saving a GPIO

Is it possible to create an instance with the supplied midi library for creating a Midi Output device only? (E.g. only using one GPIO). I am running out of ports and could really need the GPIO currently blocked for Midi Input.

No, the Serial port used by the MIDI library does not use the GPIOs, it uses a separate peripheral in the chip that is connected to the TX and RX pins.
When you call MIDI.begin or Serial.begin, the TX and RX pins are disconnected from the GPIO hardware and connected to the UART (serial port) hardware.
On some architectures, you might be able to tweak some registers to enable only UART TX or RX while leaving the other pin as a GPIO. Unless you're really interested in how these chips work internally, I wouldn't recommend this. If you are interested, the datasheet for your specific microcontroller has all the answers.

An alternative might be to use a bit-banged UART output, but it's a hack, it's much less efficient than doing it in hardware.

If you are running out of GPIO pins, there might be better solutions, if you post more details about your project and the hardware you have connected, we might be able to suggest a better alternative.

I'm building a small Midi Controller device based on the Teensy 4.1, due to the size restrictions i don't want to add multiplexers to extend the GPIO inputs. Since i'm just missing one GPIO my idea was to check out if the Midi Library can be set up for Output (HW UART TX) only. My hope was that the Midi library would cater for this as there are quite a range of either Midi Input or Output only devices.

If the Midi library does not support this then it might be easier to implement this bare bones via serial.output as a 31250 baud device, or would this also allocate both TX and RX UART ports?

It's not a limitation of the MIDI library per se, the UART peripheral supports both TX and RX, and the Arduino Core code that configures the UART doesn't support connecting only one of them, because there is no portable way to do this, different chips require different configurations.

The Teensy chips are quite advanced, with a powerful pin mux, they will most likely offer a way to only select a single pin to the UART peripherals, but there's no software support for this, you'll have to dive into the datasheet yourself and configure all registers correctly. Here's the Teensy code that configures the UART:

1 Like

Thanks, will do!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.