Our Arduino sketch is limited compared to an actual keyboard's MIDI implementation, as it lacks:
Pitch bend - change the pitch (frequency) of the playing sounds
Polyphony - play more that one note at the same time
We can use OpenAI / ChatGPT [https://chatgpt.com/] to write a sketch for us.
Will it work? There are many web sites that describe MIDI and Arduino projects that ChatGPT uses in its language model.
MIDI interface using Arduino Pro Micro and the MIDIUSB library. Pitch bend controlled by potentiometer on pin A0. Speaker is connected to pin 2. 3 buttons are connected to pins 7,8, and 9. 3 buttons send the MIDI notes C, D, and E, and also turn on the corresponding LED, and play the note on the speaker. Incoming MIDI messages play the notes on the speaker and turn on and off the corresponding LEDs.
Link: https://chatgpt.com/c/67f610a6-98c0-8008-ba51-cd80999c6fb2
Arduino sketch to download and try: MIDI_keys_pitchbend_ProMicro_ChatGPT.ino
Read the sketch that ChatGPT generated.
Does it make sense? Is correct? Does it work?
Questions?
Great! You're building a MIDI controller and synthesizer using an Arduino Pro Micro, potentiometer-controlled pitch bend, and basic note playback with LEDs and a speaker. Here's a complete example sketch using the MIDIUSB library:
Potentiometer on A0 sends Pitch Bend messages.
Buttons on 7, 8, 9 send MIDI notes C4, D4, E4.
Corresponding LEDs turn on/off on note press/release.
Notes also play on a small speaker connected to pin 2.
Incoming MIDI Note On/Off messages also trigger speaker and LEDs.
Make sure you install:
MIDIUSB (official library from Arduino IDE Library Manager)
A0 Pitch Bend Potentiometer
2 Speaker
7 Button + LED for C4
8 Button + LED for D4
9 Button + LED for E4
#include <MIDIUSB.h>
const int buttonPins[3] = {7, 8, 9}; // Buttons
const int ledPins[3] = {14, 15, 16}; // LEDs on A0, A1, A2 (if free) — change if needed
const int notes[3] = {60, 62, 64}; // C4, D4, E4
const int speakerPin = 2;
const int pitchBendPin = A0;
bool buttonStates[3] = {false, false, false};
void setup() {
for (int i = 0; i < 3; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
pinMode(ledPins[i], OUTPUT);
}
pinMode(speakerPin, OUTPUT);
}
void loop() {
handleButtons();
handlePitchBend();
readMIDI();
}
// Read and send pitch bend
void handlePitchBend() {
int bendValue = analogRead(pitchBendPin); // 0–1023
int midiBend = map(bendValue, 0, 1023, 0, 16383); // MIDI pitch bend range
pitchBend(midiBend);
}
// Handle button presses
void handleButtons() {
for (int i = 0; i < 3; i++) {
bool pressed = !digitalRead(buttonPins[i]); // active low
if (pressed && !buttonStates[i]) {
noteOn(0, notes[i], 127);
MidiUSB.flush();
digitalWrite(ledPins[i], HIGH);
tone(speakerPin, midiNoteToFreq(notes[i]));
buttonStates[i] = true;
} else if (!pressed && buttonStates[i]) {
noteOff(0, notes[i], 0);
MidiUSB.flush();
digitalWrite(ledPins[i], LOW);
noTone(speakerPin);
buttonStates[i] = false;
}
}
}
// Receive MIDI and react
void readMIDI() {
midiEventPacket_t rx;
do {
rx = MidiUSB.read();
if (rx.header == 0x09 && rx.byte1 >= 0x90) {
uint8_t note = rx.byte2;
uint8_t velocity = rx.byte3;
for (int i = 0; i < 3; i++) {
if (note == notes[i]) {
digitalWrite(ledPins[i], HIGH);
tone(speakerPin, midiNoteToFreq(note));
}
}
} else if (rx.header == 0x08 || (rx.header == 0x09 && rx.byte3 == 0)) {
uint8_t note = rx.byte2;
for (int i = 0; i < 3; i++) {
if (note == notes[i]) {
digitalWrite(ledPins[i], LOW);
noTone(speakerPin);
}
}
}
} while (rx.header != 0);
}
// MIDI helper functions
void noteOn(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOn = {0x09, byte(0x90 | channel), pitch, velocity};
MidiUSB.sendMIDI(noteOn);
}
void noteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, byte(0x80 | channel), pitch, velocity};
MidiUSB.sendMIDI(noteOff);
}
void pitchBend(int value) {
byte lsb = value & 0x7F;
byte msb = (value >> 7) & 0x7F;
midiEventPacket_t bend = {0x0E, 0xE0, lsb, msb};
MidiUSB.sendMIDI(bend);
MidiUSB.flush();
}
// Convert MIDI note number to frequency
float midiNoteToFreq(byte note) {
return 440.0 * pow(2, (note - 69) / 12.0);
}
Speaker: Use a piezo or small speaker with a resistor in series.
LED Pins: If you're short on pins, you can multiplex or use RGB LEDs.
MIDIUSB: Only works when connected via USB to a computer that reads MIDI.
Want to add velocity control with another potentiometer or maybe a mode switch for drums? I can help you expand this too!