Serial.print and .write between two Arduinos

Hi,

I'm trying to get two Arduinos to communicate using the TX/RX serial pins.

Arduino one (the sender) is hooked up to a 9V battery with a wire going from pin TX to pin RX of Arduino two. Pin RX of arduino one is going to Pin TX of Arduino two. Arduino two is also powered by another 9V battery.

The code used by Arduino one is:

void setup()
{
  // initialize the serial communication:
  Serial.begin(9600);

}

void loop() {

delay(1000);
Serial.print(255);
delay(1000);
Serial.print(0);
}

and the code used by Arduino two is:

void setup()
{
  // initialize the serial communication:
  Serial.begin(9600);

}

void loop() {
  byte brightness;

  // check if data has been sent from the computer:
  if (Serial.available()) {
    brightness = Serial.read();
    analogWrite(5, brightness);
  }
}

I've tested each code using the serial monitor with usb cable and Arduino one is printing out 255, 0, 255, 0 as it should. When I test Arduino two by sending 0 using the serial monitor the LED comes on brightly and sending any other value between 0 and 255 doesn't change its brightness. Where abouts am I going wrong? :slight_smile:

I thought about this, and SoftSerial 2,3 (on 1) and (3,2) on the other and read / send data like that without interfering with the usb datalines?

Arduino one (the sender) is hooked up to a 9V battery with a wire going from pin TX to pin RX of Arduino two. Pin RX of arduino one is going to Pin TX of Arduino two. Arduino two is also powered by another 9V battery.

Are the grounds connected?

I've tested each code using the serial monitor with usb cable and Arduino one is printing out 255, 0, 255, 0 as it should. When I test Arduino two by sending 0 using the serial monitor the LED comes on brightly and sending any other value between 0 and 255 doesn't change its brightness. Where abouts am I going wrong?

When you type 127 in the Serial Monitor, and hit send, what gets sent is '1', '2', and '7' (plus maybe some other stuff, depending on options selected).

You are mistakenly expecting to read 127 as a byte. That is not what is sent.

Even when you enter 0, what is sent is '0'. not 0.

Your sending Arduino is doing the same thing that the Serial Monitor does, except that it doesn't add any optional characters.

Time to read up on how to receive strings of data from the serial port, and convert the strings to numbers.

Hint: Start and end of packet markers will be real useful.

Sorry I'm not sure what you mean cjdelphi.

It seems I have two problems, when Arduino two is plugged in on its own to usb (without connection to tx/rx of Arduino one) it won't alter the brightness of the LED after data (0 or 255) is sent through the serial monitor.

The second is that the TX of Arduino one doesn't flash when everything is connected up between the two arduinos.

Thanks PaulS. When both running on battery I've got a ground pin of Arduino one hooked up to a ground pin of Arduino two. It lights the breadboard LED of Arduino two, but the TX LED of Arduino one doesn't flash every 1000ms as I believe it should.

Also,should I alter the code of Arduino one to send Serial.write(255) in that case so that it sends the number as a byte? I know this will mean that serial input through the serial monitor won't work correctly still.

It lights the breadboard LED of Arduino two, but the TX LED of Arduino one doesn't flash every 1000ms as I believe it should.

The LED on the Arduino is controlled by the USB-to-Serial chip, not the ATMega328 chip. So, unless you are sending data via the USB cable, the TX light should not be expected to flash.

Also,should I alter the code of Arduino one to send Serial.write(255) in that case so that it sends the number as a byte?

That's one way to address the issue.

I know this will mean that serial input through the serial monitor won't work correctly still.

You can write code on the receiver that accepts strings, as long as there is some kind of marker that says "Hey, this is the end of the packet". You can have the sender continue to send strings, too, as long as you add that same marker.

The Serial Monitor is capable of appending a carriage return and line feed to each string sent. The Serial.println() method does the same thing.

You simply read any serial data that is waiting to be read, and store it in an array, followed by a NULL. When the "Hey, this is the end of the packet" marker arrives. convert the value in the array to a number (atoi()), then reset the index into the array, and stuff a NULL in the first position of the array.

Thank you once again Paul :slight_smile: That makes sense as to why the TX LED wasn't flashing. I've used Serial.write for now just to test it out and now the arduinos are communicating with each other. I've also just added some wireless XRF chips and now they're communicating wirelessly with each other over serial.

I'll read up on sending and recieving strings with markers and aim to implement that in the next bit of code :slight_smile: