2 channel relay is already on when not needed

So i have a small Programm where my relay is already on when i dont need it
Example:

  1. Pushed Button 1
    2.Move servo
  2. Relay 1 on
    4.delay
    5.Relay 1 off
    6.delay
    7.Relay 2 on
    8.delay
    9.Relay 2 off
    10.delay
  3. Servo 0 Pos
    but the Problem is i dont even Push a button and both relays are on


// Include servo library
#include <VarSpeedServo.h>

// Define servo pins
#define servo1Pin 2 //ARM 1
#define servo2Pin 3 //ARM 2
#define servo3Pin 4 //DREHTELLER

// Define button pins
int button1Pin = 5;
int button2Pin = 6;
int button3Pin = 7;
int button4Pin = 8;
int button5Pin = 9;

// Define relay pin
#define relayPin 11
#define relayPin2 12
// Define servo objects
VarSpeedServo servo1;
VarSpeedServo servo2;
VarSpeedServo servo3;

// Define servo parameters
int servo1ZeroPos = 40; // Zero position for servo 1 ARM 1!!!!!!!!


int servo2ZeroPos = 0; // Zero position for servo 2 ARM 2 !!!!!!!


int servo3ZeroPos = 60; // Zero position for servo 3

// Define button states
int button1State = 0;
int button2State = 0;
int button3State = 0;
int button4State = 0;
int button5State = 0;

// Define relay state
int relayState = 0;

void setup() {
  // Initialize servo objects
  servo1.attach(servo1Pin);
  servo2.attach(servo2Pin);
  servo3.attach(servo3Pin);

  // Initialize button pins as inputs
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  pinMode(button3Pin, INPUT);
  pinMode(button4Pin, INPUT);
  pinMode(button5Pin, INPUT);

  Serial.begin(9600);
  Serial.println("Roboter started");
  // Initialize relay pin as output
  pinMode(relayPin, OUTPUT);
  pinMode(relayPin2, OUTPUT);
}

void setPos1() {
    servo1.write(17, 10);
    servo2.write(6, 20);
    servo3.write(44, 20);
    delay(1000);
    digitalWrite(11, HIGH);
    delay(3500);
    digitalWrite(11, LOW);
    delay(5000);
    digitalWrite(12,HIGH);
    delay(500);
    digitalWrite(12, LOW);
    delay(200);
}

void setPos2() {
    servo1.write(19, 10);
    servo2.write(2, 20);
    servo3.write(51, 20);
    delay(1000);
    digitalWrite(10, HIGH);
    delay(1500);
    digitalWrite(10, LOW);
    delay(5000);
}

void setPos3() {
    servo1.write(19, 10);
    servo2.write(0,20);
    servo3.write(57,20);
    delay(1000);
    digitalWrite(10, HIGH);
    delay(2500);
    digitalWrite(10, LOW);
    delay(2000);
}

void setPos4() {
    servo1.write(19, 10);
    servo2.write(2, 20);
    servo3.write(63, 20);
    delay(1000);
    digitalWrite(10, HIGH);
    delay(4000);
    digitalWrite(10, LOW);
    delay(2000);
}

void setPos5() {
    servo1.write(19, 10);
    servo2.write(2, 20);//POS 5
    servo3.write(70, 20);
    delay(1000);
    digitalWrite(10, HIGH);
    delay(4000);
    digitalWrite(10, LOW);
    delay(2000);
}

void setEndPos() {
    servo1.write(servo1ZeroPos, 10);
    servo2.write(servo2ZeroPos, 20);
    servo3.write(servo3ZeroPos, 20);
    delay(2000);
}

void loop() {
  // Read button states
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);
  button3State = digitalRead(button3Pin);
  button4State = digitalRead(button4Pin);
  button5State = digitalRead(button5Pin);
  servo1.write(servo1ZeroPos);
  servo2.write(servo2ZeroPos);
  servo3.write(servo3ZeroPos);

  // If button 1 is pressed, move servo 1 to position and turn on relay
  if (button1State == HIGH) {
    Serial.println("1 shot will be filled");
    // servo1.write(23, 10);
    // servo2.write(4, 10);
    // servo3.write(39, 10);
    setPos1();
    setEndPos();

  }
  // If button 2 is pressed, move servo 2 to position and turn on relay
  else if (button2State == HIGH) {
    Serial.println("2 shots will be filled");
    setPos1();
    setPos2();
    setEndPos();
  }
  // If button 3 is pressed, move servo 3 to position and turn on relay
  else if (button3State == HIGH) {
    Serial.println("3 shots will be filled");
    setPos1();
    setPos2();
    setPos3();
    setEndPos();

  }
  // If button 4 is pressed, move all servos to zero position and turn off relay
  else if (button4State == HIGH) {
    Serial.println("4 shots will be filled");
    setPos1();
    setPos2();
    setPos3();
    setPos4();
    setEndPos();

  }
  // If button 5 is pressed, move all servos to zero position and turn off relay after 5 seconds
  else if (button5State == HIGH) {
    Serial.println("5 shots will be filled");
    setPos1();
    setPos2();
    setPos3();
    setPos4();
    setPos5();
    setEndPos();
  }

}


Many relay boards are "Active LOW"
This means that:
LOW turns them on.
and
HIGH turns them off.

// Initialize relay pin as output and turn it OFF
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, HIGH); // turn OFF relay

  pinMode(relayPin2, OUTPUT); 
  digitalWrite(relayPin2, HIGH); // turn OFF relay

1 Like

How are your buttons connected to the inputs?

Are you using an input that is pulled up, or pulled down?

a7

1 Like

With lack of any circuit diagram, just guessing you have no debounce either.....and floating pins

Can you post an annotated schematic showing exactly how you have wired it, include all power, ground connections and power sources. Post a link to the technical information on your relay board.

Never heard of something like Pulled up and down i think i dont have any of them

Just hook the button between the pin and ground.
Then in setup() activate the builtin pullup like so:

pinMode(pin,INPUT_PULLUP);

Like the relays
LOW = button pressed.
HIGH = button not pressed

Relay:
https://www.amazon.de/dp/B01H2D2RI0?psc=1&ref=ppx_yo2ov_dt_b_product_details


i made a Circuit how it is wired right now, first time making a Circuit i´m sorry :grin:

i will try that. PULLUP means that the input is High and as soon as i press the button the input will be low ?

The 5v for your relays should be connected to the power supply like the servo's and NOT to the Arduino.

Yes, it seems backwards but that's how it is usually done.

it is connected to the Power supply my mistake

That's because your code doesn't do anything with the relay pins, you only set them as outputs, then never use them. Try seting them HIGH to turn them OFF

In setup() put these
digitalWrite(relayPin, HIGH);
digitalWrite(relayPin2, HIGH);

I think you can figure out how to do the rest of the code.
Have a nice day!

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