Tuesday, January 08, 2013

Control a Small Servo Motor from Live via a USB MIDI Connection




I wanted to be able to control a servo motor from Live directly via a USB connection using MIDI data and no software layers. This was done via a Teensy board.

Most servo motors have three connections. The ground is usually black, the power is usually red and the signal is usually white.

In terms of setting up the hardware: 
• Connect ground to Teensy ground
• Connect power to Teensy VCC
• Connect signal to Teensy digital pin 0

Below is some basic code. With this code, the servo will respond to MIDI CC messages. Edit to taste.



 
void setup() {
  usbMIDI.setHandleControlChange(OnControlChange);
  pinMode(0, OUTPUT);
}

void loop() {
  usbMIDI.read(); 
}


void OnControlChange(byte channel, byte control, byte value) {
  for(int i = 0; i < 20; i++) {
    digitalWrite(channel, HIGH);
    delayMicroseconds(value * 6 + 700);
    digitalWrite(channel, LOW);
    delayMicroseconds(22000 - (value * 6 + 700));

  }
}
 
 
video
 

2 comments:

Michael Colombo said...

Hi Sebastian - I covered this project on blog.makezine.com - it goes live at 12:30 pm PT - I'd love to see/hear the music you end up making with your servo(s) - please keep me up to date as I'd love to do a follow up. I'm at mcolombo (at) oreilly (dot) com. Thanks!

Anonymous said...

Very nice! The Teensy series (I see you're using a Teensy 3.0) is just awesome for music apps because of its built-in USB MIIDI support. Paul Stoffgren rocks. You should include a link to the Teensy web site in your post.

Good luck!