Author Topic: Fastest way to scan keys if each row is a separate PCB?  (Read 1973 times)

0 Members and 1 Guest are viewing this topic.

Offline taako

  • Thread Starter
  • Posts: 22
Fastest way to scan keys if each row is a separate PCB?
« on: Fri, 01 June 2018, 19:42:19 »
So i am designing a split keyboard where each row on each hand is a separate PCB. There will be four PCBs that connect to a thumb cluster that has the main microcontroller. Each PCB has 7 keys for a total of 28 keys and the thumb cluster with 5 buttons. So the thumb clusters microcontroller needs to scan a total of 33 keys split across 5 PCBs and then send that data off to the microcontrolle that connects to the USB.

Initially i though it would be smart to put an I/O expander on each of the row PCBs and then all i'd need to do is wire a single I2C bus that branches off to each PCB. But i am hesitant because i am wondering if this will be very slow for the keyscanning? Even fastmode I2C is 400kb/s. So back of the hand calculations go like this:

(400e3 bits/s) / (8 bits per byte * 4 bytes per transcation [slave address write, command byte, slave address read, return data] * 4 transactions [one per row PCB]) = 3125 keyscans/second from the row PCBs. Then i need to add in the 5 thumb button scans (very easy); but this all assumes no overhead. There will a lot more going on when i have to interpret the scancodes and ship it off to the main USB microcontroller.

What are the fastest methods for scanning keys on separate PCBs that also reduces number of inter-PCB wires? Are shift registers much faster? What sort of overhead for the the processing of the scancodes should i expect?

If the method i described with the I/O expanders on 400kb/s fastmode I2C is actually fast enough to effectively scan the keys can someone give me some reassurance that it wont limit the keyboard and that i did my calculations correctly?


I realize i can use SPI for much faster transmission but then i have 7 wires (CS, SCK, SDA, MOSI, MISO, Vcc, GND) going to each board as opposed to four (SDA, SCL, Vcc, GND) for I2C and i am trying to keep the wiring between boards to a minimum. In addition SPI would need a CS line PER row from the main micro whereas I2C is a common bus.
« Last Edit: Fri, 01 June 2018, 20:46:55 by taako »

Offline Findecanor

  • Posts: 5040
  • Location: Koriko
Re: Fastest way to scan keys if each row is a separate PCB?
« Reply #1 on: Fri, 01 June 2018, 21:36:24 »
I would connect the 4 rows and 7 columns with ribbon cables between them. Simplest, cleanest and fastest.
A 7-wire ribbon cable from the controller PCB to the first row's and a 7-wire from one to the next. Then a single line from the controller to each row, which could be a 4-wire ribbon cable sliced up and tapped off at different lengths.
In total (7+4) + 4*(7+7+1) = 71 soldering joints.

With Shift registers, the smallest config would shift the columns in parallel, reading all rows. Each register chip needs five lines (PL, CP, D, +5, GND). That's a 5-wire ribbon cable from the controller PCB to the first PCB and a 5-wire cable from each to to the next. Then a single line from the controller to each row like above.
In total (5+4) + 4*(5+5+1) = 53 soldering joints .... plus you would also need to solder the shift registers' 16 or 20 pins for a total of 117 soldering joints.

With a simple matrix, it would be fastest to strobe the side of the matrix that you have the fewest of and read as many lines as possible in parallel: which in this case is rows.
But with shift registers like connected above you are restricted to using them for strobing and reading the four rows in parallel, or strobing the rows and reading the columns serially.

The thumb cluster could be a separate 3×2 cluster read in parallel, which would not affect the finger matrix.
« Last Edit: Fri, 01 June 2018, 21:41:10 by Findecanor »
🍉

Offline Koren

  • Posts: 133
  • Location: France
Re: Fastest way to scan keys if each row is a separate PCB?
« Reply #2 on: Sat, 02 June 2018, 12:30:20 »
That's the simplest, I agree... and a good solution.

Still, 7 keys need 6 matrix wires at most (3x3 and 4x2 are both above 7)

So I would probably do, for the four 7-keys PCB

A1 A2 A3 A4 B1 B2 B3  (solder A B 1 2 3 4)
C1 C2 C3 C4 D1 D2 D3 (solder C D 1 2 3 4)
B7 B6 B5 B4 A7 A6 A5  (solder B A 7 6 5 4)
C7 C6 C5 C4 D7 D6 D5  (solder D C 7 6 5 4)

Or a permutation of those, depending on how you route them (important point: you do the above with the SAME pcb)

And use E1 E2 E3 E4 E5 for the thumb clusters

On the main controller board, you can do a 2x12 matrix of holes, linked to 11 controller pins :

AB|CD|1234|4567
AB|CD|1234|4567

which is easy to route, and use four 2-wires and four 4-wires ribbons to the PCBs.

That's 16 less solder joints... That being said, that's verging on OCD.

Still, that means that a bus solution using 4 wires may be somehow interesting, while 7 is definitively not (since you can have direct wiring with less wires)
« Last Edit: Sat, 02 June 2018, 12:35:48 by Koren »

Offline taako

  • Thread Starter
  • Posts: 22
Re: Fastest way to scan keys if each row is a separate PCB?
« Reply #3 on: Sat, 02 June 2018, 12:44:20 »
That's the simplest, I agree... and a good solution.

Still, 7 keys need 6 matrix wires at most (3x3 and 4x2 are both above 7)

So I would probably do, for the four 7-keys PCB

A1 A2 A3 A4 B1 B2 B3  (solder A B 1 2 3 4)
C1 C2 C3 C4 D1 D2 D3 (solder C D 1 2 3 4)
B7 B6 B5 B4 A7 A6 A5  (solder B A 7 6 5 4)
C7 C6 C5 C4 D7 D6 D5  (solder D C 7 6 5 4)

Or a permutation of those, depending on how you route them (important point: you do the above with the SAME pcb)

And use E1 E2 E3 E4 E5 for the thumb clusters

On the main controller board, you can do a 2x12 matrix of holes, linked to 11 controller pins :

AB|CD|1234|4567
AB|CD|1234|4567

which is easy to route, and use four 2-wires and four 4-wires ribbons to the PCBs.

That's 16 less solder joints... That being said, that's verging on OCD.

Still, that means that a bus solution using 4 wires may be somehow interesting, while 7 is definitively not (since you can have direct wiring with less wires)

Thanks for the response but I'm having a really hard time visualizing what you mean? Could you potentially do a crude MS Paint drawing for me? I'd really appreciate it

Offline Koren

  • Posts: 133
  • Location: France
Re: Fastest way to scan keys if each row is a separate PCB?
« Reply #4 on: Sat, 02 June 2018, 14:22:53 »
Crude is harsh...

On the left, the four 7-keys PCBs (keys are cyan squares)

On the right, the thumb/microcontroller PCB

You need to connect the dots... red pair on red pair, green 4-ribbon green 4-ribbon, etc. I hope it's understandable enough.

The microcontroller pins are the 12 cyan circles on the right. 5 are outputs (#1 to #5, on the left), 7 are inputs (#1 to #7, on the right).

To scan, you put a single output at 1, all others at 0. And you read the 7 input pins.

Basically, if when you put 1 on output 3 (B) and you measure 1 on input 3, that means B3 is pressed.

You need diodes for N-key rollover (the ability to press any number of keys at the same time).

This way, you can scan all keys by just reading 5 bytes, so scanrate will be high.

There's an output linked to nothing, you need to wire each key on thumb cluster between this one an any of the seven inputs.

Bonus, you don't need dual sided PCBs... so it's cheaper (you may want to wire the PCBs differently if you need a hole in the middle of keys (e.g. switch A3 and A4), but that's not hard to wire it differently on the same principle)

« Last Edit: Sat, 02 June 2018, 14:25:46 by Koren »

Offline taako

  • Thread Starter
  • Posts: 22
Re: Fastest way to scan keys if each row is a separate PCB?
« Reply #5 on: Tue, 05 June 2018, 12:28:20 »
Crude is harsh...

On the left, the four 7-keys PCBs (keys are cyan squares)

On the right, the thumb/microcontroller PCB

You need to connect the dots... red pair on red pair, green 4-ribbon green 4-ribbon, etc. I hope it's understandable enough.

The microcontroller pins are the 12 cyan circles on the right. 5 are outputs (#1 to #5, on the left), 7 are inputs (#1 to #7, on the right).

To scan, you put a single output at 1, all others at 0. And you read the 7 input pins.

Basically, if when you put 1 on output 3 (B) and you measure 1 on input 3, that means B3 is pressed.

You need diodes for N-key rollover (the ability to press any number of keys at the same time).

This way, you can scan all keys by just reading 5 bytes, so scanrate will be high.

There's an output linked to nothing, you need to wire each key on thumb cluster between this one an any of the seven inputs.

Bonus, you don't need dual sided PCBs... so it's cheaper (you may want to wire the PCBs differently if you need a hole in the middle of keys (e.g. switch A3 and A4), but that's not hard to wire it differently on the same principle)

Show Image


Ah this is a good way to do it! Simpler (and probably faster) than I2C too! Apprecciate the help, i don't know if i can get it any simpler and faster than this in terms of number of wires. Theoretically in your image i could even have an 8th switch that connects to green 4 and pink 4 for each letter as well, couldn't i? Nevermind, green 4 and pink 4 are linked so if i wanted an 8th switch i'd need to separate them giving me one more input line. But the same number of lines going into each of the row PCBs.
« Last Edit: Tue, 05 June 2018, 12:35:17 by taako »

Offline Koren

  • Posts: 133
  • Location: France
Re: Fastest way to scan keys if each row is a separate PCB?
« Reply #6 on: Tue, 05 June 2018, 13:11:11 »
In term of number of wires, 8 is easy (just use 5678 instead of 4567 for the bottom two and you can add a 8th switch on each). In fact, it's probably even simpler, but I went with the idea to limit the number of pins needed on the microcontroller in your case (IIRC, you can handle both hands with a single Teensy 2 and still have 3 leds (more with tricks)

With 6 wires/PCB, the best you can do is 9 switches/pcb (3 inputs and 3 outputs on each), but assuming you limit yourself to 8 inputs on the microcontroller (so that it's a byte) and not 9, you'll need 6 outputs, so scanning rate will be slightly slower.

Well, technically, you can use more switches than that with 4/6 wires, without the need for an I/O extender or bus, but to get x-key rollover, you would need several buffers/mulitplexers on the main pcb, so that's unpractical if you're after something simple and efficient.