Arduino relay control coding help please

Digital ATV - The latest generation, cutting edge ATV - Please discuss it all here.
Forum rules
This forum is run by the BATC (British Amateur Television Club), it is service made freely available to all interested parties, please do not abuse this privilege.

Thank you
Post Reply
Basil
Posts: 105
Joined: Tue Sep 10, 2013 7:28 pm

Arduino relay control coding help please

Post by Basil » Wed Nov 24, 2021 9:19 pm

I am trying to use an ethernet controlled Arduino on the same LAN as my outside sited Pluto to change the states of a 4 digitally controlled relay module.

I plagiarised some code off the French site, and although they control just fine I find that whatever state the relays are in when powering off the Arduino Uno, on powering back up (powered via a USB cable at the moment) all relays immediately go to the energised state, which could be a problem depending on what I use them for.

Is this a facet of the code (attached as a zipped .ino file) or a facet of how the Arduino ports work on boot up please? Thanks.
Attachments
ethernet-relay-control.zip
(3.17 KiB) Downloaded 21 times

g0mjw
Posts: 1748
Joined: Sat Sep 20, 2014 9:15 am

Re: Arduino relay control coding help please

Post by g0mjw » Wed Nov 24, 2021 9:52 pm

Here is a clue

// init : je mets toutes les pins à l'état haut car transistor vers relais inverse l'état
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);

So it is written in the comments, these relays need a high to be off, that's common for those relay boards from China. When it starts up the pins will be tri-state so the relays (assuming pull ups) will be on until the Arduino wakes up and turns them off. The only way to fix that (easily) would be to reverse the sense of the relays. Practically you would need to invert the drive signals / modify the relay board and modify the code accordingly. Not difficult.

Mike
Last edited by g0mjw on Wed Nov 24, 2021 11:57 pm, edited 1 time in total.

Basil
Posts: 105
Joined: Tue Sep 10, 2013 7:28 pm

Re: Arduino relay control coding help please

Post by Basil » Wed Nov 24, 2021 10:18 pm

Thanks Mike, I actually found that line and translated the comment just as you posted. Changing the HIGH to LOW now forces the Arduino to start with the relays de-energised, which is what I think (for my potential uses) is best.

I had already altered the code to reverse states as below:

// checks if received HTTP request is switching on/off Relays
// also saves the state of the Relays
void SetRelays(void)
{
// Relay 1 (pin 6)
if (StrContains(HTTP_req, "Relay1=1")) {
Relay_state[0] = 1; // save Relay state
digitalWrite(6, HIGH);
}
else if (StrContains(HTTP_req, "Relay1=0")) {
Relay_state[0] = 0; // save Relay state
digitalWrite(6, LOW);
}
// Relay 2 (pin 7)
if (StrContains(HTTP_req, "Relay2=1")) {
Relay_state[1] = 1; // save Relay state
digitalWrite(7, HIGH);
}
else if (StrContains(HTTP_req, "Relay2=0")) {
Relay_state[1] = 0; // save Relay state
digitalWrite(7, LOW);
}
// Relay 3 (pin 8)
if (StrContains(HTTP_req, "Relay3=1")) {
Relay_state[2] = 1; // save Relay state
digitalWrite(8, HIGH);
}
else if (StrContains(HTTP_req, "Relay3=0")) {
Relay_state[2] = 0; // save Relay state
digitalWrite(8, LOW);
}
// Relay 4 (pin 9)
if (StrContains(HTTP_req, "Relay4=1")) {
Relay_state[3] = 1; // save Relay state
digitalWrite(9, HIGH);
}
else if (StrContains(HTTP_req, "Relay4=0")) {
Relay_state[3] = 0; // save Relay state
digitalWrite(9, LOW);
}
}

So as to have the buttons showing off when the relays are *de-enegised* as the opposite was occurring with the original code. Needless to say I have never dabbled in coding at all, so this is all very new to me, but, as far as I can tell, these relays should now do the job. I also see why some relay modules have jumpers to affect how HIGH and LOW digital inputs set the relay states, and will probably look to buying that type in future.

Thanks again!

g0mjw
Posts: 1748
Joined: Sat Sep 20, 2014 9:15 am

Re: Arduino relay control coding help please

Post by g0mjw » Wed Nov 24, 2021 10:56 pm

These relay boards come in different senses. Probably the author had relays opposite to yours.

I have several boards, of different types. Some have an opto-isolator connected via a resistor to VCC. This rather defeats the purpose of the opto-isolator and you need to ground the input to turn on the relay. Others have the opto isolator wired the other way so you have to set the input high to turn it on. Other others have a hi-lo selection via a jumper, either for each relay or one for all of them. Beware that those that require grounding to turn on will have VCC on the input pins. If that is 12V, it far exceeds the level you are supposed to put on the IO pins. 5V relays are OK for 5V tolerant IO. However, if you use 12V relays the arduino may survive as there is a resistor in line to limit the current. Not sure about the PI. Best to make sure that the 12V boards are configured active high. I cut off the VCC pin on the jumper to avoid accidents. Active low relays are handy for PTT input switching as long as your PTT source can handle the voltage. You can combine PTTs as well with diodes to isolate the sources.

Anyway, whatever the relay sense, if the start up is fast enough you should be able to set them however you want in the setup() function before the relay has had time to energise. With the Arduino running at 16MHz it's very likely to be fast enough, unless you do other stuff setting up.

Mike

Basil
Posts: 105
Joined: Tue Sep 10, 2013 7:28 pm

Re: Arduino relay control coding help please

Post by Basil » Thu Nov 25, 2021 9:33 am

I'll be sure to check any relay modules for voltage on the input pins if used with micro controllers, thanks for taking the time to list this very useful information Mike.

Post Reply

Return to “DATV - Digital ATV”