Serail.write appears to be sending strange values

I am troubleshooting a project that was working but has stopped making the required communication to a MIDI board. I have an Arduino that was sending MIDI commands to a MIDI keyboard. The communication is serial from the Tx pin on the Arduino. MIDI protocol is an asynchronous serial interface. The baud rate is 31.25 Kbaud (+/- 1%). There is 1 start bit, 8 data bits, and 1 stop bit (ie, 10 bits total). The MIDI command code requires serial bytes to be sent.
My MIDI keyboard stopped responding to input from the Arduino setup. I have tested using 3 different Arduinos and 2 separate MIDI boards I no longer get any response from setup. It seams I have ruled out hardware issue as I have changed everything in the setup. I tried several serial monitors and get what appear to be the same results.

I set up a CP2102 module to bridge the Arduino serial output to USB. I established a communication to a windows 10 PC. I reset the baud rate and tested the connection at 9600, 14400,31250 baud and get the same results. I have used the Arduino serial monitor, putty, and CoolTerm to try and determine what the micro is sending. I have rewritten the Arduino code so that only a minimal amount of code remains for the tests. This is the entire code I am now using for the tests. I have

  #include <SoftwareSerial.h>

  void noteSend(byte cmd, byte data1, byte data2) {
   Serial.write(cmd);
   Serial.write(data1);
   Serial.write(data2);
  }

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

   void loop() {
  for(byte i=60;i<75;i++) 
 {
    noteSend(0x90, i, 0X7F);
   delay(500);
   noteSend(0x80, i, 0);
    delay(1500);
  } 
  }

Using the View Hex feature available on CoolTerm I can see the hex values of the data received. I do not understand what is causing the unexpected values.

This is what I am seeing for HEX values from the serial on the Arduino.

90 3C FF 00 3C 00 90 3D FF 00 3D 00 90 3E FF 00 3E 00 ...

Most of the values are what I was expecting . However every instance of Serial.write (0x80) is showing as 00, Serialwrite(0X7F) are showing as FF. also 64 dec (hex 40) show as hex 80

I have attempted everything I can think of to remedy this issue. I don't know what else to try.
.

Which?

so you don't even use SoftwareSerial, just Serial for the test. what do you see in a binary compatible terminal, at 9600 bauds if you run this code?

void setup() {
  Serial.begin(9600);
  Serial.write(0x80);
  Serial.write(0x7F);
  Serial.write(0x40);
}

void loop() {}

coolterm shows all is working fine in my setup (of course nothing connected to pins 0 and 1, just the USB cable to my UNO):

have you tried a more conventional approach which produces

903C7F 803C0
903D7F 803D0
903E7F 803E0
903F7F 803F0
90407F 80400
90417F 80410
90427F 80420
90437F 80430
90447F 80440
90457F 80450
90467F 80460
90477F 80470
#include <SoftwareSerial.h>
void noteSend(byte cmd, byte data1, byte data2) {
#if 0
    Serial.write(cmd);
    Serial.write(data1);
    Serial.write(data2);
#else
    Serial.print(cmd, HEX);
    Serial.print(data1, HEX);
    Serial.print(data2, HEX);
#endif
}

void setup() {
    Serial.begin(9600);
}
void loop() {
    for(byte i=60;i<75;i++)
    {
        noteSend(0x90, i, 0X7F);
        delay(500);

        Serial.print (" ");

        noteSend(0x80, i, 0);
        delay(1500);

        Serial.println ();
    }
}

which Arduino ? Well! to by more accurate. I originally was using a mini pro 16- 5volt. and later switched the system over to an Atmel 328P microprocessor. supported by a voltage regulator with power supply capacitors and a 14.7456MHZ crystal.

I now see that the issue originates from using the Arduino IDE to code on a bare bones processor. A practice I have done before without any issues. I may have to change all my code to C

this simple code

       void setup() {
         Serial.begin(9600);
         Serial.write(0x80);
         Serial.write(0x7F);
         Serial.write(0x40);
       }

      void loop() {}

shows this .
image

if you are using the standard "UNO" type target to compile and upload the code, the expected frequency is 16MHz. This is probably enough to mess up the baud rate timing and creating the issue you see.

I will switch to a 16 MhZ crystal and try it again. Thanks I had overlooked that obvious error on my part. I have to go now I will try this when I get a chance .

my output from the code suggested by gcjr
image

Thanks again J-M-L.

All I had to do is match up the crystal and the baud register bits.

I always felt that the solution was just some simple mistake that I had made but was failing to recognize. Sure enough I forgot about the USART baud register bits at UBRRnH and UBRRnL. I typically would have set that register when coding in C. I completely forgot about them for this project that I coded in Arduino IDE. I swapped out the crystal to 16MhZ on the project board and everything appears to be working again.

Actual! I am now wandering how and why the system was ever working as well as it once did with the crystal not matching up with the baud register settings.

Anyway it is working again and all is well. I greatly appreciate the help.

1 Like

Good to hear!
Have fun