Can Teensy 3.x receive MIDI on pin 7 or 9? Or is it really only Serial1?

Status
Not open for further replies.

monkeybiscuits

Active member
MIDI.h says...
Code:
// Use Serial1 on all Teensy boards, because Serial is USB virtual serial
// Arduino.h must be included before MIDI.h to define CORE_TEENSY
#if defined(CORE_TEENSY)
#undef USE_SERIAL_PORT
#define USE_SERIAL_PORT         Serial1
#endif
...but the date on that file is older than Teensy 3 (which introduced 3 serial ports). Am I supposed to be able to "#define USE_SERIAL_PORT Serial3" now? Or can this be handled in the sketch?
 
That's good to know. It means 3 things.

1) Maybe MIDI.h needs an update.
2) I don't need to re-solder anything.
3) My problem with getting MIDI input is something else.

For starters I've edited MIDI.h to use serial port 3 and I'm trying this...
Code:
#include <MIDI.h>

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

void loop() {
  if (MIDI.read()) {
    Serial.println("there's MIDI");
  }
}

Getting nothing.
 
What MIDI device do you have connected to Serial3?
If it can receive note on/off, add some code to the setup function to send a note to it.


Pete
 
I've got an Alesis Performance Pad plugged into a MIDI IN connector which is routed to pin 7 on my Teensy 3.1 according to (the left side of) this schematic.
td_libs_MIDI_sch.gif
The quality of my craftsmanship is certainly in question but I want to make sure I'm testing with code that should definitely be working before I go pulling things apart again. Having the drum pad plugged in an beating on it should be enough to get MIDI.read() to register TRUE shouldn't it?
 
Last edited:
I have had midi send on all. There is code and some discussion here: https://forum.pjrc.com/threads/31315-teensyduino-midi-sync?highlight=midi
The thread refers to using littlescales work on sending beat, but usefully reminds to add serial1 2 and 3 .begin to setup...
void setup() {
Serial.begin(31250);
Serial1.begin(31250);
Serial2.begin(31250);
Serial3.begin(31250);
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
usbMIDI.setHandleRealTimeSystem(RealTimeSystem);
}

void loop() {
usbMIDI.read();
}

void RealTimeSystem(byte realtimebyte) {
if(realtimebyte == 248) {
Serial1.write(248);//pin1 =transmit
Serial2.write(248);//pin10 =transmit
Serial3.write(248);// pin8 =transmit
}}
 
Thanks. I'm actually trying to go the opposite direction though. I want the MIDI out from my drum pad to run into the MIDI IN connector I have set up on pin 7 of the Teensy. Then I want the Teensy to send that MIDI info through usbMIDI to the computer.
 
Yep, I got that. My point was to perhap explore using serialx.begin() in setup and serialx.read() in your loop code. Sorry, I should have been more obvious.
 
Last edited:
Ok thanks. Am I getting any closer with this...
Code:
#include "Arduino.h"
#include <MIDI.h>

void noteoff(byte channel, byte note, byte velocity){
  Serial.println("note OFF");
}

void noteon(byte channel, byte note, byte velocity){
  Serial.println("note ON");
}

void setup() {
  Serial.begin(31250); 
  Serial3.begin(31250);
  MIDI.begin(MIDI_CHANNEL_OMNI);
  MIDI.setHandleNoteOff(noteoff);
  MIDI.setHandleNoteOn(noteon) ;
}

void loop(){
  Serial3.read();
}
 
And print the incoming to the serial terminal to see what you are reading. perhaps something along the lines of
void loop(){
Serial.println (Serial3.read());
}
 
I ran a test on the three serial options for midi read this evening, and yes, serial3.read() gives different output to the serial monitor than Serial1 and serial2 read prints to serial monitor. However, this may be because I did not put proper hardware in, no resistors etc, not as should be as described at https://www.pjrc.com/teensy/td_libs_MIDI_sch.gif
With the addition of: Teensy 3.0: Use two 47 ohm resistors on MIDI OUT, with the pin 4 resistor to +3.3 volts. For MIDI IN, connect the 270 ohm resistor to +3.3 volts.

However, I did get input to the teensy 3 from serial3.read(), it wasn't totally empty. All I used as a test was:

Code:
#include <MIDI.h>

void setup() {
Serial.begin(31250); 
Serial3.begin(31250);
 MIDI.begin();
}

void loop() {
  Serial.print(Serial3.read());
}
 
Last edited:
Thanks for doing that. I tried "Serial.print(Serial3.read());" too and I just got "-1" streaming across the page. Then I tried this...
Code:
  if (Serial3.read != -1){
    Serial.print("MIDI");
  }
Still can't get notes to register. You say Serial1 and Serial2 work ok though?
 
Still can't get notes to register. You say Serial1 and Serial2 work ok though?
Yes, i have a stream of input coming from Serial1, and I use serial1 as a midi input for my Korg monotron project to convert MIDI Din to CV. Looks like you may need to check how its wired up, even the little things, like tx -> rx.
 
Final answer...YES, Teensy 3.x (and presumably LC) can receive MIDI on all three serial ports. I moved the MIDI input to pin 0 and, when that wasn't working, I knew I had some wires crossed on my MIDI circuit. I sorted that out and got it working on Serial1 but I figured it would be more noteworthy if I actually got it working on Serial3. So I move it BACK and after changing line #44 in midi.h to this:
Code:
#define USE_SERIAL_PORT         Serial3
This test code works fine.
Code:
#include "Arduino.h"
#include <MIDI.h>

void noteon(byte i, byte j, byte k){
  Serial.println("note ON");
}

void setup() {
  Serial.begin(31250);
  MIDI.begin(MIDI_CHANNEL_OMNI);
  MIDI.setHandleNoteOn(noteon) ;
}

void loop(){
  MIDI.read();
}
Thanks for sticking with me on this. It's the last part of this prototype I needed to get working before I could have a proper PCB made and be done with this hairball.
 
Note, the Teensy 3.1/3.2 has 8 byte FIFOs on Serial1/Serial2 (none on Serial3). The LC has no FIFOs on any serial port. If you are receiving a lot of input, I would expect the FIFOs on Serial1/Serial2 to help avoid losing data.

I don't know if MIDI has RTS/CTS support, but if so, recent releases allow you to add a pin for hardware flow control. Note, I haven't used it myself, I just remember seeing the announcements for it.

If you still have the original Teensy 3.0, it only has an 8 byte FIFO on Serial1.
 
MIDI in works fine but the signal seems to be jumping to adjacent pins. Down on pin 7 it was between the clock and latch pins of my serial display causing all kinds of weird things to happen. I figured I'd just fiddled with pin 7 one too many times so I moved MIDI in back up to pin 0. Pin 1 is a switch using INPUT_PULLUP and Bounce. Now every time I send a note to pin 0, the input on pin 1 also gets triggered.

UPDATE: I just answered my own question. It was a simple oversight. If you want to use a TX pin for something other than MIDI output, you need to tell MIDI.h
Code:
#define COMPILE_MIDI_IN         1           // Set this setting to 1 to use the MIDI input.
#define COMPILE_MIDI_OUT        0           // Set this setting to 1 to use the MIDI output. 
#define COMPILE_MIDI_THRU       0           // Set this setting to 1 to use the MIDI Soft Thru feature
 
Last edited:
Status
Not open for further replies.
Back
Top