40 SPST switch status to USB

Discussion in 'The Projects Forum' started by coderwalker, Oct 30, 2012.

  1. coderwalker

    Thread Starter New Member

    Oct 30, 2012
    I need to make a device that can check the state of 40 SPST switches and then send this data to a computer via USB.

    The core of this board will be the SX28 micro-controller. I plan on making it check the states of the 40 SPST switches, serialize the data, send the data to a PC via USB specifications for the computer to handle.

    The part throwing me for the biggest loop is handling 40 switches while the SX28 has only 20 ( -1 for USB connection) possible inputs to the registers.

    I come from a Programmer and Server Management background so the solution that first comes to mind is, have 2 slave SX28 chips checking 20 switches each then send their serialized data back to a master SX28 chip that polls the slaves and updates the PC via USB.

    Does anyone have any recommendations or ideas how to pull this off more efficiently?

    Also does anyone know a great tutorial or document explaining how to work with the USB interface and how to process the signal on Linux?

    Thanks in advance!
  2. panic mode

    Senior Member

    Oct 10, 2011
    have you heard of multiplexing?
    you can have 8*5 matrix with 40 switches. if ghosting is an issue and you expect to see more than switch on at a time, add diode in series with each switch.

    other solution is to use PISO registers like 74hc597 and read any number of switches serially in. you would need 5 chips and only some 3 pins on controller
  3. ScottWang


    Aug 23, 2012
    If you use 40 pins to input the sw, then you can use CD4051 x5, 74HC138 x1, and 8 bits of port.
  4. coderwalker

    Thread Starter New Member

    Oct 30, 2012
    Could you elaborate a little more on both options?


    also, ghosting?
  5. Kermit2

    AAC Fanatic!

    Feb 5, 2010
    One way would be to have your micro count to five. This will use three ports.

    The relays would be broken into banks of 8 and 5 other [chips] would stay updated with their own status on those switches.

    Your micro would then read only 8 bits of data for each 'count' from 1 to 5 which it generates, giving you the status of the 40 relays.

    I think this is along the line of what PanicMode was suggesting?
  6. panic mode

    Senior Member

    Oct 10, 2011
    there are number of alternatives, i still don't know what you need.
    if you want to add bunch of inputs and outputs you can use expanders. if you don't like I2C for example and want something possibly simpler, then generic chips will be an interesting option (very simple to implement and program):

    74HC595 is SIPO (Serial In - Parallel Out) as shown here. note that you need only three pins to drive any number of outputs, each chip you add, will give you another 8 outputs. regardless of number of outputs you get, only 3 pins are required from microcontroller (data, clock and latch).

    74HC597 is PISO (parallel in - serial out). the idea is the same, you get bunch of inputs (each chips give you 8 inputs) and you cascade them all you like. then use same shifting method to bring the values of all those inputs into your microcontroller. you can reuse pins used for outputs.

    some people were able to reduce number of needed pins by using clever tidbits like RC delays. here is 7-seg display by Roman Black:

    if you only need bunc on inputs from dry-contact devices (buttons, DIP switches, etc.) you can simply do multiplexing:

    if saving pin count on mcu is goal, you can resort to external chips like 74HC138 or 74HC238. they need 3pins and give you 8 outputs. using additional 5-pins as inputs, you can easily scan 8*5=40 switches using only one port (3+5=8 pins). diodes are only needed when multiple switches can be activated at the same time.
  7. MMcLaren

    Well-Known Member

    Feb 14, 2010
    I beg your pardon... that design is by Mike McLaren...
  8. panic mode

    Senior Member

    Oct 10, 2011
    my apologies, yes i didn't write it correctly.