I would like to control some microcontroller¹ code based on the status of a push-button switch in an external 3.3V circuit via a digital input.

The external circuit has a little under 3.3VDC going through a SPST push-button switch, normally open. I don't have access to the rest of the circuit (it's pretty thoroughly hermetically sealed, and I'd like to avoid destroying the device), but I can splice into the switch line, either parallel across the switch or in series with the switch.

UPDATE: voltage measurements of the switch:

  • across open switch: 3.15V (±0.01V)
  • common ground to open switch, + side: 3.25V (±0.01V)
  • common ground to open switch, - side: -0.01V (seems to vary between -7mV to -11mV depending on what the microcontroller is doing)
  • common ground to either side of closed switch: 3.25V (±0.01V)

The switch can't be hooked in series directly to a digital input, as there seems to be too much resistance in the microcontroller for the switch to complete the external circuit. A 330Ω resistor in series with the switch seems not to interfere with the operation of the external circuit, though. I should also note that the external circuit and my circuity will in most configurations share a common ground.

I have managed to get the code to respond as I want with the switch wired in parallel to an analog input (with a resistor in series). However, I would like to leverage interrupts (instead of looping to read the analog input), so would like to use a digital input instead.

My initial approach to changing the level of the digital input based on the external circuit's switch has been via a transistor switch. I have attempted both an NPN- and a PNP-based switch, but haven't managed to get the resistor values right to even completely switch an LED on and off with the external circuit (most recent attempts have been in series with the switch, but I've tried in parallel too).

If a transistor switch is indeed a good approach, I would like guidance in how to get this right. Is an NPN or PNP switch better (my guess is that NPN is going to be more problematic, given that they share a common ground already?)? How should I go about calculating the resistor values to get the output right for proper 0/1 levels in a microcontroller digital input, and also allow the external circuit to respond properly when the switch is closed? Should I use the negative or positive side of the external circuit's switch in controlling the transistor switch?

¹ I'm currently working with an ATmega328P board, but I plan to switch to an ATmega32U4 board as I continue development in order to use a few of the extra features available with that board.

  • 2
    \$\begingroup\$ I'm assuming, since you say you can put something in series with the switch or in parallel to it, that you have access to both ends of the switch. Can you take voltage measurements of both ends for us? That would be a measurement for each end when the switch is open and one measurement for either end (but not both) when the switch is closed? These should be measurements with respect to the shared ground voltage. \$\endgroup\$
    – jonk
    Commented Feb 28, 2020 at 2:56
  • 1
    \$\begingroup\$ Sounds good. That will go a long way in helping you. \$\endgroup\$
    – jonk
    Commented Feb 28, 2020 at 3:27
  • 1
    \$\begingroup\$ I was worrying about below a few microseconds, is all. Below a millisecond is a piece of cake. Is there a reason you'd rather BJTs than an IC? Or would a simple IC be better for you? \$\endgroup\$
    – jonk
    Commented Feb 28, 2020 at 23:46
  • 1
    \$\begingroup\$ To start: a, b, c, d, e, f, g, h, i, j. \$\endgroup\$
    – jonk
    Commented Feb 29, 2020 at 0:08
  • 1
    \$\begingroup\$ Of course, you will need a custom design. But those give you four or five basic topologies to consider, broadly speaking. \$\endgroup\$
    – jonk
    Commented Feb 29, 2020 at 0:09

1 Answer 1


First off, let's make some assumptions about the internal schematic that is unknown to you. There's a switch, given. And you've measured some voltages, with switch closed and open. These suggest to me something like this:


simulate this circuit – Schematic created using CircuitLab

In other words, it sounds as though there is a pull-down resistor and that one side of the switch is tied to a nearly ideal \$+3.25\:\text{V}\$ voltage source. The reason I suggested \$33\:\text{k}\Omega\$ for the internal pull-down is because of your measurements. (Which may not be perfectly accurate, but probably sufficient.) You measured the voltage across the switch, when open, as \$3.15\:\text{V}\$. So, assuming that your voltmeter presents \$1\:\text{M}\Omega\$ impedance (a common assumption, lacking better information) this just means solving \$3.25\cdot\frac{x}{x+1\:\text{M}\Omega}=3.15\:\text{V}\$. And I get \$x\approx 31.7\:\text{k}\Omega\$. The pull-down might be \$10\:\text{k}\$, though. I don't know much about your meter. But the basic idea remains the same. It looks like there is a low-side pull-down resistor with the pushbutton attached at the high-side. The middle point is probably used internally (shown by the arrow) for whatever purposes -- which may be to simply power the internal circuit.

Now, before I move on, it's possible there isn't a pull-down resistor at all. It may be that this is the resistance presented by the circuit that the arrow points at, when the series resistance of your voltmeter is inserted between the power supply and the circuit. And if there is a pull-down resistor, then it in parallel with the circuit, presents something on the order of what I'm showing above. I just want to make sure that you don't assume I'm right. This is just a "model" that I'm using for illustration regarding your measurements.

The fact that a \$330\:\Omega\$ resistor can be added in series with SW just means that the circuit pointed at by the arrow probably doesn't draw more than \$1\:\text{mA}\$. You could play around with lots of different values. But I don't think it is necessary.

The main thing is this:


simulate this circuit

The giant question-mark will now be filled in for you:


simulate this circuit

That should use just about any junk box NPN pair. They do NOT need to be matched for \$\beta\$ or \$I_\text{SAT}\$ or much of anything else and it should work well over a wide ambient temperature range, as well.

The worst-case I/O pin leakage (configured as an input) for the ATmega32U4 is \$1\:\mu\text{A}\$. So the \$39\:\text{k}\Omega\$ resistors I've used shouldn't be affected by more than about \$40\:\text{mV}\$ due to I/O pin leakage. And I don't see a problem with that.

The circuit will work equally well on \$3.3\:\text{V}\$ and \$5.0\:\text{V}\$ power supply rails from the ATmega side. The I/O pin to the MCU from this circuit will simply use the given rail voltage, so the I/O voltage levels will match the requirements for the MCU I/O pin.

The circuit has substantial hysteresis, as well. Over temperature and mismatched BJTs, it should be at least a band that is \$200\:\text{mV}\$ wide -- but probably more than that by a fair margin.

There is an RC filter from the ATmega power supply (\$C_1\$ and \$R_7\$) to help avoid noise from the ATmega getting into the circuit. There's also an added capacitor (\$C_2\$) from the switch side to help moderate noise from that side, as well.

There's plenty of hysteresis in the circuit and so a lot of room for adjusting the resistor values (with the cost of somewhat less margin vs part variations.) So don't worry a lot about finding exact values.

The basic idea of the circuit isn't complex. If you only look at \$Q_1\$, \$Q_2\$, and \$R_3\$-\$R_6\$, you can see that the circuit looks "balanced."

But notice that if \$Q_1\$ is assumed to be on then \$Q_1\$'s collector pulls down on \$R_4\$ causing \$Q_2\$ to be off. With \$Q_2\$ off, it's collector is "open" and so \$R_3\$ and \$R_5\$ are free to supply base current to \$Q_1\$ so that it is, in fact, **on*. In other words, it's a stable assumption.

But also notice that the opposite is also true, if you assumed that \$Q_2\$ is on. Then that also would be a stable assumption for similar reasoning.

When you have a circuit like that, you don't know which way it will go.

Now, I added \$R_1\$ (which has another purpose I'll talk about later) and \$C_2\$ to one side. This means that upon power-up, \$Q_2\$ is pretty much guaranteed to be off and so the circuit will come up from power-on with \$Q_1\$ on and \$Q_2\$ off. Since the output is taken from \$Q_1\$'s collector, and since \$Q_1\$ powers up as on, the output to the I/O pin should be "LOW" and read as a "0". That's the case without anything else to disturb the circuit.

Then I added \$R_2\$ to bridge between this circuit and your other circuit's switch. When the other circuit powers up (its switch is turned on), then there will be about a \$2500\:\frac{\text{V}}{\text{s}}\$ rise in voltage on capacitor \$C_2\$ as current leaks in through \$R_2\$. So it doesn't take very long until \$Q_2\$'s base rises up enough to start turning it on. The current through \$R_2\$ will start out close to \$33\:\mu\text{A}\$ but as some current feeds the base of \$Q_2\$ and drains through \$R_1\$, it will droop down perhaps as low as \$25\:\mu\text{A}\$. Half that will be sunk through \$R_1\$, but enough remains to activate \$Q_2\$. And as \$Q_2\$ activates, it's collector will sink \$Q_1\$'s base supply via \$R_5\$. Since, at most, \$R_5\$ can only supply about \$120\:\mu\text{A}\$ and since even fully saturated and a \$\beta\approx 10\$ the base of \$Q_2\$ needs \$\le 12\:\mu\text{A}\$. And that is easily supplied. Not only that, as soon as \$Q_1\$ is pulled off by this process, \$R_6\$ and \$R_4\$ start supplying additional base current into \$Q_2\$ to snap it into the on state. And that's where it stays... so long as the external circuit continues to supply some added current.

Once the external device is turned off, \$R_2\$ no longer supplies any current. It might even withdraw some. But that depends on your external device. Let's assume no current via \$R_2\$. When that occurs, and here is the other purpose of \$R_1\$, \$R_1\$ will start to drain \$C_2\$ and will sink enough current to pull \$Q_2\$ off. So this is the other purpose of \$R_2\$. It's needed to allow the circuit to return to its default state.

Working its value out is important. Too small of a value and it will hold \$Q_2\$ off no matter what happens with the external circuit. Too large of a value and it can't sink enough current for the circuit to work properly and reset itself. So the value of \$R_2\$ is important -- it's value determines the centering position of the hysteresis -- and since there are transistor part variations and ambient temperature details to be worried about you don't want to mess with its value too much.

However, you can narrow the hysteresis by increasing the values of \$R_3\$ and \$R_4\$. A more narrow hysteresis will provide less noise rejection but it allows more room for change with \$R_2\$, too.

With the circuit above, run at temperatures below room temperature and up to \$55^\circ\text{C}\$ and over part variations of \$\beta\$ from 100 to 300 and a factor of 5 variation in saturation currents for the BJTs, the following hysteresis chart is simulated as:

enter image description here

(In the simulation, \$R_7\$ has the role of \$R_2\$ in the schematic shown above.)

This chart suggests that it's fairly safe to use junk box parts. And if you increased \$R_3\$ and \$R_4\$ to \$560\:\text{k}\Omega\$ the lower portion of the hysteresis band shifts from centered around \$6\:\mu\text{A}\$ to being centered around \$9\:\mu\text{A}\$. Plenty of hysteresis, still.

  • 1
    \$\begingroup\$ @JonathanW. \$R_2\$ really isolates the circuit and should be completely unnoticeable by the circuit whose switch position you are monitoring. And it draws only a few microamps, at most, from the external device's supply. So it should be quite safe to use. Be careful, though. Your finger can probably trigger the input one way or the other. In fact, you may want to test it by putting one finger of one hand on your MCU VCC and a finger from the other hand on one end of \$R_2\$. And then change the MCU VCC hand to touch ground. If the circuit works, you'll see. If not, you'll also see. \$\endgroup\$
    – jonk
    Commented Feb 29, 2020 at 2:55
  • 1
    \$\begingroup\$ @JonathanW. Note that a voltmeter will have resistance that is near the circuit's output resistance. So you may need to buffer the output if you plan on using a voltmeter to measure it. Or measure between the output and Vcc, rather than output and ground, perhaps. You could almost use this to detect wet surfaces, I think. (Or just write some software to monitor the I/O pin and light up an LED, appropriately.) \$\endgroup\$
    – jonk
    Commented Feb 29, 2020 at 3:05
  • 1
    \$\begingroup\$ Aha, I got it! The transistors were reversed (there's conflicting information out there on the orientation of PN2222s...). Now it works beautifully! \$\endgroup\$ Commented Feb 29, 2020 at 20:43
  • 1
    \$\begingroup\$ @JonathanW. I'll also give some other possibilities a thought in the meantime, as well. \$\endgroup\$
    – jonk
    Commented Mar 16, 2020 at 4:16
  • 1
    \$\begingroup\$ @JonathanW. Well, the first one was "pushing it" a bit. I should have used lower impedance and saved you the trouble. All I did with the last one was to make it "more reasonable" and less edgy. I'l glad to hear it's working fine -- and yes, 330k would be just fine, too. I actually had calculated out 330k as the better value and decided to be "creative" and make everything 39.... So you actually read my mind, I guess! I'm going to delete the earlier schematic and clean up the answer a bit. May as well, to avoid confusion. \$\endgroup\$
    – jonk
    Commented Mar 17, 2020 at 1:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.