Send data to Arduino MEGA 2560 from Electron

Hi all,

I tried to send data to Arduino MEGA 2560 from Electron but cannot get any meaningful data from the Arduino side.

The Arduino code:

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

void loop() {
  if(Serial2.available()) {
    Serial.write(Serial2.read());
  }
}

The Electron code:

int i = 0;

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

void loop() {
  Serial.print("test " + String(i++) + "\n");
  delay(1000);
}

I can see the output of the Electon is exactly right by running ‘particle serial monitor’.

So my problem is how to get the data sent from the Electron. The TX of the Electron is connected to the PIN 17(RX of Serial2) of the Arduino MEGA board. The RX of the Electron is connected to the PIN 16(TX of Serial2) of the Arduino MEGA board.

Thanks.

@anderson916
You need a way for the Mega to parse strings. I have used code for this in a project recently, and here it is:

String readString = "";

void loop()
{

  while (Serial2.available()) // While receiving characters over serial...
  {
    delay(3); // Necessary delay
    char c = Serial2.read(); // Read the character
    readString += c; // Add the character to the string
  }

  readString.trim();

  if (readString.length() > 0) // If a string has been read...
  {
    Serial.println("Received: " + readString); // Send the parsed string to Serial for debugging
    parseCommand(readString); // Do something with the string...
    readString = ""; // Clear the string
  }
}

You need to use Serial1 for RX/TX on the Electron.

@ScruffR Can you explain why I need to use Serial1? Thanks.

@nrobinson2000 Thanks. But still doesn’t work.

On the Electron, Serial1 is the TX/RX pins. Serial is through the USB.

Are you sure?

Your Arduino Mega code should look something like this:

String readString = "";

void parseCommand(String command)
{
  if (command == "foo")
  {
    bar();
  }
}

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

void loop()
{

  while (Serial2.available()) // While receiving characters over serial...
  {
    delay(3); // Necessary delay
    char c = Serial2.read(); // Read the character
    readString += c; // Add the character to the string
  }

  readString.trim();

  if (readString.length() > 0) // If a string has been read...
  {
    Serial.println("Received: " + readString); // Send the parsed string to Serial for debugging
    parseCommand(readString); // Do something with the string...
    readString = ""; // Clear the string
  }
}


As @nrobinson2000 already said, that's the object connected to the RX/TX pins, while Serial is connected to the virtual serial port via USB.
And the Electron also features Serial2, Serial4 and Serial5 each connected to a set of other pins.

But isn't that similar to Serial vs. Serial2 in your Arduino sketch? Hence I don't quite get the reason for the question (or "misbelief").

finally I got it work using another Electron device and an Arduino MEGA board. But after I tried to send data from Arduino to Electron, I cannot get any data from Electron.

Is the Electron device damaged?

should I put any resistors between Electron and Arduino board? or just connect the wires directly?

It's rather unlikely that you damaged the Electron if you're only connecting RX/TX & GND between them, since all (but the DAC pins A3 & A6) GPIOs on the Electron should be 5V tolerant (unless they are in analogRead() mode or have internal pull-resistors attached).

Maybe there is no code that does that on the Electron. At least the code you provided further up does not do such thing.

You need to have a version of the code I posted earlier running on the Electron side in order for the Electron to be able to receive messages back from the Mega.

@anderson916, not to sidestep @nrobinson2000’s fantastic advice and code, you may also want to look at this library:

In the past, I have managed to damage the RX and/or TX pins on a Photon though I’m not sure how I did so. If you have a Photon as well, you could do the testing between these two devices without concern for the 5v issue. If you have an oscilloscope or logic analyzer, you should look at the TX output of the suspect Electron. Otherwise, you could also put an LED on the pin, connecting the anode to 3V3 via a 1K resistor and the cathode to the TX pin. The LED should blink when data is transmitted. :smiley:

1 Like

It works now. Thanks for all help.