Sonic Interfaces: Build Your Own Experimental MIDI Instruments: Difference between revisions
No edit summary |
|||
| Line 35: | Line 35: | ||
VCV Rack is a free, open-source, virtual modular synthesizer that simulates the hardware Eurorack format. | VCV Rack is a free, open-source, virtual modular synthesizer that simulates the hardware Eurorack format. | ||
why VCV Rack: | |||
free, open source, flexible, can build unique sounds and explore endless sonic possibilities, stable and performable in live situations. a large community and a grassroots level of innovation, resulting in a vast and diverse library of modules that are often more creative and experimental than traditional hardware. | |||
=='''Adafruit Circuit Playground Express'''== | =='''Adafruit Circuit Playground Express'''== | ||
Latest revision as of 14:59, 23 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.
In this elective, we will learn how to program a microcontroller to function as a MIDI instrument. To do this, we need a microcontroller with a compatible chip. Since the Interaction Station has a number of Adafruit Circuit Playground Express boards available, we will be using these for the course. However, it's worth noting that you can also use an Arduino Leonardo or Arduino Micro to accomplish the same task. Keep in mind that the code will differ slightly due to the use of different Arduino libraries.
This page will contain all the technical instructions for the elective course.
Basic history of modular synthesis and music theory
What is MID? https://www.youtube.com/watch?v=NgIzSxTmQYA
Music & Sound Vocabulary for Beginners
https://www.youtube.com/watch?v=c3udLCvoCC0
A Brief History of Synthesizers:
https://www.youtube.com/watch?v=5sjreF6H_rY
How to use it:
Modular synthesis EXPLAINED by Andre Huang:
https://www.youtube.com/watch?v=cWslSTTkiFU
What is VCV rack:
VCV Rack is a free, open-source, virtual modular synthesizer that simulates the hardware Eurorack format.
why VCV Rack:
free, open source, flexible, can build unique sounds and explore endless sonic possibilities, stable and performable in live situations. a large community and a grassroots level of innovation, resulting in a vast and diverse library of modules that are often more creative and experimental than traditional hardware.
Adafruit Circuit Playground Express
How's that different from an Arduino?
The Circuit Playground Express & Blue Fruit, produced by Adafruit, are microcontrollers, just like the Arduino Uno, but designed for wearable projects.
Thus, 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)
because Circuit Playground Express running on a faster processor compared to Arduino Uno. We can also use it for audio-based projects.
CPX onboard sensors & Pinouts
1. Green LED lets you know that the CPX is powered on.------ 2. Red LED on Digital pin#13
3. This small button is for Resetting the board. ------------------ 4. Button A and B on Digital pin #4 and #5
5. slide switch on Digital pin #7 - - - - - - - - - - - - - - - - - - - - - - 6. 7 Capacitive Touch pads from A1-A7
7. Light Sensor on pin A8 - - - - - - - - - - - - - - - - - - - - - - - - - - - 8. Temperature Sensor on A9
9. motion sensor (LIS3DH xyz) - - - - - - - - - - - - - - - - - - - - - - - - - 10. Microphone Audio Sensor
11. speaker and audio output pin on A0
Pinouts:
A0 / D12 - true analog output so it's great for playing audio clips.
A1 / D6 - digital I/O, or analog Input, PWM output,capacitive touch sensor
A2 / D9 - digital I/O, or analog Input, PWM output,capacitive touch sensor
A3 / D10 - digital I/O, or analog Input.PWM output,capacitive touch sensor
A4 / D3 - digital I/O, or analog Input, capacitive touch sensor
A5 / D2 - digital I/O, or analog Input, capacitive touch sensor
A6 / D0 - digital I/O, or analog Input, PWM output,capacitive touch sensor
A7 / D1 - digital I/O, or analog Input, PWM output,capacitive touch sensor<be>
Set Up Circuit Playground Express (CPX) in Arduino IDE
Download and install Arduino IDE
you can download the software here:
Setting up CPX in Arduino IDE
Copy and paste the URL in the settings--> Additional boards manager URLs
https://adafruit.github.io/arduino-board-index/package_adafruit_index.json
See original instruction here
Boards Manager:
Now go to the board manager:
search and install Adafruit SAMD boards:
Install Libraries
Now let's go to tools--> manage libraries
search and install Adafruit Circuit Playground
search and install Adafruit TinyUSB
Program CPX with Arduino IDE
"upload blink example"
Now that everything is set up, we can learn how to code.
To start, we can open the blinks example by :
file—>examples—>basics—> blink
To program the CPX we need to double-press the reset Button on the board And wait till all the LEDs turn green.
After that, we need to make sure the correct board and port are selected:
We can upload a new program to the board by clicking the upload button:
If you uploaded the code successfully, you should see the Red LED blinking one second on, one second off.
Basic coding knowledge
Where to find the reference?
What is a variable?
Data Types
Serial monitor
If ... Else control structure
Map function
What is a library, and how to include a library in Arduino code?
left right button and neopixels
After learning some basic coding theory, let's see what they look like in existing examples.
Now let's open file--> examples-->Adafruit circuit playground-->Hello circuitPlayground-->Hello Buttons.
This example will print different messages in the serial monitor when you press the left and right buttons on the cpx
Let's see if you can identify variable data types and control structures in the code. and try to change the messages printed in the serial monitor.
Now let's open file--> examples-->Adafruit circuit playground-->Hello circuitPlayground-->Hello NeoPixels.
When you finish uploading the code, you will see the onboard CPX LEDs all appear in different colors.
in the code:
CircuitPlayground.setPixelColor(0, 255, 0, 0);
This function means: the first number indicates which LEDs on cpx, the next three numbers indicate an RGB value to give it a color.
CircuitPlayground.setPixelColor(index_of_led, R, G, B);
CircuitPlayground.clearPixels();
is to reset the values.
CircuitPlayground.setBrightness(30);
is to set the brightness of all the NeoPixels. The default setting is 30 brightest setting is 255, but the brightest setting might shorten the lifespan of the NeoPixels.
To turn off the individual NeoPixel, you can also set the RGB to 0:
CircuitPlayground.setPixelColor(3, 0, 0, 0);
Small coding excersise
Now, can we combine these two existing arduino examples to create a new code:
1. when we press the left button, only the left side of the neopixels lights up(whatever color you like)
And when we press the right button, the right side lights up. And when we are not pressing Neopixels off.
#include <Adafruit_CircuitPlayground.h>
bool leftButtonPressed;
bool rightButtonPressed;
void setup() {
Serial.begin(9600);
CircuitPlayground.begin();
}
void loop() {
leftButtonPressed = CircuitPlayground.leftButton();
rightButtonPressed = CircuitPlayground.rightButton();
CircuitPlayground.clearPixels();
CircuitPlayground.setBrightness(25);
if (leftButtonPressed) {
CircuitPlayground.setPixelColor(0, 255, 0, 0);
CircuitPlayground.setPixelColor(1, 255, 0, 0);
CircuitPlayground.setPixelColor(2, 255, 0, 0);
CircuitPlayground.setPixelColor(3, 255, 0, 0);
CircuitPlayground.setPixelColor(4, 255, 0, 0);
}
if (rightButtonPressed) {
CircuitPlayground.setPixelColor(5, 255, 255, 0);
CircuitPlayground.setPixelColor(6, 255, 255, 0);
CircuitPlayground.setPixelColor(7, 255, 255, 0);
CircuitPlayground.setPixelColor(8, 255, 255, 0);
CircuitPlayground.setPixelColor(9, 255, 255, 0);
}
delay(50);
}
The code above works fine in Arduino; however, later, when we send signals as MIDI instruments, it might not be the best logic structure. Think about it this way: when we play an instrument such as a keyboard, it only plays the note once; the next note will come in only when we release the key and press it again. However, in our current code, the command is sent many times in the void loop if the button is pressed. It might sound abstract to you now. But no worries, let's observe the difference. The code below is a modified version. The left button press sends the signal to the neopixel only once, and sends it again while releasing the button. and the right button remains the same.
#include <Adafruit_CircuitPlayground.h>
bool leftButtonPressed;
bool rightButtonPressed;
void setup() {
Serial.begin(9600);
CircuitPlayground.begin();
}
void loop() {
static bool left_last=false;
static bool right_last=false;
leftButtonPressed = CircuitPlayground.leftButton();
rightButtonPressed = CircuitPlayground.rightButton();
CircuitPlayground.clearPixels();
CircuitPlayground.setBrightness(25);
if (leftButtonPressed==true && left_last==false) {
CircuitPlayground.setPixelColor(0, 255, 0, 0);
CircuitPlayground.setPixelColor(1, 255, 0, 0);
CircuitPlayground.setPixelColor(2, 255, 0, 0);
CircuitPlayground.setPixelColor(3, 255, 0, 0);
CircuitPlayground.setPixelColor(4, 255, 0, 0);
}
if(leftButtonPressed==false && left_last==true){
CircuitPlayground.setPixelColor(0, 0, 0, 255);
CircuitPlayground.setPixelColor(1, 0, 0, 255);
CircuitPlayground.setPixelColor(2, 0, 0, 255);
CircuitPlayground.setPixelColor(3, 0, 0, 255);
CircuitPlayground.setPixelColor(4, 0, 0, 255);
}
left_last=leftButtonPressed;
if (rightButtonPressed) {
CircuitPlayground.setPixelColor(5, 255, 255, 0);
CircuitPlayground.setPixelColor(6, 255, 255, 0);
CircuitPlayground.setPixelColor(7, 255, 255, 0);
CircuitPlayground.setPixelColor(8, 255, 255, 0);
CircuitPlayground.setPixelColor(9, 255, 255, 0);
}else{
CircuitPlayground.setPixelColor(5, 0, 255, 0);
CircuitPlayground.setPixelColor(6, 0, 255, 0);
CircuitPlayground.setPixelColor(7, 0, 255, 0);
CircuitPlayground.setPixelColor(8, 0, 255, 0);
CircuitPlayground.setPixelColor(9, 0, 255, 0);
}
delay(200);
}
MIDI note on note off
Let's keep using the code above and program it into a MIDI device, which means when we press the buttons, it will work like a MIDI piano.
To do that, we need to import some libraries and add some lines of code in the setup(){}
Here is what it looks like. If you want to program the cpx into a MIDI device, you can always copy the code structure below:
#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);
void setup() {
TinyUSBDevice.begin();
CircuitPlayground.begin();
MIDI.begin(MIDI_CHANNEL_OMNI);
}
void loop() {
// put your main code here, to run repeatedly:
}
ok, let's add our code to this structure. It should look like this:
#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);
bool leftButtonPressed;
bool rightButtonPressed;
void setup() {
TinyUSBDevice.begin();
CircuitPlayground.begin();
MIDI.begin(MIDI_CHANNEL_OMNI);
}
void loop() {
static bool left_last=false;
static bool right_last=false;
leftButtonPressed = CircuitPlayground.leftButton();
rightButtonPressed = CircuitPlayground.rightButton();
CircuitPlayground.clearPixels();
CircuitPlayground.setBrightness(25);
if (leftButtonPressed==true && left_last==false) {
MIDI.sendNoteOn(45, 100, 1); // A2 Button just pressed -> Note On
CircuitPlayground.setPixelColor(0, 255, 0, 0);
CircuitPlayground.setPixelColor(1, 255, 0, 0);
CircuitPlayground.setPixelColor(2, 255, 0, 0);
CircuitPlayground.setPixelColor(3, 255, 0, 0);
CircuitPlayground.setPixelColor(4, 255, 0, 0);
}
if(leftButtonPressed==false && left_last==true){
MIDI.sendNoteOff(45, 0, 1); // A2 Button just released -> Note Off
CircuitPlayground.setPixelColor(0, 0, 0, 255);
CircuitPlayground.setPixelColor(1, 0, 0, 255);
CircuitPlayground.setPixelColor(2, 0, 0, 255);
CircuitPlayground.setPixelColor(3, 0, 0, 255);
CircuitPlayground.setPixelColor(4, 0, 0, 255);
}
left_last=leftButtonPressed;
if (rightButtonPressed) {
MIDI.sendNoteOn(48, 100, 1); // C3 Button just pressed -> Note On
CircuitPlayground.setPixelColor(5, 255, 255, 0);
CircuitPlayground.setPixelColor(6, 255, 255, 0);
CircuitPlayground.setPixelColor(7, 255, 255, 0);
CircuitPlayground.setPixelColor(8, 255, 255, 0);
CircuitPlayground.setPixelColor(9, 255, 255, 0);
}else{
MIDI.sendNoteOff(48, 0, 1); // Button just released -> Note Off
CircuitPlayground.setPixelColor(5, 0, 255, 0);
CircuitPlayground.setPixelColor(6, 0, 255, 0);
CircuitPlayground.setPixelColor(7, 0, 255, 0);
CircuitPlayground.setPixelColor(8, 0, 255, 0);
CircuitPlayground.setPixelColor(9, 0, 255, 0);
}
delay(50);
}
This time, when we upload the code, we need to choose a different USB stack.
This option only appears when we use the tinyUSB library in our code and select Board: Adafruit Circuit Playground Express (SAMD21)
After uploading the code, open GarageBand, choose MIDI, select a default instrument, and press the CPX buttons; it should work now like an instrument keyboard. You now might notice the difference between the trigger from the left button and the right button; one is a clear one-time trigger, one is multiple triggers as long as you keep pressing. There is no right and wrong; however, if you decide to go with multiple triggers, I would recommend you make sure to add a delay(50); at the end of your code so the multiple triggers won't end up crashing the music software.
MIDI Messages
"Note On / Note Off” vs. “Control Change (CC)”
A discrete event: play or stop a note
We often use Note On / Note Off to trigger a sound, like hitting a drum or playing or stopping a note.
Control Change is often used for Continuous control, like moving a knob or slider for audio effects.
In Arduino function, MIDI note on/off looks like this:
MIDI.sendNoteOn(note, velocity, channel) MIDI.sendNoteOff(note, velocity, channel)
MIDI.sendNoteOn(60, 100, 1); // play Middle C at velocity 100, velocity range is also from 0-127
MIDI.sendNoteOff(60, 0, 1); // stop it
MIDI note is from 0-127, representing note range C2-G8
You can find out more about it here:
https://studiocode.dev/resources/midi-middle-c/
In Arduino function, MIDI Control Change is like this:
MIDI.sendControlChange(ccNumber, value, channel)
MIDI.sendControlChange(1, 64, 1); sending value 64 on cc#1 via channel 1, note that the value range normally is also from 0-127
This does not start a sound — it modifies something already sounding.
The receiving synth changes a parameter (like filter cutoff or reverb).
Digital signals, such as buttons, are perfect for MIDI note on/off
Analog signals such as Accelerometers, Light sensors, Knobs, faders, and continuous input are perfect for CC.
! However, it is not a rule; you can use whatever sensor triggers you want and be creative about it.
light sensor as MIDI Control Change
#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);
void setup() {
TinyUSBDevice.begin();
CircuitPlayground.begin();
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, 1000, 0, 127);
MIDI.sendControlChange(1, midivalue, 2);// midi control value 0-127 send to chan 2.
delay(50);
}
Touch sensor as midi note on/off
#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
}
Map the Accelerometers for MIDI Control Change
Let's try out the accelerometer from the example. We can see that the reading is a floating value, with a range of -10.00 to 10.00.
arduino map() only works with integers, so in this case, we can map the accelerometer with some math and constrain.
#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);
// --- Map accelerometer to CC values (for smooth modulation) ---
int midix = (int)((X + 10.0) * 127.0 / 20.0);
int midiy = (int)((Y + 10.0) * 127.0 / 20.0);
int midiz = (int)((Z + 10.0) * 127.0 / 20.0);
// we can't map the sensor value directly because map() only works with integers
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)((X + 10.0) * 127.0 / 20.0);
int midiy = (int)((Y + 10.0) * 127.0 / 20.0);
int midiz = (int)((Z + 10.0) * 127.0 / 20.0);
//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
}
Map the Accelerometers sensor reading as midi note range
#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;
int currentNote = -1; // store which note is currently active
int midiChannel = 1; // your MIDI channel (1–16)
void setup() {
TinyUSBDevice.begin();
CircuitPlayground.begin();
CircuitPlayground.setBrightness(20);
MIDI.begin(MIDI_CHANNEL_OMNI);
}
void loop() {
// Read accelerometer data
X = constrain(CircuitPlayground.motionX(), -10, 10);
Y = constrain(CircuitPlayground.motionY(), -10, 10);
Z = constrain(CircuitPlayground.motionZ(), -10, 10);
// Map X axis to MIDI note range (C3–C5 → 48–72)
int note = map((int)(X * 10), -100, 100, 48, 72);
note = constrain(note, 48, 72); // ensure within MIDI range
// Simple thresholding to reduce jitter (you can tweak this)
static int lastNote = -1;
if (note != lastNote) {
// send note off for previous note
if (lastNote >= 0) {
MIDI.sendNoteOff(lastNote, 0, midiChannel);
}
// send note on for new note
MIDI.sendNoteOn(note, 100, midiChannel); // velocity 100
lastNote = note;
// Optional LED feedback
CircuitPlayground.setPixelColor(0, CircuitPlayground.colorWheel((note - 48) * 10));
}
delay(50); // balance responsiveness vs jitter increase if it is needed
}
Connect external sensors to CPX
connect buttons
pull up and pull down resistors
A pull-up resistor connects the input pin to VCC (usually +5V or +3.3V) through a resistor.
How it works:
When the button is open (not pressed), the pin is pulled HIGH through the resistor.
When the button is pressed, the pin is connected directly to GND, so it reads LOW.
A pull-down resistor connects the input pin to GND through a resistor.
How it works:
When the button is open, the pin is pulled LOW through the resistor.
When the button is pressed, the pin is connected directly to VCC, so it reads HIGH.
use internal PULL_UP resistors
If you don't want to add a resistor to your circuit, you can also use the internal pull-up resistor by adding a line in the code
void setup() {
//configure pin 2 as an input and enable the internal pull-up resistor
pinMode(6, INPUT_PULLUP);// on CPX A1 = D6, you can find this at the pinout section of this page
pinMode(13, OUTPUT);
}
The full code will look like this:
#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);
void setup() {
TinyUSBDevice.begin();
CircuitPlayground.begin();
MIDI.begin(MIDI_CHANNEL_OMNI);
pinMode(6, INPUT_PULLUP);//A1=D6
}
void loop() {
int sensorVal = digitalRead(6);
if (sensorVal == HIGH) {
digitalWrite(13, LOW);
MIDI.sendNoteOff(60, 0, 1);
} else {
digitalWrite(13, HIGH);
MIDI.sendNoteOn(60, 100, 1);
}
delay(50);
}
5 external buttons from A1-A5 sending MIDI note on note off message
#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);
bool b1_state = false, b2_state = false, b3_state = false, b4_state = false, b5_state = false;
const int b1_pin = 6;
const int b2_pin = 3;
const int b3_pin = 9;
const int b4_pin = 10;
const int b5_pin = 2;
void setup() {
TinyUSBDevice.begin();
CircuitPlayground.begin();
MIDI.begin(MIDI_CHANNEL_OMNI);
pinMode(b1_pin, INPUT_PULLUP);
pinMode(b2_pin, INPUT_PULLUP);
pinMode(b3_pin, INPUT_PULLUP);
pinMode(b4_pin, INPUT_PULLUP);
pinMode(b5_pin, INPUT_PULLUP);
pinMode(13, OUTPUT);
}
void loop() {
int s1 = digitalRead(b1_pin);
int s2 = digitalRead(b2_pin);
int s3 = digitalRead(b3_pin);
int s4 = digitalRead(b4_pin);
int s5 = digitalRead(b5_pin);
// button pressed = LOW (because INPUT_PULLUP)
if (!s1 && !b1_state) { MIDI.sendNoteOn(60, 100, 1); b1_state = true; digitalWrite(13, HIGH); }
if (s1 && b1_state) { MIDI.sendNoteOff(60, 0, 1); b1_state = false; digitalWrite(13, LOW); }
if (!s2 && !b2_state) { MIDI.sendNoteOn(62, 100, 1); b2_state = true; }
if (s2 && b2_state) { MIDI.sendNoteOff(62, 0, 1); b2_state = false; }
if (!s3 && !b3_state) { MIDI.sendNoteOn(64, 100, 1); b3_state = true; }
if (s3 && b3_state) { MIDI.sendNoteOff(64, 0, 1); b3_state = false; }
if (!s4 && !b4_state) { MIDI.sendNoteOn(65, 100, 1); b4_state = true; }
if (s4 && b4_state) { MIDI.sendNoteOff(65, 0, 1); b4_state = false; }
if (!s5 && !b5_state) { MIDI.sendNoteOn(67, 100, 1); b5_state = true; }
if (s5 && b5_state) { MIDI.sendNoteOff(67, 0, 1); b5_state = false; }
delay(10);
}
connect analog sensors
MIDI Mapping with softwares
REAPER
https://www.youtube.com/watch?v=LmEy49PH9p8&t=253s