Hi I got 4 buttons to 4 relays
I want to push a button 1 and a relay 1 must pull in and stay in for 3sec then kick out, for each of them.
There must be a 2min delay before another button 2,3 or 4 can be pushed.
Hello memepowerteam
Welcome to the worldbest Arduino forum.
It seems to be a school assigment, isn´t it?
Post your sketch, well formated, with well-tempered comments and in so called code tags "</>" and schematic to see how we can help.
Have a nice day and enjoy coding in C++.
hi I'm 56 and just got a Arduino from my son junk so i want to build a small project, I been checking YouTube but they all do it latching and not Deadman action, I can also get one to do wat I want but then it seems to become greek to me ( sorry for the Greeks)
how do I post my sketch as it tells me new users cannot upload
No, I'm 56 and got an Arduino from my son's junk, so I'm trying to do a little project. I dun some other things and I enjoy Arduino.
I want to push button 1 and re-lay 1 must pull in for 3sec and kick out, the same with others. there must be a 2min delay before you can push the next button.
Thanks, I'll appreciate it alot with some help.
4-b to 4-r eeee.pdf (212 KB)
Does your "little project" involve a "kill switch" or "safety cutoff" that protects people from danger?
No, I'm 56
Lots of those taking school courses...
Please, don’t flaunt your youth at us, we fell bad enough already.
-
The relays need a positive to their coils.
-
Each relay needs a driver transistor.
-
Each relay needs a kickback diode across the coil.
-
Use diagonal pins on the switch, as it appears they are not going to work the way you have them.
It's provided by the Arduino I/O pins. I'm not saying it's an appropriate thing to do... posting for clarification.
You really must read the forum introductory guidelines about how to post...
Sure, not cool. I wonder where the idea came from? Still waiting for an answer to reply #6.
If that's the setup I found this
So operated at 5V pins the current would be 0.20 W / 5 V = 40 mA if I'm not mistaken. That is close to the very limit. Hope the relays know about that.
I would recommend to use an uln2003a chip with the NPN darlingtons. It is quite cheap and easy to handle also for the less experienced. And they have integrated suppression diodes for inductive
loads already.
A simple explanation can be found here
https://pijaeducation.com/arduino/relay/interfacing-of-relay-with-arduino-using-uln2003/
But I do not recommend to use the code they provide there for testing; it's not best practice regarding
- debouncing of buttons
- use of millis() instead of delay() in loop()
ec2021
Interesting, but it is not the easiest beginner method, which is using relay PCB modules. Still no answer to reply #6.
Agree, but if these relays are already there ... I found ULN easier than wiring some NPN transistors and suppression diodes and resistors ...
Source: https://microcontrollerslab.com/relay-driver-circuit-using-uln2003/
But let's see how reactive our young TO is ...
P.S.: There is in minimum one solution more than developers involved ...
Actually there are pending requests from before post #6...
Post your sketch, well formated, with well-tempered comments and in so called code tags "</>" and schematic to see how we can help.
New users are asked to read the instructions for that, did you skip over it?
40mA, that's the maximum value where operating at too long at can damage the controller.
Use a 20mA maximum for the safe operating maximum.
Agree, in this specific case the relay may be supplied for a short time (here it says 3 sec) ...
But anyway, I would go with a PCB relay (usually with optocouplers), transistors or the ULN2003A.
Show us the code you have written so far.
I would be nervous about driving them directly. Even for 3 seconds...
OP has left the building...
Unless you have a pile of Arduino boards trying to get rid off ... ;-))
Anyway, it was waiting time enough to at least solve the software issue
/*
https://forum.arduino.cc/t/4-buttons-to-control-4-relays/1109997
https://wokwi.com/projects/360921014043806721
=======================================
Switch one out of four relay on/off for 3 seconds
Block all buttons for a 2 minutes (here for test purposes 10 seconds only)
=======================================
2023-04-02
coded by ec2021
*/
constexpr byte button1Pin = 7;
constexpr byte button2Pin = 6;
constexpr byte button3Pin = 5;
constexpr byte button4Pin = 4;
constexpr byte relay1Pin = 13;
constexpr byte relay2Pin = 12;
constexpr byte relay3Pin = 11;
constexpr byte relay4Pin = 10;
struct ButtonType {
byte Pin;
unsigned long lastChange = 0;
int lastState = HIGH;
int state = HIGH;
boolean released();
};
boolean ButtonType::released() {
int actState = digitalRead(ButtonType::Pin);
if (actState != ButtonType::lastState) {
ButtonType::lastChange = millis();
ButtonType::lastState = actState;
}
if (actState != ButtonType::state && millis() - ButtonType::lastChange > 50) {
ButtonType::state = actState;
if (actState) return true;
};
return false;
}
ButtonType button1;
ButtonType button2;
ButtonType button3;
ButtonType button4;
constexpr int NoOfRelais = 4;
byte relayPin[NoOfRelais] = {relay1Pin, relay2Pin, relay3Pin,relay4Pin};
void setup() {
Serial.begin(115200);
Serial.println("Start");
button1.Pin = button1Pin;
button2.Pin = button2Pin;
button3.Pin = button3Pin;
button4.Pin = button4Pin;
pinMode(button1.Pin, INPUT_PULLUP);
pinMode(button2.Pin, INPUT_PULLUP);
pinMode(button3.Pin, INPUT_PULLUP);
pinMode(button4.Pin, INPUT_PULLUP);
for (int i = 0; i < NoOfRelais;i++) pinMode(relayPin[i], OUTPUT);
}
constexpr unsigned long RelaySwitchTime = 3000UL; // 3 sec = 3000 milliseconds
constexpr unsigned long ButtonBlockTime = 10000UL; // 10 sec = 10000 milliseconds
//constexpr unsigned long ButtonBlockTime = 120000UL; // 2 min = 120 sec = 120000 milliseconds
int SwitchNo = 0;
void loop() {
HandleButtons();
HandleRelaisOnOff();
}
void HandleButtons(){
static boolean BlockButtons = false;
static unsigned long lastButtonBlockTime = 0;
if (!BlockButtons) {
if (button1.released()) SwitchNo = 1;
if (button2.released()) SwitchNo = 2;
if (button3.released()) SwitchNo = 3;
if (button4.released()) SwitchNo = 4;
if (SwitchNo > 0) {
lastButtonBlockTime = millis();
BlockButtons = true;
Serial.println("Buttons disabled");
}
} else {
if (millis() - lastButtonBlockTime > ButtonBlockTime){
BlockButtons = false;
Serial.println("Buttons enabled");
}
}
}
void HandleRelaisOnOff(){
static int RelayOn = 0;
static boolean RelaySwitchedOn = false;
static unsigned long lastRelayOnTime = 0;
if (SwitchNo) {
lastRelayOnTime = millis();
RelayOn = SwitchNo-1;
SwitchNo = 0;
digitalWrite(relayPin[RelayOn],HIGH);
RelaySwitchedOn = true;
Serial.println("Relay\t"+String(RelayOn+1)+ " on");
}
if (RelaySwitchedOn && millis() - lastRelayOnTime > RelaySwitchTime){
digitalWrite(relayPin[RelayOn],LOW);
RelaySwitchedOn = false;
Serial.println("Relay\t"+String(RelayOn+1)+ " off");
}
}
See https://wokwi.com/projects/360921014043806721
And yes, I used the PCB relay boards (as they were available in Wokwi )
So farewell, I've done my good deeds for today ...
ec2021
It is made as a mixture of using arrays for relay and discrete structures for button ... Could be written less verbose but I hope it is more understandable for people new to coding.