1
\$\begingroup\$

I am experimenting with a NodeMCU. I want to use a push button to give input to the microcontroller. My setup is as follows:

schematic

simulate this circuit – Schematic created using CircuitLab

The Arduino IDE sketch is as follows:

const int relay = D5;
const int wait_time = 4000;
bool is_on = false;
    
void setup() {
  Serial.begin(115200);
  pinMode(relay, OUTPUT);
  pinMode(D6, INPUT_PULLUP);
}
    
void loop() {
  // Check if button is pressed
  int state = digitalRead(D6);
        
  if (state == LOW && is_on == false) {
    Serial.println("ON");    
    digitalWrite(relay, HIGH); // Turn relay on
    is_on = true;
    delay(wait_time);              // Wait for some seconds
  }
  else if (state == LOW && is_on == true) {
    Serial.println("Off");    
    digitalWrite(relay, LOW); // Turn relay off
    is_on = false;
    delay(wait_time);              // Wait for some seconds
  }
}

Below is a photograph of the setup (minus the pushbutton):

enter image description here

This works as intended, however it shows one bizarre behavior:

When I merely insert the resistor into the slot of the yellow wire, it registers as a push of the button (in the absence of any contact with the ground). I don't understand why that happens. Does this indicate that the internal pull-up resistor might be faulty? Or is it expected behavior and I am doing something wrong?


Edit:

Well the same behavior continues with an external pull up resistor. And I have observed that this occasionally happens even when I am not touching the wires (maybe when I move near the board). For now I have bypassed this by using the following code to detect a press of the button:

  int state = digitalRead(D6);       
  if (state == LOW)
  {
    delay(200);
    state = digitalRead(D6);
    if (state == LOW)
    {
          // do what needs to be done
    }
  }

Basically when it detects that D6 is connected to ground, the board waits for 200 ms and then rechecks if the connection is still there. If so it considers it a 'real' push of the button. Otherwise it disregards it. This seems to filter out the false detection.

\$\endgroup\$
1
  • \$\begingroup\$ How do you power your device? With ungrounded power supply perhaps? \$\endgroup\$
    – Justme
    Commented Jul 21, 2022 at 18:20

2 Answers 2

2
\$\begingroup\$

Normally you pullup the very high input impedance with 10k to Vdd then switch to 0V on the input. (or visa versa)

Since 10k is lower than skin resistance, touching the contacts should not create a low voltage trigger and it will not consume much power.

Where did you get the idea for the series R?

The series R can be useful with a shunt Cap if your conductors are exposed to line stray electric fields.

The body acts like an antenna to e-fields and passes through which can couple much of the radiated voltage with the small air capacitance between you and wiring in the wall and isolated from ground >> 10M. Your body has a high dielectric constant like water (80) but also conductive salts and the fingertip is about 100 pF with a hand grasping insulated wire more than 10x.

schematic

simulate this circuit – Schematic created using CircuitLab

\$\endgroup\$
2
  • \$\begingroup\$ I see in code internal pullup activated on D6 \$\endgroup\$
    – Juraj
    Commented Jul 22, 2022 at 9:42
  • \$\begingroup\$ TY Juraj....... \$\endgroup\$ Commented Jul 22, 2022 at 11:29
0
\$\begingroup\$

You are powering the device with an ungrounded power supply.

That's enough to capacitively float the circuit common ground at few tens if not nearly hundred volts in respect to earth ground.

A two-pronged power supply with no earth connection has a capacitor between mains side and isolated output side, to prevent electromagnetic interference, and there is nothing you can do about it. Except maybe connect the PC to another device that is grounded.

A power supply with mains earth input has filter capacitors from live and neutral to earth. The low voltage output may be floating or have the ground connected to earth. Either way, a power supply with earthed inlet must not be connected to a non-earthed mains socket. It also likely reads in the manuals too. If you do the output may capacitively float at half mains voltage.

When the NodeMCU ground is basically floating at mains frequency and even at few volts compared to your voltage, when you touch the pin, to the MCU it looks like mains frequency square wave.

\$\endgroup\$
8
  • \$\begingroup\$ on the photo the Arduino is powered from USB \$\endgroup\$
    – Juraj
    Commented Jul 22, 2022 at 9:43
  • \$\begingroup\$ @Juraj I see that. But we don't know what USB device and if the photo relates to the scenario in any way. Is it a laptop? An USB wall adapter? A powerbank? There was a comment here saying that the power supply was ungrounded, and I wrote my answer based on that. The comment is now gone, maybe it was removed while I wrote my answer. \$\endgroup\$
    – Justme
    Commented Jul 22, 2022 at 10:04
  • \$\begingroup\$ I guess to see the Serial prints it has to be a computer like device \$\endgroup\$
    – Juraj
    Commented Jul 22, 2022 at 10:52
  • \$\begingroup\$ @Juraj That is true, but it can also be powered with a wall adapter and the operation is verified only by relay clicking. Even if it is connected to a computer, and you see the prints, the computer can have an ungrounded supply or a grounded supply connected to ungrounded socket. \$\endgroup\$
    – Justme
    Commented Jul 22, 2022 at 11:04
  • \$\begingroup\$ I am using an underground power supply, but I don't have the option to change that for this project. \$\endgroup\$
    – UmH
    Commented Sep 11, 2022 at 17:59

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.