Which is the best circuit to use

I have a floating pin issue on D2 which is causing an interrupt to trigger at random times. I have tried using the firmware pull down resistor ( pinMode(TouchPin, INPUT_PULLDOWN); ) but this does not seem to work.

I have also tried 3 wiring options to sort out the problem, of which option 3 is the only one that really works and is stable (no random triggers or triggers when touching the circuit).

I am not an electronics eng, so I am in the dark a bit.

Please could someone with the relevant skills guide me please. The code and the circuits are shown below.

const int LED_OUTPUT = D7;
const int TouchPin = D2;
const int Relay = D0;
bool lighton = false;

void setup() {

    RGB.control (true);
    RGB.control (false);
    pinMode(LED_OUTPUT, OUTPUT);
    pinMode(TouchPin, INPUT_PULLDOWN);
    pinMode(Relay, OUTPUT);

    digitalWrite(LED_OUTPUT, LOW);
    delay (60000);
    attachInterrupt (TouchPin, switchLight, RISING);

void loop () {

    if ((Time.hour() == 19 && Time.minute() == 00 && Time.second() < 5) && ! lighton) {
        delay (5000);
    delay (1000);

void switchLight () {
    static volatile unsigned long lastInterrupt = 0;
    unsigned long currentInterrupt = millis();
    if (currentInterrupt - lastInterrupt > 1000) {
        lastInterrupt = currentInterrupt;
        if (lighton) digitalWrite (Relay, LOW);
        if (! lighton) digitalWrite (Relay, HIGH);
        lighton = not lighton;
        digitalWrite(LED_OUTPUT, lighton);

The circuit options are:

I prefer to make the switch connect to GND. This eliminates having to run power to the switches, and eliminates any confusion when you have multiple voltage levels in your system (3.3V and 5V, for example).

In this little circuit, for D3 (top switch) has an external pull-up resistor so you use:

pinMode(D3, INPUT);

For D2 (lower switch) you use:

pinMode(D2, INPUT_PULLUP);

In both cases:

  • HIGH means the switch is open
  • LOW means the switch is closed

This is kind of backward of what you might expect, but you get used to it after a while.

1 Like

Or, the opposite circuit looks like this:

In this little circuit, for D3 (top switch) needs an internal pull-down:


D2 (lower switch) has an external pull-down, so you use:

pinMode(D2, INPUT);

In both cases:

  • HIGH means the switch is closed
  • LOW means the switch is open

I prefer the other design, but this one works too.

Thanks. Both make sense. Will try out the Pull up version next. I so appreciate the quick and simple response.

By always having the GPIO connected to VCC, does this not draw current or power from the circuit? would this draw down a battery very quickly?

Since the input current to a GPIO set to INPUT is in the range of µA you don’t need to worry :wink:

1 Like