Shop OBEX P1 Docs P2 Docs Learn Events
Read 8 switches w/ least pins — Parallax Forums

Read 8 switches w/ least pins

As part of a weather recording project, the task at hand is to read 8 reed switches (1 or 2 will be closed at a time) to determine the cardinal or in-between wind direction. I do not have many pins left so 'if ina[ ]' statements will not work.

I've slightly considered using a 8 channel ADC or a multiplexer IC but thought that a ? here on the forum might lead to other approaches and probably the best answer.

So the ? is, How can I read 8 inputs w/ the least prop pins used. Speed is not an issue and the input will only be on or off. The propeller will display or save the direction.

Thanks
Aaron
«1

Comments

  • Speed is not an issue?
    So something like PCF8574(A) may help.
    Other vendors have similar chips with other names...
  • I do have a MCP 23017 that I've not used yet
  • Charlieplexing, maybe? By adding a diode to each line, you can test up to 12 switches with 4 pins (3 pins can do only 6 switches). Once wired, you set one pin to output and you read the other three pins, then repeat for the other three combinations.
  • Very easy with PCF8574 for 8 i/o, or PCF8575 for 16 i/o. Recently started using MAX7311 which has extra features. 16 i/o.

    https://www.maximintegrated.com/en/products/interface/controllers-expanders/MAX7311.html

    All these will connect to your existing i2c pins. All will run on 3.3v.
  • Seairth wrote: »
    Charlieplexing, maybe? By adding a diode to each line, you can test up to 12 switches with 4 pins (3 pins can do only 6 switches). Once wired, you set one pin to output and you read the other three pins, then repeat for the other three combinations.

    I've heard the word before but haven't tried it. Probably a good time!

    Never fails that a question asked teaches me way more than the simple answer. THIS is a great forum!!!

  • kwinnkwinn Posts: 8,697
    Three pins with a '165 parallel to serial shift register chip, and it can be expanded if needed without additional pins.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2015-12-04 15:26
    A shift register will work, but requires clocking. Diodes will work, but require building from scratch.

    What you want is a chip that will take 8 high low inputs and convert it to a 3 bit binary code. One chip does it all without clocking for serial input and then decoding the serial input. You just poll the three i/o pins for a value.

    https://en.wikipedia.org/wiki/List_of_7400_series_integrated_circuits

    Perhaps a 74148 or 8 Line to 3 Line priority encoder. Try to locate a PDF - if may include the 10 Line to 4 Line priority encoder as well. There are 5VDC and 3.3VDC verison of this logic.
  • jmgjmg Posts: 15,173
    AGCB wrote: »
    As part of a weather recording project, the task at hand is to read 8 reed switches (1 or 2 will be closed at a time)

    That last detail matters in choosing a reading scheme.
    Priority encoders are simple and static, but they only decode single-bit, and so miss the extra information two reeds closed can give.
  • I suppose you could also use the counters to do an ADC (which requires only 2 pins). To make this work, you would need 8 series resistors, where each resistor is twice the value of the one before it. Then, wire each reed switch in parallel with each resistor. Because of the power-of-two relationship of the resistors, you would theoretically get 256 unique voltage levels. However, since only one or two switches are closed at a time, you only really ever encounter 16 possible voltages. You could probably even make those 16 level more distinct by interleaving the reed switches (i.e. R0 has S0, R1 has S4, R2 has S1, R3 has S5, etc.). In other words, don't put adjacent reed switches across adjacent resistors.

    I've never tried this, mind you. But it seems like it would work...

    (note: to see how to do ADC with the Propeller counters, take a look at https://www.parallax.com/sites/default/files/downloads/AN001-P8X32ACounters-v2.0.pdf)
  • Since reading a similar suggestion on the forum, I have been considering this Picaxe 20M2 that I've had lying around for Prop I/O expansion.
  • YanomaniYanomani Posts: 1,524
    edited 2015-12-04 22:08
    Hi AGCB

    If you can ensure the following conditions, then two pins, eight diodes and a 4017 Johnson counter will do the job.

    1 - at any given moment, at least one of the reed switches will be closed (only for logic validation).

    2 - no more than two consecutive reed switches will be closed at the same time (a cardinal and any one of the in-betweens that surround it)

    Assembly:

    At the reed switches, do the following:

    - solder each diode cathode to one side of the reed switches.

    - solder the eight other sides of the reed switches together, then connect that junction to your input port. A suitable series resistor on that wire could provide extra protection to the input pin.

    *** P.S. I forgot a biasing resistor to ensure logic "0" to the readings, from the input pin of your processor to GND. Sorry!

    At the 4017, do the following:

    - connect VCC (pin 16) and GND (pin 8) to the same power lines that feed your processor. A 0.1uF ceramic, close to pin 16 will provide better noise margins.

    - connect -CE (pin 13) to GND, that will enable the counter to operate properly.

    - connect CP (pin 14) to any available output pin at your processor. That will be your CLOCK (positive edge).

    - connect Q9 (pin 11) to MR (pin 15), this will reset the counter when count 10 is reached (Q9 goes HIGH, for a short period of time, then, almost immediately, goes LOW again).

    - as for Q0 (pin 3) and TC (pin 12), they will be left unconnected.

    - connect the remaining 4017 outputs, from Q1 (pin2) thru Q8 (pin 9) to the anodes of the diodes. I've choose (in my elderly logic mind) the following sequence, Q1 (pin 2) goes to the north anode, Q2 (pin 4) to the northeast anode, ....and Q8 (pin 9) to the northwest anode.

    Operation:

    - switch CP (pin 14) between "0" and "1" (positive logic) until you read "0" at the input pin of your processor, signaling it has reached Q0 active, after being reset by Q9 going high. I believe it will perform way better than 100 nS cycle time at 3.3 Vdc; more at 5 Vdc (if your processor could deal with such voltage level).

    - from that point, each positive clock pulse will scan a reed switch; North, then Northeast, then East, ... and so on, untill you'll get a solid LOW level, at count 10, i. e., MR being activated by Q9.

    Sorry for not providing some schematic drawing at the moment.

    Henrique
  • jmg wrote: »
    AGCB wrote: »
    read 8 reed switches (1 or 2 will be closed at a time)

    That last detail matters in choosing a reading scheme.
    Priority encoders are simple and static, but they only decode single-bit, and so miss the extra information two reeds closed can give.

    In looking at that data sheet I was wondering how 2 adjacent could be read
    Seairth wrote: »
    you would need 8 series resistors, where each resistor is twice the value of the one before it. Then, wire each reed switch in parallel with each resistor

    This I've also read about but not tried it. Guess it's time to learn

    A shift register will work, but>>

    What you want is a chip that will take 8 high low inputs and convert it to a 3 bit binary code.
    7400_series_integrated_circuits

    Perhaps a 74148 or 8 Line to 3 Line priority encoder.

    I checked my parts drawer and found a P-S shift register(74LS165) but nothing else of noted chips

  • jmgjmg Posts: 15,173
    AGCB wrote: »
    Seairth wrote: »
    you would need 8 series resistors, where each resistor is twice the value of the one before it. Then, wire each reed switch in parallel with each resistor

    This I've also read about but not tried it. Guess it's time to learn
    A variant on a common-leg resistor button encoder, is to use 2 wires and allocate 4 switches to each.
    That allows all equal value R's to be used and the tolerances are much more relaxed.
    A full 8R:256 encode really needs a calibrate step, (as it resolves to 0.4%), whilst a simpler interleaved 4R + 4R only needs resolve to 33% & can catch 2-on.

    You can also terminate with (eg) R+R to give a connection-confirm sense.

  • SeairthSeairth Posts: 2,474
    edited 2015-12-05 00:46
    jmg wrote: »
    AGCB wrote: »
    Seairth wrote: »
    you would need 8 series resistors, where each resistor is twice the value of the one before it. Then, wire each reed switch in parallel with each resistor

    This I've also read about but not tried it. Guess it's time to learn
    A variant on a common-leg resistor button encoder, is to use 2 wires and allocate 4 switches to each.
    That allows all equal value R's to be used and the tolerances are much more relaxed.
    A full 8R:256 encode really needs a calibrate step, (as it resolves to 0.4%), whilst a simpler interleaved 4R + 4R only needs resolve to 33% & can catch 2-on.

    You can also terminate with (eg) R+R to give a connection-confirm sense.

    I'm not quite following you.

    Edit: also, there are really only 16 possible combinations, so if you can distribute them properly, they would resolve to 6%, which isn't too tight.

  • jmgjmg Posts: 15,173
    Seairth wrote: »
    I'm not quite following you.
    Which part - the two arms, with 4 switches each seems clear enough ?
    Seairth wrote: »
    Edit: also, there are really only 16 possible combinations, so if you can distribute them properly, they would resolve to 6%, which isn't too tight.
    True, but even 6% would need some centering, but maybe with 1% Cap and 0.5% resistors and threshold tracking on Prop, you might avoid per-unit calibrate.
    Values like 1-2-3-4 on one arm, and 7.5,8.5,9.5,16 on the other seem to give ~ 6% banding. based on a simple any-2-of-4

  • jmg wrote: »
    Seairth wrote: »
    I'm not quite following you.
    Which part - the two arms, with 4 switches each seems clear enough ?

    How are the resistors and the 4 switches organized on each pin?
  • @AGCB,

    It occurs to me that if you have 6 pins, you can test all 8 without any additional hardware. Based on your description, it seems that at any given time, at most one odd-numbered switch and one even-numbered switch can be closed. That makes 2 groups of 4 switches, where each group has zero or one switch active. With this, you can use 4 pins for inputs (each input connected to switch N and N+4) and 2 pins for outputs (each pin wired to an odd/even group). By driving one output at a time, you can sample 4 of the switches at a time (odd, then even).
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2015-12-05 03:35
    Why not a continuous RS232 stream? A 555 timer, a 4017 Decade counter, Nine diodes, and one resistor, and you have a continuous 8N1 serial data on one pin you can read whenever you want.

    Note:(add a resistor and capacitor for the 555) ... it is possible to wire a 555 as an oscillator using just one cap and one resistor. See link-->(http://forums.parallax.com/uploads/attachments/41345/82173.jpg)

    RS232-Trick.gif

    Note: The image says "STAMP" ... I put this circuit together more than 20 years ago before the Propeller existed for an Alarm Keypad to send the Pressed keys over a distance without having to dedicate a micro processor at the location of the keypad.
  • kwinnkwinn Posts: 8,697
    You could also get away with using only 2 lines on the '165 if you added an RC circuit on the load pin and connected the clock directly to the serial shift clock and through the resistor to the load pin. Loading the parallel data would be done with a long low pulse, clocking the serial data out with a short low pulse.
  • jmgjmg Posts: 15,173
    Seairth wrote: »
    How are the resistors and the 4 switches organized on each pin?

    In my 2 path case, there are two ADCs used on the Prop, so you build 2 independent 4 switch networks.
    Entirely passive at the head-reader, and uses same pin count as Digital schemes, but with relaxed tolerance requirements.

    Those are simple 4 equal R-R-R-R taps, and I'd add a terminator so no-switch gives a connected confirmation value. 6R keeps the margins at above 33%.

  • jmgjmg Posts: 15,173
    kwinn wrote: »
    You could also get away with using only 2 lines on the '165 if you added an RC circuit on the load pin and connected the clock directly to the serial shift clock and through the resistor to the load pin. Loading the parallel data would be done with a long low pulse, clocking the serial data out with a short low pulse.
    Those tricks works best on parts with schmitt trigger control pins.


  • jmgjmg Posts: 15,173
    Seairth wrote: »
    @AGCB,
    It occurs to me that if you have 6 pins,...

    Sure that works, but the OP did say "I do not have many pins left.. "
    it is also not clear how far the wires run from the head unit to the Processor.

    The OP has not mentioned Wind Speed, but you could modulate wind speed into the Reed signal, and use the average for reed-determine and the ripple as Wind speed. No additional wires.
  • YanomaniYanomani Posts: 1,524
    edited 2015-12-05 05:19
    Image failed to upload properly!
  • YanomaniYanomani Posts: 1,524
    edited 2015-12-05 05:21
    Hi Beau Schwabe

    After some rethinking (during a trip to bring my wife back home, because she was lecturing at the local university) and with the help of a freeware schematics tool (thanks Scheme-it), I've drawn the circuit described in my earlier post, with a little tweak, and surprisingly (because I had never saw yours, nor had any knowledge of its existence) they are very similar.

    The main diference, when compared to the earlier one, is on the fact that now I've used only one processor pin, as an I/O, both to clock the Johnson counter and to read back the resulting reed switch scan.

    Naturaly, it don't needs the 555 and associated components, but depends on the proximity of the processor and some coding efforts.

    Henrique

    Windirect-Final.png
    737 x 459 - 22K
  • jmgjmg Posts: 15,173
    Yanomani wrote: »
    The main diference, when compared to the earlier one, is on the fact that now I've used only one processor pin, as an I/O, both to clock the Johnson counter and to read back the resulting reed switch scan.
    I think this circuit has frame issues - ie with just one reed closed, you get one pulse every 10 clocks, but have no idea which of the 8 it is.
    It would need Q0.Q9 feed to give a 2 wide reference, and then you can work out which the other 8 positions are.
    ( Beau's circuit used the RS232 START bit as the frame align.)

  • The amount of information returned is almost overwhelming. At least 9 different ways of doing this have been presented.

    THANK YOU!

    A little more background on the project is in order. So far this is built on a Quickstart breadboard and these modules are included in the main weather program. Each sensor is read and data saved to Winbond flash memory with time (date, hour, minute, second).

    1 Relative humidity from HTU21D
    2 Temperature from DS18B20 (could also come from BMP180)
    3 Sea level pressure from BMP180

    Not yet included in main weather program but tested separately are

    1 Wind speed from JL-FS2 anemometer (some calibration is still needed)
    2 Lightning information from AS3935

    Still to write are

    1 Wind direction
    2 Time of maximum wind speed in each day
    3 time of minimum and maximum temperature in each day

    I also have a separate program to read Winbond memory and display hourly measurements on TV or PST for last 24 hours. (I'll be able to print the PST screen)

    For this weather program I'd like to keep everything as simple as possible both hardware and software wise but I will experiment with the different ways presented in this discussion thread for my learning experience.

    The main program already includes both I2C and SPI (ARIBA's method) so using one of these I believe saves duplication and pins.

    Thanks again and as I've said many times before "I always learn way more than asked for"

    Aaron

  • I2C I/O expander such as the MCP23017 using P28,P29 which is already connected to the EEPROM = zero I/O = absolute "least pins"
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2015-12-05 13:08
    Peter Jakacki once again demonstrates being spot on as a thinking engineer.

    I just want to add a bit of contruction consideration.
    Reed switches are notoriously bouncy. That may create problems with getting good readings of wind direction.

    So consider using 8 Hall Effect Sensors that have absolutely no bounce. The only added problems if that you have to provide power to them and they are OPEN Collector configuration. So they all are pulled HIGH went not sensing, and pulled LOW when a position is indicated.

    There may be a tenth choice the the open collector outputs offer. I just don't know for sure.
  • kwinnkwinn Posts: 8,697
    jmg wrote: »
    kwinn wrote: »
    You could also get away with using only 2 lines on the '165 if you added an RC circuit on the load pin and connected the clock directly to the serial shift clock and through the resistor to the load pin. Loading the parallel data would be done with a long low pulse, clocking the serial data out with a short low pulse.
    Those tricks works best on parts with schmitt trigger control pins.


    That's very true, but it works quite well on the '165 since it makes no difference if the switch data is loaded multiple times during the transition from high to low and back to high.
  • I2C I/O expander such as the MCP23017 using P28,P29 which is already connected to the EEPROM = zero I/O = absolute "least pins"
    Since I have one of these chips, it seems to be the logical choice.
    Reed switches are notoriously bouncy. That may create problems with getting good readings of wind direction.

    So because the switches go to the MCP 1st, a debounce routine in the prop software won't work (or will it). If I use a routine in the Prop code that looks for a stable output from the MCP before reading it as data it is effectively debounced. And since this particular data will change relatively slow, even a time delay after an initial change before acceptance as true data might work. I may be thinking and writing over my head but sounds good to me.

    Or is there a different way to debounce the inputs to the MCP? An RC network???
Sign In or Register to comment.