ESP32 amend TX pin help please :) #284
-
I am working on a project which will need a MIDI output on an ESP32, and am hoping for some advice. Due to the other elements of the project, I would like to use UART2 TX on GPIO27 (the default for UART2 TX is GPIO17) The code below works as expected and outputs on pin 27 #define RXD2 16
#define TXD2 27
void setup()
{
// Set MIDI baud rate:
Serial2.begin(31250, SERIAL_8N1, RXD2, TXD2);
}
void loop()
{
//Note on channel 1 (0x90), middle C (0x3C), middle velocity (0x45):
sendMIDI(0x90, 0x3C, 0x45);
delay(100);
//Note on channel 1 (0x90), middle C (0x3C), silent velocity (0x00):
sendMIDI(0x90, 0x3C, 0x00);
delay(1000);
}
void sendMIDI(int cmd, int pitch, int velocity)
{
Serial2.write(cmd);
Serial2.write(pitch);
Serial2.write(velocity);
} The code below produces MIDI output on pin 17. I got an error without all Is there something I can add into this to alter the pin used by serial2. I have looked through the documentation but struggling to find this. I am still quite new to C. Thanks for any help or guidance. #include <MIDI.h>
struct MySettings : public MIDI_NAMESPACE::DefaultSettings
{
static const long BaudRate = 31250;
// CAN I ADD SOMETHING HERE TO SPECIFY THE TX PIN OF SERIAL2?
};
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial2, dinMIDI, MySettings);
void setup()
{
dinMIDI.begin(MIDI_CHANNEL_OMNI); // Listen to all incoming messages
}
void loop()
{
// Send note 42 with velocity 127 on channel 1
dinMIDI.sendNoteOn(42, 127, 1);
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
The MIDI library only uses the Serial (or Serial2 in your case) to send data, it's up to you to configure this Serial2 object to use a custom pin. I'm not familiar with the ESP32, but if you can reconfigure a serial port to use another pin, I figure the ESP documentation is a good place to look for a solution. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
The MIDI library only uses the Serial (or Serial2 in your case) to send data, it's up to you to configure this Serial2 object to use a custom pin.
I'm not familiar with the ESP32, but if you can reconfigure a serial port to use another pin, I figure the ESP documentation is a good place to look for a solution.