Add MIDI Input in old Casio Keyboard with Arduino NANO (clone)

Hello,

I have an old Casio keyboard, model CTK50, a very simple one, no velocity, aftertouch, just a keyboard matrix being processed by an LSI which outputs some simple sounds. I'm trying to add MIDI in so I can play some midi clips from my PC or a sequencer and make the keyboard play from an external source. Luckily, I found the service manual (CASIO CTK-50 SERVICE MANUAL Pdf Download | ManualsLib) and I have the exact matrix used in this keyboard:

Then with the help of the service manual I found which pins in the ribbon cable are the KCs and the KIs:

So I wired up the arduino according to the schematic, using D2-D10 for the 9 KCs control signals and A0-A5 for the KIs input signals and using a simple 6N139 configuration for the midi connection.

After that, I started to write the code. If I didn't get it wrong, I understood that the KCs are continuosly put to 5V by the LSI one after the other every 2ms (according to the manual), so I have to make the arduino put out 5V when a certain midi note arrives, as if it's a KI signal; that way the LSI recognizes that, for example sending a C2, KC1 is 5V and so KI is going up to 5V while the note on signal is sent, then KI should go to 0V when the note off is sent.
So, the combination of a certain KC and a certain KI make a certain note sound from the keyboard.
I found an old post of a project similar to mine, which says that with the classic functions (digitalWrite, digitalRead) all the scanning and control would be very slow and so the direct port manipulation would be the right way to proceed (Adding MIDI input to pre-MIDI synthesizer with matrix keyboard - Audio - Arduino Forum). So, I started taking the last code in this thread and modifying with my declarations: I can see that midi signals are received as the arduino RX light goes up with note on-note off signals but I have no sound from the keyboard. Another problem I encountered is that if I put the arduino A0-A5 in input mode, all the keys in the keyboard won't work anymore, so I'm thinking if I have to put diodes (RIBBON PIN ---K|----ARDUINO PIN) to prevent any problem related to the input being a current sink.
Sorry for the long post, I wanted to be as detailed as possible.
Any help would be appreciated, thank you! :slight_smile:

This is the sketch as of now

midi_faster.ino (8.72 KB)

I am puzzled by the project, you say:-

I'm trying to add MIDI in so I can play some midi clips from my PC or a sequencer and make the keyboard play from an external source.

So do you want the PC to send a MIDI message to your keyboard and have the keyboard make a sound as if a specific key has been pressed?

If so I don't see how you can do that without an analogue switch across each key matrix intersection. The key matrix will be scanned so you can only assert the connection at certain times in the keyboard's.

so I have to make the arduino put out 5V when a certain midi note arrives, as if it's a KI signal; that way the LSI recognizes that, for example sending a C2, KC1 is 5V and so KI is going up to 5V while the note on signal is sent, then KI should go to 0V when the note off is sent.

You do but that assumes that the keyboard is not being scanned by the original keyboard electronics. Have these been disabled? If so is your nano scanning the keyboard as well. I can't tell from just looking at the code.

Grumpy_Mike:
I am puzzled by the project, you say:-
So do you want the PC to send a MIDI message to your keyboard and have the keyboard make a sound as if a specific key has been pressed?

If so I don't see how you can do that without an analogue switch across each key matrix intersection. The key matrix will be scanned so you can only assert the connection at certain times in the keyboard's.
You do but that assumes that the keyboard is not being scanned by the original keyboard electronics. Have these been disabled? If so is your nano scanning the keyboard as well. I can't tell from just looking at the code.

Hi Mike, first of all, thanks for answering.
I want to send a midi message with a note from the pc, "faking" the keyboard; but I don't wan't to control the keyboard, I want to send a signal as if the key has been pressed when the key has not been pressed at all. It's like the arduino is a second keyboard sending 5V signals into the LSI. From the schematic, when pressing a key, let's say a D3, because we have KC3 on a side and KI3 on the other side, KI3 is initially set to 0V and KC3 to 5V (not continuously, but with the intervals that I wrote in the first post), and when the key is pressed we have a direct link between KC3 and KI3 so they are both at 5V. So the arduino when sending a midi note, for example the same D3, will have to scan when KC3 is high and send a 5V signal to the KI3 lane.
The original keyboard will not be scanned by the arduino, but as I said in the other post, when I set the outputs I have none of the two things working (the MIDI signals and the original keyboard signals), that made me thinking about putting diodes between the outputs and the ribbon (I also forgot to write that I put 470 Ohm resistor in series with each one of the outputs, to prevent high current circulation that can harm the arduino itself and the keyboard LSI)

when I set the outputs I have none of the two things working (the MIDI signals and the original keyboard signals),

I am not surprised because you have a keyboard scanning pattern coming from the keyboard and then the Arduino goes and sticks a HIGH on one of those lines and screws up the whole thing. I can't see how the method you are trying is going to work. Have you looked at the signals on an oscilloscope?

thinking about putting diodes between the outputs and the ribbon

As I said before the only thing I can think of doing is putting an analogue switch, like one of the eight gates you get in a 74HC4066, across each matrix intersection. I would try just one switch on one chip first before you wire them all up.

OK so suppose you got that working and you pressed a key on the keyboard, then this would send out a MIDI message back to the PC. Is that correct?
If so would that not also send a message to the PC when a MIDI input from the PC requested a sound? Maybe you can do something in the Arduino software to disregard any message received after you send one.

Grumpy_Mike:
I am not surprised because you have a keyboard scanning pattern coming from the keyboard and then the Arduino goes and sticks a HIGH on one of those lines and screws up the whole thing. I can't see how the method you are trying is going to work. Have you looked at the signals on an oscilloscope?
As I said before the only thing I can think of doing is putting an analogue switch, like one of the eight gates you get in a 74HC4066, across each matrix intersection. I would try just one switch on one chip first before you wire them all up.

OK so suppose you got that working and you pressed a key on the keyboard, then this would send out a MIDI message back to the PC. Is that correct?
If so would that not also send a message to the PC when a MIDI input from the PC requested a sound? Maybe you can do something in the Arduino software to disregard any message received after you send one.

But the keyboard cannot scan a 5V KI from the keyboard if a key is not pressed, because a KI goes high only when a key is pressed. Maybe now when a key is pressed, the 5V KI from the key goes partly into the arduino input pins (which shouldn't happen) which are at 0V when there's no midi note sent and partly into the LSI, and maybe the LSI doesn't recognize a signal lower than a certain threshold (I'm not 100% sure because I cannot find any datasheet for the LSI). Unfortunately I don't have an oscilloscope to check the signals.
This is why I thought about the diodes, they'll stop the 5V signal from the keyboard, so this signal goes entirely into the LSI and when a 5V signal is sent by the arduino it'll go only in one direction.
When a key is pressed no signal should be sent into the arduino and so no message should go back as a midi signal to the PC, this has to be a one-directional connection, midi signal with a note->arduino converts midi signal in a KI+KC signal->LSI.

What is your experience in electronics?
I am thinking that you might not understand how a scanning matrix works.
Phrases like:-

key goes partly into the arduino input pins

which are at 0V when there's no midi note sent and partly into the LSI,

An input needs to be driven high or low. When there is no drive either way the input floats, which means it could read as a high or a low.

maybe the LSI doesn't recognize a signal lower than a certain threshold

Yes that is how all logic chips work.

With a scanning matrix the scanning can go one of two ways, rows have a walking one and columns are inputs, or the other way round. This is explained here only they are in terms of an LED rather than a switch matrix. http://www.thebox.myzen.co.uk/Workshop/LED_Matrix.html

Grumpy_Mike:
What is your experience in electronics?
I am thinking that you might not understand how a scanning matrix works.
Phrases like:-An input needs to be driven high or low. When there is no drive either way the input floats, which means it could read as a high or a low.
Yes that is how all logic chips work.

With a scanning matrix the scanning can go one of two ways, rows have a walking one and columns are inputs, or the other way round. This is explained here only they are in terms of an LED rather than a switch matrix. http://www.thebox.myzen.co.uk/Workshop/LED_Matrix.html

I have a degree in EE. I understand how a scanning matrix works, in this case, referring to the casio key switch matrix, KCs are the rows and KIs the columns, control signals are sent by the LSI, changing position every 2 ms (according to the service manual graphs), so the first is KC1, then after 2ms KC2, then KC3 and so on; when I press a key on the keyboard, the corresponding KI signal goes into the LSI, then the LSI interprets the combination of the KC and KI signal and outputs the right note.
But I'm not talking about the switch matrix, I'm talking about the code. I think Pinksoir code (Adding MIDI input to pre-MIDI synthesizer with matrix keyboard - Audio - Arduino Forum) is a great way to start for this project, using logic and DPM instead of functions. The thing is, I cannot understand why the code, even if it looks right to me, won't work at all.
I also did a little edit to the circuit, I added the diodes and now the keyboard works. Now I only have to understand how to write the right code

The thing is, I cannot understand why the code, even if it looks right to me, won't work at all.

Well for a start:-

&& PIND == selectPins1[0])

You defined this value of your array to be B00000001
But you defined port D to be :-

// Set pins D2-D7 as read, and leave pins 0 & 1 as is (RX/TX)

So you are looking to read a value that can not exist because pins 0 & 1 are the RX/TX pins.

There is lots more wrong with the code as well. The else clause in your if statements ensures that any logic HIGH written to port C will be overridden by the next if statement to fail. So unless the note is picked up in the last if statement it will only be high for a matter of 4uS or so. This high has to persist long enough for your keyboard electronics to see it and process it.

But more fundamental than this is the fact that your note on function is only called once when a MIDI note on message is received, where this has to run all the time.

Your overall strategy seems to be to read the KC outputs and if it is in the range of a note you want to hold then write HIGH to the KI pin. So you are shadowing the scanning of the matrix by the keyboard. As it stands this will only "work" for one key at a time, otherwise you need to work in a list of notes that are supposed to be on.

Every time that KCx changes you have to go through this routine of looking for what the KC pins are and writing a HIGH or LOW to the appropriate KI pin. So to do this you would require all the KCx pins to trigger an interrupt ( using the pin change mode ) and run the appropriate section ( the if statement part ) of your note on function. Your "note off" function needs to simply remove the appropriate note from the list your "note on" function sets.

Such heavy use of interrupts might scupper reading the serial port, and the overall strategy is a bit hairy to say the least. This is because it is only sometime after the KC pin has changed that the HIGH on a KI pin will be removed, so there will be a short time when the KI pin is being asserted for the wrong KC. This may or may not fool the keyboard into triggering a note.

And one more thing:-

Serial.begin(31250);

and

Serial.print(incomingByte);

You do know that you can't set the Arduino serial monitor to 31250 baud, so the print statement is not going to do anything?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.