ESP01 and relay startup close

Hi!

I tried to make a MQTT relay to open my door from my Home Assistant.
The stuff is working but when i switch on the power the relay is unwaited close at the momet of start.
I read the posts, but this not working.

 pinMode(PumpRelay, INPUT_PULLUP);
 pinMode(PumpRelay, OUTPUT);

The relay:

My code:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
const char* ssid = "x";
const char* password = "x";
const char* mqtt_server = "192.168.0.x";
const char* brokerUser = "x";
const char* brokerPass = "x";
const char* MQTT_topic = "x";
const char* MQTT_topicState = "x";
WiFiClient espClient;
PubSubClient client(espClient);


int SwPin1 = 0;

String strTopic;
String strPayload;



void setup_wifi() {
  
  Serial.begin(115200);
  delay(100);
 
  // We start by connecting to a WiFi network
 
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  payload[length] = '\0';
  strTopic = String((char*)topic);
  if(strTopic == MQTT_topic)
    {
    strPayload = String((char*)payload);
    if(strPayload == "ON")
      {
        Serial.println("ON");
        digitalWrite(SwPin1, LOW);
        client.publish(MQTT_topicState,"ON");
        delay(500);

        Serial.println("OFF");
        digitalWrite(SwPin1, HIGH);
        client.publish(MQTT_topicState,"OFF");
     }
    }
}
 
 
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("arduinoClient", brokerUser, brokerPass)) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.subscribe("ha/#");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
 
void setup()
{
  digitalWrite(SwPin1, HIGH);
  pinMode(SwPin1, INPUT_PULLUP);
  pinMode(SwPin1, OUTPUT);
  
  setup_wifi();
  
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

}
 
void loop()
{
  
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}

I thinking about use a pullup resistror, but i read not use this on the output pins.

Is there a working solution to start this relay without 'click'?

Thanks,
Att.

That is crap. Either a pin is an input or an output. This code will leave the SwPin1 as an output. An output is either high or low.

Set the output to the inactive state by a digital.write( Pin, inactive); and in the next statement set the pin as an output.

@Railroader I try the digital.write( Pin, inactive); but it throw an error.
In the reference i found only digitalWrite() function

`digitalWrite(pin, value)

Parameters

pin: the Arduino pin number.
value: HIGH or LOW`
What is the digital.write() function?
I dont find same topic here.
Thank,
Att

As in reply #2

  digitalWrite(SwPin1, HIGH);
  pinMode(SwPin1, OUTPUT);  

if the switch pin is GPIO 0 (or 1 or 2 for that matter) the relay must be active 'LOW" so setting the pin HIGH should turn it off.

to confirm that, upload the simplest of sketches

int SwPin1 = 0;

void setup {
  digitalWrite(SwPin1, HIGH);
  pinMode(SwPin1, OUTPUT);  
}

void loop() {
}

the Relay has 3 pins, 2 of which make contact when not powered, and 2 others make contact when powered, make sure you connect to the proper pins.

@Deva_Rishi I tried that ( but need void setup(){ formula) but same. When I power on the relay with ESP01 the relay close a little time. Just "click" noise once.
When I dont conect the ESP01 to the relay pins there isn't click noise.
I try it with the industrial "virgin" ESP01 - same: When power on the relay close shortly.

OK that wasn't clear from your previous post. Then i figure the issue is this
With the hardware the way you have it now, i don't think it can be helped.
I have had a similar issue (albeit with a different type of output being Mosfets to drive LEDs) since all exposed pins of an ESP-01 (being GPIO 0, 1, 2 & 3) suffer from the same issue to some extend that the pins go momentarily LOW and or HIGH during boot before the pinMode will even be set.
What you need to do is create an electronic debounce, which is normally done with a capacitor/resistor pair. Easiest would be to add a capacitor to the proper side of the resistor marked '103' (R2) which is used to drive the transistor that is driving the relay, and GND. The capacitor probably doesn't need to be very big with the 10K resistor limiting the current., 1uF should suffice. If you have a DMM you should be able to work out which end of the 10K resistor is connected to the transistor, i suspect it is the end where the 'R2' silk is printed.

When the controller starts it executes the bootloader to figure things out. During that execution outputs are set in tristate. Use an external pullup, or pulldown, resistor to make the output for the relay inactive.

A pullup !! not a pulldown. A pulldown will result in the ESP going into upload mode. (also the relay is active LOW)

Thanks!

mind you, in my case, the pullup did not result in the stable behavior, but every case is different.

I tried, 10k pullup resistor from the ESP01 GPIO0 pin to Vcc. Nothing changed. When i start the power on the relay close a shortly.
I use an empty ESP01 - same.

Empty ?
anyway try the capacitor on R2 & GND start with 10uF (watch the polarity)

there actually is a resistor (i think its 470R marked 471) pulling up the pin already, through the PC817.