Make Your Own Midi Controller With Arduino IDE STM32

In this article you will know:
1. How to make a keypad controlled by stm32.
2. How to make a Keypad without libraries on Arduino IDE.
3. How to make a midi controller using keypad and stm32.

There are several midi software that can be controlled using a pc keyboard and you can use these keyboard modules for custom midi controllers.

However the midi controller that uses the pc keyboard module will stop working when it is opening another window. For example, when you open notepad, the keyboard module will switch its function to a typing tool. Unlike the midi controller, it will still work as its function even though the controlled software is working in the background.

In this article I will show you how to make your own midi controller at a very low cost but with luxurious quality. I provided a schematic of the keypad circuit to start this tutorial. Look at this picture, there are four pins on the column and four pins on the row, so we call this a 4x4 keypad.


C1 is the first column, C2 is the second column and so on. Likewise with R1, R2, R3 and R4. They are the first row, second row and so on.

This time we use stm32 as the controller. I have tried the Arduino keypad library, but it seems this library can't be used for stm32. But don't worry, the midi controller sketch will use an arduino keypad library that has been modified and can be used for stm32.

Now we have to connect the pins on the keypad to the pins on the microcontroller. Take a look at the following pictures for easy installation.

Keypad to Controller pin connection
R1 to PA0                
R2 to PA1
R3 to PA2
R4 to PA3
C1 to PB9
C2 to PB8
C3 to PB7
C4 to PB6


Before uploading the sketch of the midi controller, let's try first to see if the circuit works well. we will use the keypad sketch without the library in this test.

Now let's test whether the keypad is working properly?. Use the sketch below to test the keypad function.

//Keypad sketch without libraries
const char baris = 4; // Set to 4 rows keypad
const char kolom = 4; // Set to 4 columns keypad

int keys[baris][kolom] = {
               {0,1,2,3},
               {4,5,6,7},
               {8,9,10,11},
               {12,13,14,15}
};

char rowPins[baris] = {PA3, PA2, PA1, PA0};
char colPins[kolom] = {PB6, PB7, PB8, PB9};

void setup () {
      Serial.begin(9600);
      
   for(char r = 0; r < baris; r++){
        pinMode(rowPins[r], INPUT_PULLUP);  //set the row pins as input_pullup
        digitalWrite(rowPins[r], HIGH);     //turn on the pullups
   }
   
   for(char c = 0; c < kolom; c++){
        pinMode(colPins[c], OUTPUT);        //set the column pins as output
   }
   
}

void loop() {
      String key = getKey();
      
      if(key != 0){
   Serial.println(key);
      }
}

String getKey(){
      int pressed;
      String ko = "";
      for(int c = 0; c < kolom; c++){
        digitalWrite(colPins[c], LOW);
         for(int r = 0; r < baris; r++){
            if(digitalRead(rowPins[r]) == LOW){
            while(digitalRead(rowPins[r])== LOW);
            pressed = keys[r][c];
            ko = pressed;
            }
         }
   digitalWrite(colPins[c], HIGH); 
      }
      return ko;
}

Before going to make our midi controller, make sure this circuit and sketch work well. Don't proceed to the next step if this doesn't work.

Pay attention to the sketch above and the part that I have copied below.

int keys[baris][kolom] = {
               {0,1,2,3},
               {4,5,6,7},
               {8,9,10,11},
               {12,13,14,15}
};

In this section, the numbers 0 to 15 will later function as notes that will be sent to the midi host. We are free to change the value as needed. The next step is to change the USB on the stm32 board as USB midi.

NOTE!!
If you use the Arduino bootloader on your stm32 board, the bootloader will be erased and replaced with USB Composite. That means, the USB on the board can no longer be used for programming and serial because the USB function has changed to USB Midi. So I hope you have a separate serial USB device or ST-Link for programming.

To convert the USB on the stm32 board to USB Midi is very simple. If you upload this sketch, your board will change as USB midi. But I don't want you to upload it, because it will just work as a working pinless USB midi.

//Don't upload it.
#include <USBComposite.h>
USBMIDI midi;
void setup() {
    USBComposite.setProductId(0x0031);
    midi.begin();
    while (!USBComposite);
	}
void loop() {
}
//Don't upload it.

Comments