[ASK]Need help about interference on internal Pull-Up for multiple button

hello every one
I am new here and in electronic, please forgive my english, or repost.

I have a project with arduino nano. wiring scheme:

and the sketch:

// Counters of goods stored on the conveyor slot. Each slot contains 1 pair

#include <Button.h>
#include <SevenSegmentExtended.h>
#include <SevenSegmentTM1637.h>

// Nano pin assignment
#define CLK 3
#define DIO 4
#define BtnUpPin 5
#define BtnDnPin 6
#define BtnResPin 7
#define BtnSetPin 8
#define BuzzPin 9

// button parameter
#define PULLUP true
#define INVERT true
#define DEBOUNCE_MS 20

// declare variable
int pair = 0;
int dozen = 0;
int total = 0;

/* To give a warning (beep) every time reach setberrier
 * then empty the goods slot.
*/
int setBerrier = 8;
int berrier = 0;

bool setState = false;

SevenSegmentExtended display(CLK, DIO);
Button BtnUp(BtnUpPin, PULLUP, INVERT, DEBOUNCE_MS);
Button BtnDn(BtnDnPin, PULLUP, INVERT, DEBOUNCE_MS);
Button BtnRes(BtnResPin, PULLUP, INVERT, DEBOUNCE_MS);
Button BtnSet(BtnSetPin, PULLUP, INVERT, DEBOUNCE_MS);

void setup() {
  // put your setup code here, to run once:
  // init display
  display.begin();
  display.setBacklight(50);
  pinMode(BuzzPin, OUTPUT);
  display.setPrintDelay(200);
  display.print("DOZEN COUNTER -88-");
  display.blink(500,2);
  display.clear();
  display.setColonOn(true);
  display.printDualCounter(dozen, pair); // normal/counting mode
  beep(50);
  beep(50);
  delay(100);
}

void loop() {
  // put your main code here, to run repeatedly:
  BtnUp.read();
  BtnDn.read();
  BtnRes.read();
  BtnSet.read();

/* Reset button is used every 1 hour of work to display a dozen values back to 0 
   and save a dozen to total.
*/
  if (BtnRes.wasPressed()) {
      if (setState == false) {  // counting mode
        total = total + dozen;
        dozen = 0;
        beep(50);
        display.setColonOn(false);
        display.clear();
        display.print(total); // show total for a moment
        display.blink(500,5);
        delay(2000);
        display.setColonOn(true);
        display.printDualCounter(dozen, pair);
      }
      else {  // set mode
        display.setColonOn(false);
        display.clear();
        display.print(setBerrier);
        display.setCursor(0,2);
        display.print("01");
        display.blink(500,3);
        display.setColonOn(true);
        display.printDualCounter(dozen, pair);
        display.blink(500,3);
        setState = false; // back to counting mode
      }
  }
  
  if (BtnUp.wasPressed()) {
    if (dozen <=99) {
      if (setState == false) {
        berrier+=1;
        pair+=1;
        if (pair == 12){ // if dozen reach
          pair = 0;
          dozen+=1;
          display.printDualCounter(dozen, pair);
          if (berrier != setBerrier) beep(50);
        }
        else display.printDualCounter(dozen, pair);
        if (berrier == setBerrier) {
          beep(50);
          beep(50);
          berrier = 0;
        }
      }
      else {
        if (setBerrier <= 11) {
          setBerrier+=1;
          display.clear();
          display.print(setBerrier);
        }
      }
    }
  }
  
  if (BtnDn.wasPressed()) {
      if (setState == false) {
        if (dozen != 0) {
          if (pair == 0) {
            pair = 11;
            dozen-=1;
          }
          else pair-=1;
        }
        else {
          if (pair != 0) pair-=1;
        }
        display.printDualCounter(dozen, pair);
      }
      else {
        if (setBerrier >= 6) {
          setBerrier-=1;
          display.clear();
          display.print(setBerrier);
        }
      }
  }

  if (BtnSet.wasPressed()) {
      if (setState == false) {
        setState = true;
        display.setColonOn(false);
        display.clear();
        display.print(setBerrier);
      }
      else {
        display.setColonOn(false);
        display.clear();
        display.print(total);
        display.blink(500,5);
        delay(1000);
        display.setColonOn(true);
        display.printDualCounter(dozen, pair);
        setState = false;
      }
  }
}

void beep(unsigned char delayms) {
  digitalWrite(BuzzPin, HIGH);
  delay(delayms);
  digitalWrite(BuzzPin, LOW);
  delay(delayms);
}

My Problem:

due to long term use, I need power adapter to power my project. I have a used adapter with 9v 600mA output.
But if using this adapter, the btnUp button often presses itself (if (btnUp.wasPressed())) randomly. The problem is often, so it is very disturbing the accuracy of the counting and the real counting.

i have 2 option to using baterry with regulator 5v via 5v pin, and 9v battery via vin pin. Using this power (battery), my project runs quite smoothly, but still have interference of btnUp button. but not often. still annoying. :). for 8 hours used, approximately 5 times random interference BtnUp occur. That's what's caught on display, what about the unconscious count has increase by itself?

what's the solution? Is there anything in the hardware or software that I need to fix, Is there an article link I should read or an incorrect power source issue? Please help.

Thank you

I don't see how your code does the equivalent of
pinMode (pinX, INPUT_PULLUP);
to turn on the internal pullup resistor.

We used to do this
pinMode (pinX, INPUT);
digitalWrite (pinX, HIGH);
to turn on the internal pullup.
Maybe you just need to add the digitalWrite's to the end of your setup() code.
If the extended switch is the one causing the problems, try adding a 10K, or as low as 1K, pullup to that line.

Things that might help:

  • Using a stronger external pull-up resistor.
  • Routing the wires to the button away from sources of electrical interference.
  • Using shielded cable to the button
  • Using a larger debounce value

You might also consider protecting your input pin from possible damage caused by voltage spikes:

CrossRoads:
We used to do this
pinMode (pinX, INPUT);
digitalWrite (pinX, HIGH);
to turn on the internal pullup.
Maybe you just need to add the digitalWrite's to the end of your setup() code.

Thank you, sir
pinMode (pinX, INPUT);
digitalWrite (pinX, HIGH);

it's already write in button library: (button.cpp)

pinMode(_pin, INPUT);
    if (_puEnable != 0)
        digitalWrite(_pin, HIGH);       //enable pullup resistor
    _state = digitalRead(_pin);

If the extended switch is the one causing the problems, try adding a 10K, or as low as 1K, pullup to that line.

I will try this one first

pert:

  • Using a larger debounce value

do you mean value on this: #define DEBOUNCE_MS 20 ??

You might also consider protecting your input pin from possible damage caused by voltage spikes:
Protecting Inputs in Digital Electronics | DigiKey

This one can be enlightening.

thank you for all.

ahlan:
do you mean value on this: #define DEBOUNCE_MS 20 ??

That's correct. The Button library hides it from you but it means that the button has to be pressed for more than 20 ms in order to register. This is used to prevent contact bounce from causing a single switch closure to be registered as multiple. You need to set a value that is long enough to prevent this while short enough that intentionally will always be detected. 20 ms is a reasonable value but you could probably get by with a larger one. The idea I had is that maybe the electrical interference could be happening in short spikes, which would be filtered out by the debounce code. I don't know if that's actually the case.

Many thanks, I will try all the options and report the results when have time to post

pert:
Things that might help:

  • Using a stronger external pull-up resistor.
  • Routing the wires to the button away from sources of electrical interference.
  • Using shielded cable to the button
  • Using a larger debounce value

After trying various options. Finally tried adding ferrite on the end of the adapter cable + shielded cable for the button. Work well. There is almost no interference at all. Thank you all

Glad to hear you've solved it! Thanks for taking the time to update the thread with your solution, which may be helpful to others who have the same problem.

Hi,
How long is the cable from the switch to the UNO?

Tom.. :slight_smile: