How to communicate between Arduinos using SoftwareSerial.h

On my Uno the standard library example "SoftwareSerialExample" just prints "Goodnight moon!" and then a whole bunch of junk to the serial monitor:

Goodnight moon! ·odngÿt mÿon!ÿ ·dgt onÿ ·onn

If I add this line: pinMode(11, OUTPUT); then the junk changes to:

Goodnight moon! ·odnÿghtÿmooÿ! ·ntÿ

I changed Serial, softSerial and serial monitor all to 4800 but its still not working.

What pins can be used? The example uses 10 (RX) and 11 (TX). I connected pin 10 to pin 1 (TX) and pin 11 to pin 0 (RX) on my Uno. I want to use pins 4,5 for softserial in my project. I just want to send two ints (wheel speed setpoints) from a Uno to a Nano. I have been trying all afternoon to get this softserial example working. Are there any examples of using SoftwareSerial between two Arduinos? Should I use I2C? Would that be easier?

And your code is where?

The code is the standard SoftwareSerialExample.ino example code that is supplied with the SoftwareSerial library installed with the Arduino.exe 1.0.5 IDE. You can find it in your SoftwareSerial library. Here is a copy if you don’t have it:

/*
  Software serial multple serial test
 
 Receives from the hardware serial, sends to software serial.
 Receives from software serial, sends to hardware serial.
 
 The circuit: 
 * RX is digital pin 10 (connect to TX of other device)
 * TX is digital pin 11 (connect to RX of other device)
 
 Note:
 Not all pins on the Mega and Mega 2560 support change interrupts, 
 so only the following can be used for RX: 
 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
 
 Not all pins on the Leonardo support change interrupts, 
 so only the following can be used for RX: 
 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
 
 created back in the mists of time
 modified 25 May 2012
 by Tom Igoe
 based on Mikal Hart's example
 
 This example code is in the public domain.
 
 */
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(4800);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(4800);
  mySerial.println("Hello, world?");
}

void loop() // run over and over
{
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
}

Do you really have a myserial attached to those pins?

How are the two Arduinos actually connected? Show a picture.

I'm just testing on a single Uno first, trying to send two ints from the software serial port (pins 10,11) to the hardware serial port (pins 0,1) on one Uno. I connected pin 10 (soft RX) to pin 1 (hard TX) and pin 11 (soft TX) to pin 0 (hard RX) on my Uno as described in my post. I guess this is how it should be wired for the library example to test on one Arduino to read and write between the soft and hard ports?

Are you still hooked up to USB? The USB uses the hardware serial.

I have wired pin 10 to pin 4 and pin 11 to pin 5 on my Uno. The USB is on pins 0 and 1.
Why does this code not work? Does parseInt() work with SoftwareSerial? I am trying to send two ints from mySerial1 to mySerial2:

#include <SoftwareSerial.h>
int speedL, speedR;
int setL, setR;

SoftwareSerial mySerial1(10, 11); // RX, TX
SoftwareSerial mySerial2(4, 5); // RX, TX

void setup()  
{
  
  speedL = 123;    // constant left and right wheel set speeds for testing only
  speedR = -456;
  
  Serial.begin(4800);
  Serial.flush();
  mySerial1.begin(4800);
  mySerial1.flush();
  mySerial2.begin(4800);
  mySerial2.flush();
  
}

void loop() 
{
  mySerial1.print(speedL);            //send the two speeds
  mySerial1.print(",");
  mySerial1.print(speedR);
  mySerial1.print('\n');
  
  while (mySerial2.available() > 0) {   //read the two speeds

    int setL = mySerial2.parseInt(); 
    int setR = mySerial2.parseInt(); 
    
    if (mySerial2.read() == '\n') {
      Serial.print("setL = ");
      Serial.print(setL);
      Serial.print(", setR = ");
      Serial.println(setR);
    }  
  }
  delay(100);
}

Did this even compile?

ParseInt does not work on Software serial. ParseInt only works on capital-Strings.

You also need to set Myserial2 as the listener.

I've been experimenting with this for a while and the whole thing is quite confusing. For a start, if you run the same sketch on the one Uno it isn't really what it is designed for. It is designed to send from one Uno to another. And indeed, in your requirements:

I just want to send two ints (wheel speed setpoints) from a Uno to a Nano.

Why not just use Hardware Serial for both ends?

For one thing, for the test to have any hope of working both baud rates need to be the same otherwise you are sending out at 4800 baud and receiving at 57600 baud at the other end of the wire. And then this code:

void loop() // run over and over
{
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
}

Is not echoing back down the Tx line what it got on the Rx line.

Using hardware serial and swapping Tx/Rx should work. Or use I2C if the cable runs are short.

http://www.gammon.com.au/i2c

As for sending numbers, try reading this:

http://www.gammon.com.au/serial

I have finally got it working. parseInt() DOES work with softwareserial. If I used the hardware serial it got confusing with other prints such as debugs on the same serial (USB) line. Here are my solutions for sending values from a Uno to a Nano using SoftwareSerial on any pins. I use pins 4 and 5. You could transmit more than two values using similar logic. Its pretty simple. I hope this helps someone else trying to do this:

//Uno transmitter
// Send two ints to a Nano (wheel speed setpoints)
// Connect pin5 Uno to pin 4 Nano
// Connect pin4 Uno to pin 5 Nano
// Receiver is Nano_reciever.ino

#include <SoftwareSerial.h>
int speedL, speedR;

SoftwareSerial mySerial(4, 5); // RX, TX

void setup()  
{
  
  Serial.begin(4800);
  Serial.flush();
  mySerial.begin(4800);
  mySerial.flush();

  speedL = 123;          // for testing
  speedR = -456;
}

void loop() 
{
  mySerial.print(speedL);            //send the two speeds
  mySerial.print(",");
  mySerial.print(speedR);
  mySerial.print('\n');
  
  Serial.print(speedL);            //just for debugging
  Serial.print(",");
  Serial.print(speedR);
  Serial.print('\n');
  
  delay(100);
}
//Nano reciever
// read two ints from a Uno
// see Uno_transmitter.ino

#include <SoftwareSerial.h>
int setL, setR;

SoftwareSerial mySerial(4, 5); // RX, TX

void setup()  
{
  
  Serial.begin(4800);
  Serial.flush();
  mySerial.begin(4800);
  mySerial.flush();
  
}

void loop() 
{
  if (mySerial.available()) {   //read the two speeds

    int setL = mySerial.parseInt(); 
    int setR = mySerial.parseInt(); 
    
    if (mySerial.read() == '\n') {
      Serial.print("setL = ");
      Serial.print(setL);
      Serial.print(", setR = ");
      Serial.println(setR);
    }  
 }
  delay(100);
}

warren631:
I have finally got it working. parseInt() DOES work with softwareserial. If I used the hardware serial it got confusing with other prints such as debugs on the same serial (USB) line. Here are my solutions for sending values from a Uno to a Nano using SoftwareSerial on any pins. I use pins 4 and 5. You could transmit more than two values using similar logic. Its pretty simple. I hope this helps someone else trying to do this:

//Uno transmitter

// Send two ints to a Nano (wheel speed setpoints)
// Connect pin5 Uno to pin 4 Nano
// Connect pin4 Uno to pin 5 Nano
// Receiver is Nano_reciever.ino

#include <SoftwareSerial.h>
int speedL, speedR;

SoftwareSerial mySerial(4, 5); // RX, TX

void setup() 
{
 
  Serial.begin(4800);
  Serial.flush();
  mySerial.begin(4800);
  mySerial.flush();

speedL = 123;          // for testing
  speedR = -456;
}

void loop()
{
  mySerial.print(speedL);            //send the two speeds
  mySerial.print(",");
  mySerial.print(speedR);
  mySerial.print(’\n’);
 
  Serial.print(speedL);            //just for debugging
  Serial.print(",");
  Serial.print(speedR);
  Serial.print(’\n’);
 
  delay(100);
}






//Nano reciever
// read two ints from a Uno
// see Uno_transmitter.ino

#include <SoftwareSerial.h>
int setL, setR;

SoftwareSerial mySerial(4, 5); // RX, TX

void setup() 
{
 
  Serial.begin(4800);
  Serial.flush();
  mySerial.begin(4800);
  mySerial.flush();
 
}

void loop()
{
  if (mySerial.available()) {  //read the two speeds

int setL = mySerial.parseInt();
    int setR = mySerial.parseInt();
   
    if (mySerial.read() == ‘\n’) {
      Serial.print("setL = “);
      Serial.print(setL);
      Serial.print(”, setR = ");
      Serial.println(setR);
    } 
}
  delay(100);
}

Very nice!! Should be included in official examples to test serial communications devices between Arduinos (I used this to test HC-12 communications between 2 Arduino Uno’s)

Anyone tested this? I just tried warren's code; but no echo. Receiver wont say a thing... :confused:

rgxHost: Anyone tested this? I just tried warren's code; but no echo. Receiver wont say a thing... :confused:

This discussion is old, so best you start a new discussion so you can explain your particular setup and issues.