Wednesday, March 15, 2023

Connecting a simple Cherry MX breakout board to Teensy 4.1

Overview

The simple Cherry MX breakout board can be used with a breadboard to have a nice mechanical key switch / button. The Teensy 4.1 can use this as an input to send USB MIDI data. 


Hardware







The breakout board has two pin rows of 4 pins each. Each row of 4 pins is split into two pairs. When the button is pressed, both pairs are connected. When the button is depressed, both pairs are disconnected. 

One pin of the first pair should be connected to Teensy ground. The other pin should be connected to a digital pin. In this example, Teensy pin 0 is used. 

Software

int button_pin = 0;
int debounce = 5;

int button_current;
int button_previous;

int note = 60;
int velocity = 127;
int channel = 1;

void setup() {
Serial.begin(57600);
pinMode(button_pin, INPUT_PULLUP);
}

void loop() {
button_current = digitalRead(button_pin);

if (button_current != button_previous) {
button_previous = button_current;
usbMIDI.sendNoteOn(note, (1 - button_previous) * velocity, channel);
delay(debounce);
}
}


0 comments: