Weirdness with DIY MIDI keyboard & shift registers

Hello all, I’ve been desperately trying to convert an electronically knackered Yamaha B35-N organ into a midi interface for the past few weeks after seeing LMNC’s super-simple MIDI interface video. I’ve built my circuit, but in order to decrease the amount of pins used by each keyboard I’ve used a pair of shift registers, with the intention of using 3 to accomodate both keyracks.

The trouble I’m having is now I’ve breadboarded the circuit the arduino mega is immediately turning off when I power it on. Everywhere I’ve looked for an answer so far either suggests a short to ground somewhere or a faulty IC. I’ve stripped it down just to the 74HC595s and still I can’t get it to stay on. Have I borked something somewhere? Is my circuit just pure whack? Should I just dog off the shift registers, throw more pins at it and be done with it? Any help/criticism is appreciated

I’m a new user and I’m forced to only have one image per post, I’ve uploaded the rest into a google drive folder. If you don’t trust me, I can always post more. Just don’t wanna start off on the wrong foot and quintuple post on my first go. Thanks guys

B35-N - Google Drive

1 Like

Per the datasheet:

image

You should have VCC (+5 V) on pin 16 and ground on pin 8, and pins 2 and 5 are outputs. If I’m looking at your layout right you appear to have +5 V on pins 2 and 8 and ground on pins 5 and 16. If so not only will that not work, but I would guess you’ve fried your chips.

3 Likes

I guess that explains why the video i used for the shift register stuff can only be found on dailymotion now, thanks!

This video, if you’re interested. I could’ve just ruined it anyway to be honest. This is why you have spares!

EDIT: Good news! My shift registers survived!

1 Like

And it took me a while but I just realized you’re using a 74HC595 here, which is what you’d use to control multiple outputs such as LEDs. But for a keyboard what you want is a 74HC165, which controls multiple inputs, like a lot of keys. [Edit: Wrong! see parallel out shift register switch multiplexer | Open Music Labs]

I’m sure you can use Google as well as I can, but this seems a pretty good discussion of using both chips with an Arduino. They’re using an Arduino Uno instead of a Mega but converting one to the other should be pretty straightforward. Though I think their bypass capacitor advice is faulty. Use a 100 nF ceramic from VCC to ground right next to each chip. Then use a single 10 µF electrolytic where the power supply connects, too, from the supply rail to ground.

There are other options for reading lots of switches, including diode matrix and multiplexers, and you might want to look into them too. One consideration is whether you want to be able to read and process multiple keys pressed at once. Some methods are well suited only if there’s just one key pressed.

2 Likes

I saw this and already wondered if i’m chasing a wild goose with the wrong chips, a lot of the tutorials I’ve seen say I can use the 595 but what you say does make sense. I link another tutorial that suggests the 595s here, and also here moral of the story is always read the specsheet :smiley:

I also appreciate there are many ways to do things, and the easy ‘it works’ way is often not the best way! So if you can point me in the right direction of which one would produce the best results in terms of polyphony, I’d really appreciate it!
Thank you for your help!

1 Like

Yeah, now that I think about it, looks like a 595 can be used. Kind of backwards from what I’d think, but maybe it makes more sense.

1 Like

I usually stick with chips like CD74HC4067 or CD74HC4051. They are multiplexer/demultiplexer and can share a bus communicating to the Arduino.

I’m pretty busy this week, but I need to remind myself to write examples for this in my midi controller wiki.

3 Likes

Ah, here’s the resource I was thinking of. I need to bookmark this where I can find it.

5 Likes

I found some of the info here useful when I was looking into similar things:

Cheers

2 Likes

I can second that resource! That is one of the sources I used when doing keyboard matrix decoding myself. You can read a bit more about my approach using an Arduino Nano here: Keyboard Matrix Decode – Simple DIY Electronic Music Projects

And a similar project using a Raspberry Pi Pico here: Toy Keyboard Matrix Decode with the Pi Pico – Simple DIY Electronic Music Projects

There are no diodes on that second keyboard, so it is prone to ghosting when playing several notes together :slight_smile: (I talk about ghosting and how diodes help here: Pi Pico MIDI Matrix Decode – Part 4 – Simple DIY Electronic Music Projects)

I do have some posts using multiplexers but I’ve not used them for keyboard input scanning like this, only really for multiplexing potentiometers.

Kevin
(Aside, I’ve started replying here with links to notes on my blog if I think they are relevant to a question someone is asking - if that isn’t appropriate, someone let me know and I’ll stop! But if I already have a post that I think might help, then I know I can talk in a bit more detail about it if required, hence suggesting them!)

2 Likes

Thank you all for the incredibly helpful resources and insight, I’ll try a new circuit armed with what I know now and hopefully test it tomorrow

EDIT: I have yet another problem, though I don’t think this forum is the best place to ask about it so feel free to tell me to naff off. I’ve found while trying to debug code from my arduino mega clone that the USB debug serial monitor in the arduino IDE only seems to spit out backwards question marks. Am I stuck with a board I can’t debug?

I should add that I’ve taken the step of making sure the serial rates in the sketch and the monitor match and I still can’t get anything useful out of it, even using Serial.println with no luck. If not fixable I’ll try and use the LED arrangement from here to debug the circuit physically and then hopefully write code that isn’t terrible. Worst case scenario I can use SimulIDE to test, hoping this works

Also, I’ve since found that all the solutions I’d come up with for connecting the atmega’s MIDI output directly to the computer are no good. I can’t use any of the chip reflashing hacks like HIDUINO because my clone uses the cheaper CH340 which I don’t think is suitable for this purpose. I don’t believe Hairless will work because my serial monitor is speaking in tongues.

I also had a cheap chinese MIDI2USB device knocking about that I also considered hacking up and using, but after much deliberation and reading peoples posts online I found these devices are not only missing multiple components but have quite bad reliability when it comes to the MIDI signals themselves.

My question on this matter is, would you think it safe to connect the midi output of the breadboard to a Creative Soundblaster X-Fi Fatal1ty front panel MIDI IN port? With all circuit safety precautions taken beforehand, of course. And if so, are there any special measures I should take when doing so?

If not, I have a last resort which is a genuine Uno that I know I should be able to reflash to support USB MIDI plug and play. However, I’d be sacrificing a lot of I/O if I stuck to just the Uno. Does anybody have any experience daisychaining arduinos into serial networks for exchanging input data, and if so is the latency acceptable? Forgive my naivety if not, I don’t know how easy it’d be to make an Uno speak to a Mega

This organ was recovered from the back of a community center a few years ago by a friend of mine who died recently. Every resource goes a long way, as I’m a programmer with fairly limited experience of circuit design and little to no experience using ICs. Once again, thank you all for your help

2 Likes

Sorry for the double post, I’ll merge with the one above if I’m breaking any rules but I’ve found a resource that might be of use to not only myself but anyone who comes after me trying to convert an organ into a MIDI controller. A great amount of detail on the hardware, software, even daisy-chaining arduinos together to increase inputs. Admittedly he’s working on an actual pipe organ but the concepts should be similar enough

https://sites.google.com/site/casavantopus400/welcome
Thread by the same guy @ OrganForum

3 Likes

I wasn’t sure whether I should make another thread, but thought it best not to as it was on the same subject of converting a Yamaha B35-N organ into a MIDI interface, just minus the shift registers as i’ve been advised by vets over on Organ Forum to do away with them and make my life easier lmao. If I’ve broken any multipost rules please merge my posts and I’ll try and keep it cleaner

I’ve resolved the ‘?’ issues in Serial Monitor with debugging code from the board and am now able to get useful debug output from the mega, I’d left some tweaks behind from another project.

Kevin, thank you once again for your very insightful blog. I’ve been trying to use your code with slight modification for a 4x12. I wired up my circuit making tweaks to your schematic and code to accomodate for the Arduino Mega, and after uploading the code and inserting another Serial.println for debugging to show when the board is ready I was unable to get any midi messages from the keyboard. I know for sure the problem isn’t your code, but there’s always the possibility that in my naivety trying to make 4x12 work I’ve broken something that end. I’d be incredibly grateful if you could look over this code and tell me if I’ve done silly things to it, haha.

Code
/*
// Simple DIY Electronic Music Projects
//    diyelectromusic.wordpress.com
//
//  Keyboard MIDI Matrix Decode
//  https://diyelectromusic.wordpress.com/2020/09/22/keyboard-midi-matrix-decode/
//
      MIT License
      
      Copyright (c) 2020 diyelectromusic (Kevin)
      
      Permission is hereby granted, free of charge, to any person obtaining a copy of
      this software and associated documentation files (the "Software"), to deal in
      the Software without restriction, including without limitation the rights to
      use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
      the Software, and to permit persons to whom the Software is furnished to do so,
      subject to the following conditions:
      
      The above copyright notice and this permission notice shall be included in all
      copies or substantial portions of the Software.
      
      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
      FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
      COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHERIN
      AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
      WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
  Using principles from the following Arduino tutorials:
    Arduino MIDI Library  - https://github.com/FortySevenEffects/arduino_midi_library
    Keypad Tutorial       - https://playground.arduino.cc/Main/KeypadTutorial/
*/

#include <MIDI.h>
#include <Keypad.h>

// This is required to set up the MIDI library.
// The default MIDI setup uses the Arduino built-in serial port
// which is pin 1 for transmitting on the Arduino Mega.
MIDI_CREATE_DEFAULT_INSTANCE();

// Set up the MIDI channel to send on
#define MIDI_CHANNEL 1

#define MIDI_LED LED_BUILTIN

const byte ROWS = 4;
const byte COLS = 12;
char keys[ROWS][COLS] = {
{29,30,31,32,33,34,35,36,37,38,39,40},
{41,42,43,44,45,46,47,48,49,50,51,52},
{53,54,55,56,57,58,59,60,61,62,63,64},
{65,66,67,68,69,70,71,72,73,74,75,76}
};

byte rowPins[ROWS] = {22,24,26,28}; //connect to the row pinouts of the keyboard
byte colPins[COLS] = {30,31,32,33,34,35,36,37,38,39,40,41}; //connect to the column pinouts of the keyboard

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() {
  MIDI.begin(MIDI_CHANNEL_OFF);
  Serial.begin(9600);
  pinMode(MIDI_LED, OUTPUT);
  Serial.println("Arduino is ready");
}


void loop() {
    // Fills kpd.key[ ] array from Keypad library with up-to 10 active keys.
    // Returns true if there are ANY active keys.
    if (kpd.getKeys())
    {
        for (int i=0; i<LIST_MAX; i++)   // Scan the whole key list.
        {
            if ( kpd.key[i].stateChanged )   // Only find keys that have changed state.
            {
                switch (kpd.key[i].kstate) {
                    case PRESSED:
                    digitalWrite(MIDI_LED, HIGH);
                    Serial.print(kpd.key[i].kchar,DEC);
                    Serial.println(" Pressed");
                    MIDI.sendNoteOn(kpd.key[i].kchar, 127, MIDI_CHANNEL); // Engage MIDI note for pressed keys
                break;
                    case HOLD:
                break;
                    case RELEASED:
                    MIDI.sendNoteOff(kpd.key[i].kchar, 0, MIDI_CHANNEL); // Disengage MIDI note for released keys
                    digitalWrite(MIDI_LED, LOW);
                break;
                    case IDLE:
                break;
                }
            }
        }
    }
}  // End loop

If not, this leads to my potential primary issue and if anyone can help to point me into the right direction on how to make this work I’d be indebted.

Over on my Organ Forum thread, it’d been identified that I likely have phosphor bronze contacts which from what I’ve been told would require 12v and protection for the arduino in order to be able to use those as inputs. Can anyone give me a second opinion on this? Just to see a MIDI message out of this thing, even if utterly useless, jumpy and scratchy would be a much needed victory. I’ve updated the initial Google Drive link with photographs of the contact switches. Ignore the pictures of the 12v PSU, I’ll likely use a wall wart with lower ampage unless I find myself needing to drive anything thirsty

Thank you all once again for any help and direction you may be able to provide

2 Likes

I’m trying to remember how the serial ports work on a Mega. On an Uno you wouldn’t be able to do serial MIDI and Serial.print at the same time, as they are using the same serial port. On a Mega you have four serial ports, but I’m pretty sure Serial.print still uses UART0, which is also used by MIDI_CREATE_DEFAULT_INSTANCE…

Actually this was probably your serial monitor question mark issue too - initialising MIDI will change the baud rate from whatever you set it to with Serial.begin() to 31250 which you can’t use in the serial monitor! Now you do Serial.begin after MIDI.begin, you are changing the MIDI baud back to 9600 which means MIDI is now broken rather than Serial.print.

One option is to move the MIDI interface to UART1 if you want to use Serial.print too (which looks like its on D18 and D19). This can be done with something like the following:
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);

You can read a bit more about various alternative MIDI serial options here: Arduino Multi MIDI Merge – Simple DIY Electronic Music Projects with another example directly for the Mega here: Multi-Mode MIDI Step Sequencer – Simple DIY Electronic Music Projects

Kevin
(I only pop in and out of these forums from time to time, so apologies if I don’t see your comment straight away!)

4 Likes

It should be possible to send MIDI out over the Arduino’s main serial port, which also goes to USB, but the baud rate needs to be set to something the PC can understand, which isn’t the MIDI standard baud of 31250. If you call Serial.begin(115200) straight after calling MIDI.begin() then the MIDI will go out over the serial port at 115200 and should be able to be picked up by Hairless MIDI and then fed into loopMIDI to be routed to whatever application you want on the PC.

I talk a bit about that in this post: Arduino Simple MIDI Controller – Simple DIY Electronic Music Projects when I talk about testing.

If you can’t get a hardware serial port working, for this kind of application you could even wire up a “software serial” port and do everything in software. As I mentioned in my other reply, there is some info on that kind of thing here: Arduino Multi MIDI Merge – Simple DIY Electronic Music Projects - see the discussion and code for MIDI_SW_SERIAL.

Good luck! It’s getting closer!! :slight_smile:

Kevin

3 Likes

I think I’m answering the same question in several different ways :slight_smile: But yes, if you mean the UART0/1/whatever you end up using, then yes just connecting this via two 220ohm resistors to a MIDI DIN socket (as per any MIDI OUT circuit shown on the Internet and as per Sam’s circuit too) should create the required “MIDI OUT” port that should be ok to connect to your SB MIDI IN port.

What is going on here is the Mega’s UART0 is both connected to GPIO pins and the internal USB interface, so the baud/print/MIDI applies to both - but trying to use both at the same time means they interfere with each other.

You can use it as serial.print to serial over USB, doubling as serial.print to GPIO; or MIDI.send to serial over USB doubling as MIDI.send to serial over GPIO.

If you connected a FTDI USB/serial bridge to the GPIO you could “see” the printing in the same was as you would using the serial monitor over USB. If you connect a MIDI interface to the GPIO, then anything sent to the serial port will go out over the MIDI loop in addition to the serial-over-USB. Does that make sense?

My recommendation is either remove all your Serial.being/Serial.print statements and leave MIDI only on UART0 (Serial) or move MIDI to Serial1. Either way, you can hook up a MIDI OUT port to the appropriate GPIO pins for the UART you choose and it should work :slight_smile:

Kevin

1 Like

Following with interest… I have a project in the works very similar to this. I’ll make a new thread just now.

5 Likes

Thank you once again for all of your advice, I should’ve said that I uncommented the debugging prints after I’d connected the keyboard to the mega and tried pushing buttons, unfortunately the built-in LED being used to debug to show something is detected as input was not flashing. I think the keys may need more volts

2 Likes

Ok, just double, double checking… you also commented out the Serial.begin(9600) line which resets the baud rate? :slight_smile:

1 Like

It’s a great call, but yes unfortunately at the time I’d tried to test the keys the only things I’d modified were the row and column counts, as well as the midi key mappings. I was planning on using the built-in LED to verify my keyboard was responding before carting out the saws to wedge the PC inside it

1 Like