Help: How to read switch state

Thread Starter

tomportland

Joined Dec 31, 2011
18
I think I got the idea. Thanks a million for the detailed explanation!

And Happy New Year to everyone!

Tom

Here is a schematic to illustrate how you would interrogate four switches using the matrix scan method. This would require two output pins and two input pins for a total of four pins. Of course, you would not do this for only four switches. For 64 switches, you would use 8 input/output pins and 8 input pins for a total of 16 I/O pins.




The buffers shown are not actual circuits. They are there just to indicate which lines are outputs and which are inputs.

To begin the scan, all eight U1 lines are set to INPUT mode.
All eight bits read back at U2 will be HIGH.
If U1A alone is set to OUTPUT LOW, inputs at U2 will read back a LOW for every CLOSED switch on the U1A line.
Repeat this for every U1 line in order to scan all switches.

(Have to modify to make U1 lines into INPUT mode when not being used to send LOW).
 

Thread Starter

tomportland

Joined Dec 31, 2011
18
I'm glad to hear that you think Uno will do the job. It has 14 digital I/O pins and 6 analog input pins. So I'm thinking to use 8 digital pins as output, 6 digital pins as input and 2 analog input pins as well. In this way, no toggle is needed. Am I correct?

Actually, to make the programming easier (but physical wiring might be harder since it's 8x8 squares), I could even use 10 digital pins as output and 6 analog pins as input and it will totally support 66 switches which is more than enough for this application (64 switches). What do you guys think?

Thanks again!

Tom


The Uno could do the job by including the ANALOGS for some of the inputs. Map them to a 1 or 0.
In fact with some electronics knowledge and using resistors each line of scan could be resolved to a single analog read (only mentioned for interests sake.)

Just remember to toggle the pin mode for the output scans.

Scan Line X
Change PinMode from input to output low
Read Inputs
Change PinMode back to input
Serial Output the captured data
< Register changes and indicate moves and all the Rest handled in Game Events on computer.>

Scan Next Line

You need the outputs pins to default into high impedance input mode or else you could have two outputs wired together which can be very bad.

For example if you were to misunderstand and just toggle the pins between high and low output it would cause a conflict and high currents between the output level pins that disagree. The Uno might survive this but it wouldn't like it and it sure wouldn't give you the results you want.
 

MMcLaren

Joined Feb 14, 2010
853
If you've got 64 switches, 32 of which may be closed at one time, I would think you'd need a diode in series with each switch in order to prevent false switch readings through back signal paths through closed switches, yes, no?
 
Last edited:

thatoneguy

Joined Feb 19, 2009
6,359
If you've got 64 switches, 32 of which may be closed at one time, I would think you'd need a diode in series with each switch (where it connects to the column and row lines) in order to prevent false switch readings through back tracks, yes, no?
Actually, not using switches, and using diodes only would be a great advantage here, now knowing the application.

Have the contacts to the switch a pad with a concentric ring around it. Then in each white piece, place a diode which is forward biased from the center pad to the concentric ring. In the black piece, have the diode forward biased from the concentric ring to the center pad.

Both the row and column would need to alternate between input and output to get the full status of the board. At the same time, the entire issue of multiple switches closed is gone, in addition to knowing which pieces are white and which are black.

Then apply movement rules for each move, and you know what the piece is, or you could add resistors to each piece for identification in addition to the diode and use all ADC for reading.
 

MrChips

Joined Oct 2, 2009
23,095
If you've got 64 switches, 32 of which may be closed at one time, I would think you'd need a diode in series with each switch in order to prevent false switch readings through back signal paths through closed switches, yes, no?
I realized this after I posted the circuit diagram. I have corrected the text to indicate that outputs are put into the INPUT state. Only one line is made OUTPUT LOW at any given time.
 

MMcLaren

Joined Feb 14, 2010
853
I realized this after I posted the circuit diagram. I have corrected the text to indicate that outputs are put into the INPUT state. Only one line is made OUTPUT LOW at any given time.
But wouldn't that only prevent shorting two outputs together? Without diodes on each switch you could still get false readings through multiple closed switches, couldn't you?
 

MrChips

Joined Oct 2, 2009
23,095
Yes, you are correct. Thanks for correcting me. Is this what you meant?



In which case the output port will remain as an output and all lines will be held HIGH with only one line at a time being set to LOW.
 
Last edited:

Thread Starter

tomportland

Joined Dec 31, 2011
18
I think I got the idea, thanks a lot for the detailed diagram!

One more thing - for the 5v power on the top of the diagram, can I change it to ground? Of course, all the diodes should be put in reverse direction. In this way, all the output ports will held LOW with only one port at a time being set to HIGH.

All the best,

Tom

Yes, you are correct. Thanks for correcting me. Is this what you meant?



In which case the output port will remain as an output and all lines will be held HIGH with only one line at a time being set to LOW.
 

Thread Starter

tomportland

Joined Dec 31, 2011
18
I don't totally understand your comments, but here is what I think,

1. All the pieces will be at their own positions at the very beginning and the white will always go first, so it's not necessary to distinguish the white pieces from the black pieces.

2. You mentioned pad with a concentric ring. I don't quite understand what it does and how can it be used to detect the piece. Can you explain it with a little more details? Thanks a lot!

Tom (the new newbie :))

Actually, not using switches, and using diodes only would be a great advantage here, now knowing the application.

Have the contacts to the switch a pad with a concentric ring around it. Then in each white piece, place a diode which is forward biased from the center pad to the concentric ring. In the black piece, have the diode forward biased from the concentric ring to the center pad.

Both the row and column would need to alternate between input and output to get the full status of the board. At the same time, the entire issue of multiple switches closed is gone, in addition to knowing which pieces are white and which are black.

Then apply movement rules for each move, and you know what the piece is, or you could add resistors to each piece for identification in addition to the diode and use all ADC for reading.
 
Last edited:

thatoneguy

Joined Feb 19, 2009
6,359
I don't totally understand your comments, but here is what I think,

1. All the pieces will be at their own positions and the white will always go first, so it's not necessary to distinguish the white pieces from the black pieces.

2. You mentioned pad with a concentric ring. I don't quite understand what it does and how can it be used to detect the piece. Can you explain it with a little more details? Thanks a lot!

Tom (the new newbie :))
Picture a bullseye, you have a dot and a ring.

On the bottom of each chess piece is a central conductor and an outer conductor, placed so that if spun quickly, it too would appear like a dot and a ring as well. The reason for this is so the chess piece can be pointing any direction and still work.

The trick to it is to have a resistor and diode inside each chess piece. The two would be in series, and the contacts would be on the center pad and at the distance of the concentric ring.

The board would have a copper (or gold) bunch of concentric rings inset onto it for conductors/sensors

In white pieces, the diodes point one direction

In black pieces, the diodes point the other direction.

Then, instead of one sweep for a read, with all being outputs, you do one sweep with rows as outputs and columns as inputs, giving you the white piece locations.
The next sweep, the columns are the outputs, and rows are in the input state, which gives you the black pieces.

If you really wanted to get fancy, you'd put the output state high (+5V) and measure the current drop across each resistor (ADC read instead of digital) to find out which piece it is (pawn through King), using an extra resistor to ground for each ADC pin, and a different valued resistor for the 6 different pieces.
 
Last edited:

MrChips

Joined Oct 2, 2009
23,095
One more thing - for the 5v power on the top of the diagram, can I change it to ground? Of course, all the diodes should be put in reverse direction. In this way, all the output ports will held LOW with only one port at a time being set to HIGH.
Yes, that is ok too. The previous circuit was meant for TTL gates. With CMOS gates or MCU inputs with much lower input currents you can use POSITIVE LOGIC.




As you said, all outputs will OUTPUT LOW except one line at a time which will be set to OUTPUT HIGH.
 

Thread Starter

tomportland

Joined Dec 31, 2011
18
Great! Thanks a lot for the help!

Tom

Yes, that is ok too. The previous circuit was meant for TTL gates. With CMOS gates or MCU inputs with much lower input currents you can use POSITIVE LOGIC.




As you said, all outputs will OUTPUT LOW except one line at a time which will be set to OUTPUT HIGH.
 

Thread Starter

tomportland

Joined Dec 31, 2011
18
Got it, I like your idea!

The only concern I have is that you will see the copper rings on the board that doesn't look very neat. Also, it requires the player to put the piece on a perfect position in each square to make sure the center conductor of the piece touches the center conductor of the square and the outer conductor of the piece touches the outer conductor of the square. Is that right?

Tom

Picture a bullseye, you have a dot and a ring.

On the bottom of each chess piece is a central conductor and an outer conductor, placed so that if spun quickly, it too would appear like a dot and a ring as well. The reason for this is so the chess piece can be pointing any direction and still work.

The trick to it is to have a resistor and diode inside each chess piece. The two would be in series, and the contacts would be on the center pad and at the distance of the concentric ring.

The board would have a copper (or gold) bunch of concentric rings inset onto it for conductors/sensors

In white pieces, the diodes point one direction

In black pieces, the diodes point the other direction.

Then, instead of one sweep for a read, with all being outputs, you do one sweep with rows as outputs and columns as inputs, giving you the white piece locations.
The next sweep, the columns are the outputs, and rows are in the input state, which gives you the black pieces.

If you really wanted to get fancy, you'd put the output state high (+5V) and measure the current drop across each resistor (ADC read instead of digital) to find out which piece it is (pawn through King), using an extra resistor to ground for each ADC pin, and a different valued resistor for the 6 different pieces.
 

thatoneguy

Joined Feb 19, 2009
6,359
Got it, I like your idea!

The only concern I have is that you will see the copper rings on the board that doesn't look very neat. Also, it requires the player to put the piece on a perfect position in each square to make sure the center conductor of the piece touches the center conductor of the square and the outer conductor of the piece touches the outer conductor of the square. Is that right?

Tom
Not if you also put a magnet in each chess piece. As well as under the center of each board, they'd snap down nicely . NOT a "SuperMagnet" that would yank the piece from your fingers, but one that would simply center the piece once set.

The copper could be gold plated to look like an expensive addition, or you could use polished aluminium on white squares and a black conductive ring on dark squares, like partially anodized polished aluminum.

The circular ring would allow the piece to be facing in any direction and still have contact.
 

Thread Starter

tomportland

Joined Dec 31, 2011
18
Wow, very nice and detailed suggestion. I really appreciate it!

Again, Happy New Year to all of you!

Tom

Not if you also put a magnet in each chess piece. As well as under the center of each board, they'd snap down nicely . NOT a "SuperMagnet" that would yank the piece from your fingers, but one that would simply center the piece once set.

The copper could be gold plated to look like an expensive addition, or you could use polished aluminium on white squares and a black conductive ring on dark squares, like partially anodized polished aluminum.

The circular ring would allow the piece to be facing in any direction and still have contact.
 

MMcLaren

Joined Feb 14, 2010
853
Is there such a thing as a Hall effect switch that could be mounted under the board which would detect a magnet in each Chess piece?
 

MrChips

Joined Oct 2, 2009
23,095
Hall Effect key switches were commonly used on high quality and high reliability Micro Switch, IBM and Apple keyboards back in the 1970's. These were replaced by the lower cost membrane key switches that are in use today.

Hall Effect key switches would be very expensive to implement and difficult to multiplex.
 
Top