Serial communication between arduinos not working while USB disconnected

Hello,

I've been trying to figure out what is going wrong with my setup for several days now. I think it's time to look for fresh opinions.

Here is the background:
My objective is to create a network of 3 arduinos connected by their Serial ports.

  • The first (Master) is a MEGA which is connected to several sensors. It processes the data and displays the values on a TFT screen. Finally, it gives instructions to the Slave boards.
  • The two Slave boards are UNOs which will be connected to devices such as pumps / valves / ...

At the moment, I basically try to figure out how I could make two boards to communicate. For practical reasons (I wanted to check data received with the serial monitor) I used the MEGA as a Slave and UNO as a Master.

My test setup is : UNO is connected to a capacitive sensor. If the capacitance measured on this sensor is "high", the UNO sends a "FLAG=1" (Serial) to the MEGA, which lights a LED. Respectively if capacitance is "low", the UNO sends a "FLAG = 0". Connections between boards are RX1 <---> TX0, TX1 <---> RX0, GND <---> GND.

I have read several threads/tutorials/whatever --I think-- is available, especially the posts from Robin, to create my "transmitter" and "receptor" test sketches. I rewrote the examples provided, to make sure I understand the basics; all my sketches have compiled without error.

My problem is that once I disconnect the two arduinos from the computer, the Master does not seem to send anything. No TX/RX leds flash on both boards. When connected to the computer though, the Master's TX led flashes and if I check the serial monitor, I can see data are sent (<...>).

Hope someone can help/guide me.
Thanks in advance,
TSR

Recepteur.ino (1.64 KB)

Emetteur.ino (564 Bytes)

Hi Tsr,

you have given a detailed description of your project. This is very good. Though the whole thing has another part:

The hardware. you wrote

Connections between boards are RX1 <---> TX0, TX1 <---> RX0, GND <---> GND.

to state the obvoius: how do you supply the two boards with power if the USB-cable is disconnected?
Both boards still need power. In case of beeing connected to the USB the USB-cable delivers the power.
If disconnected the power has to come from somewhere else.
For a deeper analysis I would add debug-output at several places in your code. Or alternativ switch on of some LEDs as a visual feedback which part of the code gets executed.
best regards Stefan

Thanks for your input.

Sorry, UNO board is connected with 12V-1A power supply on the board's jack (so there is no conflict with the USB-Serial). MEGA is still connected to USB, so I can check the Serial monitor (data are received on Serial1).

Before connecting the two boards together:
I first checked that the "emetteur" sketch was performing correctly. Serial monitor displayed <1> or <0> depending on whether my hand was or wasn't on the sensor. I concluded (was I wrong ?) that everything was running as expected since data was transmitted, with correct values.
Then my objective was to check the "recepteur" sketch. Several Serial.print should have let me see what data (newData/FLAG) arrived (which apparently never happened). That's where I am stuck.

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a diagram showing how you have your controllers connected?
Have you got them with all with their gnds connected together?

Thanks.. Tom.. :slight_smile:

This is the "emetteur" code :

#include <CapacitiveSensor.h>

CapacitiveSensor cs_A0_A1 = CapacitiveSensor(A0, A1);

long cap;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  cap = Test_Sensor();
  //Serial.println(cap);
  Send_Flag(cap);
  delay(1000);
}

long Test_Sensor()
{
  long  CapSense1 = cs_A0_A1.capacitiveSensor(30);
  //Serial.println(CapSense1);
  return CapSense1;
}

void Send_Flag(long CapSense1)
{
  Serial.write("<");
  if (CapSense1 > 100)
  {
    Serial.write("1");
  }
  else
  {
    Serial.write("0");
  }
  Serial.write(">");
  //Serial.println(CapSense1);
}

and the "recepteur" code is:

const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
char tempChars[numChars];
int FLAG = 0;
int LED = 3;

//____________________________________
void setup()
{
Serial1.begin(9600);
Serial.begin(9600);
pinMode(LED,OUTPUT);
}

//____________________________________
void loop()
{
newData = Scan_Instructions();
Serial.print(newData);
Serial.print("-");
if (newData==true)
{
  Receive_Instructions();
}
FLAG = Parse_Instructions();
Serial.println(FLAG);
if (FLAG == 0)
{
  Stop_LED();
}
else
{
  Start_LED();
}
delay(1000);
}

//____________________________________
boolean Scan_Instructions()
{
  static boolean Incoming_Msg = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial1.available() > 0 && newData == false)
  {
    rc = Serial1.read();
    if (Incoming_Msg == true)
    {
      if (rc != numChars)
      {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars)
        {
          ndx = numChars -1;
        }
      }
      else 
      {
        receivedChars[ndx] = '\0';
        Incoming_Msg = false;
        ndx = 0;
        newData = true;
      }
    }
    else if (rc == startMarker)
    {
      Incoming_Msg = true;
    }
  }
  return newData;
}

//____________________________________
void Receive_Instructions()
{
  strcpy(tempChars,receivedChars);
}

//____________________________________
int Parse_Instructions()
{
  char * strktokIndx;
  strktokIndx = strtok(tempChars, ",");
  FLAG = atoi(strktokIndx);
  return FLAG;
}

//____________________________________
void Start_LED()
{
  digitalWrite(LED, HIGH);
}
void Stop_LED()
{
  digitalWrite(LED,LOW);
}

Yes, the ground are connected together. I don't have the software to provide a schematic of my connections yet. I will try to download it later (where I stay I have low data allowance).

What annoys me is that, even if the "recepteur sketch" is defectuous, the Master should still send data (TX blink)... shouldn't it? Data would simply be lost if there is no operational receptor. But here I have no sign of activity on the Master.

Hi,
Forget about software, use a pen/pencil and paper and sent a picture of your diagram.

Tom... :slight_smile:

From Your description RX1 <---> TX0, TX1 <---> RX0

I guess RX1/TX1 is the Mega because an Arduino Uno olny has one hardware-serial-interface.

So this means Mega-TX1 ==> Uno-RX0 and Uno-TX0 ===> Mega-RX1
combined with your description

My test setup is : UNO is connected to a capacitive sensor. If the capacitance measured on this sensor is "high", the UNO sends a "FLAG=1" (Serial) to the MEGA, which lights a LED

Uno-TX0 sends "FLAG=1" or "FLAG=0" to the Mega-RX1-pin

Is your IO-pin on the Arduino Uno named 0<-RX connected to Mega TX1-18
Is your IO-pin on the Arduino Uno named TX->1 connected to Mega RX1-19

To narrow down the porblem I would use a Test-sketch as simple as possible.
Nothing more than opening the serial interface and sending "Hello" each second.
Absolutely nothing more. No sensor, no conditions - nothing any more.

Anything added can be another potential source of bugs.
The receiver does nothing more than receive and echo the received bytes to the serial-monitor
No conditions about a chracter beeing this or that just echoing the received bytes.

Anything added can be another potential source of bugs.

The only thing I might add would ne a heartbeat-signal blink the on-board-LED each second that does indicate the boards code is running at all.

best regards Stefan

can you confirm this is how things are wired ?

OK guys, thanks for your support.

Yes, the connexions are like mentioned in J-M-L's post.

My first and foremost error was to focus on the TX/RX leds. I followed StefanL38's advice and made 2 simple sketches (send/receive "Hello") and realized that in my case, and I would like confirmation that it's also the case for you, the TX/RX leds do not blink when data is transmitted from an arduino to another.

Once I took that obsession out of my mind, I started to dig further in my "Recepteur" code and found errors.

First here is the modified code

const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
char tempChars[numChars];
int FLAG = 0;
int LED = 3;
char * strktokIndx;

//____________________________________
void setup()
{
  Serial1.begin(9600);
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}

//____________________________________
void loop()
{
  newData = Scan_Instructions();
  Serial.print("newData = ");
  Serial.println(newData);
  Serial.print("receivedChars = ");
  Serial.println(receivedChars);
  if (newData == true)
  {
    strcpy(tempChars, receivedChars);
    Serial.print("tempChars = ");
    Serial.println(tempChars);
    Parse_Instructions();
  }

  Serial.print("FLAG =");
  Serial.println(FLAG);
  if (FLAG == 0)
  {
    Stop_LED();
  }
  else
  {
    Start_LED();
  }
  newData = false;
  Serial.println("___");
  delay(1000);
}

//____________________________________
boolean Scan_Instructions()
{
   boolean Incoming_Msg = false;
   boolean newData = false;
  byte ndx = 0;
  char startMarker;
  char endMarker;
  char rc;
  startMarker = '<';
  endMarker = '>';

  while (Serial1.available() > 0 && newData == false)
  {
    rc = Serial1.read();
    //Serial.print(rc);Serial.print("===");
    if (Incoming_Msg == true)
    {
      if (rc != endMarker)
      {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars)
        {
          ndx = numChars - 1;
        }
      }
      else
      {
        receivedChars[ndx] = '\0';
        Incoming_Msg = false;
        ndx = 0;
        newData = true;
      }
    }
    else if (rc == startMarker)
    {
      Incoming_Msg = true;
    }
  }
  Serial.print("newData (in function) =");
  Serial.println(newData);
  return newData;
}

//____________________________________
void Receive_Instructions()
{
  strcpy(tempChars, tempChars);
}

//____________________________________
void Parse_Instructions()
{
  char * strktokIndx;
  strktokIndx = strtok(receivedChars, ",");
  FLAG = atoi(strktokIndx);
  //return FLAG;
}

//____________________________________
void Start_LED()
{
  digitalWrite(LED, HIGH);
}
void Stop_LED()
{
  digitalWrite(LED, LOW);
}

I added serveral intermediary "Serial.print" to track the results from my different orders and spot where my code was bugging.

Now that it's working, serial monitor returns :

newData (in function) =1
newData = 1
receivedChars = 1
tempChars = 1
FLAG =1

... When my hand was on the sensor and ...

newData (in function) =1
newData = 1
receivedChars = 0
tempChars = 0
FLAG =0

... When my hand was not on it.

The part that had to change from my initial code was the function boolean Scan_Instructions(). It did not return adequate newData and was not providing adequate receivedChars for the loop(). While the boolean was true in the function, it stayed false outside. Char receivedChars was "infinitely expanding".
Since newData was always false, receivedChars was never copied in tempChars and, therefore, Parse_Instructions() was useless.
Then nothing was working...

My (main) modifications were:

  • making boolean Incoming_MSG and newData non static (they all were static)
  • I corrected an error I made while typing the second condition

if (rc != numChars)

instead of

if (rc != endMarker)

My next step will be to add a second parameter to the packet data (a float that will be processed by the slave to compute pump speed) and make sure parsing is effective.

My conclusion, linked to the title of this thread : no, the board's TX/RX led DO NOT BLINK when arduinos are communicating between them. That's only the case when communication is occuring between arduino and serial monitor (computer).

Thanks again, cheers

glad you found the issue !