Relay control help

Need some help. I'm new to coding. i got some help building this code, I wanted the relay to turn on when the motion sensor was activated then shut off in 5 seconds. But when i turn the board on it activates the relay for 1m50s then shuts off. when the motion sensor is activated the relay stays on for the same amount of time. I'm using an esp-wroom-32 board. Any help would be appreciated.

const int relayPin = 17; // GPIO pin connected to SRD-03VDC-SL-C relay
const int pirPin = 16; // GPIO pin connected to HC-SR501 PIR sensor

void setup() {
Serial.begin(115200);
pinMode(relayPin, OUTPUT);
pinMode(pirPin, INPUT);

// Set relay and pir pins as digital outputs/inputs
digitalWrite(relayPin, LOW);
digitalWrite(pirPin, HIGH);
}

void loop() {
int pirState = digitalRead(pirPin);

if (pirState == HIGH) { // PIR sensor is triggered
Serial.println("Motion detected!");
digitalWrite(relayPin, HIGH); // Turn on relay

delay(5000); // Wait for 5 seconds
digitalWrite(relayPin, LOW); // Shut off relay
Serial.println("Relay shut off.");

}

Welcome to the forum

As you using a generic ESP32 board rather than Nano ESP32 board your topic has been moved to a more generic forum category

Please post your sketch, using code tags when you do. This prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.


const int relayPin = 17;  // GPIO pin connected to SRD-03VDC-SL-C relay
const int pirPin = 16;    // GPIO pin connected to HC-SR501 PIR sensor

void setup() {
  Serial.begin(115200);
  pinMode(relayPin, OUTPUT);
  pinMode(pirPin, INPUT);



  // Set relay and pir pins as digital outputs/inputs
  digitalWrite(relayPin, LOW);
  digitalWrite(pirPin, HIGH);
}

void loop() {
  int pirState = digitalRead(pirPin);

  if (pirState == HIGH) {  // PIR sensor is triggered
    Serial.println("Motion detected!");
    digitalWrite(relayPin, HIGH);  // Turn on relay

    delay(5000);                  // Wait for 5 seconds
    digitalWrite(relayPin, LOW);  // Shut off relay
    Serial.println("Relay shut off.");
  }

Thank you

Thank you

I hope that you can see how much easier it is to see the code when it is posted in code tags

Absolutely. the joys of being new to all this

The first thing to check is whether writing LOW to the relay turns it off or on. It is common for relay modules to be active LOW, ie turn on when the input is taken LOW

Which Arduino board are you using, how exactly is the relay connected to the Arduino and where does it get its power from ?

Please post a link to the relay that you are using

Hi robe4.
First off the "digitalWrite(pirPin, HIGH);" makes no sense. Why do you want to set an Input Pin High? Second thing is: do you have a freewheel diode with your relay and do you drive your relay through a transistor? Your relay works with 3V and has a coil-resistance of 25 Ohms. Now by using I = U/R we get 120 mA. That is very high Amperage to directly connect to a µC.

However try the code this way:

const int relayPin = 17;  // GPIO pin connected to SRD-03VDC-SL-C relay
const int pirPin = 16;    // GPIO pin connected to HC-SR501 PIR sensor

void setup() {
  Serial.begin(115200);
  pinMode(relayPin, OUTPUT);
  pinMode(pirPin, INPUT_PULLUP);

  digitalWrite(relayPin, LOW);
}

void loop() {
  if (digitalRead(pirPin) == HIGH) {  // PIR sensor is triggered
    Serial.println("Motion detected!");
    digitalWrite(relayPin, HIGH);  // Turn on relay
    delay(5000);                  // Wait for 5 seconds
    digitalWrite(relayPin, LOW);  // Shut off relay
    Serial.println("Relay shut off.");
  }
}

There is a good chance that the relay will turn ON again immediately due to 'bounce' in the PIR.
Add another delay after the write LOW.

This was the original way to turn on the built in pullup resistor. Personally I find a pinMode() of INPUT_PULLUP easier to use and understand, but each to their own

Huh... I didn't know that. I guess you always can learn smth.

Pah - youngsters today ! They know nothing :grinning:

I'm using a SRD-03vdc-SL-C relay connected to the 3vdc on the esp32 board. I've been learning code for 3 days now. So be gentle. And I'm not young, semi retired. Trying to learn new things to keep my brain functioning. This code I'm trying to do is for a Halloween project. Basically it will operate a linear actuator.

Is this the relay?: https://ampul.eu/de/relais-magnetische-kontakte/2683-relais-srd-03vdc-sl-c-3v-dc-250v-ac-10a
And is this the PIR?: https://www.az-delivery.de/products/bewegungsmelde-modul

Does the PIR have an adjustment pot that sets how long it stays on after activation ?

this is the relay

and that is the pir

there is 2 adjustments. my luck it will be something simple like that

Ok so the Relay is already "driven" through an optocoupler, so no worries there. But do you use an external PSU or do you just use the power through the USB?

power through the usb

Alright. So i guess you connected your circuitry like this: