Operating relay from HIGH (Positive) switching

Hi,

Due to limitiation of project, I had to use HIGH signal (positive) to operate a relay.
But below code doesn’t worked. I programmed some projects earlier but I forgot all my basics. Please help me go through this.

Button is at PIN2, sending Postive signals
Relay is at PIN 4, requiring Negative signal to operate.

const byte ButtonInPin =  2; //Grid Sense INPUT
const byte RelayOutPin =  4; // Heater OUTPUT

void setup()
{
  pinMode(ButtonInPin, INPUT); //Grid Sense INPUT
  pinMode(RelayOutPin, OUTPUT); // Heater OUTPUT
  
  digitalWrite(RelayOutPin, HIGH); //Turn off Heater contactor
}

void loop() {
 
  if (digitalRead(ButtonInPin) == HIGH) {digitalWrite(RelayOutPin, LOW) ;}
delay (1000);
  digitalWrite(RelayOutPin, HIGH) ;
   }

While do you have a delay every time round loop() - surely that’s just for if-case when switching the
relay.

Anyway you have to say what you expect the sketch to do, and what it is actually doing, not just
“doesn’t worked”

Hi
Thanks,

My object is to:

When I get + signal at PIN 2 and operate relay at PIN 4
When there is no singal at PIN 2 then relay to be off at PIN 4

How is the input wired ?
Do you have a pull down resistor in place or does it float at an unknown voltage ?

To change the logic of whether the relay is actuated when the button is pressed or not simply change the values written by digitalWrite() or if the relay has changeover contacts then change your wiring to suit your requirements

And .... how is the relay powered ?

hammy:
And .... how is the relay powered ?

Relay is powered by board

UKHeliBob:
How is the input wired ?
Do you have a pull down resistor in place or does it float at an unknown voltage ?

To change the logic of whether the relay is actuated when the button is pressed or not simply change the values written by digitalWrite() or if the relay has changeover contacts then change your wiring to suit your requirements

Input is wired by a source from other device (both sharing same power), the input could have either +voltage to operate relay or no voltage.

Please suggest piece of code.

My target is to operate a relay with +signal as input to nano.

Please suggest piece of code.

void loop()
{
  if (digitalRead(ButtonInPin) == HIGH)
  {
    digitalWrite(RelayOutPin, HIGH);
  }
  else
  {
    digitalWrite(RelayOutPin, LOW);
  }
}

or

void loop()
{
  digitalWrite(RelayOutPin, digitalRead(ButtonInPin));
}