Teensy MIDI Controller Code
- Categories Resource
Visit the GitHub page to download this code.
I’m a big fan of the Teensy microcontroller family. They are small, cheap, have plenty of ins and outs, and you can turn them into USB MIDI devices really easily (or many other types of standard USB devices). The Teensy 3 and LC also have a bunch of “touch pins” built in, which means you can just attach any bit of metal to them and they’ll become capacitive touch sensors.
I would highly recommend them to anyone embarking on building a custom MIDI controller – Arduino gets all the headlines, but the Teensy is really much better for that kind of thing. If you are sticking to MIDI devices the LC version will do everything you need at a low cost.
However, it can be tricky to get started. I’ve managed to put together a piece of code which is relatively flexible and can be used for any number of combinations of buttons/knobs/touchpins/etc.
This code was made in the Arduino environment and should work with any of the Teensy boards. The only exception is the touch pins, which will only work on Teensy 3 or newer, as well as the LC.
The code has comments to show you where to input the number of pins you want to have each function, and which specific pin numbers to use for each function. For MIDI notes there are arrays where you can specify which notes to use. The different available functions in version 2.0 are:
- Analog inputs to send CC values
- Digital inputs to send note values
- Digital inputs to send CC values
- Touch pins to send note values (with constant velocity)
- Use MIDI output to control things attached to the pins (lights/motors/etc)
- Support for both usbMIDI and regular MIDI using din connectors
Still to do:
- Add ability to use Touch pins as CC (this is tricky…)
- Add pitch bend support
You’ll have to set the serial port to MIDI – for more information read the page on the PJRC site.
Visit the GitHub page to downloadd.
Teensy MIDI Controller Code v1.0
Get in touch if you have any questions or if you run into any trouble.
hello do you have a code for teensy 3.1 or this will work on that as well
Hey! I have not yet tried a teensy 3.1, but I think it will probably work exactly the same. Let me know if you have any problems…I’ve ordered a few 3.1 boards and will be testing them out next week.
so did you try with a teensy 3.1?
oh yeah, i forgot to revisit this: yes, this code works perfectly with the teensy 3.1 as well.
Nice projet ! I’d like to try it
What about the responsivity ? is it reactiv enough or do you have a little latency ?
I’ve never noticed any latency at all, everything I’ve made is just as responsive as any commercial MIDI controller.
Could you make a tutorial on how to make our own please ?
I probably should, I’ll put it on my list of things to do!
Just a schematic would be nice to begin with !
Can you please post the schematic?
In the meantime: do the touch sense inputs use 1 meg resistors to ground or to another pin?
I don’t really have a schematic for this – you don’t need to do any special wiring on the touch inputs, they are automatically configured to work when you call them touch pins in the code. So you just need to say “touchRead(pin_number)” and then connect some sort of wire directly to that pin (and touch it).
I hope that makes sense!
Hi, do you know if you can send an especific Midi Port number using this usb.midi? something that can be read by ctlin in a Max patch. Thanks nice work!
I’m not 100% sure I understand what you’re asking – this code should work with the ctlin object in a Max patch over USB as it is. I think it comes up as a device called Teensy USB…if you want to change that name to something specific it’s apparently a bit more effort (and I’ve never bothered).
Does that make sense? Let me know if I’m not actually answering your question!
is it possible to use the touch pins as note triggers with velocity sensitivity? If yes, could you provide us with code for this feature? I haven’t found anything about this on the web so far. Thanks in advance!
In my experience, making velocity sensitive notes using touch pins is challenging, because of the way MIDI note messages work – you have to send the message for the note and the velocity at the same time. That is really impractical to do with a touch pin, because by the time you’ve sent the note value the amount of touching will have changed a lot. You can get a quite good range of values from how much you’re touching, but it’s pretty much impossible to send the value of how much you’re touching to the note velocity in a way that feels right! In practice, by the time you’ve sent the note paired with a velocity it’s too late and you only sometimes get a direct correlation between touch and volume.
It’s quite hard to explain, I hope that makes some sense. There are a couple of possible approaches:
– Make a small delay between capturing the touch value and sending the note. This would have the advantage of probably getting a more accurate correlation between how much you’re touching and the eventual velocity, but the major downside would be a significant feeling of latency.
– Avoid using velocity at all and instead use Aftertouch. I think this is the best solution, even though aftertouch isn’t used all that much in most MIDI software or hardware. You could assign aftertouch to control volume, and then use the MIDI.sendAfterTouch message using the value of your touches. I haven’t tested this but I feel like it should work!
In any case, you’d have to make a system for calibrating the touch values, as they can vary wildly depending on what you are using as your conductive object. I would approach this by making all values set to 0 on startup, and then have a button you can press whilst touching the object which would then set that as the maximum of 127.
I hope that makes sense, good luck!
Hello sir can i make a midi controller using teensy, which will control pitch or tempo with 2 pushing button (-button and + button) instead of potentiometer , can potentiometer control be changed into 2 button (- and +) if yes what is type of this controller ,,,,thank you
Yes it would certainly be possible to make a controller that uses buttons to control the tempo rather than a potentiometer. It would take a bit more code.
Off the top of my head:
You would first want to set up two buttons going into the teensy, and you’ll want to set them to increment and decrement an integer variable. There are a number of ways to do this, perhaps the best way is here: https://www.arduino.cc/en/Tutorial/StateChangeDetection
Taking that code as a starting point, you would have one button doing buttonPushCounter++ and the other doing buttonPushCounter–.
Then you send that variable out a MIDI CC controller output and map it in the software.
I hope that helps!
Hi Yann. Really cool project you have.
This may be a newby question and if so, I apologize.
Online, all of the Teensy boards I see, show to have only 11 touch sensitive pins, however, your project shows to have a few more. How did you get the extra pins?
Thanks for your time and information!
Hey there, thanks for the kind words.
The smaller Teensy boards have 12 built-in touch sensitive pins…three of these are accessible only as little pads on the underside of the board, so they are a bit impractical to use if you’re not used to surface mount soldering.
I imagine you’re referring to my touch sensitive piano prototype, which used 15 or so touch pins if I remember right…to do that I used a different method, using a Teensy++ and this system: http://www.pjrc.com/teensy/td_libs_CapacitiveSensor.html
I don’t completely remember the circuit I made, but the details are on that site. Essentially each touch pad needs to be connected to a “receive” pin, as well as a “sensor” pin. The various touch pads can share a receive pin. The resistors you use set the sensitivity. Because the Teensy++ has so many pins this allows you to have more touch sensitive inputs.
I hope that helps!
Hi Yann, thanks a lot for your code ! I want to have a number of pots and buttons sending midi notes, and have 2 other buttons selecting banks, in order to send different MIDI notes depending the bank I’m in. Is it possible ? Thanks !
That sounds like a good idea, I guess this could also be like octave up/down buttons?
I suppose the way I would do that would be to program the “bank” buttons to add an offset to the midi note number coming out of the array. If that was in multiples of 12 then it would be octaves. Does that make sense?
Hello sir , please help me & read carefully bellow statement.
I am using a music software FL Studio. in this software the main tempo is increased or decreased by turning scroll wheel of usb mouse (up & down), when pointer of usb mouse is placed on particular position (Tempo Digits) in software window . can i do this action by input from teensy board, without placing mouse pointer on that particular position in software window. if yes please tell me about correct code for teensy for these actions.
sir, i have already tried a code (in teensy board) with help of PJRC.com ,teensy is sending cc messege to music software.but it failed according to my need.because when software is run first time,parameter is increased or decreased with unwanted/big change . inc/dec of parameter is not begins from current value.
when a new software file of (150 bpm) is opened after closing of file (90 bpm),then inc/dec is not begins from current file(150),inc/dec is begins from previous file (90 bpm).here i wants that tempo inc/dec should be begins from current value by use of teensy board. i think sending cc messege to software will not update tempo from current value.
instead of it, turning scroll wheel of usb mouse is always begins inc/dec from current value, when mouse pointer is placed on Tempo digits in software window.
at last , i want to inc/dec tempo in softwre from current tempo by use of teensy board.
if it is possible,,,,,,,,,,please help me about code or anthing more………….thank you
Do you have any code for pitch and modulation wheels? I am looking to integrate these into my midi controller but I am a beginner coder to say the least. Thank you
I’m pretty sure that in most cases the modulation wheel just uses CC number 1 (as described here), so you could use this code as is to send mod wheel information.
Pitch bend is a separate thing, though! I need to update this code a bit anyway so I’ll try to include that in the next revision. It looks as though there is a function called “usbMIDI.sendPitchBend” though to be honest I’m not entirely sure how it works…
Tried compiling the v2.0 ino file. Getting missing “midi.h” error. Any inputs?
My apologies, I only just saw this comment. I’m not sure what that error is – perhaps the MIDI library is not properly installed?
The code is missing a line ->
put this just after #include
Trying to make your code work but keep getting the following error message. Can you help?
Arduino: 1.8.5 (Mac OS X), TD: 1.40, Board: “Teensy 3.2 / 3.1, MIDI, 96 MHz (overclock), Faster, US English”
teensy_MIDI_controller_v2: In function ‘void setup()’:
teensy_MIDI_controller_v2:95: error: ‘MIDI’ was not declared in this scope
teensy_MIDI_controller_v2: In function ‘void loop()’:
teensy_MIDI_controller_v2:134: error: ‘MIDI’ was not declared in this scope
MIDI.sendNoteOn(touchpitch[i], 100, channel);
teensy_MIDI_controller_v2:139: error: ‘MIDI’ was not declared in this scope
MIDI.sendNoteOff(touchpitch[i], 100, channel);
teensy_MIDI_controller_v2:151: error: ‘MIDI’ was not declared in this scope
MIDI.sendNoteOff(digitalpitch[i], 100, channel);
teensy_MIDI_controller_v2:156: error: ‘MIDI’ was not declared in this scope
MIDI.sendNoteOn(digitalpitch[i], 100, channel);
teensy_MIDI_controller_v2:166: error: ‘MIDI’ was not declared in this scope
MIDI.sendControlChange(digitalPinsCC[i], 0, channel);
teensy_MIDI_controller_v2:171: error: ‘MIDI’ was not declared in this scope
MIDI.sendControlChange(digitalPinsCC[i], 127, channel);
teensy_MIDI_controller_v2:184: error: ‘MIDI’ was not declared in this scope
MIDI.sendControlChange(analogPinsCC[i], newVal[i]>>3, channel);
‘MIDI’ was not declared in this scope
This report would have more information with
“Show verbose output during compilation”
option enabled in File -> Preferences.
Hi! I only just noticed your comment, and I realise this is very late. You’ve probably figure it out by now, but it looks to me like one f two things…you might just need to change your USB type to MIDI. Before uploading your code, you first need to change your Board to Teensy (it looks like you’ve done this already) by going Tools -> Board -> Teensy (whatever model you are using, then go back to Tools -> USB Type and select “MIDI”.
The other thing it could be is that you’re missing the Arduino MIDI library. You can install that via the library manager at Sketch -> Include Library -> Manage Libraries.
Any chance you can help to get the code modified so that a multiplexing chip can be used?
hmmm this should be possible to do but I don’t think I can do it anytime soon! how many inputs do you want to have? a Teensy can handle quite a large number!