Arduino Nano not receiving MIDI message

I’m building an MIDI to CV converter using an Arduino nano along with a MIDI input circuit, however, when uploading the code and connecting the MIDI to a laptop via USB, the Arduino doesn’t recognize any input signals. Also, I’m confused on how to code for MIDI notes if anyone can offer guidance. I’ve followed building the circuit in the following link: MIDI Input Circuit

Below is also the code I’m using:

/*

* Connections:

* (1) Midi Connector

* (2) DAC MCP4728:

* SDA pin to A4/SDA (Arduino Nano)

* SCL pin to A5/SCL (Arduino Nano)

* GND to GND

* VCC to +5V

* (3) Clock Rate Potentiometer

*

* Outputs:

* DAC OUT

*   Channel A-C to Drum Kit and Channel D to VCO

* Clock output with LED

* Gate 1 and 2 with LEDs

*/

 

#include <MIDI.h>

#include <Wire.h> // Include the Wire to talk I2C

#include <Adafruit_MCP4728.h>

 

// This is the I2C address of the MCP4728

#define MCP4728_ADDR 0x64

 

MIDI_CREATE_DEFAULT_INSTANCE();

Adafruit_MCP4728 dac;

 

#define GATE1 4   // pin D4 for Gate 1

#define GATE2 3   // pin D3 for Gate 2

#define G1LED 7   // pin D7 for signaling LED of Gate 1

#define G2LED 6   // pin D6 for signaling LED of Gate 2

#define CLOCK_OUT 5 // pin D5 (PWM) for Clock Out

#define CLED 8      // pin D8 for signaling LED of Clock out

 

float refVoltage = 5.04;  // Note cannot be higher than reference voltage

int previousNotePlayed1;

int previousNotePlayed2;

int previousNotePlayed3;

int previousNotePlayed4;

 

int clockRate = A0; // Potentiometer connected

int clockSpeed; // current clock speed, in milliseconds per beat

 

void setup() {

  // put your setup code here, to run once:

  pinMode(CLED, OUTPUT);

  pinMode(G1LED, OUTPUT);

  pinMode(G2LED, OUTPUT);

  pinMode(CLOCK_OUT, OUTPUT);

  pinMode(GATE1, OUTPUT);

  pinMode(GATE2, OUTPUT);

 

  MIDI.setHandleNoteOn(doThisNoteOn); // calls the function on note on

  MIDI.setHandleNoteOff(doThisNoteOff); // calls the function on note off

  MIDI.begin(MIDI_CHANNEL_OMNI);  // Listen to all midi channels

  Serial.begin(250000);

 

  Wire.begin();

  dac.begin(0x64);

} // end of setup

 

void loop() {

  // put your main code here, to run repeatedly:

  MIDI.read();  // Read incoming messages

  // Read the value of the tempo potentiometer and map it to a range of reasonable clock speeds

  int clockValue = analogRead(clockRate);

  clockSpeed = map(clockValue, 0, 1023, 1000, 250); // tempo range from 60 BPM to 240 BPM

 

  // send a clock pulse every clockSpeed milliseconds

  static unsigned long lastClockTime = 0;

  if (millis() - lastClockTime >= clockSpeed){

    digitalWrite(CLOCK_OUT, HIGH);

    digitalWrite(CLED, HIGH);

    delayMicroseconds(clockValue);    // initially had delayMicroseconds(10);

    digitalWrite(CLOCK_OUT, LOW);

    digitalWrite(CLED, LOW);

    lastClockTime = millis();

  }

} // end of loop

 

void doThisNoteOn(byte channel, byte note, byte velocity){

  if (channel == 1){  // Listen to channel 1 to set CV1

    previousNotePlayed1 = note;

    float voltPerOctave = (note - 24) * 0.0833;

    int dacInput = (voltPerOctave / refVoltage) * 4096;

    dac.setChannelValue(MCP4728_CHANNEL_A, dacInput);

    digitalWrite(GATE1, HIGH);

    digitalWrite(G1LED, HIGH);

  }

  if (channel == 2){  // Listen to channel 2 to set CV2

    previousNotePlayed2 = note;

    float voltPerOctave = (note - 24) * 0.0833;

    int dacInput = (voltPerOctave / refVoltage) * 4096;

    dac.setChannelValue(MCP4728_CHANNEL_B, dacInput);

    digitalWrite(GATE1, HIGH);

    digitalWrite(G1LED, HIGH);

  }

  if (channel == 3){  // Listen to channel 3 to set CV3

    previousNotePlayed3 = note;

    float voltPerOctave = (note - 24) * 0.0833;

    int dacInput = (voltPerOctave / refVoltage) * 4096;

    dac.setChannelValue(MCP4728_CHANNEL_C, dacInput);

    digitalWrite(GATE1, HIGH);

    digitalWrite(G1LED, HIGH);

  }

  if (channel == 4){  // Listen to channel 4 to set CV4... this CV will be used for the VCO

    previousNotePlayed4 = note;float voltPerOctave = (note - 24) * 0.0833;

    int dacInput = (voltPerOctave / refVoltage) * 4096;

    dac.setChannelValue(MCP4728_CHANNEL_D, dacInput);

   digitalWrite(GATE2, HIGH);

    digitalWrite(G2LED, HIGH);

  }

} // end of doThisNoteOn

 

void doThisNoteOff(byte channel, byte note, byte velocity){ //Handles note off correctly for previously played noted

  if (note == previousNotePlayed1){

    MIDI.sendNoteOff(1, note, 0);

    // velocity = 0;

    digitalWrite(GATE1, LOW);

    digitalWrite(G1LED, LOW);

  }

  if (note == previousNotePlayed2){

    MIDI.sendNoteOff(2, note, 0);

    // velocity = 0;

    digitalWrite(GATE1, LOW);

    digitalWrite(G1LED, LOW);

  }

  if (note == previousNotePlayed3){

    MIDI.sendNoteOff(3, note, 0);

    // velocity = 0;

    digitalWrite(GATE1, LOW);

    digitalWrite(G1LED, LOW);

  }

  if (note == previousNotePlayed4){

    MIDI.sendNoteOff(4, note, 0);

    // velocity = 0;

    digitalWrite(GATE2, LOW);

    digitalWrite(G2LED, LOW);

  }

} // end of doThisNoteOff

Also a picture of the module:

Set your serial to 31250 for MIDI, also is that yellow wire connected to the RX pin of the nano or nothing?

1 Like

Remove the following line:

By using the midi library the serial port is already in use for midi data and configured with 31250 baud.
If the serial port is correctly connected the serial led on the nano should blink when midi data is received. Does this work?
And as craigyb already mentioned, the yellow wire is not connected on the picture.

What do you mean with “I’m confused on how to code for MIDI notes”? By using the callback function you will get the midi note number and velocity. More information on midi notes and their number can you find here:
https://www.inspiredacoustics.com/en/MIDI_note_numbers_and_center_frequencies
or on this german site: MIDI Kompendium

1 Like

If that fails, put a 100nf cap between 8 & 5, all the data sheets show it and on one sheet i saw “MUST” . on a recent DMX512 circuit I had some 6N137’s (very cheap ones) would not work and but others more expensive ones would. Investigation showed that these non working ones also failed in my MIDI circuits, simply soldering the 100nf across the chip solved the issue.

1 Like

Did you get it working?

If it’s not too late, suggestion 1
When uploading your program to Arduino Nano via USB, disconnect the RX input from the opto-isolator otherwise the upload is likely to fail - all serial communication goes through a single port.
I added a little switch on my MIDI module’s RX input and a removeable jumper on my MIDI Arpeggiator.

And re @twinturbo, suggestion 2
You should see the Nano’s Rx LED twinkling as it receives MIDI messages.
I found a previously-working module (MIDI Arpeggiator) would not work with a new MIDI device (M-Audio UNO), although still working with other MIDI. Tracked it down to a borderline-dodgy 6N138 chip, so I swapped that chip and all working.
Can you test with another MIDI keyboard? Another 6N138?

Joan’s Organ definitely needs an arpeggiator.
And a pitch-bend wheel, but that’s trickier.