How-To Control Csound with a Custom Midi Controller: Hardware (2 of 2)

In last week’s installment, we showed how to get started with Csound.  This week we take it to the next step by constructing a homemade MIDI controller circuit and use the new device to control Csound in real time.

What you will need:
a computer on which you have Csound up and running
a MIDI adapter for your computer (usb to midi adapters are the norm here)
a microcontroller / breadboard / microcontroller programmer (in this example we will show some BX24 sample code)
a MIDI female connector (either a cable or circuit board mount type, also known as 5 pin din)
a 2N2222 NPN transistor
some resistors (10Kohm and 220ohm)
some sort of sensor or button or potentiometer or any combination of the above


Circuit

First we’ll start by programming a microcontroller to grab data from your sensors. In this case, we used a BX24 microcontroller from BasicX. You may choose to use an Atmel or PIC microcontroller instead, excellent instructions for MIDI on a PIC are found on Tom Igoe’s Physical Computing pages.

Wire up your microcontroller to power and ground according to its circuit diagram.  To the circuit you’ll now add the MIDI circuit.  For this you’ll need the 2N2222 transistor.  Hold the transistor by the legs with the tab facing you, the pinout for the transistor is: C, B, E or Collector, Base, Emitter. 

pnp

On your breadboard connect the MIDI circuit as follows:  The collector is connected to a 220 ohm resistor which is connected to the second pin from the left on the back of the female midi connector (labelled pin 5).  The base is connected to a 10K ohm reistor which is connected to the serial data output pin of your microcontroller (ours is pin 1 aka TX aka the pin on the upper left).  The emitter is connected to ground.  Now connect the middle pin (pin 2) on the female midi connector to ground. On the female MIDI connector’s second from the left pin (pin 4) put a 220 ohm resistor.  Connect that 220 ohm resistor to +5V on your breadboard.

circuit

This image shows the MIDI circuit with the breadboard and microcontroller:

midicircuit

Now connect up your sensors.  In our example we will connect two continuous controllers (think: something that sends continuous data, not a switch that sends an on/off) to our microcontroller.  On pin 15 (third up from the bottom right) we connected a potentiometer.  On pin 14 (second up from the bottom right) we chose to put a flex sensor. Notice in this image the MIDI connector at the bottom and the two continuous controllers on the bottom right hand side of the microcontroller:

circuitdiagram

These two continuous controllers now need some code to grab their data and send it over MIDI. The BX24 is programmed in Basic with free software from BasicX.  Open your programming environment and open your code editor.  Here’s our code for grabbing data from the two continuous controllers:

Option Explicit

'basic sensor to MIDI example
'Fabienne Serriere
'www.engadget.com
'feel free to reproduce, tweak, pass on, use and re-use
'potentiometer on pin 15, flex sensor on pin 14

dim outputBuffer(1 To 13) as byte
dim inputBuffer(1 To 10) as byte

dim note as byte
dim pitch as byte
dim velocity as byte

dim pot as integer
dim flex as integer

sub main()

'start the input and output buffers
call openQueue(outputBuffer, 13)
call openQueue(inputBuffer, 10)

call openCom(1, 9600, inputBuffer, outputBuffer)
register.ubrr = 14

do
pot = getadc(15) 8
flex = getadc(14) 8
'grab analog to digital data off of pins 15 and 14

'debug.Print "pot = "; CStr(pot) 'uncomment to see pot value in the bx environment
'debug.Print "flex = "; CStr(flex) 'uncomment to see flex value in bx environment

note = 144 'MIDI command for note on channel 1
pitch = cByte(pot) 'get pitch from pot value
velocity = 64 'how hard the note is struck
call putQueue(outputBuffer, note, 1)
call putQueue(outputBuffer, pitch, 1)
call putQueue(outputBuffer, velocity, 1)

note = 145 'MIDI command for note on channel 2
pitch = cByte(flex) 'get pitch from pot value
velocity = 64 '
how hard the note is struck
call putQueue(outputBuffer, note, 1)
call putQueue(outputBuffer, pitch, 1)
call putQueue(outputBuffer, velocity, 1)

delay(1.0)
note = 144 'MIDI command for note on channel 1
pitch = cByte(pot) 'value from potentiometer on pin 15
velocity = 0 'a value of zero stops the note started above
call putQueue(outputBuffer, note, 1)
call putQueue(outputBuffer, pitch, 1)
call putQueue(outputBuffer, velocity, 1)

note = 145 'MIDI command for note on channel 2'
pitch = cByte(flex) 'value from flex sensor on pin 14
velocity = 0 'a value of zero stops the note started above
call putQueue(outputBuffer, note, 1)
call putQueue(outputBuffer, pitch, 1)
call putQueue(outputBuffer, velocity, 1)

loop
end Sub

If you want to test your controllers directly in your programming environment, you can uncomment the debug.Print lines by deleting the apostrophe at the beginning of the line.  Note that this code is for the BasicX environment only and will have to be modified if you’re using another type of controller.  A great resource for PIC microcontrollers and MIDI are Tom Igoe’s Physical Computing pages.

Now connect up your new circuit to your midi adapter on your computer using

Comments

  1. ray says:

    wow.
    this is *quite* awesome. should make a very lovely starting point for my next composition.

  2. ST says:

    What good would unhackable equipment be?!
    You can get some nice MIDI’s over here -> http://www.tonefresh.com

  3. toto says:

    1337

  4. windwaker says:

    Awesome. Anyone know how to calculate what the 2n2222 outputs?

  5. fabienne says:

    windwaker: not sure what you mean by “calculate what the 2N2222 outputs”?
    if you mean what data it outputs in this midi circuit example, simply plug a midi cable into your usb to midi adapter and in your computer run a program that can see midi messages (e.g. puredata example above).

    if you mean you want to see when midi data is being sent, simply connect an led in line with the output of the collector on the 2N2222 to see the led flash when data is being sent.

    if you want to understand how the 2N2222 works see http://en.wikipedia.org/wiki/Bipolar_junction_transistor

  6. fabienne says:

    i’ve put some annotated versions of the breadboard shots up on flickr:

    circuit01

  7. Tom says:

    I think you’re overcompensating after all those “gaffa tape some guitar pedals to a board” how-tos…

  8. windwaker says:

    Ah, thanks tons fabienne. I needed to calculate the output current from the emitter, but I think your links cover that.

    Amazing article.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 96,477 other followers