Help Button input self triggering after first press, when adding motor only

Hello All,

Thanks in advance for reading and responding.

Problem:

I have a button that when pressed will turn on channel 1 of my 4channel relay.

The button is coded as Input_Pullup.
Button when not pressed returns High, when pressed returns low.
Ive added a delay function after the button press as well.

When I set this up and tested without attaching my pump to my relay. The button functionality works fine! It registers as clicked once and stops.

When i rig up my pump to the relay, the button after being pressed for first time, keeps self pressing multiple times after.

What is going on? I can’t figure it out. Based on limited research i dont think its debouncing or not having a resistor. I wonder if my pump (requires 120mA) is too much for the arduino to power and its messing up the power supply to the board or something…idk…

Please enlighten me:


Hello rickarduinotinker

Welcome to the worldbest Arduino forum ever.

Yes, the Arduino isn´t a PSU for external devices like pumps or other.

Have a nice day and enjoy coding in C++.

I read the Arduino Uno can easily provide >200mA.

So I am suspect to think this is a problem for a tiny 120mA pump.

Also note, when i just use code to turn on and off the relay/pump without the button use, everything works and runs fine. Theres enough power being supplies to the devices.

Please give us a link to that document! 200 mA is the maximum total I/O current.

You forgot to put a flyback diode over the pump!

Maybe. Better to not take any power from the Arduino beyond a few LEDs, and even then run them at low currents.

The current is not the only issue. The relay switching may cause things to happen what otherwise wouldn't. Maybe your sketch is going a little weird.

Post the code you are running. Perhaps it can be fitted with some diginostic printing that will point a finger.

a7

It depends :wink:

From the USB connection shown in your diagram you can get up to 500mA @ 5V.

Otherwise the on-board regulator limits the possible current draw. Rule of thumb: the higher the voltage supplied to the Arduino the less current can be drawn from the 5V and 3.3V pins.

Code Here:

// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;  // the number of the LED pin
const int relay_1 = 2;
const int relay_2 = 3;
const int relay_3 = 4;
const int relay_4 = 5;
#include <EEPROM.h>
#include <DHT.h>

#define DHTPIN 12     // Pin connected to the DHT11 sensor
#define DHTTYPE DHT22   // DHT11 sensor model
DHT dht(DHTPIN, DHTTYPE);
int addr = 0; //set address


#define BUTTON_PIN1 8//push button
#define BUTTON_PIN2 9//push button
#define BUTTON_PIN3 10//push button
#define BUTTON_PIN4 11//push button


// Variables will change:
int ledState = HIGH;  // ledState used to set the LED
int relayState_1 = LOW;  // ledState used to set the LED
int relayState_2 = LOW;  // ledState used to set the LED
int relayState_3 = LOW;  // ledState used to set the LED
int relayState_4 = LOW;  // ledState used to set the LED
int relay_selector = 1;

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis_1 = 0;  // will store last time LED was updated
unsigned long previousMillis_2 = 0;  // will store last time LED was updated
unsigned long previousMillis_3 = 0;  // will store last time LED was updated
unsigned long previousMillis_4 = 0;  // will store last time LED was updated
unsigned long previousMillis_temp_record = 0; //will store last time temp was recorded

// constants won't change:
// const long interval_1_Run = 20000;  // interval at which to blink (milliseconds)
//20000
//36000000
long interval_1_Run = 3000;  // interval at which to blink (milliseconds)
long interval_1_Wait = 18000000;  // interval at which to blink (milliseconds)
long interval_2_Run = 3000;  // interval at which to blink (milliseconds)
long interval_2_Wait = 18000000;  // interval at which to blink (milliseconds)
long interval_3_Run = 3000;  // interval at which to blink (milliseconds)
long interval_3_Wait = 18000000;  // interval at which to blink (milliseconds)
long interval_4_Run = 3000;  // interval at which to blink (milliseconds)
long interval_4_Wait = 18000000;  // interval at which to blink (milliseconds)
// const long interval_1_Wait = 8000;  // Test only
long interval_temp_record = 1800000;
bool Relay_On = false;


void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  pinMode(relay_1, OUTPUT);
  pinMode(relay_2, OUTPUT);
  pinMode(relay_3, OUTPUT);
  pinMode(relay_4, OUTPUT);

  digitalWrite(ledPin, HIGH);
  digitalWrite(relay_1, HIGH);
  digitalWrite(relay_2, HIGH);
  digitalWrite(relay_3, HIGH);
  digitalWrite(relay_4, HIGH);

  Serial.begin(9600); //to communicate
  pinMode(BUTTON_PIN1, INPUT_PULLUP); //push button
  pinMode(BUTTON_PIN2, INPUT_PULLUP); //push button
  pinMode(BUTTON_PIN3, INPUT_PULLUP); //push button
  pinMode(BUTTON_PIN4, INPUT_PULLUP); //push button
  dht.begin(); // for dht humidity & temp sensor
  Serial.println(EEPROM.read(0)*1000);
  //interval_1_Run = min(30000,max(5000,(EEPROM.read(0)*1000)));

}



void loop() {
  unsigned long currentMillis = millis();
  byte buttonState1 = digitalRead(BUTTON_PIN1); //read button state - HIGH if not pressed due to InputPullUp
  byte buttonState2 = digitalRead(BUTTON_PIN2); //read button state - HIGH if not pressed due to InputPullUp
  byte buttonState3 = digitalRead(BUTTON_PIN3); //read button state - HIGH if not pressed due to InputPullUp
  byte buttonState4 = digitalRead(BUTTON_PIN4); //read button state - HIGH if not pressed due to InputPullUp
  //logic for button state change of state
  

  if (buttonState1 == LOW) {
    relay_selector = relay_selector +1;
    if (relay_selector>4){
      relay_selector=1;
    }
      Serial.println("Button 1 is pressed : relay selected is :");
      Serial.println(relay_selector);
      // Serial.println(interval_1_Run/1000,DEC);
      delay(500);
      blinkyBlinky(relay_selector, 250); // 5 is number of blinks, blinkTime is the milliseconds in each state from above: int blinkTime = 500;
      //EEPROM.write(0,interval_1_Run/1000);
  }
  if (buttonState2 == LOW) {
      switch (relay_selector) {
        case 1: interval_1_Wait = interval_1_Wait+10800000;if (interval_1_Wait>82800000){interval_1_Wait=18000000;}break;
        case 2: interval_2_Wait = interval_2_Wait+10800000;if (interval_2_Wait>82800000){interval_2_Wait=18000000;}break;
        case 3: interval_3_Wait = interval_3_Wait+10800000;if (interval_3_Wait>82800000){interval_3_Wait=18000000;}break;
        case 4: interval_4_Wait = interval_4_Wait+10800000;if (interval_4_Wait>82800000){interval_4_Wait=18000000;}break;
        default:Serial.println("Invalid option.");break;
        }
      Serial.println("Button 2 is pressed");
      // Serial.println("length of pump run:");
      // Serial.println(interval_1_Run/1000,DEC);
      delay(1000);
      //blinkyBlinky(interval_1_Run/1000/5, 250); // 5 is number of blinks, blinkTime is the milliseconds in each state from above: int blinkTime = 500;
      //EEPROM.write(0,interval_1_Run/1000);
  }
  if (buttonState3 == LOW) {
     switch (relay_selector) {
      case 1: interval_1_Run = interval_1_Run+5000;if (interval_1_Run>30000){interval_1_Run=5000;}break;
      case 2: interval_2_Run = interval_2_Run+5000;if (interval_2_Run>30000){interval_2_Run=5000;}break;
      case 3: interval_3_Run = interval_3_Run+5000;if (interval_3_Run>30000){interval_3_Run=5000;}break;
      case 4: interval_4_Run = interval_4_Run+5000;if (interval_4_Run>30000){interval_4_Run=5000;}break;
      default:Serial.println("Invalid option.");break;
      }
      Serial.println("Button 3 is pressed");
      //Serial.println("length of pump run:");
      //Serial.println(interval_1_Run+interval_2_Run+interval_3_Run+interval_4_Run);
      delay(500);
      // blinkyBlinky(interval_1_Run/1000/5, 250); // 5 is number of blinks, blinkTime is the milliseconds in each state from above: int blinkTime = 500;
      //EEPROM.write(0,interval_1_Run/1000);
  }
  
  if (buttonState4 == LOW) {
      //interval_1_Run = interval_1_Run  + 5000 ;
    switch (relay_selector) {
      case 1:digitalWrite(relay_1, LOW);relayState_1 = LOW;previousMillis_1 = currentMillis; break;
      case 2:digitalWrite(relay_2, LOW);relayState_2 = LOW;previousMillis_2 = currentMillis; break;
      case 3:digitalWrite(relay_3, LOW);relayState_3 = LOW;previousMillis_3 = currentMillis; break;
      case 4:digitalWrite(relay_4, LOW);relayState_4 = LOW;previousMillis_4 = currentMillis; break;
      default:Serial.println("Invalid option.");break;
    }
    //if (interval_1_Run>30000){
     // interval_1_Run=10000;
    //}
      Serial.println("Button 4 is pressed : turning on relay :");
      Serial.println(relay_selector);
      // Serial.println(interval_1_Run/1000,DEC);
      delay(500);
      // Serial.println(buttonState4);
      // blinkyBlinky(interval_1_Run/1000/5, 250); // 5 is number of blinks, blinkTime is the milliseconds in each state from above: int blinkTime = 500;
      //EEPROM.write(0,interval_1_Run/1000);
  }
  if (currentMillis - previousMillis_temp_record >= interval_temp_record) {
    previousMillis_temp_record = currentMillis; //reset last time stamp
    int temperature = dht.readTemperature(); // Read temperature and humidity
    int humidity = dht.readHumidity(); // Read temperature and humidity

    EEPROM.write(addr, temperature*9/5+32); //write temp
    // EEPROM.write(addr+1, humidity); //write humidity

    Serial.print("Temperature: ");
    Serial.print(temperature*9/5+32);
    Serial.print(" °F\t");
    Serial.print("Humidity: ");
    Serial.print(humidity);
    Serial.println(" %");
    Serial.println("run times:");
    Serial.println(interval_1_Run);
    Serial.println(interval_2_Run);
    Serial.println(interval_3_Run);
    Serial.println(interval_4_Run);
    Serial.println("wait times:");
    Serial.println(interval_1_Wait);
    Serial.println(interval_2_Wait);
    Serial.println(interval_3_Wait);
    Serial.println(interval_4_Wait);
    addr=addr+1; //next i & addr
  } //end if interval passed
      
  //logic for when LED is lit and pump is on - how long to run for
  //relay = LOW means its ON

 if(relayState_1 == LOW){
    Relay_On = true;
    if (currentMillis - previousMillis_1 >= interval_1_Run) {
      Serial.println(currentMillis/1000/60/60); // how many minutes into program
      Serial.println("Min since start: Pump just finished running"); // tell user that pump is running
      previousMillis_1 = currentMillis; // save the last time you blinked the LED
      //ledState = LOW;
      relayState_1 = HIGH;
      // set the LED with the ledState of the variable:
      digitalWrite(ledPin, ledState);
      digitalWrite(relay_1, relayState_1);
      Relay_On = false;}
      // Serial.println(buttonState4);
  }

 if(relayState_2 == LOW){
   Relay_On = true;
    if (currentMillis - previousMillis_2 >= interval_2_Run) {
      Serial.println(currentMillis/1000/60/60); // how many minutes into program
      Serial.println("Min since start: Pump just finished running"); // tell user that pump is running
      previousMillis_2 = currentMillis; // save the last time you blinked the LED
      //ledState = LOW;
      relayState_2 = HIGH;
      // set the LED with the ledState of the variable:
      digitalWrite(ledPin, ledState);
      digitalWrite(relay_2, relayState_2);
      Relay_On = false;}
  }
 if(relayState_3 == LOW){
   Relay_On = true;
    if (currentMillis - previousMillis_3 >= interval_3_Run) {
      Serial.println(currentMillis/1000/60/60); // how many minutes into program
      Serial.println("Min since start: Pump just finished running"); // tell user that pump is running
      previousMillis_3 = currentMillis; // save the last time you blinked the LED
      //ledState = LOW;
      relayState_3 = HIGH;
      // set the LED with the ledState of the variable:
      digitalWrite(ledPin, ledState);
      digitalWrite(relay_3, relayState_3);
      Relay_On = false;}
  }
 if(relayState_4 == LOW){
   Relay_On = true;
    if (currentMillis - previousMillis_4 >= interval_4_Run) {
      Serial.println(currentMillis/1000/60/60); // how many minutes into program
      Serial.println("Min since start: Pump just finished running"); // tell user that pump is running
      previousMillis_4 = currentMillis; // save the last time you blinked the LED
      //ledState = LOW;
      relayState_4 = HIGH;
      // set the LED with the ledState of the variable:
      digitalWrite(ledPin, ledState);
      digitalWrite(relay_4, relayState_4);
      Relay_On = false;}
  }
  //logic to wait for next interval
  //relaystate = HIGH means its off
 if(relayState_1 == HIGH){ 
    if (currentMillis - previousMillis_1 >= interval_1_Wait) {
      if (Relay_On == false){
        Relay_On = true;
        // save the last time you blinked the LED
        previousMillis_1 = currentMillis;
        // if the LED is off turn it on and vice-versa:
        //ledState = HIGH;
        relayState_1 = LOW; 
        // set the LED with the ledState of the variable:
        digitalWrite(ledPin, ledState);
        digitalWrite(relay_1, relayState_1);}}}

 if(relayState_2 == HIGH){ 
    if (currentMillis - previousMillis_2 >= interval_2_Wait) {
       if (Relay_On == false){
         Relay_On = true;
        // save the last time you blinked the LED
        previousMillis_2 = currentMillis;
        // if the LED is off turn it on and vice-versa:
        //ledState = HIGH;
        relayState_2 = LOW; 
        // set the LED with the ledState of the variable:
        digitalWrite(ledPin, ledState);
        digitalWrite(relay_2, relayState_2);}}
  }

 if(relayState_3 == HIGH){ 
    if (currentMillis - previousMillis_3 >= interval_3_Wait) {
       if (Relay_On == false){
         Relay_On = true;
        // save the last time you blinked the LED
        previousMillis_3 = currentMillis;
        // if the LED is off turn it on and vice-versa:
        //ledState = HIGH;
        relayState_3 = LOW; 
        // set the LED with the ledState of the variable:
        digitalWrite(ledPin, ledState);
        digitalWrite(relay_3, relayState_3);}}
  }

 if(relayState_4 == HIGH){ 
    if (currentMillis - previousMillis_4 >= interval_4_Wait) {
       if (Relay_On == false){
         Relay_On = true;
        // save the last time you blinked the LED
        previousMillis_4 = currentMillis;
        // if the LED is off turn it on and vice-versa:
        //ledState = HIGH;
        relayState_4 = LOW; 
        // set the LED with the ledState of the variable:
        digitalWrite(ledPin, ledState);
        digitalWrite(relay_4, relayState_4);}}
  }
}

void blinkyBlinky(int repeats, int time)
{
  for (int i = 0; i < repeats; i++)
  {
    digitalWrite(ledPin, HIGH);
    delay(time);
    digitalWrite(ledPin, LOW);
    delay(time);
  }
}

Here is a link to discussion.

I seemed to piece together:
200mA from I/O
500mA from 5v
150mA from 3v

Sorry. Will not spend time on any discussion. The datasheet is clear, discussions are unnecessary.
Like Already told, even a Mega having 50+ outputs the maximum sum of the output is 200 mA, or for sinking, 200 mA.

HI All update:

I switch the relay input power and output power sources to a secondary external source.

It is behaving slightly better but I am still getting the phantom button pressing.

Now the only thing from the relay connected to the main board is the input signal - could that be causing this wild behavior?

See post #5 from a day ago.

Hi DrDiettrich.

Thanks so much for the tips.

I reconfigured my setup so there is a seperate and distinct power source for the relay and pump, yet I am still getting same effects.

Can you see if this setup eliminates the meed for a fly back diode? If so, what else could be the cause?

Thank You

No.

Hi All!

Is this due to a weak power supply?

I am powering my arduino setup form a computer usb outputting 5v @ 500mA.

Could this be the cause of my odd behavior?

Is there anyway to test this theory?

Thanks!

You can use a stronger PSU and kill your Arduino and more.

Or you can install a fly back diode.

If you can't hear then I'm out.

Hi DrD-,

Yes ive ordered a fly back diode and capacitors in the mail. They are coming and I will install them as you suggest.

Just trying to debug in the meantime. I expect package deliver and installing mid next week.

Will be careful until then.

I'm onto a new project and definitely installing a flyback diode over my 12v solenoid!

Regards

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.