Drink Machine Hardware/Software Issues

Hello all!

My hardware:

  • 12v 10A power supply
  • Arduino Mega 2560 R3
  • x6 12v hydraulic pumps
  • 8 relay module board
  • Buttons, LCD, potentiometer (not important to current issue)

Please see my attached schematic, I reference it below.

Description:
I am creating a machine that uses 6 12v hydraulic pumps to pump liquid from a bottle into a glass. In order to control the pumps, I purchased an 8 relay control board that also runs on 12v. When both are connected to a 12v power supply, and the Mega board is connected to my laptop running the IDE code, one button controls drink type, while the other serves as a "select" button. Also in the machine is a 16x2 LCD display for information graphics.

Problem:
"Drink One" is navigated to and selected. The code tells the relay board to activate Relay 1 for 2 seconds, then Relay 2 for 1 second. However, both relays (only 1 and 2, none of the other 6) activate for 3 seconds, then turn off. I am unable to figure out how to get only one relay to activate at a time. In order to test if the problem was a coding error, I programmed only ONE Relay 1 to activate. Both were still connected, however only Relay 1 was supposed to activate. Both relays (1 and 2) activated for the desired time. The problem persists if I move the connections to two separate relays (1&7, 2&3, etc).

What I'm looking for help with:
I would like to know what the issue with my project is. I am not sure if my code is the culprit, or if I have a hardware/wiring issue. Also, the motor is not attached in the diagram because I do not know the proper way to wire it. It is not urgently important, because the relay board has built in LEDs that activate when the relay does. Thank you for your assistance.

Code information (please read):

  • I am programming for two buttons, however I intend to switch to a rotary encoder once it comes in. This is why the two button names are odd
  • The serial monitor is there for debugging, it can be removed
  • I am not sure if this is the "right" code to use with relay boards
#include <LiquidCrystal.h>

const int pushyPin=53;
const int rotatePin=51;
const int motorPin=13;
const int motor2Pin=12;
int pushyState=0;
int rotateCurrentState=0;
int rotatePreviousState=0;
int rotateCounter=0;

LiquidCrystal lcd(23, 25, 27, 29, 33, 35);

void setup() {
  Serial.begin(9600);
  pinMode(pushyPin,INPUT);
  pinMode(rotatePin,INPUT);
  pinMode(motorPin,OUTPUT);
  pinMode(motor2Pin,OUTPUT);

 lcd.begin(16, 2);
  lcd.print("BeerTender 9000");
}

void loop() {
// read the pushbutton input pin:
  rotateCurrentState = digitalRead(rotatePin);
  // compare the rotateCurrentState to its previous state
  if (rotateCurrentState != rotatePreviousState) {
    // if the state has changed, increment the counter
    if (rotateCurrentState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      rotateCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(rotateCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  rotatePreviousState = rotateCurrentState;

  if (rotateCounter==1){

  lcd.begin(16, 2);
  lcd.print("Drink Uno");
    
  pushyState=digitalRead(pushyPin);
  if(pushyState==HIGH){
    //If the switch has been pushed make the motor spin
    digitalWrite(motorPin,HIGH);
    delay(2000);
    digitalWrite(motor2Pin,HIGH);
    delay(1000);
    digitalWrite(motorPin,LOW);
    digitalWrite(motor2Pin,LOW);
   }
  }

 if (rotateCounter==2){

 lcd.begin(16, 2);
  lcd.print("Drink Dos");
  
  pushyState=digitalRead(pushyPin);
  if(pushyState==HIGH){
    //If the switch has been pushed make the motor spin
    digitalWrite(motorPin,HIGH);
    delay(1000);
    digitalWrite(motor2Pin,HIGH);
    delay(1000);
digitalWrite(motorPin,LOW);
digitalWrite(motor2Pin,LOW);
  }
 }
}

Thank you kind people!!!

Copied and serached Your code.

 if (pushyState == HIGH) {
      //If the switch has been pushed make the motor spin
      digitalWrite(motorPin, HIGH);
      delay(2000);
      digitalWrite(motor2Pin, HIGH);
      delay(1000);
      digitalWrite(motorPin, LOW);
      digitalWrite(motor2Pin, LOW);
    }

First You activate "motorPin" and halts the progam for 2 seconds. Then Tou activate "motor2Pin" and halt execution for 1 second. That sums up to 3 seconds for "motorPin" and me and 1 second for "motor2Pin".

Try this! And think about the difference!

 if (pushyState == HIGH) {
      //If the switch has been pushed make the motor spin
      digitalWrite(motorPin, HIGH);
      delay(2000);
      digitalWrite(motorPin, LOW);
      digitalWrite(motor2Pin, HIGH);
      delay(1000);
      digitalWrite(motor2Pin, LOW);
    }

If You want both relays run at in paralell, at the same time, then try:

      if (pushyState == HIGH) {
      //If the switch has been pushed make the motor spin
      digitalWrite(motorPin, HIGH);// Motor 1 starts running
      delay(1000);
      digitalWrite(motor2Pin, HIGH);// Motor 2 starts running
      delay(1000);
      digitalWrite(motorPin, LOW);//2 seconds elapsed for motor 1, stop motor 1
      digitalWrite(motor2Pin, LOW);// 1 second elapsed for motor 2, stop motor 2
    }

What type of relay board are you using? Your schematic appears to only show the two output lines from the arduino to the relay board, and no common reference point (either a common ground, or a common 5V Vcc, depending on the relay board you have).

Hi,
Welcome to the forum.
Can you post specs/data of the relay PCB please.

You appear to have a connection between the PCB and the Mega, a 5V and/or gnd connection missing.

Do you have a DMM to check the digital input operated by the button?

Thanks.. Tom.. :slight_smile:
PS. I have given you a Karma as you appear to have read the intro instructions and presented the info in the appropriate format.

If you plan a more extensive project I reckon all those delay()s will bite you in the bum.

The functions delay() and delayMicroseconds() block the Arduino until they complete. That will make the program very unresponsive.

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R

Thank you all for your responses!

Here's the amazon product listing for the Relay Board:

It runs on 12V, and has 10 connection terminals. DC+, DC-, and the 8 relay activation terminals (connected to Arduino board)

I connected the positive and negative terminals of the relay board to the DC power supply.

Focused

P.S. I appreciate all your responses to the code - I'll look into millis() and the like.

Unfortunately, the schematic they supply does not match the pictures of the board. The board doesn't have the "P9" shown on the schematic where VCC (+5V) and JD_VCC (+12V) are supplied. The schematic doesn't show the 8 jumpers for selecting Active High or Active Low.

My guess is that DC+ is the 12V supply and DC- is Ground, both for the 12V supply and the Arduino.

johnwasser, are you referring to my schematic or the board's official schematic in your response? I believe the jumpers are located on the same side of the main terminal connections, colored in black on the product listing.

Focused

Hi,
We might need a picture of your relay assembly, there are so many different ones out there now and they aren't all the same even though they advertise as such.

Thanks.. Tom.... :slight_smile:

Because the forum was having issues uploading my image (don't know why?), here's a link to it:
https://drive.google.com/file/d/1hqSulyS67863BvmphW8favE0hb_UNbkm/view?usp=sharing

Sorry everyone!

The main board is black and the relay board is red. The white wire connects the relay board to 12V+, while the orange wire connects the board to 12V-.

Thank you all for your help,
Focused

Hi,
Keep your images to jpg or png and below 1M in size.

I use WIN10 and any large image I put in PAINT and proportionally resize to about 1100 pixels on its longest side.
Your image was over 2M and 2200 pixels, I resized it to 1100.

Tom... :slight_smile:

Hi,
Connect the gnd of your 2560 the the -DC terminal of the relay board.
Your control signal from the 2560 has no proper gnd reference, it is trying to use its control signal connections as a current return.

What is your 12V supply?

Thanks...Tom... :slight_smile:

Oh, thank you so much! I'll make sure my images are the correct size from now on, sorry about that!

I'll try that connection this afternoon, thank you for that tip.

Here's the DC Supply I'm using:

Focused

Thank you all for your help!

Per the recommendation of TomGeorge, after connecting the 2650's ground connection to the DC- on the power supply, the relay works wonderfully!

I would like to thank all of you for your support during this journey, I couldn't have done it without you! I will look over the supplied code improvements (thank you!) and implement them as needed.

Focused