Read 8 switches w/ least pins
AGCB
Posts: 327
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
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
Comments
So something like PCF8574(A) may help.
Other vendors have similar chips with other names...
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.
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!!!
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.
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'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)
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
In looking at that data sheet I was wondering how 2 adjacent could be read
This I've also read about but not tried it. Guess it's time to learn
I checked my parts drawer and found a P-S shift register(74LS165) but nothing else of noted chips
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.
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
How are the resistors and the 4 switches organized on each pin?
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).
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)
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.
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%.
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.
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
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.)
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
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.
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.
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???