How to Control a Pushbutton with Arduino Feb 25th 2022 Words: 276

I am surprise to find out that despite of many tutorials on how to control an Arduino with a pushbutton, hardly can I find a tutorial on how to control a pushbutton with an Arduino.

Suppose you have a device with a pushbutton:

Now you want to control this button programmatically, so you hook up the button to a pin on the Arduino:

What’s next is you set the pin to OUTPUT mode, then use digitalWrite(pin, LOW) to close the switch, and digitalWrite(pin, HIGH) to open the switch. Isn’t that simple? Right?

WRONG! Well this solution seems to be working, it has design flaws that can release the magic smoke.

  1. There’s no guarantee that the arduino output voltage is within the operation range of the left side board. For example, you could kill a 3.3V input pin with 5V output from the Arduino. A high output on the pin does not equal to an open switch.

  2. When the Arduino pin is in high state, if the button is pressed, it connects the ouput pin directly to ground, causes a short circuit that can easily damage the Arduino.

The proper method to simulate a open switch is to make the ouput pin as if it is disconnected. This can be achieved with High-Z (high impedance) state of the pin.

In Arduino, the input pin is high impedance by default, so pinMode(pin, INPUT) set the switch open. To simulate the button push, simply use pinMode(pin, OUTPUT), digitalWrite is not required, since the default state of output pin is low.

And of course, two boards must share a common ground.