Using the Arduino to manage several serial connections

Hi all,

I'm playing with the idea of building a MIDI (serial musical protocol at 31250 baud) patch bay, so I can connect different MIDI devices to a series of other MIDI devices (in the beginning in just a one to one basis).
In other words, I would be building a switch board that samples serial data on one port (in) and copies it to another serial port (out).
Sounds simple enough, but the Uno just has one serial port. I've seen the SoftwareSerial library, but this uses only one port at the time. I suspect possible latency problems in that scenario.
I think I would probably need some sort of UART to get this done, but I would like to have some tips on how approach this process. Maybe the Arduino is not the best choice for this, but running at 16 MHz, I would like to believe it could be done,

Cheers!

BC

An Arduino Mega has 4 Serial devices in hardware.

A cheap way to add extra USARTs is to add extra Atmega 328s connected to the "master" Arduino by SPI.

...R

A one-to-one can be done. A one-to-many can be done. A many-to-many can't be done reliably without uarts.

But why the use of the uart? What you want to build is basically a router. And that can simply be done with multiplexers. I have no experience with MIDI (so not sure about electrical requirements) but you can look at e.g. the 74HC4067.

Each 4067 has 16 inputs; one of them can be routed to the output. Use two or more (each individual input going each an input on each 4067) and you can expand the number of outputs. With ease you can go to 16x16.

You can use port expanders or shift registers to control which input has to go to which output.

You will have the same problem as with a real patch panel; that is, if you patch (switch) while a command is being send, it will result in data corruption.

Note:
you might need additional electronics to meet the specification

DrDiettrich:
An Arduino Mega has 4 Serial devices in hardware.

I was aiming for more inputs, about 16 :slight_smile:

Robin2:
A cheap way to add extra USARTs is to add extra Atmega 328s connected to the "master" Arduino by SPI.

...R

That's an interesting idea, but to avoid having to use a lot of microcontrollers (I aim to use 16 inputs and outputs) I think I would need to involve "bit banging" (emulating serial connections by programming digital inputs).
In that context I worry about how to scan 16 different pins and moving the same info in a timely manner to the output pins,

cheers,

BC

sterretje:
A one-to-one can be done. A one-to-many can be done. A many-to-many can't be done reliably without uarts.

But why the use of the uart? What you want to build is basically a router. And that can simply be done with multiplexers. I have no experience with MIDI (so not sure about electrical requirements) but you can look at e.g. the 74HC4067.

Each 4067 has 16 inputs; one of them can be routed to the output. Use two or more (each individual input going each an input on each 4067) and you can expand the number of outputs. With ease you can go to 16x16.

You can use port expanders or shift registers to control which input has to go to which output.

You will have the same problem as with a real patch panel; that is, if you patch (switch) while a command is being send, it will result in data corruption.

Note:
you might need additional electronics to meet the specification

Hi Sterretje (Vlaams of Nederlands?),

thanks for the elaborate answer.

Indeed, one-to-one and one-to-many are the only feasible (relative simple) situations. Many-to-many involves a maxing of the signals, which adds considerable complexity.

MIDI is basically using 5V for highs and 0V for lows, pretty straightforward.

I already considered multiplexers but I was worried about the situation where several data trains arrive at different ports at the same time: MIDI is time sensitive, so the same speed needs to be more or less honored. Maybe some electronics to mimic interrupt requests are necessary to have this all handled by a simple microcontroller ...

Can multiplexers handle multiple incoming data signals?

Thx!

BC

As indicated, you would have the same problem with a physical patch panel. If that is a concern, the solution will not work.

bcnx:
I think I would need to involve "bit banging" (emulating serial connections by programming digital inputs).

That won't be any better than SoftwareSerial and that is pretty crap. A 16 MHz MCU is not fast enough.

You need multiple UARTs - hence my suggestion. You could write a standard program for each "slave" Atmega328 so that it receives and buffers the incoming data and passes it to the master whenever requested. Likewise for sending stuff on behalf of the master.

...R

sterretje:
As indicated, you would have the same problem with a physical patch panel. If that is a concern, the solution will not work.

If we would adhere to the patch panel metaphore, only one-to-one connections are possible. One-to-many would be desirable however.

thx,

BC

Robin2:
That won't be any better than SoftwareSerial and that is pretty crap. A 16 MHz MCU is not fast enough.

You need multiple UARTs - hence my suggestion. You could write a standard program for each "slave" Atmega328 so that it receives and buffers the incoming data and passes it to the master whenever requested. Likewise for sending stuff on behalf of the master.

...R

OK, thank you. Maybe I need to reconsider using an Arduino for this. What would be a fast enough speed for this application?
Using multiple Arduinos also requires some protocol to sync the MIDI clock that is present in the MIDI signal.

I was also thinking that, to allow multiple incoming MIDI signals, I would periodically scan the input ports to detect low-to-high transitions, which indicate starting MIDI conversations. From that point on, the MIDI clock in the signal needs to be synced to.

Cheers, I appreciate the feedback and tips,

BC

I found this as a possible faster Arduino:

I've also been looking at digital crosspoint switches, which are ICs that basically present a big matrix of switch that can be one by one configured to pass through a digital signal. These are big SMD packages though, hence not very easy to breadboard with,

BC

bcnx:
I found this as a possible faster Arduino:

Quote from that website

As of March 2015, the LeafLabs Maple line and the libmaple library are end-of-life and no longer supported by LeafLabs.

There are Arduino products with high-end MCUs but I still think you will need multiple hardware UARTs

You have not mentioned at all how much data will need to be manipulated - how many bytes per second when they are all working, and what sort of manipulations will need to be done.

Perhaps you really need the power of a high-end PC?

By the way I know nothing about midi or music - except to listen to.

...R

Fritzing Project - Midi Router

It was done before; I can't say how successful it was; but you can always try it :wink:

I also found Next/previous switching a multiplexer

Again I have no idea how well it works.

I don't understand your problem with the timing; multiplexers are like electrical switches (basically no delays). As said, you might interrupt a message and switch into the middle of another message and that will cause corruption. The second link has a "sound detection" circuit in one of the pages to light a LED while data is being received; you might be able to use that to prevent switching.

But then it becomes a lot more complicated.

Robin2:
Quote from that website
There are Arduino products with high-end MCUs but I still think you will need multiple hardware UARTs

You have not mentioned at all how much data will need to be manipulated - how many bytes per second when they are all working, and what sort of manipulations will need to be done.

Perhaps you really need the power of a high-end PC?

By the way I know nothing about midi or music - except to listen to.

...R

Hi,

the amount of MIDI data is relative. For example: one note requires 3 bytes for a "note on" message and two more bytes for a "note off" message (if I'm notmistaking). You have however also performance data which possible create relatively more data. However, a composition can be made of several simultanious instruments all producing MIDI data. All and all it should however be quite manageable, as devices that do what a describe, already exist and contain also "simple" MCUs. I have such a device with 8 ports. But I'd like to go higher.

BC

sterretje:
Fritzing Project - Midi Router

It was done before; I can't say how successful it was; but you can always try it :wink:

I also found Next/previous switching a multiplexer

Again I have no idea how well it works.

I don't understand your problem with the timing; multiplexers are like electrical switches (basically no delays). As said, you might interrupt a message and switch into the middle of another message and that will cause corruption. The second link has a "sound detection" circuit in one of the pages to light a LED while data is being received; you might be able to use that to prevent switching.

But then it becomes a lot more complicated.

Hi Sterretje,

the first link points to a project with a rather incorrect name: this guy did not make a router, he made a simple MIDI keyboard which input is converted to a MIDI message (if I get the rather limited information on that page).

The second link is indeed a router, but this represents a small portion of my goal: 16 INs that can be selectively routed to 16 OUTs. That link has 16 INs to 1 OUT.

Timing: since MIDI is a clocked signal, it's clock needs to be respected to be understood correctly buy the receiving MIDI device. MIDI is also latency sensitive: if you listen to music based on MIDI and some notes come late, it's easy to see how that could be a problem. I direct and immediate routing should happen, while respecting the 31250 baud speed, which is the MIDI standard.

Corruption is only a problem if you do many-to-one or many-to-many, you need to mix MIDI signals in that case. I'm not doing that, I'm doing one-to-one or one-to-many. If a certain output is chosen in one-to-many, it can not be used by another port. That should both be pratical and eradicate corruption is no signals are conflicting.

The challenge remains to get MIDI signal from input A to output C (and possibly D,E,F and G) in a timely fashion. If during that process a MIDI signal arrives at input D, it should be routable to output A,B,C and whatever (not D,E,F and G since input A already uses those outputs).

It sounds easy, but it seems not to be. I have a hard time developing a circuit for this.

BC

Test with one 4067. If it works reliably, you can easily put 16 4067s in parallel to create more outputs.

While thinking about this problem in bed (this is where I do a lot of my thinking), I had the idea to mimic the switch matrix by using 16 74HC4067 devices and creating rows and columns to create a matrix where I activate connections in between inputs and outputs by sending the correct binary info to each multiplexer IC.
Good idea, were it not that this multiplexers only allow one active connection at a time, where I need a one-to-many situation. I think I will try and find multiplexer that open several ports at the same time,

BC

I don't understand. Rows and columns?

16 inputs to one 4067 gives you one output.
The same 16 inputs to a second 4067 gives you the second output.
Again the same 16 inputs to a third 4067 gives you the third output.

And so on.

sterretje:
I don't understand. Rows and columns?

16 inputs to one 4067 gives you one output.
The same 16 inputs to a second 4067 gives you the second output.
Again the same 16 inputs to a third 4067 gives you the third output.

And so on.

Hi Sterretje,

a matrix consists of rows and columns. Where the rows and columns cross, one can put a switch (or a LED for a LED matrix), to connect that particular row (input) to that particular columns (output). A keyboard matrix wotks in the same way.

The 4067 has an important disadvantage making it no use for my project, namely that I cannot make one-to-many connections, because it only allows one input at a time to be switched.
This means an input can only be switched to one output and not to several outputs, which is needed quite often for MIDI setups, where you have for instance a MIDI sequencer sending data to several MIDI instruments.
A digital crosspoint switch does allow for more output connections, maybe I should go that route.

BC