Auto-sense whether a pin should be ADC or serial-input

wapo54001

Senior Member
I am building a stereo preamplifier and will use a 14M2 for relay-based line level input control.

I would like to use a single pin for the input select control line and somehow give that pin the ability to self-determine whether it is being addressed by serial data or by a mechanical selector switch using the supply voltage and a resistor chain on the switch with the 14M2 pin reading switch position with ADC, or possibly with a button press up/down to change input.

Is there a software-only solution to test and identify the control method in use without using another pin or any other additional components? I do have one extra pin available for manual selection but I'm packing the board and with space constraints I'd like to avoid the resistor and pads required to create a jumper-based solution.

Any insights or suggestions appreciated.
 

rq3

Senior Member
It is possible to apply an internal "weak pullup" to an ADC input pin, and then use the switched resistor chain from the ADC pin to ground to get various ADC readings. The only gotcha is that the weak pullup value can be all over the map, so you'd have to select the values in the resistor chain. appropriately.

I'd expect the weak pullup value to stay consistent within any particular chip, but not between chips.

Just a thought.
 

erco

Senior Member
Maybe limit the ADC values to mid-range (2-4V ). Have the pin READADC a few values to determine if the signal is digital (0 or 5V) or analog (2-4V).
 

wapo54001

Senior Member
Maybe limit the ADC values to mid-range (2-4V ). Have the pin READADC a few values to determine if the signal is digital (0 or 5V) or analog (2-4V).
I like this idea, sounds easy to implement, if there's no chance of confusing the chip. There are four inputs, plus if possible I'd also like to program two more levels for an option to use pushbutton "up" and "down" to move the input that way also so there would ideally be six analog levels if that many can be reliably implemented. The picaxe manual says

Inputs:
An input should be above (0.8 x power supply voltage) to be high, below (0.2 x
power supply voltage) to be low. It is recommended, but not essential, to tie
unused inputs low via a 10k resistor.

That would make the "low" range end at 1.0V and "high" start at 4.0V and I suppose I should leave a buffer, maybe 0.5V, so a 1.5V-to-3.5V ADC range perhaps?
 

AllyCat

Senior Member
Hi,
The only gotcha is that the weak pullup value can be all over the map, .....
In practice, the value of the Weak Pullups is not as poor/pessimistic as the Data Sheet implies (equivalent to a range of about 16k - 200k). Personally, I normally assume a value of about 30k - 35k, but I wouldn't make a design totally dependent on that level of precision.

Similarly, the "inputs" quote in #4 is a very generalised indicator for the "threshold level" of the PIC(axe) digital inputs. Much more detail is available in the relevant "Base PIC" data sheet (section 30).

The WPUs are available on ALL of the 14M2 pins and the ADC on most I/O pins, but if pins B.0 and/or B.1 (Legs 13 and 12) are "available" then the internal "DAC" might be useful. This is effectively a "potentiometer" formed by a string of 32 x 5k (nominally) resistors (adjusted by the DACLEVEL command). The "wiper" can be optionally connected to pin B.0 (normally the Serial/Programming output) and read by the READDAC command (whether connected or not). The "top" (Reference) of the chain is normally connected to the supply rail but can be optionally connected to pin B.1, thus giving a "160k" pulldown on that pin and various capabilities to read and set the Analogue levels there. Pin B.1 is also the HSERIN pin (Idle-High only) if required.

Cheers, Alan.
 

hippy

Ex-Staff (retired)
I would be tempted to configure the pin as ADC and then see what voltages are being read. Providing analogue use will not put the same level signal as serial use will on the pin it should be fairly easy to tell whether an analogue or serial connection.
 

Flenser

Senior Member
self-determine whether it is being addressed by serial data or by a mechanical selector switch using the supply voltage and a resistor chain on the switch
I'm not sure I understand how you plan this circuit to be implemented and so I can't judge if my suggestion applies.

Can you use a mechanical switch with one extra position for the serial connection connected to an ADC pin?
  • When the switch is connected to the serial input the idle line voltage will be 0 or 5V, depending on whether you are using true or inverted logic
  • Arrange your resistor chain so that when the switch is connected to one of the intermediate points in the resistor chain the voltage will be some other voltage in this range.
 

hippy

Ex-Staff (retired)
Arrange your resistor chain so that when the switch is connected to one of the intermediate points in the resistor chain the voltage will be some other voltage in this range.
That's what I was thinking -
Code:
            .------.   -.-   ___         .------.
 SERIAL --->| Ix   |    `---|___|---.--->| Ix   |
            |      |               _|_   |      |
            |      |       Switch |   |  |      |
            |      |       Matrix |___|  |      |
            |      |                |    |      |
 0V --------|      |                `----| 0V   |
            `------'                     `------'
The serial input will likely be 0V or 5V, so if the switch matrix delivers 1V-4V for example you can determine which it is just by reading that voltage. If the switch matrix can produce 0V then defaulting serial to idle high will help there.

It's similar to using a pot with an on and off override -
Code:
-.----.------- V+
.|.   |
|_|   |
 |    |
.|.   `--O
|_|<-----O O--> ADC
 |    .--O/
.|.   |
|_|   |
 |    |
-^----^------- 0V
 

wapo54001

Senior Member
I'm not sure I understand how you plan this circuit to be implemented and so I can't judge if my suggestion applies.

Can you use a mechanical switch with one extra position for the serial connection connected to an ADC pin?
  • When the switch is connected to the serial input the idle line voltage will be 0 or 5V, depending on whether you are using true or inverted logic
  • Arrange your resistor chain so that when the switch is connected to one of the intermediate points in the resistor chain the voltage will be some other voltage in this range.
My goal is to make this sort of universal -- an input selector system that will work with any of the three main selector methods which are serial data, mechanical (rotary) switch selector, and mechanical up/down button selector -- with automatic sensing of the method in use so there is no setup hassle. But I can't see anyone combining methods in one installation.
 

wapo54001

Senior Member
That's what I was thinking -
Code:
            .------.   -.-   ___         .------.
SERIAL --->| Ix   |    `---|___|---.--->| Ix   |
            |      |               _|_   |      |
            |      |       Switch |   |  |      |
            |      |       Matrix |___|  |      |
            |      |                |    |      |
0V --------|      |                `----| 0V   |
            `------'                     `------'
The serial input will likely be 0V or 5V, so if the switch matrix delivers 1V-4V for example you can determine which it is just by reading that voltage. If the switch matrix can produce 0V then defaulting serial to idle high will help there.

It's similar to using a pot with an on and off override -
Code:
-.----.------- V+
.|.   |
|_|   |
|    |
.|.   `--O
|_|<-----O O--> ADC
|    .--O/
.|.   |
|_|   |
|    |
-^----^------- 0V
hippy, I do believe this will work. I have an earlier design based on an 08M2 which I will pull out and start experimenting with.

So, a question: Given the somewhat lower-than-5V voltage of a picaxe logic high, I think the realistic current drawn by the relay is going to be about 18mA for a period of 4ms per switching action and then decreasing after that as the capacitor on the other side of the latching relay charges. Given the short duration of 4ms, is that 2mA cushion between max current surge and the picaxe 20mA limit an adequate safety margin? If yes, I'd go back to using one picaxe pin per relay instead of the two that I am now using.
 

hippy

Ex-Staff (retired)
I would guess it comes down to the in-rush current. PICAXE are fairly robust and can usually withstand over-current for short periods but I wouldn't like to try and characterise it. I'd go for a destructive soak-test with a sacrificial PICAXE and see how long it lasts or just stick with two pins.

You might be able to gain some insight by monitoring the IO and PSU voltages on a scope as there's probably going to be voltage sag as current draw increase.
 

wapo54001

Senior Member
I would guess it comes down to the in-rush current. PICAXE are fairly robust and can usually withstand over-current for short periods but I wouldn't like to try and characterise it. I'd go for a destructive soak-test with a sacrificial PICAXE and see how long it lasts or just stick with two pins.

You might be able to gain some insight by monitoring the IO and PSU voltages on a scope as there's probably going to be voltage sag as current draw increase.
Good idea. I'll set up an on/off routine with enough delay to let the capacitor fully charge/discharge between cycles and run it for a couple of days and see if it's still working at the end. I've got a board with four relays on it so I could test four pins at once. Since the voltage and coil resistance numbers equate to a maximum of 18~19mA I'm thinking that is the maximum surge current and it'll be OK for 4ms at a time. Really close to the limit but will never quite reach it. In real life use the switching will be quite infrequent.
 
Top