Thursday, 5 April 2018

How To Digitally Push a Button

If you ever need to interface to an existing piece of hardware, for example because you're modifying or otherwise tampering with something, then sooner or later you'll find yourself wanting to simulate pressing a button with a digital signal.

The foolproof way to do this is to use a small relay, which is literally a switch closed by an electrical signal. However, there's another way that's cheaper and simpler. If the switch you're wanting to "press" connects an input to ground (which is very common), then you're in luck. You can do it with a single NPN BJT transistor (like a 2N3904 or 2N2222) and a resistor.

I have used this method many times in the past, and most recently used it when I modified a cheap USB mouse so that I could click the buttons with an Arduino. When mixing and matching different hardware, sooner or later you'll want to just simulate a button press and this way is smaller and cheaper than using relays and isn't fussy about voltage levels.

The transistor is used in saturation mode, meaning that it works much like a switch. When a digital signal is applied (through the resistor) to the base of the NPN, the collector is essentially connected to ground. By connecting the collector to the switch's input we can simulate the button being pressed, with no need to replace the actual switch.