Arduino MIDI Volume Pedal

I’m playing keyboards this fall in another rock concert to benefit the high school robotics team, and for some of the tunes I need to be able to fade an organ in and out over a period of a measure or two. My keyboards are velocity-sensitive, so if you hit the keys harder they play louder (like a piano); and they have aftertouch, so if you press down extra-hard on the keys you can get special effects. But there’s no good way to change the volume of their organ sounds dynamically, and these synths don’t have inputs for volume pedals.

This is the MIDI volume pedal project I was starting to work on when I took apart a Baldwin organ swell pedal and decided to leave it intact based on what I found inside. I got another analog volume pedal from a pile of unknown origin at the school lab and finished the job.

Homebrew MIDI volume pedal using Arduino

Why MIDI

Old-school audio devices control volume with a potentiometer, usually in the input or preamplification circuits, to choose how much of the audio input signal to feed into the power amp stage. My synthesizers have master volume knobs that (as far as I know) work this way. The designers could also have offered a jack to allow an external volume knob (or pedal) to do the same job in the same way, but they didn’t — among other reasons, because it would be prone to delivering interference and hum right into the preamp section, where it would create the most problems.

Note that it would be possible to run the synths’ line-level output through an external volume pedal on its way to the amplifier, much like guitar effects pedals. But that would require stereo potentiometers to control the stereo output of the synths, which the inexpensive pedal I’ve borrowed doesn’t have. And while it might have a low risk of introducing noise here at home, I know Ron (the perfectionist organizing the concert) wouldn’t approve of an extra device inline in the signal path on stage.

But in addition to the physical master volume control, my synths also have an internal volume setting that’s controlled by software (like the volume control on an iPod, or a PC, or a higher-end car stereo). And this internal volume setting can be addressed by MIDI, since MIDI is digital commands sent serially over a cable. Finally, controlling the volume with MIDI has essentially no risk of introducing hum, since the MIDI cable doesn’t even make an electrical connection to the receiving device — the MIDI specification requires that the receiver uses an optocoupler to provide electrical isolation from the transmitter and the cable.

MIDI volume pedals are available commercially, although the ones I looked at appear to be designed primarily to plug into an external inline effects box, which I really don’t think Ron would be happy with my using on stage. Plus why spend $80 to buy something when you can spend nothing and have a few hours of fun scrounging parts, soldering, and writing code???

So I needed to build a device to read the position of an analog pedal, translate the A/D value into the proper numerical range, and send MIDI signals to a synthesizer. Fortunately, the Arduino makes it all easy.

The Circuit

MIDI connects devices using a current loop. That is, it’s not voltage on the line that makes it work, it’s a certain amount of current. And the interface is cleverly designed to reduce the risk of damage if something is shorted or wired wrong. Have a look at schematics of the input and output circuits below borrowed from Tom Igoe’s MIDI communication page, keeping in mind that he drew the optocoupler with its LED “upside-down,” so mentally flip the reverse-polarity-protection diode and the orientation of the optocoupler’s pins 2-3 top-to-bottom:

Tom Igoe's MIDI input and output schematic

On the output side, the sender supplies current-limited power on pin 4 that goes through the cable to the receiver and back to the sender’s pin 5, and then based on the serial data bits “grounds” pin 5 through another current-limiting resistor and a buffer transistor. On the input side, the current is limited and fed through the LED side of the optocoupler. Inadvertently short any one of these pins to 5V or ground at either end and you’re not shorting directly across your power supply lines — you’re just running them through 220Ω, which they can handle easily without damage. That’s good design.

So to transmit MIDI, we need a serial output running at 31250 baud, with the output being open-collector or driving an NPN transistor. The Arduino’s serial TX pin isn’t open-collector and I didn’t feel like messing with an OC buffer chip, so I went with the transistor method. The catch is that a transistor being used as a logic buffer inverts the signal coming through it — a high signal on the base pulls the collector low (for NPN) — so I needed two transistor stages to get the serial signal positive-going again. Tom’s circuit (above) uses a hex inverter to restore the signal, but I used a second transistor.

MIDI output schematic with two-transistor buffer

On another of Tom Igoe’s MIDI pages, he uses a shortcut and drives pin 5 directly out of the Arduino, to avoid the hassle of the transistor buffer. I tried it and of course it works; but I’d recommend for safety putting another 220Ω resistor between the Arduino and pin 5. It’s not electrically quite the same as using the transistor buffer, but it’s closer than without that resistor.

Now we’re wired up on the MIDI side, and wiring the volume pedal potentiometer to an analog input is old hat. The volume pedal enters on the black cable and 1/4″ jack; the MIDI exits on the white cable, scrounged from an old AT computer keyboard. (Who knew that MIDI and AT keyboards were both DIN-5/180? How handy!) When I build this into a box, I’ll put a MIDI jack in it and use a regular MIDI patch cord, but the keyboard cord was handy for prototyping.

A Little Bit of Software

The software for this is so easy, it’s almost embarrassing. The framework came from Tom Igoe’s second page referenced above, with additional MIDI code information from the Harmony Central reprint of MIDI status and data bytes and Jen Lewin’s MIDI for the BX-24 web page.

/******************************************************************************
 *  midi_volume
 *  Keith Neufeld
 *  July 8, 2008
 *
 *  Read an analog input from a pedal and generate MIDI volume commands.
 ******************************************************************************/

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

void loop() {
  midi_volume(1, analogRead(0) / 8);
  delay(1);
}

void midi_volume(byte channel, byte vol) {
  Serial.print(0xB0 | (channel & 0xf), BYTE);  //  control change command
  Serial.print(0x07, BYTE);  //  volume command
  Serial.print(vol & 0x7f, BYTE);  //  volume 0-127
}

That’s the whole thing!

MIDI commands begin with a status byte with the high bit set and continue with two data bytes with the high bit clear. So the code reads the potentiometer position in the range 0-4095; divides it to get it to the range 0-127; and sends:

  • the status byte 0xB0 for a control change (kind of an extended command)
  • the first data byte 0×07 for a volume change, then
  • the second data byte containing the desired volume in the range 0-127 (remember, high bit clear, so limited to 7-bit data values).

It then delays briefly and does it again. The delay isn’t strictly necessary — a synth should be able to receive MIDI at full speed — but my synths are old (1984) and I don’t want any risk that they’ll overload their processors and “hiccup” if they’re receiving a steady stream of MIDI signals at the same time I’m playing.

And It Works

It works great! Here’s a rough sample of the organ chords from a lesser-known Molly Hatchett song. Nothing fancy (and it sounds odd out of context), but it demos the pedal.

dreams.mp3

The MIDI-controlled volume of my synth doesn’t have as wide a range as I’d like, but I’m grateful for being able to control the volume at all.

Future Plans

Build an enclosure. Add a backlit LCD and a rotary encoder to change parameters, allowing the pedal to send different signals: volume (how it’s set now), expression (which I understand is the right parameter for dynamics, but my synths are too old to implement it), and any other commands you want to send. (I’ve already tweaked the program to let me use the pedal to control the pitch bend, which is extremely silly. But it could potentially control anything a given synth offers via MIDI, like phasing/flanging, leslie, etc.)

Build a double-wide and allow the two pedals to be assigned to different settings and/or different synths through software. Accept MIDI input to the pedal(s), to allow the synth’s patch chain commands to reprogram the pedals for different effects on different songs.

Make a PCB and publish the plans. Offer kits. Become famous and change the world. :-)

20 Responses to “Arduino MIDI Volume Pedal”

  1. remco says:

    hey, this is very interesting. I wanted to make an optical ‘theremin’ like controller using 4 (or 6) IR-sensors. But I was wondering how exactly to do this.. I thought about buying the arduino, but I’m not sure what else I will need for this. Can I make 6 midi-controls from a single arduino with this mod you’ve showed here?
    remco

  2. Keith Neufeld says:

    Remco, you can send data on multiple MIDI channels over a single cable. So you could easily, for example, use a single Arduino to control the pitch and volume of three sound-generating synths — or probably even three software synths on the same computer.

    What you can’t easily do is have one Arduino control multiple separate MIDI cables. It shouldn’t be required; but if you run into a situation where commands to one synth on one channel are interfering with a different synth on a different channel (and I believe I’ve encountered a degenerate case where that happens, but only while doing SYSEX, which you shouldn’t need in a faux-Theremin application), you might need to split out to separate cables. And that will require external UARTs or clever multiplexing and polling.

  3. remco says:

    thanks for the reply! I know you can send different midiparamaters through a single midi cable.
    but the ‘problem’ I have is that I’m not really familiar with the arduino, so before buying one, I would like to know if I will succeed in making it at all…

    for example,
    I’ve found this ‘miduino’, which should make midi commands from the 6 analoge inputs automatically (as far as I understood). But I’m wondering if I’m in need of any other arduino-pieces before I can finish the controller using this miduino. Is it really as simple as connecting 6 infrared sender/receivers to the miduino, setting the midi channels and GO! ? (ok, adding the addaptor of course..)

  4. remco says:

    ah, this is the sensor I’ll be needing :) ‘Sharp GP2D12′

  5. Keith Neufeld says:

    Remco, you’ll need some kind of a board to connect everything (like my breadboard above), and you need some circuitry to convert the Arduino’s output to MIDI’s electrical specifications (which was largely the point of this blog post).

    Other than that, nope, you’ve picked a project that should be really simple to do! The Sharp sensors need power and ground and will output voltages that you can feed straight into the Arduino’s analog inputs. Then you’ll write a tiny little program like mine above that reads each analog input and sends a MIDI pitch or volume signal in response. That’s it!

  6. Drew Franklin says:

    I just found this blog today, and I am excited I found it. A fellow looper and arduinoer (I am sure there is a better term for that). The interesting thing is I googled “using a volume pedal control data” just to figure out the mechanics of how a volume pedal sent the potentiometer values, but ended up getting a complete solution to the exact arduino problem I was trying to solve code and all.

    So my question has to do with the swell pedal 1/4″ cable that is plugged into the black box with tree wires coming out.

    So my first question is what is that female input called? The quarter inch female input I have only has 2 leads coming off it, and it looks like yours has 9.

    Second question, is the 1/4″ cable coming off of the swell pedal only tip and sleeve? If so (which I assume it is) how are those two connecting to the tree leads?

  7. Keith Neufeld says:

    Drew, this pedal is wired as a potentiometer — you give it V+ and GND and it gives you a voltage back — so it uses tip/ring/sleeve. I understand there are some wired as variable resistors — just two wires, connected to one end of the pot and its wiper. But that’s why three.

    The jack is a switched stereo headphone jack. You know how on many receivers, you plug in the headphones and the speakers automatically cut out? Independent of the connections that go to the plug, this jack has three NC switches inside that get opened when the plug is physically inserted, that in whatever equipment I pulled it out of would have interrupted the internal audio preamp signal from getting to the power amp when you plugged in the headphones. That’s why nine connections, when three would have sufficed for this project — it’s just what I had on hand.

    If you have a three-lead pedal, you can set it at a mid position and take ohmmeter readings to figure out which are the ends of the pot and which is the wiper; then move the pedal and measure from the wiper to each end to determine which is the “top” of the potentiometer and which is the “bottom.” Then zoom on one of the closeup pictures of the breadboard in my post — connect the “top” to +5V, the “bottom” to GND, and the wiper to an analog input, and you’re good to go.

    After you wire it up, meter it again as a sanity check to make sure you got all the jack pins correct. It’d be a pity to swap wiper with +5V or GND and short the Arduino power supply through your pedal the first time you pushed it to one end or the other.

    All that make sense?

  8. Drew Franklin says:

    Thanks a lot for your answer Keith. They always tell you when you make assumptions what happens, but I never listen.

    So the way you have it set up makes perfect sense, but let me explain why I thought it was a tip/sleeve cable. So with the echoplex there is a input jack in the back in which you can plug a mono passive volume pedal into and which it controls feedback.

    But I want to be able to plug this same volume pedal into this arduino midi foot controller I am building. Since the midi controller has different banks the volume pedal could control different parameters. On one bank I could use the volume pedal to control feedback level and on another bank use it to control loop volume ( and maybe later be a wah pedal on NI Guitar Rig).

    So you said that there are some wired as variable resistors, and I don’t quite understand what that means. Do you think you could help me understand the wiring of that or is this not possible with the arduino? Maybe this is better suited off-blog if you want the comments to be more on topic.

    Again thank you so much for your help. I really appreciate it.

  9. Andy says:

    Hey Keith,
    Quick question…with the Arduino MIDI output, how does it change things if I’m connecting to Vcc rather than +5V. Can I still do this with Tom’s circuit (with the addition of the 220R you suggest), or do I need to then go with the transistor?

    I’d be doing this because I’m trying to parasitically power the Arduino off a MIDI input, and while in theory that should provide +5V, which I could just connect straight to the Arduino as the 5V supply, a lot of manufacturer’s don’t follow spec, and can be as low as 3.3V, or higher than 5V, so I need to use a 3.3V Arduino Pro and go through its regulator. But if the source is, indeed, providing an actual 5V, I want to pass that along to the output, rather than regulating it back to 3.3V, if that makes sense.

    Thanks,
    Andy

  10. Keith Neufeld says:

    Andy, I would say it’s not going to make a whole lot of difference using VCC rather than +5V. In theory the 220Ω resistor on pin 5 should be a lower value for 3.3V operation than for 5V; but in reality I would guess that modern optoisolators will work just fine with a little less current.

    When you say “Tom’s circuit,” I assume you’re referring to using the 7404 hex inverter. Make sure you use a 74*04 that can run on the 3.3V supply.

    Finally, I’m not sure why I used the wacky PNP-NPN buffer circuit instead of two NPNs with a 10K from TX to Q1′s base, emitter tied to ground, 10K from VCC to Q1′s collector and Q2′s base (acting as a pull-up), and the remainder of Q2 and the DIN connector’s configuration the same.

  11. Andy says:

    My bad, I should have clarified :-) I meant Tom’s simplified circuit, without the inverter/transistors.

    My concern is that a problem might arise if Vcc is higher than the 3V3 the processor is running at (most likely itd be the standard MIDI 5V), thered be a potential voltage difference of up to 1.7V between the serial out and Vcc. If it were an open-collector, no prob, I’m not sure about the Arduino’s, though.

    (And there’s the further chance that it might hit another “utility extension” device (like a MIDI Solutions Relay) that isn’t opto-isolated, so…)

    Thanks,
    Andy

  12. Elvio says:

    Dear Keith,
    When I saw the picture on this page, I thought: “That’s it!”… but it was not quite, actually… :-(
    I recently bought a Motu Ultralite MK3 interface. I would like to connect my guitar and a mic. As I would like to control the volume of the guitar with a pedal, I tried an Ernie Ball volume pedal… but I didn’t like it at all :
    1) the tone really gets worse
    2) and the range of the pedal is closer to a switch!
    So, I am trying another approach. I have a MIDI expression pedal. Connecting it to my Mac, with a simple Max/MSP patch I can simply lower the volume of the channel, with no tone loss, and with a scale that I decide. So, bingo?
    Well, not yet… The only way I can connect this pedal (an MGear Ex-P, by the way) is through a keyboard or a control surface that has the appropriate stereo-jack entry… and I wouldn’t have to transport this equipement just for this purpose.
    So, I ask you: is it possible to connect an expression pedal to the 5-poles DIN standard MIDI IN of the MOTU interface ?
    I am a musician, and all I can do with electronics is soldering some audio cables, but I did my best to understand the MIDI process.
    If I got it well, a 5v tension runs to and from pins 4 and 5; the pedal is just a potentiometer, a variable resistance that reduces this tension…
    I wired it all, but it does not work. Why?
    Thanks in advance,

    Elvio

  13. Keith Neufeld says:

    Elvio, what I built is exactly what you need, for exactly the reason that what you tried didn’t work. :-)

    The problem is that MIDI (“Musical Instrument Digital Interface”) is a digital signal and expression pedals are analog resistance. Even expression pedals sold for “MIDI controllers” are analog inputs to a controller that’s doing what my little circuit does. Connecting the analog signal across pins in the MIDI doesn’t do anything useful.

    The circuit I wired is basically a tiny MIDI controller built custom just to run an expression pedal. It’s the same as if you bought a MIDI controller (or control surface) and plugged the pedal into it and it into the Motu. The only difference is (perhaps) size — you can see how small this circuit is. It could be made even smaller if it were purpose-built instead of on a general-purpose prototyping board.

    So … I’m not sure what to suggest. I could certainly build you this circuit into a box the size of a guitar pedal and you’d have a smaller thing to take with you, but I don’t know if that’s what you want. Plus it’d cost somewhere around $35-50 in parts (including the case), and I don’t know how that compares with what you’d pay for other solutions.

  14. Elvio says:

    Thanks so much, Keith!
    …seems crazy to me: do you mean that an audio volume pedal and a MIDI expression pedal have the same output???
    Could I possibly connect an audio pedal to a controller and have the same results?

    As for the circuit… I found two commercial solutions, both quite expensive:
    http://www.thomann.de/fr/midi_solutions_pedal_to_midi_converter.htm (117€, one input, one 5pin output)
    http://www.eowave.com/controllers.php (120€, four inputs, one USB output) (for some reason the site is down at the moment)
    …so well, I’d like totry the DIY way! :-) Do you know if the parts are available in Europe?

    €:-I

  15. Keith Neufeld says:

    Elvio, actually, see my conversation in the comments with Drew. There are pedals that change the resistance between tip and ring on a TR 1/4″ plug, and there are pedals that change the voltage on one of the pins (I forget which) on a tip-ring-sleeve (TRS) 1/4″ plug. The two aren’t compatible, and this particular circuit uses the latter.

    I can’t guarantee which of the two methods an audio volume pedal and a MIDI expression pedal use — in fact, I can’t even guarantee it’ll be the same for all audio volume pedals or all MIDI expression pedals. (It may be, but I’m not familiar enough with those markets.) But all pedals with TR plugs should be compatible with each other, and all pedals with TRS plugs should be compatible with each other (with the exception that some might work backwards, and some might have a switch to correct for working backwards).

    The Arduino is developed and manufactured in Italy, so I know you can get it in Europe (although I don’t know where). A 1/4″ TRS jack, the 5-pin DIN connectors, and the resistors and transistors for the MIDI interface should also be available at any electronics parts store.

    Can any of my European readers tell me, do you have electronics stores like Radio Shack that are everywhere but have only the barest essential components for hobbyists, and stores like Frye’s that are sparse but have everything? Also do you have preferred mail-order sources different than Digi-Key and Mouser that have better shipping costs to Europe?

  16. Marco says:

    Alo Keith,
    Nice work, congratulations for sharing this information.
    I´trying to find a single midi controller pedal, do you know something like this…?
    like a single control pedal for keyboard but as a midi controller.

    If you know something please let me know…

    Thanks and kind regards
    Marco Santos

  17. Andy says:

    Marco,
    If you want something pre-built, I don’t know of anything built into a pedal, but MIDI Solutions makes the footswitch controller, which can take a footswitch input and spit out whatever MIDI Message you require. They also make the Pedal Controller, which accepts an expression pedal and can convert that to any desired message (aftertouch, pitch bend, cc, or sysex). So it’s basically a closed-source version of the same concept as Keith’s pedal, if you’re just looking for something ready-to-use. http://midisolutions.com

    –Andy, a MIDI Solutions customer, and somewhat of a friendly-competitor

  18. Giovanni says:

    Thank you for the example.
    But above I read :”So the code reads the potentiometer position in the range 0-4095; divides it to get it to the range 0-127; and sends:…”

    Only a little comment :-)
    The Arduino A/D converters are 10 bit wide so the values obtained are in range of 0-1023. And thus it is right to divide by 8 to obtain a number among 0-127

  19. Misha says:

    Can you make this work with 4 pedals, and can you run MIDI through thins unit? I have an organ project, and if it is possible, it could save hundreds of dollars!

  20. GDLive says:

    Hi, nice work !

    I’m trying your example since I’m not able to reproduce simplest arduino-midi out wiring (such as here :
    http://itp.nyu.edu/physcomp/Labs/MIDIOutput
    ).Doesn’t work for me, as I said here :
    http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1290794831

    Since I’ve haven’t PNP transistor nor 7404 Hex inverter (i’m note sure to understand what it is for), I tried your second proposal with two NPN 2PN2222.

    “>Finally, I’m not sure why I used the wacky PNP-NPN buffer circuit instead of two NPNs with a 10K from TX to Q1’s base, emitter tied to ground, 10K from VCC to Q1’s collector and Q2’s base (acting as a pull-up), and the remainder of Q2 and the DIN connector’s configuration the same.<"

    Result : I don't see any midi message on my soundcard !

    I don't know what I could try, every suggestion would be welcome !

    Thanks

Leave a Reply