Max485 nano-mega relay controller

Hello there!

As in the title I'm trying to build a manual relay controller with a Nano board, a Mega and MAX485 Modules to connect the two.

I already built a similar set but with two nanos and nrf24l01.

I thought it would be similar to what I did in that project: sending an array containing the buttons state from the sender nano to the receiver which then will control the relays.

As I have learned spi in the nrf24l01 and serial communication are way too much different.
The problem I have is that there seem to be no similar example or project. I can only find projects where serial monitor is used to control a led. And they only are with toggle mode. Or projects where only one comand is sent between two Arduinos, and these also are toggle mode.

What I would like this thing to do is, to have the Nano reading (once every half second) the state of 12 momentary switches, and send this configuration to the Mega by the max485 Modules.
The mega should read the configuration and trigger the relative relay for the time that the button is pressed.

Now, if anyone knows somewhere to get some info I would appreciate it.
Or maybe someone who manages this like bike ride to lay down some code lines for sender and receiver. :smiley:

I have tried learning how serial communication and write/read functions work but I'm banging head over nothing.

Thanks for reading and helping.

If needed I could post the code from the nrf24l01 controller to maybe explain myself better.

Look at it this way:
Serial monitor can deliver data, commands etc to its controller. Then the controller evaluates, changes, manipulates data and sends them out.

Don't search for complete projects that do what You want. They seldomly exist. Look for the pieces in the jig saw puzzle!

Again, split the chain of devices and logics into smaller pieces and find solutions to those small steps. Else You will mostly (always?) get stuck.

Thank you for your answer. That is actually what I tried to do.
But I really haven't found any one that sends and receive a whole configuration in one single package (like I did with the wireless version). :sweat_smile:
And the attempts I did didn't even passed the compile phase on the IDE :smiling_face_with_tear:

how far apart are the nano and mega? if within a few metres of each other TTL serial would probably be OK - if further apart use RS232 shields
you only require RS485 if you are connecting more than two serial devices or using a protocol such as Mobus
which Nano are you using? there are several
any particular reason why you are using two microcontrollers?

I think the two boards will be like 3m far but the cable wont be in a straight line between the two. So I think the cable will be between 5 to 7 meters.

The need to use two microcontrollers comes from the fact that the mega will be controlling 25 relays. But only 13 of them will need to be controlled at the same time.

So two sets of 12 relays and one that should be triggered together with any of the others.

The nano board on the sender side is to limit the number of switches to install on the controller. It will have six SPDT momentary lever switches and another permanent switch that will select which one of the relay set to control.

Thank you for the answer

Edit: I forgot answering a question. I chose the max485 Modules because I read that it is very reliable in transmission and insensitive to noise. (The cable will be routed aside 4 12V DC power cables)

what loads are the relays switching ? the typical Arduino relay is rated at 10amps 250V resistive load although I would not try to switch more than 5 or 6amps - if it is an inductive load (e.g. a pump motor) no more than 2 or 3 amps

I doubt if you require RS485 - it also has the added complication of being half duplex and one has to control the receive/transmit switching - try a couple of RS232 shields

Sending f ex an array containing the information, coded as You like it, ought to be possible to find.
Tell in detail how Your "configuration" is done. Array? String?...

I have never managed to run any code that doesn't pass the compiler. The compiler catches the hard, basic errors but not any of all the logic mistakes a programmer makes.

Why?
All large current equipment can be handled provided common powering rules, kick back diodes, dimension of power cables, power supplies, etc. Measures have to be used to kill electrical noise.

That's not ideal but might work. Make sure relays & Co have the noise killers applicable.
Bit coding 25 relays can be held within an unsigned long, 4 bytes. That must be easy peacy to send.

The relays will switch 12V 2,5A. But that is all sorted out. And usually there will be maximum 3 relays switched at the same time.

I thought that since the only one that will need to send is one board it will be simple and ok.
I don't need confirmation of receiving, because the button will be holded and the feedback will be visual.
(The relays will power hydraulic valves and if triggered there will be a moving part).

Sorry, maybe I'm using wrong terminology.
The wireless controller used a struct to send the on/off state of the switches and on the receiving side another struct was built with the data received by radio. I'm writing on my phone now but I can post that part of the code from the PC if you want.

I'm using market available relay boards.
Do they need them?


I'm trying to replace this.

I meant that what I tried to do by modifying some examples didn't even pass the compiler check.
Sorry I'm not English native speaker

It happens. Sometimes easily to see, sometimes not.
You're talking both about wireless, and RS485. Why? For "a few meters" cable is a lot easier.

Please post a link to their datasheets. Usually relay bords are denoised regarding the controller side. Then it's up to You to apply denoising to the load of the relays.

Thanks for the picture but it's impossible to see what board it is.

I'm referring to my other project in which I used nrf24l01. In that case I needed the sender and receiver in two different places, the first on a moving machine and the second on a steady one. At a distance of maximum 30m.


This is the transmitter.

They are the common relay boards that are easily found on Amazon.

Okey but please keep the talking focused to this project. It's easy to misunderstand You, or mix with other questions in forum.

Thanks for the picture but it tells me nothing.

Come on my friend! You really need to do better! Amazon has a jungle of relay boards!
There are 16 posts and not much has been achieved. I'm not prepared for supporting You for ever going on like this.

Sorry to offend or wasting your time.
Not my intention.
I only brought out the wireless thing because in that project the buttons configuration was sent as a struct and wanted to replicate that but on a wired serial transmission.

Sorry for this but the relay board really isn't the problem.
The issue here is how to write a code that makes the nano send by serial a group of ones or zeros, ons or offs, so that at the other side the mega will trigger a relay

Should not be too difficult to using wire.

That ought to be possible. Make transmitter and receiver use the same struct.

So far You have used words, words and words. Nobody can give precise answers to that. You have not posted one line of code of Your attempts as I can see. No helper will send You any ready code based on the communication this far.

You need to be precise, show the record/struct, the code You try... Posting a struct into serial ought to have a lot in common with wireless using some protocol.

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>



RF24 radio(9, 10);  //Set CE and CSN pins
const uint64_t my_radio_pipe = 0xE8E8F0F0E1LL; //Remember that this code should be the same for the receiver

// The sizeof this struct should not exceed 32 bytes
struct Data_to_be_sent {
  byte ch1;
  byte ch2;
  byte ch3;
  byte ch4;
  byte ch5;
  byte ch6;
};

//Create a variable with the structure above and name it sent_data
Data_to_be_sent sent_data;
  
//assigning pins to the swithes
int apri     = 2;   //open the cart switch
int chiudi   = 3;   //close the cart switch
int abbassa  = 4;   //lower the fork switch
int alza     = 5;   //rise the fork switch
int indietro = 6;   //move backward the bales switch
int avanti   = 7;   //move forward the bales switch

//--------------------------------------------------------------------------------------- 
void setup() 
{

  Serial.begin(9600);

  pinMode(apri,     INPUT_PULLUP); 
  pinMode(chiudi,   INPUT_PULLUP);
  pinMode(abbassa,  INPUT_PULLUP);
  pinMode(alza,     INPUT_PULLUP);
  pinMode(indietro, INPUT_PULLUP);
  pinMode(avanti,   INPUT_PULLUP);
  
  // begin and radio configuration
  radio.begin();
  radio.setAutoAck(false);
  radio.setDataRate(RF24_250KBPS);
  radio.openWritingPipe(my_radio_pipe);

  //Reset each channel value
  sent_data.ch1 = 1;
  sent_data.ch2 = 1;
  sent_data.ch3 = 1;    // 1 (high) to keep the relays untriggered
  sent_data.ch4 = 1;
  sent_data.ch5 = 1;
  sent_data.ch6 = 1;
  
}

//---------------------------------------------------------------
void loop() 
{
  sent_data.ch1 = digitalRead(apri);
  sent_data.ch2 = digitalRead(chiudi);
  sent_data.ch3 = digitalRead(abbassa);
  sent_data.ch4 = digitalRead(alza);
  sent_data.ch5 = digitalRead(indietro);
  sent_data.ch6 = digitalRead(avanti);

  Serial.println(sent_data.ch1);
  Serial.println(sent_data.ch2);
  Serial.println(sent_data.ch3);
  Serial.println(sent_data.ch4);
  Serial.println(sent_data.ch5);
  Serial.println(sent_data.ch6);
  
  Serial.println(" "); //leave a blank line on the serial monitor for better observation

  radio.write(&sent_data, sizeof(Data_to_be_sent));
  
  delay(250);
}

This is the transmitter code for the nrf24l01.
I tried to use a similar way to send the configuration but wasn't successful