Arduino project_ Master slave with rs232

I am completely new to arduino and need some advice on if my project idea is feasable.

I am using 2 arduino micro boards as a basis.

One will be a controller for the other board (master, slave type set up).

The slave is only required to close a relay when a button is pressed on the master.

I need the link between the master and slave to be rs232..

How would i go about this? or any advice? The rs232 is mandatory as it will integrate into a system that uses rs232 data channels only.

thanks in advance

Welcome to the forum,

RS232 comes with different voltage levels, so use a converter if needed.

MOst important is to design a robust protocol

  • handshake
  • begin of packet byte end of packet byte
  • use of checksum to test integrity of command
  • use confirmations on receive of a package - ACK and NACK
  • design it to handle failure. (flush

For longer distances you might consider RS485 ?

That should be very straight forward - especially with a Micro as pins 0 and 1 are available as Serial1 for communication.

The only cautionary note - since you say you want to work with an existing RS232 system - is that standard RS232 uses 12v wheras the Arduino can only use 5v. You can get chips such as the Max232 that are designed to change the signal levels.

The examples in serial input basics illustrate how to receive data reliably. As the examples are written they use Serial (for communication with a PC) but they can easily be changed to use Serial1 to communicate with another Arduino.

You may also find something useful in planning and implementing a program and in the many examples that come with the Arduino IDE.

...R

Thanks for the replies :slight_smile:

The system this project will integrate into uses rs232 multiplexed through a multmode fiber, so distance is not an issue. It has multiple rs232 comms channels and has 1 available for 5vdc signal levels.

Cheers for the links i will have a thorough read through now.

and i am sure i will have more questions after :slight_smile:

So i quickly tried two boards with these codes... but I could not get it to work.

I used a serial to usb converter to confirm i was receiving data out on digital pin 1 on the transmitting board. It showed 0 on no button press and 1 on button press, as expected.

I then used the same serial converter to input 1 on to the rx pin (digital input 0) through hyper terminal.. and the led came on when 1 was pressed and was off when 0 was pressed.. as expected.

Though, when connecting tx from the transmitting board to rx on the receiving... button presses produced no effect...

I am sure i have probably missed something simple:)

any ideas?

/*
  DigitalReadSerial
 Reads a digital input on pin 2, prints the result to the serial monitor 
 
 This example code is in the public domain.
 */

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  // print out the state of the button:
  Serial.println(buttonState);
  delay(2000);        // delay in between reads for stability
}
/*
 Simple LED sketch
 */
     
int led = 13; // Pin 13
     
void setup()
{
    pinMode(led, OUTPUT); // Set pin 13 as digital out
     
    // Start up serial connection
    Serial.begin(9600); // baud rate
    Serial.flush();
}
     
void loop()
{
    String input = "";
     
    // Read any serial input
    while (Serial.available() > 0)
    {
        input += (char) Serial.read(); // Read in one char at a time
        delay(5); // Delay for 5 ms so the next char has time to be received
    }
     
    if (input == "1")
    {
        digitalWrite(led, HIGH); // 1
    }
    else if (input == "0")
    {
        digitalWrite(led, LOW); // 0
    }
}

Hi dbeat

Though, when connecting tx from the transmitting board to rx on the receiving... button presses produced no effect...

Have you connected the GNDs from each board together?

Regards

Ray

Hackscribble:
Hi dbeat

Have you connected the GNDs from each board together?

Regards

Ray

I sure have. Its got me stumped

Thanks for the reply...

Try this simpler version of loop():

void loop()
{
    if (Serial.available())
   {
        char input = Serial.read();
        if (input == '1')
       {
           digitalWrite(led, HIGH);
       }
       else if (input == '0')  // only respond to '1' or '0', ignore CR, LF, etc
       {
           digitalWrite(led, LOW);
       }
    }
}

Hackscribble:
Try this simpler version of loop():

void loop()

{
    if (Serial.available())
  {
        char input = Serial.read();
        if (input == '1')
      {
          digitalWrite(led, HIGH);
      }
      else if (input == '0')  // only respond to '1' or '0', ignore CR, LF, etc
      {
          digitalWrite(led, LOW);
      }
    }
}

that did it! Thanks Ray!

Any pointers on what may have been the issue with the original code?

I think the input string was picking up extra characters, so your strict comparisons with "1" and "0" were failing.

On reflection, it might have been enough to change this in your sending program, so that CR and LF characters were not sent:

Serial.print(buttonState); // not println

If your requirement is not safety / mission critical, and your comms links are clean, you can probably get away with this simple "single character message" approach.

But if you need to send more data items and/or confirm receipt of the commands, follow the advice above from @robtillaart and @Robin2.