I am hoping some one can help me with this programming. I am new to this. I have a 5 volt in put. Turning on a relay I want the relay to turn on for 3 seconds then turn off. And stay off l. till the switched 5 volt signal is turned off then back on again. Is this possible.
What kind of event turns the relay on? Which relay? Which Arduino? How is it all powered?
Could you please post a simple schematic showing how you have wired everything? Additionally, share links to the technical documentation for the hardware devices, such as the relay and button. If you’ve already written some code, please post it as well, regardless of whether it works; it will serve as a starting point.
If you read and understand the IDE example "state change detection" you will see how to do something once when a button goes from not being pressed to being pressed.
Whe the button becomes pressed, turn on the relay, delay for three seconds and turn the relay off.
The button is a stand-in for whatever it is the signal you want to react to each time it goes from low to high, or high to low.
a7
Please start even earlier:
a7
Doesn't sound difficult, but I bet there is more to it. New folks often leave stuff out they think isn't important but it turns out to be a project killer.
@tommy699 connecting the wrong relay to a controller pin could possibly damage the controller, look for an optically isolated relay that has low trigger current perhaps something like this
Use a NE555 to build a monoflop.
Read the pinned post re How to get the best from the forum. Code has to be posted in a specific way. Also look for a tutorial on buttons.
You should post code as a code-section. Take 3 minutes time to learn how to do it with even less effort than uploading a picture
Your code has to be modified to detect the transitions from low to high
if this transition is detected switch on relay and start a timer
check if 3 seconds of time have passed by if yes switch relay off
then check for transition from high to low to reset
The best way to do this is to use a code called state-machine
state 1: wait for IO-pin to change to HIGH
if this happends switch relay on, start timer and switch to state 2
state 2: check if 3 seconds of time have passed by
if this happends switch relay off and change to state 3
state 3: check if switch changes to low
if this happends change to state 1
Here is a tutorial that shows the principle of state-machines
@tommy699 here are some examples for monoflops:
https://werner.rothschopf.net/202003_arduino_retriggerbares_nachlaufrelais.htm
(page is in German, but the code examples use variables names in English)
The scenario you're describing is called a one-shot timer. Here's some basic code for a one-shot. It requires a trigger input (pin 2) to pull in the relay, but you can edit it so it turns on immediately at startup.
look this over
- it demonstrates how to recognize the change in state of a mechanical switch that bounces
- consider what you want to happen whe the sw closes
const byte PinOut = 12;
const byte PinSw = 4;
byte swState;
// -----------------------------------------------------------------------------
void
loop (void)
{
byte sw = digitalRead (PinSw);
if (swState != sw) {
swState = sw;
delay (20); // debounce
if (LOW == sw)
digitalWrite (PinOut, ! digitalRead (PinOut));
}
}
// -----------------------------------------------------------------------------
void
setup (void)
{
Serial.begin (9600);
pinMode (PinSw, INPUT_PULLUP);
swState = digitalRead (PinSw);
pinMode (PinOut, OUTPUT);
}
Yes! This is definitely possible using an Arduino and a relay module. You can achieve this by detecting the 5V signal as an input, turning the relay on for 3 seconds, and ensuring it stays off until the input is toggled off and back on.
Here’s a simple approach using an Arduino:
const int relayPin = 7; // Relay connected to pin 7
const int inputPin = 2; // 5V signal input
bool triggered = false;
void setup() {
pinMode(relayPin, OUTPUT);
pinMode(inputPin, INPUT);
}
void loop() {
if (digitalRead(inputPin) == HIGH && !triggered) {
digitalWrite(relayPin, HIGH); // Turn relay ON
delay(3000); // Keep it ON for 3 seconds
digitalWrite(relayPin, LOW); // Turn relay OFF
triggered = true;
}
if (digitalRead(inputPin) == LOW) {
triggered = false; // Reset when input is turned OFF
}
}
This code ensures that every time the 5V signal is applied, the relay turns on for 3 seconds and stays off until the input is reset.
If you want to dive deeper into relay control with Arduino, you can check it out here: https://playwithcircuit.com/interfacing-5v-4-channel-relay-module-with-arduino/
Hope this helps!