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
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.
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.");
}
}
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
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.
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?