Help: How to read switch state

Thread Starter

tomportland

Joined Dec 31, 2011
18
I'm planning to create an input device for PC (e.g. connected via USB cable) to read the state (on/off) of 64 small switches (e.g. reed switches). Do I need a micro-controller to do this job? If yes, any recommendation for a particular controller?

I have been a software engineer for more than 10 years but never done anything with micro-controller. If you know any kits/books for beginners with programming background, please do let me know. Thanks a lot!

Tom
 

M11

Joined Dec 27, 2010
18
Hi,

I have programmed a PIC (PIC18F4550 to be specific) microcontroller to transmit and recieve data to my PC using USB. It was not straight forward to me (I am just a beginner), but I got it to work at the end.
I am pretty sure that you can read the state of the switches using a microcontroller, but don't know if there is another way.

There is a very good book for beginner PIC programers that helped me a lot (here). The problem is that the examples in the book are written using mikroC compiler and it is not free. The free version has a limit program size (4Kbyte I think).

I hope this helped
 

Thread Starter

tomportland

Joined Dec 31, 2011
18
Thanks for the reply!

I am thinking to use the following microcontroller board. It has 54 digital input/output pins and 16 analog inputs. I need to read totally 64 switches but I think I can utilize the 10 of the 16 analog inputs as well.

Arduino Mega 2560 ($65)
http://arduino.cc/en/Main/ArduinoBoardMega2560

Is this the right way to go? Is there any cheaper and simpler solution? I hope there is because what I need is just to read 64 switches' state (on/off).

Any input is welcome!

Tom
 

Thread Starter

tomportland

Joined Dec 31, 2011
18
Thanks a lot for the quick reply!

I am thinking to use Arduino Mega 2560 which has 54 digital inputs and 16 analog inputs. I need to read totally 64 switches and I guess I could use 10 of the 16 analog inputs as well.

Is this the right way to go? It costs about $65 and I think there must be a cheaper and simpler way to do the job because all I need is to read 64 switches' state (on/off).

Any input is welcome and thanks again for the help!

Tom
 

SPQR

Joined Nov 4, 2011
379
The Arduino is a very nice platform for beginners - you can get it up and running rapidly - your longest time will be spent hooking up the reed switches.
The programming is REAL easy.

But I wonder if more information might help us.
Can you give us an idea of what you are doing with the switches?

There are also input cards that you can slide into your PC to collect information.
 

MrChips

Joined Oct 2, 2009
30,795
The general technique for detecting a large number of key-switches such as a computer keyboard is to arrange the keys in the form of a matrix and to use multiplexing techniques. A problem occurs if two or more switches are pressed simultaneously. If you are only interested in the static state of the switches then this is the simplest solution. You would only need 8 input pins and 8 output pins (two ports in total) for 64 switches.

Another technique is to use parallel input/serial output latches/shift registers. Eight 8-bit devices will give you the desired 64 inputs. The data can be shifted serially using a single bit input port of a MCU.

Either technique is best implemented using a micro-controller unit (MCU) interfaced to the PC.
 
Last edited:

Thread Starter

tomportland

Joined Dec 31, 2011
18
Thank you all for the inputs!

I am trying to create a electronic chess board - under each square of the board I will put a reed switch and under each piece I will put a magnet. When a piece was moved from one square to another, a new switch is turned on and the old switch will be turned off.

The chess board is connected to a PC (e.g. via USB cable) and the software running on the PC will read the state of all the switches from time to time to detect the moves and record the moves accordingly.

Tom
 

Thread Starter

tomportland

Joined Dec 31, 2011
18
The LabJack Looks great, but it only has ~20 digital inputs and ~16 analog inputs. I need to read at least 64 switches' state. Also, it cost >$100 comparing to $65 of the Arduino MCU I mentioned. Thanks anyway for the information.

Tom

I believe you are looking for something like this:

USB LabJack

You'd need more than one, but you have lots of USB ports.

No programming required, windows software included.
 

thatoneguy

Joined Feb 19, 2009
6,359
It does have two outputs, which could be used to drive shift registers that would give you the full 64 I/O you are looking for.

Same goes for Arduino, but you need to write the arduino software and windows software, whereas the labjack already has a windows DLL that can be used for direct access in programs.
 

Thread Starter

tomportland

Joined Dec 31, 2011
18
>> You would only need 8 input pins and 8 output pins (two ports in total) for 64 switches.

I don't quite understand this comment. Can you explain it with a little more details? Thanks!

Tom

The general technique for detecting a large number of key-switches such as a computer keyboard is to arrange the keys in the form of a matrix and to use multiplexing techniques. A problem occurs if two or more switches are pressed simultaneously. If you are only interested in the static state of the switches then this is the simplest solution. You would only need 8 input pins and 8 output pins (two ports in total) for 64 switches.

Another technique is to use parallel input/serial output latches/shift registers. Eight 8-bit devices will give you the desired 64 inputs. The data can be shifted serially using a single bit input port of a MCU.

Either technique is best implemented using a micro-controller unit (MCU) interfaced to the PC.
 

MrChips

Joined Oct 2, 2009
30,795
I am trying to create a electronic chess board - under each square of the board I will put a reed switch and under each piece I will put a magnet. When a piece was moved from one square to another, a new switch is turned on and the old switch will be turned off.

The chess board is connected to a PC (e.g. via USB cable) and the software running on the PC will read the state of all the switches from time to time to detect the moves and record the moves accordingly.

Tom
It is always better to come out and state fully the intended application in the first place because it can make a huge difference on the solutions being offered.

My solution would be to use a MCU with two 8-bit ports and USB capability.
 

Thread Starter

tomportland

Joined Dec 31, 2011
18
I c. But from cost perspective, I think the Arduino MCU is a cheaper solution. Am I correct? Thanks again - Tom

It does have two outputs, which could be used to drive shift registers that would give you the full 64 I/O you are looking for.

Same goes for Arduino, but you need to write the arduino software and windows software, whereas the labjack already has a windows DLL that can be used for direct access in programs.
 

thatoneguy

Joined Feb 19, 2009
6,359
If it is an 8x8 square, you don't need 64 switches.

Think rows and columns.

Put voltage on one row, and read the voltages on those 8 columns.
Put voltage on row #2 and read voltage on those columns
repeat through row 8

You now know the state of all 64 positions.

This scan can happen tens of thousands of times per second.
 

Thread Starter

tomportland

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

Although I still don't know how two 8-bit ports can read 64 switches (newbies :)), I think I will figure it out later.

So, do you think Arduino Uno will do the job? It has 14 digital I/O pins and 6 analog input pins. And it costs only ~$30. Or I have to use the Mega 2560 which is ~$65. Thanks again - Tom

http://arduino.cc/en/Main/ArduinoBoardUno

My solution would be to use a MCU with two 8-bit ports and USB capability.
 

Thread Starter

tomportland

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

I still need some time to figure out how to use two 8-bit ports to read 64 switches though (newbie :)).

So is Arduino Uno MCU good enough? It has only 14 digital I/O pins and 6 analog input pins. But the price is just half of the Mega 2560. Thanks again!

Tom

My solution would be to use a MCU with two 8-bit ports and USB capability.
 

Thread Starter

tomportland

Joined Dec 31, 2011
18
I was thinking to put 1 reed switch under each square. If there is no need for 64 switches, how many do you think it needs? And How to arrange those switches under the squares? Thanks a lot! - Tom

If it is an 8x8 square, you don't need 64 switches.

Think rows and columns.

Put voltage on one row, and read the voltages on those 8 columns.
Put voltage on row #2 and read voltage on those columns
repeat through row 8

You now know the state of all 64 positions.

This scan can happen tens of thousands of times per second.
 

MrChips

Joined Oct 2, 2009
30,795
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).
 
Last edited:

Potato Pudding

Joined Jun 11, 2010
688
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.
 
Top