Maker Pro
Maker Pro

Interfacing with a switch in an external circuit

cspencer

Aug 4, 2010
11
Joined
Aug 4, 2010
Messages
11
Hi all,

I am trying to figure out how to interface with a switch in an external circuit. Normally I'd connect the pin to +V with a pull-up resistor and connect the switch to ground, so it's logic 1 when the switch is open and logic 0 when closed; however, the circuit I'm trying to connect to has its own 15k resistor, so when the switch is closed the voltage only drops by ~30mV; obviously still logic 1. How exactly am I supposed to interface to this?

This is the circuit I am testing with on my breadboard. Components R3 and to the right are in the external circuit and can't be changed.

oYlBm.png


This is the schematic for the external circuit. Follow the PTT line to ground.

Thanks.
 

cspencer

Aug 4, 2010
11
Joined
Aug 4, 2010
Messages
11
Maybe try changing R1 to around a 200K.
Best I could do was four 62k resistors in series, and the only difference it made was instead of dropping from 4.90V to 4.88V it went from 4.78V to 4.76V..

I don't know if it matters, but I get odd readings from the ohmmeter on the PTT circuit. If it's set to 20k or less I get overload, if it's on 200k I get ~181k, if it's on 2M I get ~741k, and if it's on 20M I get ~3.07M, so the resistance seems to depend on the input voltage for some reason. 5V is applied to the PTT line on the rig it's supposed to connect to.

Edit: Sorry, I'm a bit of an idiot; I wired it up all wrong. Two 62k resistors in series brings the voltage down to ~0.9V which does the trick. I'll use some higher resistance in the final circuit, but this is fine for now.

Can you tell me how you arrived at the 200k value? Perhaps it might help me understand better what's going on; I'm still pretty new to this.

Thanks.
 
Last edited:

Militoy

Aug 24, 2010
180
Joined
Aug 24, 2010
Messages
180
....
Can you tell me how you arrived at the 200k value? Perhaps it might help me understand better what's going on; I'm still pretty new to this.

Thanks.

I just picked a standard resistor value that would drop your final voltage to below 10% of your 5V bus, for a good, solid "low", without being so high of an impedance, that your pullup "high" was too noisy.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
cspenser, in your circuit, with the switch open the voltage on the RA1 pin is 5V.

With the switch closed it is about (15 / (15 + 10) ) * (5 - 0.6) + 0.6 volts (assuming a drop of 0.6 v across the diode -- at low current it will be less). That works out to be about 3.24 volts. That isn't even below Vcc/2.

Militoy's solution is better, the voltage falling to about (15 / (15 + 200) ) * (5 - 0.6) + 0.6, or about 0.9V (possibly a little lower)

The naive solution is to remove R3 and D1 completely. Is there a reason for R3 or D1? If RA1 is ever designated an output, R3 might be there for a reason, otherwise I can't see it.
 

alenco

Aug 25, 2010
3
Joined
Aug 25, 2010
Messages
3
The external circuit cspenser is interfacing with has a fairly stiff pull up to an LED and 560 ohm resistor connected to +8VDC so the diode helps avoid a latchup. Since the external circuit has it's own blocking diode and 15k series diode, it's too much of a good thing.

What's odd is that should still result in more than a 30mv drop. I suspect, like you, the input pin on the PIC chip is one of those that doubles as an output pin when it's not being read as an input and it's set to a '1'. PICs are weird like that. It's been ten years since I messed with them so I may have it wrong for this model PIC.
 

Laplace

Apr 4, 2010
1,252
Joined
Apr 4, 2010
Messages
1,252
According to the PIC datasheet the RA1 pin is a tri-state I/O port which in the floating state acts as a high impedance input port with leakage current of +/- 1 uA and TTL logic levels, i.e., 0.0-0.8 V is logic Lo, 2.4-5.0 V is logic Hi. The first question is how large can R1 be and still pull the input Hi? In this case, R1 = (5.0 - 2.4)/(1 uA) = 2.6 M. The second question is how small can R1 be and still have the input pulled Lo? Or stated another way, how much current can flow through D1 and R3 to generate 0.8 V at the input port? Now if there were no forward voltage drop across D1, then I = (0.8 V)/(15 K) = 53 uA. At that current level we can assume the forward voltage drop across the diode will be 0.5 V, then I = (0.8 - 0.5)/(15 K) = 20 uA. So use a value of 20 uA + port leakage current = 21 uA. In this case, the smallest value of R1 would be R1 = (5.0 - 0.8)/(21 uA) = 200 K. So pick a value for R1 somewhere between 200 K and 2.6 M and it should work. What I would pick is 330 K.
 

Militoy

Aug 24, 2010
180
Joined
Aug 24, 2010
Messages
180
Oops – I didn’t mean to frustrate you with the answer I tossed out, cspencer! Actually, I didn’t go so far as to pick up my calculator, and work out the exact final voltage including diode drop. Most of my designs have a tendency towards “Meatball Engineering” – since I’m the main decision-maker in a high-stress manufacturing environment. In fact, I wouldn’t really consider using the specific diode called out – with a forward drop of 715 mV at only 1 mA. I typically use a schottky diode in this kind of circuit – where you can almost ignore the couple tenths of a volt drop at low current.

Just in case the (really good) explanations above don’t make sense yet – I’ll expand a bit. The circuit is a simple voltage divider between the +5V bus and ground. You can usually ignore the input current to the microcontroller as insignificant. For instance, a PIC micro I use on a robotic vehicle – PIC18F4580 – draws only about 1uA to each input when digital I/O’s are set as inputs.

All I did was add a resistance to your 15K so that the percentage of resistance that the 15K makes up of the total (between +5V and ground) is less than around 10%. Since the voltage “dropped” across each part of the series resistor string will be proportional to the percentage of the total resistance, I made sure the total resistance was more than 150K ohms, so that the voltage across the 15K part would be less than 10% of the total. Bringing the added resistance up to 200K was to compensate for diode drop – but it wasn’t calculated precisely. You’ll find with experience that resistors aren’t typically selected for a precise value – except when used in a divider to set an exact voltage. They’re more often selected to set up a reasonable current in a circuit - maybe a mA or two for low-signal circuits – and selected for standardized values that can be pulled off the shelf.
 

cspencer

Aug 4, 2010
11
Joined
Aug 4, 2010
Messages
11
Thanks for the answers everyone, everything is much clearer now. :)

alenco: The 30mV drop was just because I had wired it up wrong. When I fixed it it dropped by the right amount.
 
Top