rabit1
Posts: 7
Joined: Sun Mar 05, 2017 3:45 am

Connecting a Pi to about 100 push buttons

Sun Mar 05, 2017 4:01 am

Hi,

How would I approach this project using a Pi? I have about 100 physical push buttons that works as a counter. Each button will be pressed several times a minute, but lots of buttons can be pressed at the same time.

Each button pressed will count as 1 press and put to the database. This buttons will be pressed by sewing machine operators every time each operator finish sewing an item, as a mean to collect/capture production output and speed.

Can someone advise me on what would be the best approach? Is there a hardware add-on for Pi that let me connect to 100 push buttons? or possible more.

Thanks in advance,
Regards,

Crumbs
Posts: 22
Joined: Thu Jan 05, 2017 9:40 am

Re: Connecting a Pi to about 100 push buttons

Sun Mar 05, 2017 1:44 pm

Your post is a little unclear but it sounds like you have a sweatshop of 100 sewing machines and you want each slave to hit a button when they're done sewing a pair of G-star whatever pants so you can keep track of who is sewing the most... or something.

You would use a keypad scanning technique.. also called charlieplexing:
http://www.mosaic-industries.com/embedd ... can-decode

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13688
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Connecting a Pi to about 100 push buttons

Sun Mar 05, 2017 2:27 pm

Okay, I hade a nice PM from the OP, and it seems we don't have a case of a sweatshop here:

So I unlocked this post.

I think the previous answer which involved a form of key-array was an "tongue in cheek" answer, as charlieplexing doesn't have the main characteristic, that is you cannot read out multiple simultaneous keypresses, this normally the case with any kind of key matrix solution.

You need a solution where you can read out each an every key independently of all the other keys.
I know two solutions, the first one is to use I2C I/O expanders, and for one hundred inputs I think that will work, as there is a limit of I/O expanders you can connect to I2C, namely with most of them 8, and they normally have up-to 16 inputs so that is good for 8 x 16 = 128 inputs So that could work.

A second, perhaps simpler solution is to use shift registers with input latches, these are normally 8-bit, so you need 13 of them in series, with the input latch signal and shift-data-clock signal all parallel.
The procedure is that you generate a input latch signal to all the shift registers, and then clock in the whole train of one hundred bits.

rabit1
Posts: 7
Joined: Sun Mar 05, 2017 3:45 am

Re: Connecting a Pi to about 100 push buttons

Mon Mar 06, 2017 1:26 am

Thank you,

Yes you're right, in this case we have to consider many buttons being pressed all at the same time, and the system has to be able to collect and account for each distinct press reliably.

Is there a tutorial, for obviously a newbie, for using shift registers with Pi? Sounds like that might be a solution.

Another thing to consider is the length of the wiring for the physical button, which may go as far as 25-30 meter if I have to guess.

Again, thank you for helping.
Regards,

peterlite
Posts: 720
Joined: Sun Apr 17, 2016 4:00 am

Re: Connecting a Pi to about 100 push buttons

Mon Mar 06, 2017 2:33 am

For a larger I2C setup, you could have a Pi Zero on each row of buttons to feed up to 128 I2C buttons into your main machine using a protocol other than I2C. The Pi Zero W could feed through Wifi. In a multiroom setup, You could then have other sensors for temperature, humidity, lighting. I am waiting for a Pi Zero W to test the Wifi range.

IanS
Posts: 276
Joined: Wed Jun 20, 2012 2:51 pm
Location: Southampton, England

Re: Connecting a Pi to about 100 push buttons

Mon Mar 06, 2017 10:56 am

I have a setup that includes two 61 button sets (most of an 8x8 matrix each) plus 35 individual buttons, all monitored by two RPis. A single one could do all the monitoring, but they are processor limited doing something else based on the button states, so I've spread the load. You can differentiate multiple presses in a switch matrix provided that the buttons have suitable diodes with the switches to prevent current flow across the grid. I used stacked MCP23017 boards, https://www.abelectronics.co.uk/p/54/IO-Pi-Plus, but found I needed an external power supply for the 2nd board (as the documentation states you might).

If the switches are spread over a wide area, watch out for voltage drop if you use too thin wire.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13688
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Connecting a Pi to about 100 push buttons

Mon Mar 06, 2017 11:47 am

This university of Cambridge lesson tells you how to wire up shift registers to read buttons, the example uses just two 8-bit shift register, but this is easily expanded to many more.
https://www.cl.cam.ac.uk/teaching/1617/ ... ftreg.html

Image

YCN-
Posts: 246
Joined: Fri Jun 10, 2016 3:18 pm

Re: Connecting a Pi to about 100 push buttons

Mon Mar 06, 2017 2:36 pm

Hm, I had the same problem once, and I'll tell you what, the solution is under your eyes. At least if your in front of a computer. You should do mutiplexing, just as your keyboard. If you got an old keyboard just oppen it and you'll understand how it's working, bicicly it's just some kind of massive multiplexer.
When you'll press a button you'll get a combination of 2 input.
For instance :
Port A has 16 input linked with ADC
Port B has 16 output with different voltage (let's say first pin has 0V to 17V)

then when you'll read 2V on pin A:1 : you'll know you have a switch between A:1 and B:2
when you'll read 5V on pin A:16 : you'll know you have a switch between A:16 and B:5

and you can go from there. With this technic you've got 2^16 possible switchs. You can have as many input as you'd like. You'll be limitated with the number of ADC you can have and number of PIN of the Pi. But you can find some way to fit into the number of GPIO you have.

Does that help ?

hippy
Posts: 9170
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Connecting a Pi to about 100 push buttons

Mon Mar 06, 2017 5:24 pm

mahjongg wrote:Okay, I hade a nice PM from the OP, and it seems we don't have a case of a sweatshop here:

So I unlocked this post.
Great; so we're locking posts now based simply on moral judgement and unsupported suspicions, which may themselves have been made in jest :(
mahjongg wrote:I think the previous answer which involved a form of key-array was an "tongue in cheek" answer, as charlieplexing doesn't have the main characteristic, that is you cannot read out multiple simultaneous keypresses, this normally the case with any kind of key matrix solution.
Normal row/column matrix keypad scanning can read multiple simultaneous key presses proving each button also has a diode fitted.

I can't however see charlieplexing working, even with diodes, however looking at the diagram on the linked page, it might..

User avatar
mikronauts
Posts: 2822
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: Connecting a Pi to about 100 push buttons

Mon Mar 06, 2017 5:49 pm

I remember detecting multiple presses without diodes:

- rows have a weak pullup
- columns are either low, or set to input to disconnect them (or tri-stated)

As I recall, it worked
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13688
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Connecting a Pi to about 100 push buttons

Mon Mar 06, 2017 6:53 pm

Normally matrix keyboards have a problem with ghosting, that means that if you press more than three keys at the same time the software can see key-presses that are not really made, the n-key rollover problem. That is not acceptable in this case, see https://en.wikipedia.org/wiki/Rollover_%28key%29.

The solution, used for example with organ keys, is to add a diode in series with each key, this will eliminate ghosting.
A key matrix is cost effective as with a minimal number of GPIO's you can detect lots of keys, without having to add extra hardware, the downside is that software can be complex. The best solution (IMHO) is to have a background task that constantly reads out the keys and copy their state to an internal image, then you can detect changes between the previous image and the latest image, so you can convert it to "key pressed" and "key released" events. The scanning routine should do two (or more) actual scans per readout to detect key-bounce. only accepting a valid read if the key read, reads out the same as the previous time(s).

also read: http://blog.komar.be/how-to-make-a-keyboard-the-matrix/

User avatar
mikronauts
Posts: 2822
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: Connecting a Pi to about 100 push buttons

Mon Mar 06, 2017 7:22 pm

Thanks - great second link.

I'd fail the "infinite monkeys will re-write Shakespear" test, I never tried that keypress combination - but I can see how it would cause ghosting.
mahjongg wrote:Normally matrix keyboards have a problem with ghosting, that means that if you press more than three keys at the same time the software can see key-presses that are not really made, the n-key rollover problem. That is not acceptable in this case, see https://en.wikipedia.org/wiki/Rollover_%28key%29.

The solution, used for example with organ keys, is to add a diode in series with each key, this will eliminate ghosting.
A key matrix is cost effective as with a minimal number of GPIO's you can detect lots of keys, without having to add extra hardware, the downside is that software can be complex. The best solution (IMHO) is to have a background task that constantly reads out the keys and copy their state to an internal image, then you can detect changes between the previous image and the latest image, so you can convert it to "key pressed" and "key released" events. The scanning routine should do two (or more) actual scans per readout to detect key-bounce. only accepting a valid read if the key read, reads out the same as the previous time(s).

also read: http://blog.komar.be/how-to-make-a-keyboard-the-matrix/
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 28358
Joined: Sat Jul 30, 2011 7:41 pm

Re: Connecting a Pi to about 100 push buttons

Mon Mar 06, 2017 9:20 pm

hippy wrote:
mahjongg wrote:Okay, I hade a nice PM from the OP, and it seems we don't have a case of a sweatshop here:

So I unlocked this post.
Great; so we're locking posts now based simply on moral judgement and unsupported suspicions, which may themselves have been made in jest :(
The mods make moral decision everyday, on spam emails, on reported posts. We only have what is in front of our eyes to go on, and as you can see, if we make a mistake we put it right.

Still no like? Well, make a moral judgement. I'll respect it.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

hippy
Posts: 9170
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Connecting a Pi to about 100 push buttons

Mon Mar 06, 2017 11:51 pm

jamesh wrote:Still no like? Well, make a moral judgement. I'll respect it.
I appreciate mods having to make judgements but there was nothing in the OP's post that would seem to give cause for it being locked. Even if the OP were running a sweatshop; does that make the question undeserving of community response? I don't think so but it's moot as the thread has been unlocked.

I imagine Mamoud from Kandahar will get short-shrift if he comes to ask how he can use Bluetooth on his Zero W to activate a GPIO and needs an answer quick because he's flying to the US tomorrow :lol:

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 28358
Joined: Sat Jul 30, 2011 7:41 pm

Re: Connecting a Pi to about 100 push buttons

Tue Mar 07, 2017 7:19 am

hippy wrote:
jamesh wrote:Still no like? Well, make a moral judgement. I'll respect it.
I appreciate mods having to make judgements but there was nothing in the OP's post that would seem to give cause for it being locked. Even if the OP were running a sweatshop; does that make the question undeserving of community response? I don't think so but it's moot as the thread has been unlocked.

I imagine Mamoud from Kandahar will get short-shrift if he comes to ask how he can use Bluetooth on his Zero W to activate a GPIO and needs an answer quick because he's flying to the US tomorrow :lol:
That's a bit racist.....
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

hippy
Posts: 9170
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Connecting a Pi to about 100 push buttons

Tue Mar 07, 2017 9:35 am

jamesh wrote:
hippy wrote:
jamesh wrote:Still no like? Well, make a moral judgement. I'll respect it.
I appreciate mods having to make judgements but there was nothing in the OP's post that would seem to give cause for it being locked. Even if the OP were running a sweatshop; does that make the question undeserving of community response? I don't think so but it's moot as the thread has been unlocked.

I imagine Mamoud from Kandahar will get short-shrift if he comes to ask how he can use Bluetooth on his Zero W to activate a GPIO and needs an answer quick because he's flying to the US tomorrow :lol:
That's a bit racist.....
How so ? It highlights the point I am making that readers can make bad moral judgements because of how they read and interpret something which is not how it was intended.

I would hope that Mamoud would have his post allowed and not locked, would get the help he needed to turn the external power on for the winch he has fitted to his drone so it was working when he brought it to Amazon for his job interview in the delivery research department, got his work visa.

I think it would be terrible if he had his post locked, he didn't get it working, missed his opportunity, because someone thought he was asking how to do something less acceptable than he was actually asking.

Did you think I was suggesting something else ? Those were your thoughts not mine, derived from what you thought was written, not what was written. There's nothing in that which says why Mamoud wants to do what he wants to do.

If I'd said "Sven from Sweden" would that have still been "a bit racist" ?

YCN-
Posts: 246
Joined: Fri Jun 10, 2016 3:18 pm

Re: Connecting a Pi to about 100 push buttons

Tue Mar 07, 2017 9:57 am

hippy wrote: If I'd said "Sven from Sweden" would that have still been "a bit racist" ?
Of course... Racism isn't about white/black(/any other non-white people) relationship, it's only about making jugement about someone because of his race and/or birth state. But it was even more obvious racism when you said what you said...

Sorry about that but you're wrong.

I mean, don't try to fool us with long justification, you said something racist, deal with it... You even wrote it witch makes it even harder to hide.

hippy
Posts: 9170
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Connecting a Pi to about 100 push buttons

Tue Mar 07, 2017 10:03 am

YCN- wrote:
hippy wrote: If I'd said "Sven from Sweden" would that have still been "a bit racist" ?
Of course... Racism isn't about white/black(/any other non-white people) relationship, it's only about making jugement about someone because of his race and/or birth state. But it was even more obvious racism when you said what you said...

Sorry about that but you're wrong.

I mean, don't try to fool us with long justification, you said something racist, deal with it... You even wrote it witch makes it even harder to hide.
I made no judgement other than on the people who make judgements.

rabit1
Posts: 7
Joined: Sun Mar 05, 2017 3:45 am

Re: Connecting a Pi to about 100 push buttons

Tue Mar 07, 2017 1:58 pm

YCN- wrote:Hm, I had the same problem once, and I'll tell you what, the solution is under your eyes. At least if your in front of a computer. You should do mutiplexing, just as your keyboard. If you got an old keyboard just oppen it and you'll understand how it's working, bicicly it's just some kind of massive multiplexer.
When you'll press a button you'll get a combination of 2 input.
For instance :
Port A has 16 input linked with ADC
Port B has 16 output with different voltage (let's say first pin has 0V to 17V)

then when you'll read 2V on pin A:1 : you'll know you have a switch between A:1 and B:2
when you'll read 5V on pin A:16 : you'll know you have a switch between A:16 and B:5

and you can go from there. With this technic you've got 2^16 possible switchs. You can have as many input as you'd like. You'll be limitated with the number of ADC you can have and number of PIN of the Pi. But you can find some way to fit into the number of GPIO you have.

Does that help ?
Thank you,

Wouldn't this have problems with lots of multiple keys all being pressed at the same time? Pressing my keyboards with 10 fingers only registers a few keys and missed some other keys. How can I guarantee reliability?

rabit1
Posts: 7
Joined: Sun Mar 05, 2017 3:45 am

Re: Connecting a Pi to about 100 push buttons

Tue Mar 07, 2017 2:08 pm

peterlite wrote:For a larger I2C setup, you could have a Pi Zero on each row of buttons to feed up to 128 I2C buttons into your main machine using a protocol other than I2C. The Pi Zero W could feed through Wifi. In a multiroom setup, You could then have other sensors for temperature, humidity, lighting. I am waiting for a Pi Zero W to test the Wifi range.
Thank you, this sounds really cool. In fact it gives me some more ideas to setup up a monitor display on each Pi Zero grouping of buttons for further informative data display. Cool.

YCN-
Posts: 246
Joined: Fri Jun 10, 2016 3:18 pm

Re: Connecting a Pi to about 100 push buttons

Tue Mar 07, 2017 2:22 pm

rabit1 wrote: Thank you,

Wouldn't this have problems with lots of multiple keys all being pressed at the same time? Pressing my keyboards with 10 fingers only registers a few keys and missed some other keys. How can I guarantee reliability?
Actualy you probably will, but you can do the following, I shortly draw you a schematic that might be usefull. It's basic electronic, I'm not sure that it's perfect as it is but here is the idea.

Here's the draft :
https://drive.google.com/open?id=0B0yka ... k1QZUdGSVk

The 4 Resistor have different Ohm, so when a switch will be closed you'll add a resistor in parrallel witch will give you an equivalent resistor of Req= R0.R1.R2.R3 / (R0+R1+R2+R3) when all switchs will be closed, you'll have Req = R0.R1.R2. / (R0+R1+R2) if the 3 switchs are closed all together. Do the math and the basic electronic with a bread board a Voltmeter and you'll soon have a nice prototype working.

But be carefull because I can't guarantee you that the Pi can provide enough power to do that with up to 100 switchs. In such case you can A) use another power supply and B) use differents pins. It will simplify the math if you use different pins anyway !

What do you think ?

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13688
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Connecting a Pi to about 100 push buttons

Tue Mar 07, 2017 3:23 pm

rabit1 wrote:
YCN- wrote:Hm, I had the same problem once, and I'll tell you what, the solution is under your eyes. At least if your in front of a computer. You should do mutiplexing, just as your keyboard. If you got an old keyboard just oppen it and you'll understand how it's working, bicicly it's just some kind of massive multiplexer.
When you'll press a button you'll get a combination of 2 input.
For instance :
Port A has 16 input linked with ADC
Port B has 16 output with different voltage (let's say first pin has 0V to 17V)

then when you'll read 2V on pin A:1 : you'll know you have a switch between A:1 and B:2
when you'll read 5V on pin A:16 : you'll know you have a switch between A:16 and B:5

and you can go from there. With this technic you've got 2^16 possible switchs. You can have as many input as you'd like. You'll be limitated with the number of ADC you can have and number of PIN of the Pi. But you can find some way to fit into the number of GPIO you have.

Does that help ?
Thank you,

Wouldn't this have problems with lots of multiple keys all being pressed at the same time? Pressing my keyboards with 10 fingers only registers a few keys and missed some other keys. How can I guarantee reliability?
Your keyboard uses a matrix of keys without diodes (diodes cost money, and no computer keyboard I know uses them) because (without diodes) the matrix will have key ghosting, the software in the micro-controller in the keyboard doesn't even attempt to catch more than three pressed keys at the same time. Ctrl-Alt-Del is possible as all three keys are on the same row (or column) of the matrix. It was designed that way.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13688
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Connecting a Pi to about 100 push buttons

Tue Mar 07, 2017 3:27 pm

YCN- wrote:
rabit1 wrote: Thank you,

Wouldn't this have problems with lots of multiple keys all being pressed at the same time? Pressing my keyboards with 10 fingers only registers a few keys and missed some other keys. How can I guarantee reliability?
Actualy you probably will, but you can do the following, I shortly draw you a schematic that might be usefull. It's basic electronic, I'm not sure that it's perfect as it is but here is the idea.

Here's the draft :
https://drive.google.com/open?id=0B0yka ... k1QZUdGSVk

The 4 Resistor have different Ohm, so when a switch will be closed you'll add a resistor in parrallel witch will give you an equivalent resistor of Req= R0.R1.R2.R3 / (R0+R1+R2+R3) when all switchs will be closed, you'll have Req = R0.R1.R2. / (R0+R1+R2) if the 3 switchs are closed all together. Do the math and the basic electronic with a bread board a Voltmeter and you'll soon have a nice prototype working.

But be carefull because I can't guarantee you that the Pi can provide enough power to do that with up to 100 switchs. In such case you can A) use another power supply and B) use differents pins. It will simplify the math if you use different pins anyway !

What do you think ?
Resistors do not have an infinite precision, normally they are just precise to 1% (or 5%) of their value. So your scheme does not work with many keys, also resistors do not normally have values that are precise multiples of each other. Then the PI has no analog inputs, and cheap external I2C ADC's have only an 8-bit range, so only 256 unique values.
Its a smart idea, and you see it sometimes with a few keys, like 5 or so using a single analog micro-controller pin but for this case its not so suitable.

p.s. to be pedantic, you draw resistors as coils, but okay.... :mrgreen:

YCN-
Posts: 246
Joined: Fri Jun 10, 2016 3:18 pm

Re: Connecting a Pi to about 100 push buttons

Tue Mar 07, 2017 3:39 pm

mahjongg wrote:Resistors do not have an infinite precision, normally they are just precise to 1% (or 5%) of their value. So your scheme does not work with many keys, also resistors do not normally have values that are precise multiples of each other. Then the PI has no analog inputs, and cheap external I2C ADC's have only an 8-bit range, so only 256 unique values.
Its a smart idea, and you see it sometimes with a few keys, like 5 or so using a single analog micro-controller pin but for this case its not so suitable.

p.s. to be pedantic, you draw resistors as coils, but okay.... :mrgreen:
Yes I'm aware of that so he has to well decide what size of resistor he should use, and try to build the system. But I think that with 10 resistor per GPIO pin, it makes a system with 10 ADC for the 100 push buttons, I think that it's doable, but not industrialisable. It's really DIY, but it's cheap and and the software side would be really simple.

Yes I'm tired sorry about the miss-drawing ahah ! :roll:

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Connecting a Pi to about 100 push buttons

Tue Mar 07, 2017 3:52 pm

Personally, I would worry that button presses would be missed, due to the person just swiping the button quickly and the Pi scanning some other part of the matrix, or just not happening to read that button at that particular moment. I imagine you would want a system that was demonstrably fair to your employees.

So what I would do is put some logic with every button that set a flip-flop when the button was pressed. The button would then light up. When the Pi scanned that button it would clear the flip-flop and so switch the light off. That gives the employee clear feedback that their button press was accepted and counted. The only complexity would be in ensuring that there was no double-counting due to the Pi reading the button during the time it was still bouncing (you could ignore any new presses that occurred within a second or so of the last unless your machinists are incredibly fast) and the added hardware and software required.

The easiest way to implement that is probably a multidrop serial line with an 8-pin microcontroller, but you could probably manage with an I2C chip shared between each four machines.

Return to “Automation, sensing and robotics”