Sonic Interfaces: Build Your Own Experimental MIDI Instruments: Difference between revisions

From PD Research Wiki
Jump to navigation Jump to search
Line 556: Line 556:


   delay(1000);
   delay(1000);
}
</syntaxhighlight>
==map the Accelrometer sensor to a midi range==
Let's try out the accelerometer from the example.
We can see that the reading is a floating value, with a range of -10 to 10.
We can use int() to convert a float number into an integer. and map() to map the sensor reading to a midi range 0-127.
<syntaxhighlight lang=c line>
#include <Adafruit_CircuitPlayground.h>
float X, Y, Z;
void setup() {
  Serial.begin(9600);
  CircuitPlayground.begin();
}
void loop() {
  X = constrain(CircuitPlayground.motionX(),-10,10);
  Y = constrain(CircuitPlayground.motionY(),-10,10);
  Z = constrain(CircuitPlayground.motionZ(),-10,10);
  int midix = int (map(X,-10,10,0,127));
  int midiy = int (map(Y,-10,10,0,127));
  int midiz = int (map(Z,-10,10,0,127));
  Serial.print("X: ");
  Serial.print(midix);
  Serial.print("  Y: ");
  Serial.print(midiy);
  Serial.print("  Z: ");
  Serial.println(midiz);
  delay(1000);
}
</syntaxhighlight>
Now let's send the data as  MIDI signal
<syntaxhighlight lang=c line>
#include <Adafruit_TinyUSB.h>
#include <MIDI.h>
#include <Adafruit_CircuitPlayground.h>
Adafruit_USBD_MIDI usb_midi;
MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, MIDI);
float X, Y, Z;
void setup() {
  TinyUSBDevice.begin();
  CircuitPlayground.begin();
  CircuitPlayground.setBrightness(20);
  MIDI.begin(MIDI_CHANNEL_OMNI);
}
void loop() {
  X = constrain(CircuitPlayground.motionX(),-10,10);
  Y = constrain(CircuitPlayground.motionY(),-10,10);
  Z = constrain(CircuitPlayground.motionZ(),-10,10);
  int midix = int (map(X,-10,10,0,127));
  int midiy = int (map(Y,-10,10,0,127));
  int midiz = int (map(Z,-10,10,0,127));
    //MIDI.sendControlChange(cc#, value, midi chan);
 
  MIDI.sendControlChange(1,  midix, 3);
  MIDI.sendControlChange(2,  midiy, 3);
  MIDI.sendControlChange(3,  midiz, 3);
  //MIDI.sendControlChange(cc#, value, midi chan);
 
  delay(5); // quick scan = low latency + polyphonic support
}
}


</syntaxhighlight>
</syntaxhighlight>

Revision as of 23:31, 15 October 2025


In this hands-on elective, you will explore how sensors and microcontrollers can become musical instruments. You will learn the basics of programming, connect touch and motion sensors, and design your own MIDI controller. With software such as VCV Rack, you will experiment with unconventional inputs—touch, light, movement, sound—and transform them into live sonic expressions.

This elective introduces students to physical computing for sound. You will build DIY MIDI controllers that expand the language of performance beyond knobs and faders. The focus is on making and experimenting: coding, prototyping, and immediately testing your designs in music software.

Classes take place on location at the Interaction Station, combining small-group sessions and individual work. You can choose to collaborate on larger setups or develop your own custom instrument.


what is MIDI

what is a MIDI controller

how to build your own MIDI controller

To make our own MIDI controller, we need microcontrollers.

First things first: what is a microcontroller?

Computer and processor are generic terms for anything that can run a program, basically.
A controller or microcontroller usually refers to a simple processor that does only one task, like listening to sensors.
In explaining microcontrollers, we’ll distinguish them from computers, which contain more powerful processors that can run an operating system.
One of the most used microcontroller is Arduino. In this case though we will be talking about Adafruit Circuit Playground, a board with a hand design for several project.

Throughout this elective, we will use the microcontroller Adafruit Circuit Playground Express. why? Because we have a lot of them at the interaction station, there are numerous fun onboard sensors and Neopixel LEDs, and most importantly, they can be programmed as MIDI USB devices.


Adafruit Circuit Playground Express & Blue Fruit

The Circuit Playground Express(CPX) & Blue Fruit(CPB), produced by Adafruit, are microcontrollers, just like the Arduino Uno, but designed for wearable projects.

Thus, it is a little different from the normal boards from the hardware side, since the pins go out to those big, wide sewable pads, instead of normal pin headers (like on the Uno) .

Da1d-ADA-3333-1-0-2-1000x667.jpgFile:4333-10 1024x1024.jpg

Setting up CPX in Arduino IDE

Even though Adafruit Circuit Playground Express is not an Arduino native board but we can still program it using Arduino IDE.

To set up the board, you can follow the instructions here:

https://learn.adafruit.com/add-boards-arduino-v164/setup

After finishing installing the board, we also need some Libraries:

1. Adafruit Circuit Playground Library

2. Adafruit TinyUSB Library


Touch sensor example

#include <Adafruit_CircuitPlayground.h>

void setup() {
  Serial.begin(115200);
  CircuitPlayground.begin();
  Serial.println("Touch test running...");
}

void loop() {
  int val = CircuitPlayground.readCap(A1);

  if (val > 1016) {
    Serial.print("Touched!  ");
  }

  Serial.print("Value: ");
  Serial.println(val);

  delay(50);
}

As you can see, when we touch the sensor, the reading reaches 1017. If we attach an aligater clip to the sensor pad without touching, it is reading 1016.

So in our code, we use an "if" statement to define the touch trigger.

We could add another sensor reading to our current code. like this:

#include <Adafruit_CircuitPlayground.h>

void setup() {
  Serial.begin(115200);
  CircuitPlayground.begin();
  Serial.println("Touch test running...");
}

void loop() {
  int val = CircuitPlayground.readCap(A1);
  int val2 = CircuitPlayground.readCap(A2);

  if (val > 1016) {
    Serial.println("Touched! A1 ");
  }
  if (val2 > 1016) {
    Serial.println("Touched! A2  ");
  }

  //Serial.print("Value: ");
  //Serial.println(val);

  delay(50);
}

However, there are in total of 7 touch sensors. If we add them individually, our code will become very long.

so let's try to use a for loop for this:

#include <Adafruit_CircuitPlayground.h>
const int touchPins[7] = {A1, A2, A3, A4, A5, A6, A7};

void setup() {
  Serial.begin(115200);
  CircuitPlayground.begin();
  Serial.println("Touch test running...");
}

void loop() {

for (int i=0;i<7;i++){

 int val =CircuitPlayground.readCap(touchPins[i]);

  if (val > 1016) {
    Serial.print("Touched! A");
    Serial.println(i+1);
   // Serial.print(" value:");
   // Serial.println(val);
  }
}

  delay(20);
}


CPX Button to midi piano notes

We first take a look at the example code via Arduino IDE File--> example--> Adafruit Circuit Playground-->Hello CircuitPlayground --> Hello Buttons This example shows how to read the changing state while pressing the left and right buttons on the CPX through the serial monitor.

read button press only once

The thing is, this example will be constantly sending states of the button. However, in most of the MIDI settings, we want to only read once when the button is pressed.

For example, while you are holding the button, you could already send 100 triggers, depending on the speed of the Arduino program and USB communication.

Compared to playing a musical key, what we want is to press and release the key as one note.


Now, let's first adjust the code to only send the trigger once:

To do that, we need to add two new variables to check the state of the buttons, and they are boolean, a data type that represents one of two possible values: true or false. As you can see from the original code, we have two booleans called leftButtonPressed and rightButtonPressed.

However, to make sure we only trigger once(like a toggle), we need to add two booleans to check the last state of the button.


#include <Adafruit_CircuitPlayground.h>

bool leftButtonPressed;
bool rightButtonPressed;

void setup() {
  Serial.begin(9600);
  CircuitPlayground.begin();
}

void loop() {
  leftButtonPressed = CircuitPlayground.leftButton();
  rightButtonPressed = CircuitPlayground.rightButton();
  static bool left_last=false; 
  static bool right_last=true;

  
  Serial.print("Left Button: ");
  if (leftButtonPressed==true && left_last==false) {
    Serial.print("DOWN");
  } if (leftButtonPressed==false && left_last==true) {
    Serial.print(" relased");
  }
  left_last=leftButtonPressed;

  Serial.print("   Right Button: ");
  if (rightButtonPressed==true && right_last==false ) {
    Serial.print("DOWN");
  } 
  if(rightButtonPressed==false && right_last==true){
    Serial.print("  relased");    
  }
  right_last=rightButtonPressed;
 Serial.println();
  
  delay(1000);
}

In this code, we can learn about:

Comparison Operators:


comparison-operators/equalTo

and control structures:

if condition

if condition reference

Send note on and note off midi signals

// cpx buttons to midi piano notes, you can find the reference on this webpage:
//https://studiocode.dev/resources/midi-middle-c/

#include <Adafruit_TinyUSB.h>
#include <MIDI.h>
#include <Adafruit_CircuitPlayground.h>
bool leftButtonPressed;
bool rightButtonPressed;
Adafruit_USBD_MIDI usb_midi;
MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, MIDI);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  CircuitPlayground.begin();
  TinyUSBDevice.begin();
  MIDI.begin(MIDI_CHANNEL_OMNI);
}

void loop() {
  // put your main code here, to run repeatedly:
   // When buttons are pressed -> Note On, release -> Note Off
  static bool left_last = false;
  static bool right_last = false;
  leftButtonPressed = CircuitPlayground.leftButton();
  rightButtonPressed = CircuitPlayground.rightButton();

   if (leftButtonPressed == true && left_last==false) {
    MIDI.sendNoteOn(45, 100, 1);   // A2 Button just pressed -> Note On
  }
    if (leftButtonPressed == false && left_last==true) {
    MIDI.sendNoteOn(45, 0, 1);   // A2 Button just released -> Note Off
  }
   left_last=leftButtonPressed;

      if (rightButtonPressed==true && right_last==false) {
       MIDI.sendNoteOn(48, 100, 1);   // C3   Button just pressed -> Note On
    
  }
     if (rightButtonPressed==false && right_last == true) {
       MIDI.sendNoteOn(48, 0, 1);  // Button just released -> Note Off
  }  
 
   right_last = rightButtonPressed; // Save state for next loop
   
   delay(20);
        
}

You can shorten the code as:


  if (leftButtonPressed && !left_last){ MIDI.sendNoteOn(45, 100, 1);}
  if (!leftButtonPressed && left_last){ MIDI.sendNoteOn(45, 0, 1);}
leftButtonPressed means leftButtonPressed == true
!leftButtonPressed means leftButtonPressed == false
! means not.

touch sensor keyboard

CPX has 7 touch sensors, A1-A7; however, A7 reading often drifts and is affected by other sensors. So in the code below, we only use A1-A6.

Touch sensors are very sensitive to the surrounding environments and humidity. To make the sensor more stable, we could use one of our handhelds on the GND from the CPX and use the other hand to interact with the sensor.

#include <Adafruit_TinyUSB.h>
#include <MIDI.h>
#include <Adafruit_CircuitPlayground.h>

// --- USB MIDI setup ---
Adafruit_USBD_MIDI usb_midi;
MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, MIDI);

// --- Config ---
const int threshold = 900; // Adjust based on your readings
bool a1On = false, a2On = false, a3On = false;
bool a4On = false, a5On = false, a6On = false;

void setup() {
  TinyUSBDevice.begin();
  CircuitPlayground.begin();
  CircuitPlayground.setBrightness(20);
  MIDI.begin(MIDI_CHANNEL_OMNI);
}

void loop() {
  // --- Read sensors ---
  int v1 = CircuitPlayground.readCap(A1);
  int v2 = CircuitPlayground.readCap(A2);
  int v3 = CircuitPlayground.readCap(A3);
  int v4 = CircuitPlayground.readCap(A4);
  int v5 = CircuitPlayground.readCap(A5);
  int v6 = CircuitPlayground.readCap(A6);

  // --- A1 -> C4 ---
  if (v1 > threshold && !a1On) {
    MIDI.sendNoteOn(60, 100, 1);
    CircuitPlayground.setPixelColor(0, 0, 150, 0);
    a1On = true;
  }
  if (v1 <= threshold && a1On) {
    MIDI.sendNoteOff(60, 0, 1);
    CircuitPlayground.setPixelColor(0, 0, 0, 0);
    a1On = false;
  }

  // --- A2 -> D4 ---
  if (v2 > threshold && !a2On) {
    MIDI.sendNoteOn(62, 100, 1);
    CircuitPlayground.setPixelColor(1, 0, 150, 0);
    a2On = true;
  }
  if (v2 <= threshold && a2On) {
    MIDI.sendNoteOff(62, 0, 1);
    CircuitPlayground.setPixelColor(1, 0, 0, 0);
    a2On = false;
  }

  // --- A3 -> E4 ---
  if (v3 > threshold && !a3On) {
    MIDI.sendNoteOn(64, 100, 1);
    CircuitPlayground.setPixelColor(2, 0, 150, 0);
    a3On = true;
  }
  if (v3 <= threshold && a3On) {
    MIDI.sendNoteOff(64, 0, 1);
    CircuitPlayground.setPixelColor(2, 0, 0, 0);
    a3On = false;
  }

  // --- A4 -> F4 ---
  if (v4 > threshold && !a4On) {
    MIDI.sendNoteOn(65, 100, 1);
    CircuitPlayground.setPixelColor(3, 0, 150, 0);
    a4On = true;
  }
  if (v4 <= threshold && a4On) {
    MIDI.sendNoteOff(65, 0, 1);
    CircuitPlayground.setPixelColor(3, 0, 0, 0);
    a4On = false;
  }

  // --- A5 -> G4 ---
  if (v5 > threshold && !a5On) {
    MIDI.sendNoteOn(67, 100, 1);
    CircuitPlayground.setPixelColor(4, 0, 150, 0);
    a5On = true;
  }
  if (v5 <= threshold && a5On) {
    MIDI.sendNoteOff(67, 0, 1);
    CircuitPlayground.setPixelColor(4, 0, 0, 0);
    a5On = false;
  }

  // --- A6 -> A4 ---
  if (v6 > threshold && !a6On) {
    MIDI.sendNoteOn(69, 100, 1);
    CircuitPlayground.setPixelColor(5, 0, 150, 0);
    a6On = true;
  }
  if (v6 <= threshold && a6On) {
    MIDI.sendNoteOff(69, 0, 1);
    CircuitPlayground.setPixelColor(5, 0, 0, 0);
    a6On = false;
  }

  delay(5); // quick scan = low latency + polyphonic support
}

use light sensor value for midi control signal

#include <Adafruit_TinyUSB.h>
#include <MIDI.h>
#include <Adafruit_CircuitPlayground.h>
Adafruit_USBD_MIDI usb_midi;
MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, MIDI);
// --- Config ---
const int threshold = 900; // Adjust based on your readings
bool a1On = false, a2On = false, a3On = false;
bool a4On = false, a5On = false, a6On = false;

void setup() {
  TinyUSBDevice.begin();
  CircuitPlayground.begin();
  CircuitPlayground.setBrightness(20);
  MIDI.begin(MIDI_CHANNEL_OMNI);
}

void loop() {
  //read light sensor
  int value = CircuitPlayground.lightSensor();
  //map light sensor value to a midi range
  int midivalue = map(value, 0, 900, 0, 127);
   MIDI.sendControlChange(1,  midivalue, 2);// midi control value 0-127 send to chan 2.
  // --- Read sensors ---
  int v1 = CircuitPlayground.readCap(A1);
  int v2 = CircuitPlayground.readCap(A2);
  int v3 = CircuitPlayground.readCap(A3);
  int v4 = CircuitPlayground.readCap(A4);
  int v5 = CircuitPlayground.readCap(A5);
  int v6 = CircuitPlayground.readCap(A6);

  // --- A1 -> C4 ---
  if (v1 > threshold && !a1On) {
    MIDI.sendNoteOn(60, 100, 1);
    CircuitPlayground.setPixelColor(0, 0, 150, 0);
    a1On = true;
  }
  if (v1 <= threshold && a1On) {
    MIDI.sendNoteOff(60, 0, 1);
    CircuitPlayground.setPixelColor(0, 0, 0, 0);
    a1On = false;
  }

  // --- A2 -> D4 ---
  if (v2 > threshold && !a2On) {
    MIDI.sendNoteOn(62, 100, 1);
    CircuitPlayground.setPixelColor(1, 0, 150, 0);
    a2On = true;
  }
  if (v2 <= threshold && a2On) {
    MIDI.sendNoteOff(62, 0, 1);
    CircuitPlayground.setPixelColor(1, 0, 0, 0);
    a2On = false;
  }

  // --- A3 -> E4 ---
  if (v3 > threshold && !a3On) {
    MIDI.sendNoteOn(64, 100, 1);
    CircuitPlayground.setPixelColor(2, 0, 150, 0);
    a3On = true;
  }
  if (v3 <= threshold && a3On) {
    MIDI.sendNoteOff(64, 0, 1);
    CircuitPlayground.setPixelColor(2, 0, 0, 0);
    a3On = false;
  }

  // --- A4 -> F4 ---
  if (v4 > threshold && !a4On) {
    MIDI.sendNoteOn(65, 100, 1);
    CircuitPlayground.setPixelColor(3, 0, 150, 0);
    a4On = true;
  }
  if (v4 <= threshold && a4On) {
    MIDI.sendNoteOff(65, 0, 1);
    CircuitPlayground.setPixelColor(3, 0, 0, 0);
    a4On = false;
  }

  // --- A5 -> G4 ---
  if (v5 > threshold && !a5On) {
    MIDI.sendNoteOn(67, 100, 1);
    CircuitPlayground.setPixelColor(4, 0, 150, 0);
    a5On = true;
  }
  if (v5 <= threshold && a5On) {
    MIDI.sendNoteOff(67, 0, 1);
    CircuitPlayground.setPixelColor(4, 0, 0, 0);
    a5On = false;
  }

  // --- A6 -> A4 ---
  if (v6 > threshold && !a6On) {
    MIDI.sendNoteOn(69, 100, 1);
    CircuitPlayground.setPixelColor(5, 0, 150, 0);
    a6On = true;
  }
  if (v6 <= threshold && a6On) {
    MIDI.sendNoteOff(69, 0, 1);
    CircuitPlayground.setPixelColor(5, 0, 0, 0);
    a6On = false;
  }

  delay(5); // quick scan = low latency + polyphonic support
}

map the Accelrometer sensor to a midi range

Let's try out the accelerometer from the example. We can see that the reading is a floating value, with a range of -10 to 10. We can use int() to convert a float number into an integer. and map() to map the sensor reading to a midi range 0-127.


#include <Adafruit_CircuitPlayground.h>

float X, Y, Z;

void setup() {
  Serial.begin(9600);
  CircuitPlayground.begin();
}

void loop() {
  X = constrain(CircuitPlayground.motionX(),-10,10);
  Y = constrain(CircuitPlayground.motionY(),-10,10);
  Z = constrain(CircuitPlayground.motionZ(),-10,10);

  int midix = int (map(X,-10,10,0,127));
  int midiy = int (map(Y,-10,10,0,127));
  int midiz = int (map(Z,-10,10,0,127));

  Serial.print("X: ");
  Serial.print(midix);
  Serial.print("  Y: ");
  Serial.print(midiy);
  Serial.print("  Z: ");
  Serial.println(midiz);

  delay(1000);
}

map the Accelrometer sensor to a midi range

Let's try out the accelerometer from the example. We can see that the reading is a floating value, with a range of -10 to 10. We can use int() to convert a float number into an integer. and map() to map the sensor reading to a midi range 0-127.


#include <Adafruit_CircuitPlayground.h>

float X, Y, Z;

void setup() {
  Serial.begin(9600);
  CircuitPlayground.begin();
}

void loop() {
  X = constrain(CircuitPlayground.motionX(),-10,10);
  Y = constrain(CircuitPlayground.motionY(),-10,10);
  Z = constrain(CircuitPlayground.motionZ(),-10,10);

  int midix = int (map(X,-10,10,0,127));
  int midiy = int (map(Y,-10,10,0,127));
  int midiz = int (map(Z,-10,10,0,127));

  Serial.print("X: ");
  Serial.print(midix);
  Serial.print("  Y: ");
  Serial.print(midiy);
  Serial.print("  Z: ");
  Serial.println(midiz);

  delay(1000);
}

Now let's send the data as MIDI signal

#include <Adafruit_TinyUSB.h>
#include <MIDI.h>
#include <Adafruit_CircuitPlayground.h>


Adafruit_USBD_MIDI usb_midi;
MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, MIDI);
float X, Y, Z;
void setup() {
  TinyUSBDevice.begin();
  CircuitPlayground.begin();
  CircuitPlayground.setBrightness(20);
  MIDI.begin(MIDI_CHANNEL_OMNI);
}

void loop() {

  X = constrain(CircuitPlayground.motionX(),-10,10);
  Y = constrain(CircuitPlayground.motionY(),-10,10);
  Z = constrain(CircuitPlayground.motionZ(),-10,10);

  int midix = int (map(X,-10,10,0,127));
  int midiy = int (map(Y,-10,10,0,127));
  int midiz = int (map(Z,-10,10,0,127));
    //MIDI.sendControlChange(cc#, value, midi chan);
  
  MIDI.sendControlChange(1,  midix, 3);
  MIDI.sendControlChange(2,  midiy, 3);
  MIDI.sendControlChange(3,  midiz, 3);
  //MIDI.sendControlChange(cc#, value, midi chan);

   

  delay(5); // quick scan = low latency + polyphonic support
}