Shouldn't this work ? Connecting two Arduinos ?

I want to connect a Uno with a Leonardo. So I simply connect pin 0 RX on the Uno to pin 1 TX on Leonardo and then TX on Uno to RX on Leonardo. I also connect grounds with a wire on both arduinos. Then I use Serial.print and Serial.read. I list the two arduino sketches below. Have been thinking wrong? Since this does not work. I see through the terminal window that the Uno really does print what I expect it to do.

The Uno sketch

#include <OneWire.h>
#include <stdlib.h>

#define ONE_WIRE_BUS 2

OneWire ds(2);
float   temp;													//Temperature
byte	outaddr[]={0x28,0x19,0xFA,0xC8,0x04,0x00,0x00,0x73};	//Address 18B20
	
void setup()
{
	/* add setup code here */
	Serial.begin(9600);
	pinMode(2,INPUT);
	pinMode(0,INPUT);
	pinMode(1,OUTPUT);
}

void loop()
{
	char	temp_str[10];
	/* add main program code here */
	temp=getTemperatures(outaddr);	//Read temperature
	dtostrf(temp,4,1,temp_str);		//Convert float to string
	Serial.print('H');				//Header character
	Serial.println(temp_str);		//Print temperature string
}

float getTemperatures(byte addr[])
{
  byte i;
  byte present = 0;
  byte data[12];
  
  ds.reset();
  ds.select(addr);
  // Start conversion
  ds.write(0x44, 1);
  // Wait some time...
  delay(850);
  present = ds.reset();
  ds.select(addr);
  // Issue Read scratchpad command
  ds.write(0xBE);
  // Receive 9 bytes
  for ( i = 0; i < 9; i++) 
    data[i] = ds.read();
  // Calculate temperature value
  return ( (data[1] << 8) + data[0] )*0.0625;
}

The Leonardo sketch

void setup()
{
	/* add setup code here */
	Serial.begin(9600);
	Serial.println("Leonardo reciever");
	pinMode(0,INPUT);
	pinMode(1,OUTPUT);
}

void loop()
{
	  /* add main program code here */
	  char	c[4];
	  char	h;
	  
	  h=0;
	  do
	  {
		  if ( Serial.available() )
			h=Serial.read();
	  } while (h != 'H' );	//Wait for 'H'

	  for (int i=0;i<4;i++)  //Read temperature string
	  {
		  if ( Serial.available() )
		  {
			c[i]=Serial.read();
		  }
		  else
			Serial.println("Missed character");
	  }
	  for (int i=0;i<4;i++) //Print temperature string
		Serial.print(c[i]);
	  Serial.print(" ");
}

Your second arduino is probably receiving characters and sending its status strings right back to the first arduino.

Instead of trying to print stuff out on the second, flash the received temperatures on an LED or output them as PWM or something. Or save them to EEPROM, then use a different sketch to print the EEPROM contents out to serial and therefore to your PC.

It should work.

The Uno:
The “Serial” port with pin 0 and 1 is used for the serial monitor on the computer and to upload a sketch. It is possible to override those signal, but perhaps you might use SoftwareSerial to create a new serial port.
http://arduino.cc/en/Reference/SoftwareSerial

The Leonardo:
The “Serial” port is the serial port to the computer.
The “Serial1” port is the hardware serial port on pin 0 and 1. The “Serial1” is not used for anything else, so you can use it without any problem.
You could create both “Serial” and “Serial1”. Use “Serial1” to communicate with something else, and “Serial” to send debug messages to the serial monitor on the computer.

Peter_n:
It should work.

The Uno:
The "Serial" port with pin 0 and 1 is used for the serial monitor on the computer and to upload a sketch. It is possible to override those signal, but perhaps you might use SoftwareSerial to create a new serial port.
http://arduino.cc/en/Reference/SoftwareSerial

The Leonardo:
The "Serial" port is the serial port to the computer.
The "Serial1" port is the hardware serial port on pin 0 and 1. The "Serial1" is not used for anything else, so you can use it without any problem.
You could create both "Serial" and "Serial1". Use "Serial1" to communicate with something else, and "Serial" to send debug messages to the serial monitor on the computer.

The way I have seen it is this. The RX and TX pin has the same serial data that are converted to USB and sent through the USB connector port. So when running the sketch I expect the same data being displayed on the serial monitor to also be present as TTL levels on the RX and TX pins. So why the need for any overide? Is this a misunderstanding?
I might point out that Leonardo is temperary solution. I intend to a MEGA 2560 instead.

The Arduino Mega 2560 has 4 hardware serial ports :slight_smile:

The RX pin 0 of the Uno receives data from the computer and from the Leonardo. So the Leonarde has to override the signal from the computer.

I've used the below test code between two arduinos. What is sent to the sending arduino is echoed back to the sending arduino's serial monitor and is also displayed on the receiving arduino's serial monitor.

//zoomkat 3-5-12 simple delimited ',' string tx/rx 
//from serial port input (via serial monitor)
//and print result out serial port
//Connect the sending arduino rx pin to the receiving arduino rx pin. 
//Connect the arduino grounds together. 
//What is sent to the tx arduino is received on the rx arduino.
//Open serial monitor on both arduinos to test

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.print(readString); //prints string to serial port out
        Serial.println(','); //prints delimiting ","
        //do stuff with the captured readString 
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

Peter_n:
The Arduino Mega 2560 has 4 hardware serial ports :slight_smile:

The RX pin 0 of the Uno receives data from the computer and from the Leonardo. So the Leonarde has to override the signal from the computer.

My new code below seem to work

void setup()
{
	/* add setup code here */
	Serial.begin(9600);
	Serial1.begin(9600);
	Serial.println("Leonardo reciever");
	pinMode(0,INPUT);
	pinMode(1,OUTPUT);
}

void loop()
{
	  /* add main program code here */
	  char	c[4];
	  char	h;
	  
	  h=0;
	  do
	  {
		  if ( Serial1.available() )
			h=Serial1.read();
	  } while (h != 'H' );	//Wait for 'H'

	  for (int i=0;i<4;i++)  //Read temperature string
	  {
		do {} while ( !Serial1.available() );
		c[i]=Serial1.read();	  
	  }
	  for (int i=0;i<4;i++) //Print temperature string
		Serial.print(c[i]);
	  Serial.print(" ");
}

You don't have to set the pins for TX and RX as input or output.
Can you remove that ? It might even conflict with the Serial library.
The Serial.begin() function sets the pins, so all you have to do is call Serial.begin().