Saturday, June 09, 2007

Arduino MIDI out example

A friend asked me to put together an Arduino with MIDI output example. Since i already had 16 pots and 2 multiplexers breadboarded, i decided to decode the 16 pots to MIDI control numbers 20 - 35 on channel 1. Fixed control numbers and channel are used for the sake of simplicity. They could be easily user-definable by means of a 4-way DIP switch for the channel number and a pot or something for the control number offset.

Find below a schematic, commented sketch and example patch. This has been tested and found working - one should be able to simply copy and paste the sketch and breadboard a circuit quite quickly. The 4051's can be bought at Jaycar, for example for $1.30. Or cheaper still from Futurlec, if you have a few things to buy and a couple of weeks to wait ($0.66 for CD4051).

Sketch:
[expand / collapse code]

Schematic:

Note that the Arduino analog and digital pins are offset by +1 ie. schematic diagram pin 1 = physical board pin marked pin 0 and so on. Click on the image below to see the pinouts of the MIDI DIN 5 connector. The solder side of a female socket is shown.



Example Max Patch:

31 comments:

Pat said...

this is awesome.
i just got an arduino the other day and want to make something like this.
where do you get those midi connectors?

Sebastian Tomczak said...

hi there,
if you live in austraia, you can get the MIDI connections from dick smith (cat no Z1552 for $1.24). or jaycar (cat no P0348 for $1.25). You will also need a 220Ω resistor. But that's it! so easy, huh?

Good luck with your Arduino - let me know what you come up with.

Seb

Anonymous said...

is there a way to achieve the same thing over USB? im currently trying something similar with the 4051 see the thread on arduino forum … im thinking of abandoning my project and just recreating yours since it seems so solid.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1189189043/30

Sebastian Tomczak said...

Yeah for sure, I don't see why you can't do it over USB, you just need to unpack the data correctly on the other side.

Anonymous said...

after some discussions in arduino's forum, I tend to believe that is almost impossible to send MIDI data over the USB port. Mainly because the communication is actually serial. Thus, the MIDI baud rate (31250) cannot be set.
There are some alternatives but all have pros and cons...

I wanna so much to believe that it's possible, but...:(

btw. very helpful example. thanks!

is it possible to send MIDI values from both analog and digital inputs?

Sebastian Tomczak said...

Hi there,
The easiest way IMHO to send MIDI data over the USB port is to use, say, Max/MSP or Super Collider or something like that to abstract the data down from a raw serial protocol (like, 57600 baud or whatever rate you want) to the MIDI standard.

That is how i would do it.

But. It is of course possible to send MIDI data over MIDI cabling into a computer's MIDI interface (hence this example).

It is possible to send analog and digital data as MIDI signals, it just depends on what your sketch is doing to take those values and map them to MIDI data, yeah? For example, a pot could set the pitch byte part of the MIDI data and a button could actually generate the note ons and note offs.

Cheers,

Anonymous said...

hi,

I'm aware of the Max / Processing solution.

However, I would prefer to use arduino as standalone for MIDI out over USB. That is, no need for a Max patch inbetween the Arduino and the Host application (say Logic).

I'm thinking of building a midi controller with pots and switche (as digital inputs). I don't really need to extract pitch data but only MIDI cc.
Do you believe it's possible to use both analog (pots) and digital (switches) inputs as MIDI cc?

I've tried to modify your code in order to add digital inputs but with no luck. I'm kinda new to Arduino language ( and C) so I hope it is possible to be done.

cheers

Sebastian Tomczak said...

I don't think what you want to do is exactly possible.

The closest would be to use a MIDI interface and then just go MIDI output from the Arduino to the MIDI input on the interface.

It is absolutely possible to generate CC data using digital as well as analog inputs. No worries.

I will have a look at it if you like.

Anonymous said...

oh that would be great..I'm usimg analog multiplexers but I don't use them for switches. Only for pots.
Then i can use digital shift registers for the switches..

Sebastian Tomczak said...

okay, cool. i only have 4021s lying around here at the moment, so any example i make will use a 4051 to multiplex the pots and a 4021 to shift in the digital states. give me a day or two...

Anonymous said...

that's great. thanx seb...

Sebastian Tomczak said...

Okay, here it is:
http://little-scale.blogspot.com/2007/11/pots-and-switches-to-midi-data.html

Let me know how you go. I know the code is not very optimised yet, but it is something i whipped up just now.

regards,

sebastian tomczal

Anonymous said...

very cool got this working with absynth it's great. is there a way of also having midi notes triggered with arduino at the same time? like an octave or two?

Sebastian Tomczak said...

hi there, glad you have got it working.

yes there is. how were you wanting to trigger the MIDI notes? ie. buttons, sensors, pure code?

Anonymous said...

thanks for the reply. was hoping with just 8 or 16 pushbuttons for now. love what you've done so far though it rocks.

Anonymous said...

think i may have posted this already. ya i wanted to have 8 or 16 pushbuttons for now to send midi note info-thanks for your help.

John said...

i got it to send midi with some switches with the remaining digital in's but now im wondering if i could add more as my inputs are filled up. sorry in advance if you have posted something like this before.
Thanks for all your help and awesome arduino experiments.

Sebastian Tomczak said...

Well, if you are looking for more digital inputs, perhaps using a 4021 shift register will help you.

That way you can have 8 bits of data (ie. 8 switches) using just three lines on the Arduino.

cheerio

John said...

ok cool. think i might have a few of those around even. i'm still thinking about a monome like arduino though hehe. but that's a different story. thanks for your time and cheers from Vancouver Canada

John said...

what about 8x8 matrix using 2 4021's? or am i nuts.

Sebastian Tomczak said...

well, i was thinking perhaps an 8 x 8 using a 595, some diods and a 4021.

then, the 595 has 8 outputs and the 4021 has 8 inputs. you just hold one of the outputs of the 595 high whilst measuring the state of the eight inputs on the 4021, then repeat for all 8 outputs on the 595. connected from the outputs to the inputs are 64 buttons or whaetver.

dunno if this would work though.

Anonymous said...

Hi Sebastian thanks for your help. Hey i was wondering if you could shed some light on the monome schematic here i see that they are using a 74hc165 parallel to serial chip but was wondering how they are using the 74hc164 in this situation. they bot go to the keyboard led matrix board with the max 7219 controlling the leds. just if you have a sec. don't want to keep bugging you hehe. perhaps it's the canadian in me. have a great weekend
http://monome.org/here/monome40h_thrulogic_schem.png

Unknown said...

Is there any good reason why i shouldnt be reading switches with multiplexors ? it seems to work fine. Also i couldnt get this sketch to work, the example you have with only one multiplexor and eight pos worked fine but for some reason this one in addition to sending the read values spewed out loads of cc_messages as though something was unearthed and not connected to 5v either, and proceded to do this even when i plugged analog inputs 0 and 1 directly into earth. i worked around it by adapting the other code (probably in a stupid and unelegant way) to read a second mux after looping through the 8 inpus on the first.

Haddarelli said...

I am designing and manufacturing a device for a university project
that will allow a band member, in this case the singer, to alter the volume of the overall monitor mix they hear. They can only increase or decrease the final mix. The Front Of House engineer should have overall control of what they are doing so some kind of automation can be set up at the main desk so the engineer knows what the output levels are at.

I have since been advised to find out more on MIDI Shields.

I realise this may seem like a totally random post but any input
would be greatly appreciated.

luc said...

hi

Thanks for that program and circuit. Very useful and efficient.

Well, I'm trying to get this working under 9v external powwer but it appears the arduino doesn't want to work without a connection to usb. I tried to learn more by doing many tets and it appears that the arduino needs to be plugged during a few seconds to the computer and after that it works on its own with an external power. I tried with other programs for midi code loaded into it and that doesn't need to be initialized by usb plug to work in a standalone way. It's the first time I see the arduino needing a usb initianilisation to work afer it has a program loaded. Is there any mistery?

Thanks

regards

Luc

TFr said...

Great post, in fact great blog all around. I'm subscribing (can't wait to see what you do with the Genesis next!)

I am trying to do something similar but the pots I'm using are 25ohm. In order to adapt your circuit to 25ohm pots is it sufficient to add a 25ohm resistor going to the ground between the analog input on the arduino and the mux's output? Or do I need to add a 25ohm resistor going to ground to the wiper on each pot (so the wire to the Mux's input and the resistor to ground are both connected to the wiper?)

I started out with a simple voltage divider with 4 25ohm pots/4 25ohm resistors but my plan grew. Now I wish I had started over from scratch with 10k pots instead of using the hardware I had laying around!

Tobias said...

Great thing! Do you think it's possible to do the same with 4 or 6 multiplexers? And what would be the changes in the code?

Nathaniel Shiftmore said...

In response to the arduino midi over usb issue.. I also wanted my arduino midi projects to work without a custom listener, so for a few of them, I embeded the guts of a cheap ($6) usb-midi interface cable right into the project.. I wrote it up in detail here. Also have an example of a midi controller that uses it here.

Its a hack, but it works really well for what I need it to do. Hope that helps.

btw, Sebastion.. This is an awesome blog. Lots of great ideas.

Anonymous said...

Good article, thanks. Many people have written petition for Puma / Nike shoes act. There are many people over the world protested. Chaussures Sport,tennis rackets,cosplay costumes,ed hardy clothing,Cheap Polo Shirts,
The North Face Jackets,
cheap ugg boots
tn requin,
ed hardy online store,
Nike Chaussures Hommes
discount Ugg boots,
Chaussures Requin tn

Alex said...

Hi
This is a great project! I would love to use up to 8 multiplexers. Please could you advise me how to change the code to support this. I'm still beginning...

Thanks!!

Anonymous said...

Hi!
I really like your project. I want to build and would like to use with tractor dj. But I do not know if this works the tractor dj crossfader with potmeter. How do I connect the arduinohoz and send a signal with the crossfader.
Thank you.Tobyasz