Button gets randomly triggered after a while

Hello everyone,

This is the first time that I'm using this form, and I hope someone can help me with my problem. For about a year I have been running my code on an Arduino Nano without any problems, however, several weeks ago I decided that I wanted to include some more functionalities and decided to change to a Wemos D1 Mini. I changed my code accordingly and uploaded it on my Wemos. The purpose of my code was to activate for instance my lights or other functions, by simply pressing a button. For this button, a distinction was made between a short and a long press. A short press would simply turn on or off my light which is connected through a 5v relay. And a long press would for simplicity only send a Serial.println command. The button is connected to D6 with a 10k ohm pull-down resistor to ground (also I use the 3.3v). Finally, the relay is connected to pin D1 using the following connection.

But the problem is, the code works fine, but somehow after a (random) few hours the button gets triggered making the light turn on or off. At first, I thought the problem to be with the debouncing of the button, so I started from scratch and rewrote my script and included this time the JC_button.h library instead of a simple timer, but the exact problem again occurred.

A simple version of the code I'm using is:

// WEMOS D1 MINI PRO
#include <JC_Button.h>

int lamp1                   = D1;
int lamp1Val                = 0;  

Button button1a(D6, 40, false, false);
int button1aShortLongLogic   = false;

const unsigned long LONG_PRESS(1000);


void setup() 
{
  Serial.begin(9600);
  
  // Initialize inputs and outputs
  button1a.begin();

  Serial.println("Setting initial pin states");
  pinMode(lamp1, OUTPUT);
  digitalWrite(lamp1, LOW); //HIGH
}

void loop() 
{
  // Read button state
  button1a.read();


  // Commands for button 1a
  if (button1a.pressedFor(LONG_PRESS))
  { 
    if (button1aShortLongLogic == false)
    {
      Serial.println("Button 1A triggered - long press");
      button1aShortLongLogic = true;
    }   
  }
  else if (button1a.wasReleased() && button1aShortLongLogic == false)
  {
    Serial.println("Button 1A triggered - short press");
    if (lamp1Val == 0)
    {
      digitalWrite(lamp1, HIGH);
      lamp1Val = 1;
    }
    else
    {
      digitalWrite(lamp1, LOW);
      lamp1Val = 0;
    }
  }
  else 
  { 
    button1aShortLongLogic = false;
  }

  
}

This code is a snap shot of the origional code, In my real code I'm using 5 buttons (on pins D0,D5,D6,D7 and D8) and have a relays connected to D1 and D2. Also I have a DHT22 temperature sensor connected to D3. As I expect the problem to be in the hardware, I also will include a picture of the PCB I'm currently using (I hope this picture does not create confusion):

I do not understand your schematic, lots of info has been striked out.. From where comes the 3.3v pin and how does that relate to the arduino? Are you using a pullup resistor for the button?

I have no idea whether it is a factor in your problem but pin D6 on the D1 mini has an internal pullup resistor and you have set the button not to use internal pullup by setting the third parameter to false

Sorry for confusing schematics, the board you can see in there is the Wemos D1 Mini as in the following figure:


The 3.3v comes from the Wemos board. At first, I was using the 5v, however as I had this problem I tried different options. On the internet, I found that the button on the Wemos D1 was used in combination with the 3.3v. Unfortunately, this also didn't solve the problem.
For the button, I use an external pull-down resistor. I also have tried with only the internal pull-up, but also no success.

I hope this answers your question and makes my problem more clear.

:slight_smile:

(I don't know if this is the right way to answer to comments)
@ UKHeliBob

In my case, I'm using an external pull-down resistor. However, I have tried both enabling and disabling the internal pull-up resistor, but the problems keep coming back.

I still do not understand the schematic, but that might just be me.. The first schematic (black and white one) shows that 3.3v from somewhere is used to switch some sort of pullup feature? 5v goes through a resistor and into an arduino (pullup) and when the transistor is saturated by the 3.3v on the base, the current flows to GND..?

I have to agree that the schematic is a little bit confusing. I found this solution to fix an earlier problem. As I also cannot fully explain myself, I removed the transistor part. Currently, I only have a single button connected to D6 (see attachment). This is only a single button, nothing fancy what-so-ever. But also here, after a couple of hours, it gets triggered. What really confuses me, is that even with only the button this problem reappears.

I hope this clarifies?

Capture2.JPG

Try to convert your button from pull-down to pull-up by using a 10k resistor from pin to 3.3v and have the button switch the pin to GND.

I will try doing that. Just for interest, is pull-up in general preferred over pull-down?

I would say yes to that. In a pull-down scenario noise may cause a pin to go high. If the pin is pulled up noise cannot cause a pin to go low. I'm not an expert in this, but generally push-buttons are always used in a pull-up scenario.

Just remember that in a pull-up scenario, the pin is normally in HIGH state and the button is pressed when LOW is registred. Dunno if JC_Button can figure this out.

This time I tried changing my button into the suggested pull-up using the 10k ohm resistor again on pin D6, I also included a schematic to verify if the connection is correct. With the JC_Button I was able to invert the state into LOW when pressed, by changing the last parameter from false to true. Unfortunately, again after a couple of hours, the button gets triggered.

Capture3.JPG

Does it happen if the switch is disconnected? Have you tried another switch or another pin? Have you tried with another button library?

Well, after completely re-designing the circuit board and replacing the 2n3904 transistor with a Sparkfun bi-directional logic level converter the problem seemed to be solved. What initially caused the problem is unfortunately still unclear, but I'm happy that it is working properly now.
Furthermore, I would like to thank you guys for helping me!

It's disconcerting that no explanation has been found. Can anyone think of an explanation? Is there anything that overflows after a couple of hours? Could WDT be involved? Well, just grasping at straws.

And we never got an answer to the question - does this still happen if nothing is connected to D6.

Anyone else have experience with this JC_Button.h library? Anything special about the Wemos D1 Mini?

Would running his code on a Nano lookalike with nothing connected to D6 be a fair test?