Code for controlling a servo with midi, mine doesn't work yet

Hi All,

As a newbie I am looking for some help with my project.

What I want is pretty simple, I just want to control a servo motor with midi cc messages. It would be cool if the servo only responds to a certain midi channel and with a certain midi cc number, so that it doesn’t respond to all the midi messages on all channels / cc numbers. I made a code which I put down here.

To give some background info as well: I made the midi input circuit for the arduino (Uno) with the optocoupler and that works. I tested it by using some code which makes the arduino LED light up when playing the middle C. The servo motor I connected with the PWM to pin 9 and the grnd to grnd… The servo has an external power supply.

Please let me know if you need more info or if i need to research some essential stuff first .

Thanks again for helping me out with this little project!!

#include <Servo.h>

#include <MIDI.h>

// Define Servo
Servo myservo;
// Define Value
int val;


 void setup()
     MIDI.begin(MIDI_CHANNEL_OMNI);  // Listen to All Midi input
      // Connect servo to pin 9
  // Start Serial communication

 void loop()

     // Read all incoming midi;
     // Connect midi with the Value parameter
  val =;
  // Calculate Midi values (0-127) to servo angle
  val = map(val, 0, 127, 0, 180);
  // Start serial monitoring
  // Send servo angle
  // Delay for servo movement


You do need to investigate MIDI.h in a bit more detail. To get you started:

If you want data from a specific channel just change MIDI_CHANNEL_OMNI to the channel number. may be o.k. for switching an LED on but it only returns true or false not any of the message data. You need to use getType, getData1 and getData2 to get the real information. E.g. getType will tell you if message is a CC or not.

There are several example programs with MIDI.h that should give you some ideas. E.g. try input.ino


MIDI_CREATE_DEFAULT_INSTANCE();doesn’t this creates a midi object on the hwSerial port, and there after you call  Serial.begin(9600);That changes the baud-rate to, well something other than the 31500 midi needs for it, so now it won’t receive any meaningful data.

  // Start serial monitoring

I suppose you could do this since you are not using midi-output, but the SM doesn’t support the midi baud-rate though. So either move the midi input to a swSerial, which should work just fine, or move the Serial debug there.

Serial.begin(9600);That changes the baud-rate to, well something other than the 31500 midi needs for it, so now it won’t receive any meaningful data.

sp. “31250”

sp. "31250"

Sorry, something like that.

Hi Guys!

Thanks a lot for this all, im going to dive into this :slight_smile:

Hi All!

I got it working :slight_smile:

I found some code for cc messages on notes and volts by dave. This I modified and I got it working :slight_smile:

Here a little movie I made:

Please find my working code here:

   Adjusted code for Controlling the damper of a plate reverb with midi CC by Robin Hunt
   Original code found on by Dave

#include <MIDI.h>  // Add Midi Library

#include <Servo.h> // Add Servo Library

// Define Servo
Servo PlateDamperServo;

//Create an instance of the library with default name, serial port and settings

void setup() {
  MIDI.begin(9); // Initialize the Midi Library.
  // OMNI sets it to listen to all channels.. MIDI.begin(9) would set it
  // to respond to notes on channel 9 only.
  // This command tells the MIDI Library
  // the function you want to call when a Continuous Controller command
  // is received. In this case it's "MyCCFunction".
  // Attach the servo to pin 3 on the Arduino

void loop() { // Main loop; // Continuously check if Midi data has been received.

// MyCCFunction is the function that will be called by the Midi Library
// when a Continuous Controller message is received.
// It will be passed bytes for Channel, Controller Number, and Value
// The following code checks if the controller number is on 27
// If it is, the servo receive PWM and will move corresponding to the Value byte
void MyCCFunction(byte channel, byte number, byte value) {
  switch (number) {
    case 27:
      PlateDamperServo.write (map (value, 0, 255, 90, 180));
      // Delay for servo movement


I wan't do do something exactly like this!!!! I was trying too to modify tjhe notes and volts code!
Well, I try your code and i have the same issues i got with mine...
the servos (sg90) are driving crazy!
a little video here
the probleme is not a hardware issue, the wiring is good...
An idea?

An idea?

Start your own topic ?! Explain exactly what you want, what you have done (code within </> code-tags ), how you have wired everything.

yes, i'm gone start a new topic, i posted here because I used the same code and it doesn't work... i'll be doing this tomorow, i have no time today. thank's for answering me.