Invert polarity of MIDI output?

All the serial ports on Teensy 4.x support SERIAL_8N1_TXINV mode, which you can use to achieve this. Serial documentation for reference.

It's not part of the MIDI library. It's a feature built into the serial port hardware, which the MIDI library uses to actually transmit.

Here is the normal way to use Serial2 (TX2 on pin 8).

Code:
#include <MIDI.h>

MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, mymidi);

void setup() {
  mymidi.begin(MIDI_CHANNEL_OMNI);
}

void loop() {
  mymidi.sendControlChange(9, 65, 1);
  delay(1000);
}

This is the waveform on the TX2 pin.

1737023506264.png



To invert, the trick is to call Serial2.begin() after MIDI has already started it normally. Like this:

Code:
#include <MIDI.h>

MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, mymidi);

void setup() {
  mymidi.begin(MIDI_CHANNEL_OMNI);
  Serial2.begin(31250, SERIAL_8N1_TXINV);
}

void loop() {
  mymidi.sendControlChange(9, 65, 1);
  delay(1000);
}

You might also want to add a short delay after restarting the serial port, because reconfiguring the output level could cause a brief output pulse during the time it was set to the unintended level. Waiting 10 bit times of 31250 baud ought to be enough.


Then when you use the MIDI library, the TX2 output becomes inverted. It's not the MIDI library doing this. MIDI still works the same way, but you've altered the serial port settings.

1737023605432.png
 
Last edited:
Back
Top