8-channel relay board code

Hi , I'm new to Arduino . I recently bought an arduino Uno and an 8-channel relay board.
I was browsing on this website ,amongst others , to see if I could find a code for the project I need.
The codes that are available online , all turn the relays on and then off after a delay.

What I would like to do , is use a PIR sensor to trigger the relays .
Now , once the relays are ON , they need to stay on until I push a momentary pushbutton , which will turn all of them off.

The relay board is already hooked up. Where do I hook up the 3-wire PIR sensor and the 2-wire momentary pushbutton ?

Thank you.

You say you are a newbie, and your question certainly shows it. I suggest you take some smaller bites of arduino before you try to swallow a cow. Go find some examples which show you how to interface to a pushbutton, and spend a little time getting the basics of electricity in your head. Trying to start with an advanced project just leads to frustration. With two weeks of evening study, you could know enough to do this without somebody else donig it all for you. Where's the fun in that?

Bear in mind, I'm not criticizing you: I'm just trying to make your arduino journey a lot more satisfying. Slow down and enjoy it.

Good advice.
Some good examples come with the Arduino IDE.

cladinshadows:
I recently bought an arduino Uno and an 8-channel relay board.

Which one. There are several. Provide a link.

cladinshadows:
The relay board is already hooked up.

How. An Uno can switch, but not power eight relays (8x~80mA = 640mA) at the same time.
You most likely need a dedicated power supply for the relay coils.
Leo..

I have been looking and reading about this for a while now and the problem is that I can't find anything that might be helpful for this project.
I'm sorry that I don't know much about Arduinos and if I can't get help with the code , that's fine with me but at least I could use a lead...anything ... that could help me further.

I usually figure things out by myself and don't require help but this time I ran into a brick wall.

As far as the relays go , I just need the arduino to switch them on , not power them.
I will be using a power supply to power the devices that are going to be connected.
This is the board I have :

I don't need 8 relays all at once , maybe 4 or 5 but it's good to have all the relays switch in case I want to add to it.

I am familiarized with Basic Stamps and relay boards and how to use those but not with Arduino.

cladinshadows:
I have been looking and reading about this for a while now and the problem is that I can't find anything that might be helpful for this project.
I'm sorry that I don't know much about Arduinos and if I can't get help with the code , that's fine with me but at least I could use a lead...anything ... that could help me further.

The tutorials should help.

The first thing to do is ensure you have your relay module connected properly and adequately powered. This example shows how to make use of the optic isolation feature.

INSTRUCTIONS:

  • Remove blue jumper
  • Connect JD-VCC and GND from the Relay Module to a Separate 5VDC Supply (1Amp or greater)
  • Connect VCC from the Relay Module to 5V on your Arduino
  • Connect required control signals as shown
  • Note: DO NOT connect GND from the Relay Module to your Arduino

dlloyd:
The first thing to do is ensure you have your relay module connected properly and adequately powered. This example shows how to make use of the optic isolation feature.

INSTRUCTIONS:

  • Remove blue jumper
  • Connect JD-VCC and GND from the Relay Module to a Separate 5VDC Supply (1Amp or greater)
  • Connect VCC from the Relay Module to 5V on your Arduino
  • Connect required control signals as shown
  • Note: DO NOT connect GND from the Relay Module to your Arduino

Thank you.

1 Like

aarg:
The tutorials should help.

Actually , none of the tutorials are giving me any information on how to keep the relays ON without switching off until a button is pushed.
Every tutorial I’ve come across uses code that turns a relay on , have a delay and turn back off.

cladinshadows:
Actually , none of the tutorials are giving me any information on how to keep the relays ON without switching off until a button is pushed.
Every tutorial I've come across uses code that turns a relay on , have a delay and turn back off.

Have you checked this one?

Try not to find a ready made solution (it most likely won't exist). Then what do you do if you need to make a change? Once you get comfortable with how the state change method works, it'll open up possibilities and make it easy to add relay control.

EDIT: Then, if you need to make things run a little smoother, take a look at these examples:

dlloyd:
Have you checked this one?

Try not to find a ready made solution (it most likely won’t exist). Then what do you do if you need to make a change? Once you get comfortable with how the state change method works, it’ll open up possibilities and make it easy to add relay control.

EDIT: Then, if you need to make things run a little smoother, take a look at these examples:
https://www.arduino.cc/en/Tutorial/Debounce
https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

Now , this is something I can use.
Like I said before , I don’t know anything about code , so seeing StateChangeDetection and Debounce , that didn’t sound like anything I could use.

Thank you dlloyd for steering me in the right direction.
It is greatly appreciated.

/*
Please note as I have used the android app you told , the string inputs are taken as
A,B,C,D,E,F,G and a,b,c,d,e,f,g.
I have also written code for voice on and off which has all on and all off function
but since the android app we are using dosent have such key so i didnt took them
*/
String inputs;
#define relay1 2 //Connect relay1 to pin 9
#define relay2 3 //Connect relay2 to pin 8
#define relay3 4 //Connect relay3 to pin 7
#define relay4 5 //Connect relay4 to pin 6
#define relay5 6 //Connect relay5 to pin 5
#define relay6 7 //Connect relay6 to pin 4
#define relay7 8 //Connect relay7 to pin 3
#define relay8 9 //Connect relay8 to pin 2
void setup()
{
Serial.begin(9600); //Set rate for communicating with phone
pinMode(relay1, OUTPUT); //Set relay1 as an output
pinMode(relay2, OUTPUT); //Set relay2 as an output
pinMode(relay3, OUTPUT); //Set relay1 as an output
pinMode(relay4, OUTPUT); //Set relay2 as an output
pinMode(relay5, OUTPUT); //Set relay1 as an output
pinMode(relay6, OUTPUT); //Set relay2 as an output
pinMode(relay7, OUTPUT); //Set relay1 as an output
pinMode(relay8, OUTPUT); //Set relay2 as an output
digitalWrite(relay1, LOW); //Switch relay1 off
digitalWrite(relay2, LOW); //Swtich relay2 off
digitalWrite(relay3, LOW); //Switch relay1 off
digitalWrite(relay4, LOW); //Swtich relay2 off
digitalWrite(relay5, LOW); //Switch relay1 off
digitalWrite(relay6, LOW); //Swtich relay2 off
digitalWrite(relay7, LOW); //Switch relay1 off
digitalWrite(relay8, LOW); //Swtich relay2 off
}
void loop()
{
while(Serial.available()) //Check if there are available bytes to read
{
delay(10); //Delay to make it stable
char c = Serial.read(); //Conduct a serial read
if (c == '#'){
break; //Stop the loop once # is detected after a word
}
inputs += c; //Means inputs = inputs + c
}
if (inputs.length() >0)
{
Serial.println(inputs);

if(inputs == "A")
{
digitalWrite(relay1, LOW);
}
else if(inputs == "a")
{
digitalWrite(relay1, HIGH);
}
else if(inputs == "B")
{
digitalWrite(relay2, LOW);
}
else if(inputs == "b")
{
digitalWrite(relay2, HIGH);
}
else if(inputs == "C")
{
digitalWrite(relay3, LOW);
}
else if(inputs == "c")
{
digitalWrite(relay3, HIGH);
}
else if(inputs == "D")
{
digitalWrite(relay4, LOW);
}
else if(inputs == "d")
{
digitalWrite(relay4, HIGH);
}
else if(inputs == "E")
{
digitalWrite(relay5, LOW);
}
else if(inputs == "e")
{
digitalWrite(relay5, HIGH);
}
else if(inputs == "F")
{
digitalWrite(relay6, LOW);
}
else if(inputs == "f")
{
digitalWrite(relay6, HIGH);
}
else if(inputs == "G")
{
digitalWrite(relay7, LOW);
}
else if(inputs == "g")
{
digitalWrite(relay7, HIGH);
}
else if(inputs == "H")
{
digitalWrite(relay8, LOW);
}
else if(inputs == "h")
{
digitalWrite(relay8, HIGH);
}
inputs="";
}
}

1 Like