Diy Arduino Based Touch Sensor MIDI Controller/keyboard

by gosha05 in Circuits > Arduino

1047 Views, 2 Favorites, 0 Comments

Diy Arduino Based Touch Sensor MIDI Controller/keyboard

IMG_20210329_012109.jpg
IMG_20210329_011855.jpg

Here is my attempt to make MIDI controller/keyboard:

The Touch Sensor

Untitled.png
IMG_20210325_230609.jpg

So I have experimented a lot with NPN transistors to achieve touch detection. When you touch Base pin of the NPN transistor, it lets the current thru. So I in this circuit we have a Capacitor(C), a Resistor(R) and a Transistor. The transistor am using is "BC547" NPN transistor which is pretty popular and easy to get.

I am using 0.47µF Capacitor, but 0.22µF and 1µF Capacitors also work. I have only tested electrolytic Capacitors, But feel free to try other types. If the capacity is too low, the capacitor wont be able filter the noise so we will get some distortion, but if the capacity is too high, we can get some delay which will be very annoying. if you use polarized capacitor (like electrolytic in my case) be sure to have negative pin of the capacitor towards GND.

So the tested values of R are 1MΩ and 100KΩ, so I will assume that all values between those will work. The resistor is connected to ground, and is pulling the output signal down. Whenever we touch the Base pin of the Transistor, it pulls the output high.

We are gonna be using 24 of these touch sensors for 24 piano keys. I just made 4 PCBs and each of them have 6 sensors.

The Actual Keyboard

IMG_20210319_204017.jpg
IMG_20210320_000316.jpg
IMG_20210323_161506.jpg

This step isn't that necessary, because you can use any conductor. I chose PCB because they don't break so easily and are easy to etch. I measured the PCB. its width and height was 15cm x 20cm. I cut it in half to get two 7.5cm x 20cm pieces. One PCB will be one octave which means total of 2 octaves. 12 keys per octave so we get total of 24 keys. So I proceeded to draw piano with permanent marker(permanent marker is important). there are many ways to etch PCB which you can google easily, but am using citric acid, table salt, and hydrogen peroxide method. mine turned out kind of scuffed because I left it in liquid for too long, but I hope you can do a better job than me.

CD4051 Multiplexing

Untitled2.png
Untitled3.png

Arduino nano has 14 digital pins, which of 2 are occupied by serial communication. 12 pins are not enough for 24 keys. that's why we have to use CD4051 Analog Multiplexer/Demultiplexer to increase the number of pins on the microcontroller. We need 3x CD4051 ICs, because each has 8 pins and we need total of 24. we have to use 24 of those transistor activated touch sensors. according to last step: connect transistor base to piano key, and each output of sensors to each pin(N1, N2, N3... N24) of the CD4051. D2, D3, D4, D5, D6 and D7 are connected to arduino's corresponding pins.

The Case

IMG_20210328_025931.jpg
IMG_20210326_005137.jpg
IMG_20210328_025805.jpg
IMG_20210328_025957.jpg

For the materials of the case I just went with strong cardboard. I just made a box which is very easy to make so I wont leave any measurements. Since mine didn't turn out so well I will leave it up to your imagination. just make sure its as wide or wider than your keyboard.

for the front panel, I inserted 6 potentiometer and a joystick. we will talk about values of them later.

Analog MIDI Controll

Untitled4.png
Untitled5.png
IMG_20210328_025803.jpg

As shown on the image connect all GND and 5v pins. then connect potentiometer pins to arduino A0-A5 as shown. and X and Y to A6 and A7. 10K Potentiometers are recommended but up to 100K should work too.

The Arduino Code

#include <MIDI.h>
int N1 = 0;
int N2 = 0;
int N3 = 0;
int N4 = 0;
int N5 = 0;
int N6 = 0;
int N7 = 0;
int N8 = 0;
int N9 = 0;
int N10 = 0;
int N11 = 0;
int N12 = 0;
int N13 = 0;
int N14 = 0;
int N15 = 0;
int N16 = 0;
int N17 = 0;
int N18 = 0;
int N19 = 0;
int N20 = 0;
int N21 = 0;
int N22 = 0;
int N23 = 0;
int N24 = 0;
int V1 = 0;
int V2 = 0;
int V3 = 0;
int V4 = 0;
int V5 = 0;
int V6 = 0;
int V7 = 0;
int V8 = 0;
int V9 = 0;
int V10 = 0;
int V11= 0;
int V12 = 0;
int V13 = 0;
int V14 = 0;
int V15 = 0;
int V16 = 0;
int V17 = 0;
int V18 = 0;
int V19 = 0;
int V20 = 0;
int V21 = 0;
int V22 = 0;
int V23 = 0;
int V24 = 0;

int pot1 = 0;
int pot2 = 0;
int pot3 = 0;
int pot4 = 0;
int pot5 = 0;
int pot6 = 0;
int potX = 0;
int potY = 0;

int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;
int val5 = 0;
int val6 = 0;
int valX = 0;
int valY = 0;

int val1D = 0;
int val2D = 0;
int val3D = 0;
int val4D = 0;
int val5D = 0;
int val6D = 0;
int valXD = 0;
int valYD = 0;

#define S0 2
#define S1 3
#define S2 4

struct MySettings : public midi::DefaultSettings
{
    static const long BaudRate = 38400;
};

MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial, MIDI, MySettings);
void setup() {
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
  pinMode(A4, INPUT);
  pinMode(A5, INPUT);
  pinMode(A6, INPUT);
  pinMode(A7, INPUT);
  MIDI.begin();  
}
void loop() {
  digitalWrite(S0, LOW);
  digitalWrite(S1, LOW);
  digitalWrite(S2, LOW);
  N1 = digitalRead(5);
  N9 = digitalRead(6);
  N17 = digitalRead(7);
  
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);
  digitalWrite(S2, LOW);
  N2 = digitalRead(5);
  N10 = digitalRead(6);
  N18 = digitalRead(7);
  
  digitalWrite(S0, LOW);
  digitalWrite(S1, HIGH);
  digitalWrite(S2, LOW);
  N3 = digitalRead(5);
  N11 = digitalRead(6);
  N19 = digitalRead(7);
  
  digitalWrite(S0, HIGH);
  digitalWrite(S1, HIGH);
  digitalWrite(S2, LOW);
  N4 = digitalRead(5);
  N12 = digitalRead(6);
  N20 = digitalRead(7);
  
  digitalWrite(S0, LOW);
  digitalWrite(S1, LOW);
  digitalWrite(S2, HIGH);
  N5 = digitalRead(5);
  N13 = digitalRead(6);
  N21 = digitalRead(7);
  
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);
  digitalWrite(S2, HIGH);
  N6 = digitalRead(5);
  N14 = digitalRead(6);
  N22 = digitalRead(7);
  
  digitalWrite(S0, LOW);
  digitalWrite(S1, HIGH);
  digitalWrite(S2, HIGH);
  N7 = digitalRead(5);
  N15 = digitalRead(6);
  N23 = digitalRead(7);
  
  digitalWrite(S0, HIGH);
  digitalWrite(S1, HIGH);
  digitalWrite(S2, HIGH);
  N8 = digitalRead(5);
  N16 = digitalRead(6);
  N24 = digitalRead(7);
  /////

  val1 = analogRead(A0);
  val2 = analogRead(A1);
  val3 = analogRead(A2);
  val4 = analogRead(A3);
  val5 = analogRead(A4);
  val6 = analogRead(A5);
  valX = analogRead(A6);
  valY = analogRead(A7);

  pot1 = map(val1, 0, 1023, 0, 127);
  pot2 = map(val2, 0, 1023, 0, 127);
  pot3 = map(val3, 0, 1023, 0, 127);
  pot4 = map(val4, 0, 1023, 0, 127);
  pot5 = map(val5, 0, 1023, 0, 127);
  pot6 = map(val6, 0, 1023, 0, 127);
  potX = map(valX, 0, 1023, 127, 0);
  potY = map(valY, 0, 1023, 127, 0);

  ///////
  
  if (N1 == 1 && V1 == 0) {MIDI.sendNoteOn(48, 127, 1);
    V1 = 1;
  }
  if (N1 == 0 && V1 == 1) {MIDI.sendNoteOff(48, 127, 1);
    V1 = 0;
  }
  if (N2 == 1 && V2 == 0) {MIDI.sendNoteOn(49, 127, 1);
    V2 = 1;
  }
  if (N2 == 0 && V2 == 1) {MIDI.sendNoteOff(49, 127, 1);
    V2 = 0;
  }
  if (N3 == 1 && V3 == 0) {MIDI.sendNoteOn(50, 127, 1);
    V3 = 1;
  }
  if (N3 == 0 && V3 == 1) {MIDI.sendNoteOff(50, 127, 1);
    V3 = 0;
  }
  if (N4 == 1 && V4 == 0) {MIDI.sendNoteOn(51, 127, 1);
    V4 = 1;
  }
  if (N4 == 0 && V4 == 1) {MIDI.sendNoteOff(51, 127, 1);
    V4 = 0;
  }
  if (N5 == 1 && V5 == 0) {MIDI.sendNoteOn(52, 127, 1);
    V5 = 1;
  }
  if (N5 == 0 && V5 == 1) {MIDI.sendNoteOff(52, 127, 1);
    V5 = 0;
  }
  if (N6 == 1 && V6 == 0) {MIDI.sendNoteOn(53, 127, 1);
    V6 = 1;
  }
  if (N6 == 0 && V6 == 1) {MIDI.sendNoteOff(53, 127, 1);
    V6 = 0;
  }
  if (N7 == 1 && V7 == 0) {MIDI.sendNoteOn(54, 127, 1);
    V7 = 1;
  }
  if (N7 == 0 && V7 == 1) {MIDI.sendNoteOff(54, 127, 1);
    V7 = 0;
  }
  if (N8 == 1 && V8 == 0) {MIDI.sendNoteOn(55, 127, 1);
    V8 = 1;
  }
  if (N8 == 0 && V8 == 1) {MIDI.sendNoteOff(55, 127, 1);
    V8 = 0;
  }
  if (N9 == 1 && V9 == 0) {MIDI.sendNoteOn(56, 127, 1);
    V9 = 1;
  }
  if (N9 == 0 && V9 == 1) {MIDI.sendNoteOff(56, 127, 1);
    V9 = 0;
  }
  if (N10 == 1 && V10 == 0) {MIDI.sendNoteOn(57, 127, 1);
    V10 = 1;
  }
  if (N10 == 0 && V10 == 1) {MIDI.sendNoteOff(57, 127, 1);
    V10 = 0;
  }
  if (N11 == 1 && V11 == 0) {MIDI.sendNoteOn(58, 127, 1);
    V11 = 1;
  }
  if (N11 == 0 && V11 == 1) {MIDI.sendNoteOff(58, 127, 1);
    V11 = 0;
  }
  if (N12 == 1 && V12 == 0) {MIDI.sendNoteOn(59, 127, 1);
    V12 = 1;
  }
  if (N12 == 0 && V12 == 1) {MIDI.sendNoteOff(59, 127, 1);
    V12 = 0;
  }
  if (N13 == 1 && V13 == 0) {MIDI.sendNoteOn(60, 127, 1);
    V13 = 1;
  }
  if (N13 == 0 && V13 == 1) {MIDI.sendNoteOff(60, 127, 1);
    V13 = 0;
  }
  if (N14 == 1 && V14 == 0) {MIDI.sendNoteOn(61, 127, 1);
    V14 = 1;
  }
  if (N14 == 0 && V14 == 1) {MIDI.sendNoteOff(61, 127, 1);
    V14 = 0;
  }
  if (N15 == 1 && V15 == 0) {MIDI.sendNoteOn(62, 127, 1);
    V15 = 1;
  }
  if (N15 == 0 && V15 == 1) {MIDI.sendNoteOff(62, 127, 1);
    V15 = 0;
  }
  if (N16 == 1 && V16 == 0) {MIDI.sendNoteOn(63, 127, 1);
    V16 = 1;
  }
  if (N16 == 0 && V16 == 1) {MIDI.sendNoteOff(63, 127, 1);
    V16 = 0;
  }
  if (N17 == 1 && V17 == 0) {MIDI.sendNoteOn(64, 127, 1);
    V17 = 1;
  }
  if (N17 == 0 && V17 == 1) {MIDI.sendNoteOff(64, 127, 1);
    V17 = 0;
  }
  if (N18 == 1 && V18 == 0) {MIDI.sendNoteOn(65, 127, 1);
    V18 = 1;
  }
  if (N18 == 0 && V18 == 1) {MIDI.sendNoteOff(65, 127, 1);
    V18 = 0;
  }
  if (N19 == 1 && V19 == 0) {MIDI.sendNoteOn(66, 127, 1);
    V19 = 1;
  }
  if (N19 == 0 && V19 == 1) {MIDI.sendNoteOff(66, 127, 1);
    V19 = 0;
  }
  if (N20 == 1 && V20 == 0) {MIDI.sendNoteOn(67, 127, 1);
    V20 = 1;
  }
  if (N20 == 0 && V20 == 1) {MIDI.sendNoteOff(67, 127, 1);
    V20 = 0;
  }
  if (N21 == 1 && V21 == 0) {MIDI.sendNoteOn(68, 127, 1);
    V21 = 1;
  }
  if (N21 == 0 && V21 == 1) {MIDI.sendNoteOff(68, 127, 1);
    V21 = 0;
  }
  if (N22 == 1 && V22 == 0) {MIDI.sendNoteOn(69, 127, 1);
    V22 = 1;
  }
  if (N22 == 0 && V22 == 1) {MIDI.sendNoteOff(69, 127, 1);
    V22 = 0;
  }
  if (N23 == 1 && V23 == 0) {MIDI.sendNoteOn(70, 127, 1);
    V23 = 1;
  }
  if (N23 == 0 && V23 == 1) {MIDI.sendNoteOff(70, 127, 1);
    V23 = 0;
  }
  if (N24 == 1 && V24 == 0) {MIDI.sendNoteOn(71, 127, 1);
    V24 = 1;
  }
  if (N24 == 0 && V24 == 1) {MIDI.sendNoteOff(71, 127, 1);
    V24 = 0;
  }

  if (pot1 != val1D && pot1 != val1D + 1 && pot1 != val1D - 1) {
  MIDI.sendControlChange(1, pot1, 1);
  val1D = pot1;
  }
  if (pot2 != val2D && pot2 != val2D + 1 && pot2 != val2D - 1) {
  MIDI.sendControlChange(2, pot2, 1);
  val2D = pot2;
  }
  if (pot3 != val3D && pot3 != val3D + 1 && pot3 != val3D - 1) {
  MIDI.sendControlChange(3, pot3, 1);
  val3D = pot3;
  }
  if (pot4 != val4D && pot4 != val4D + 1 && pot4 != val4D - 1) {
  MIDI.sendControlChange(4, pot4, 1);
  val4D = pot4;
  }
  if (pot5 != val5D && pot5 != val5D + 1 && pot5 != val5D - 1) {
  MIDI.sendControlChange(5, pot5, 1);
  val5D = pot5;
  }
  if (pot6 != val6D && pot6 != val6D + 1 && pot6 != val6D - 1) {
  MIDI.sendControlChange(6, pot6, 1);
  val6D = pot6;
  }
  if (potY != valYD && potY != valYD + 1 && potY != valYD - 1) {
  MIDI.sendControlChange(7, potY, 1);
  valYD = potY;
  }
  if (potX != valXD && potX != valXD + 1 && potX != valXD - 1) {
  MIDI.sendControlChange(8, potX, 1);
  valXD = potX;
  }
}

Software

Untitled6.png
Untitled7.png

First you need to download hairless midi. Its a software that converts serial communication to MIDI. Download the version that suits your computer. Then you will need loopMIDI driver. install and open both hariless midi and loopMIDI. Be sure to upload the code to arduino first. Create new loopMIDI port and select your arduino's COM port in hairless midi. also select MIDI in and MIDI out of hairless midi to loopMIDI device. and finale you will need software like mixcraft or FL studio to use your MIDI device.

Oh and also don't forget to download the arduino midi library

Demo

MIDI keyboard demo

In this demo the audio is recorded by software in computer and then merged with video.

And if you have any questions or problems feel free to ask!

Edit: if you can't press multiple keys at once, try connecting your body to 5v pin.