Arduino to Arduino Serial Communication

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Aluman000
 
Posts: 27
Joined: Sun Apr 10, 2011 4:27 pm

Arduino to Arduino Serial Communication

Post by Aluman000 »

Ok,

This seems so simple but I just can't get it to work.
I've tried buffers and subtracting '0' and looking for '/n' and about 100 other ways to get serial communication to work.
I've tried to use .print, .println, and .write.  Nothing seems to work.
I simply am stuck.
So Please, any help.

Ok, I have 2 Uno R3s connected from pin2 to pin3.
All I want to do is send a number from one to the other.
here's my code on the sending station:

Code: Select all

#include <SoftwareSerial.h>

SoftwareSerial DirectConnect(2,3);

int i;

void setup()
{
  Serial.begin(57600);
  DirectConnect.begin(57600);  
} // Setup

void loop()
{
  
    
  for ( i = 1; i <= 100; i++)
  {
    
   DirectConnect.print(i);
   Serial.println(i);
   delay(50);
   
  } 
  
} // Loop
and here's my code for the receiving station:

Code: Select all

#include <SoftwareSerial.h>

SoftwareSerial DirectConnect(2,3);

int RecVar;
void setup()
{
  Serial.begin(57600);
  DirectConnect.begin(57600);  
} // Setup

void loop()
{
  delay(50); 
    
  if(DirectConnect.available())
  {
    
    RecVar = DirectConnect.read();
   Serial.println(RecVar);
   
  } 
  
} // Loop
This is vastly simplified from the functions I really want to do, but I just can't get this to go.

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino to Arduino Serial Communication

Post by adafruit_support_bill »

What do you see on the Serial monitor for the receiving side?

tldr
 
Posts: 466
Joined: Thu Aug 30, 2012 1:34 am

Re: Arduino to Arduino Serial Communication

Post by tldr »

first of all, make sure you've got the boards wired in a null modem configuration with the wires between the boards. tx on one to rx on the other and vice versa. alternatively configure one board with

Code: Select all

SoftwareSerial DirectConnect(3,2);

User avatar
tastewar
 
Posts: 408
Joined: Thu Mar 17, 2011 10:16 am

Re: Arduino to Arduino Serial Communication

Post by tastewar »

Do you have the grounds of both boards tied together?

Aluman000
 
Posts: 27
Joined: Sun Apr 10, 2011 4:27 pm

Re: Arduino to Arduino Serial Communication

Post by Aluman000 »

Here's what my Serial Monitor Returns.
54
183
134
133
54
145
134
133
54
183
134
133
54
145
134
133
54
151
134
133
54
177
134
133
54
183
134
133
54
Though i don't have a ground between them.
How do I hook that up? Just ground to ground or is there some other pin + a command or something?

Thanks Everyone.

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino to Arduino Serial Communication

Post by adafruit_support_bill »

You can connect the grounds with just a jumper from GND on one to GND on the other.
The numbers printing out are the numeric values of the characters you are reading in. Since you have RecVar defined as an 'int' it is printing it a a number.

User avatar
tastewar
 
Posts: 408
Joined: Thu Mar 17, 2011 10:16 am

Re: Arduino to Arduino Serial Communication

Post by tastewar »

You might try .write rather than .print on your DirectConnect. On the receive side, when printing to the serial monitor, you might do Serial.println(RecVar,DEC). Also, you might try using an unsigned char rather than an int.

Aluman000
 
Posts: 27
Joined: Sun Apr 10, 2011 4:27 pm

Re: Arduino to Arduino Serial Communication

Post by Aluman000 »

Ok, no luck really.

First my full setup.
I have a Sending Station:
Uno R3
Powered by wall plug adapter
1 wire connecting the 3 pin to the 2 pin on the Receiving Station
1 Wire connecting the GND to the GND on the Receiving Station

I have a Receiving Station:
Uno R3
Powered via USB to computer
Connections as described above

No other connections on either Station.

The new Serial Monitor output looks like this:
56
57
57
57
57
57
57
57
57
57
57
49
49
50
51
52
53
54
55
56
57
55
55
56
56
56
56
56
56
56
56
56
56
57
57
57
57
57
57
57
57
57
57
49
49
50
51
52
53
54
55
56
57
54
55
56
57
49
48
49
49
49
50
49
51
49
52
49
53
49
54
49
55
49
56
49
57
50
48
50
49
50
50
50
51
50
52
50
53
50
54
50
55
50
Different, but still doesn't look like 1-100 repeating.  
Even if it's giving the ascii equivalent of the char they don't repeat properly, I don't think.  
I would expect every 3rd value to be the 'end' value.

AKSoapy29
 
Posts: 39
Joined: Sat Jan 07, 2012 6:56 pm

Re: Arduino to Arduino Serial Communication

Post by AKSoapy29 »

There is a library to handle Arduino to Arduino communications. It is called EasyTransfer by Bill Porter.

Here is the link: http://www.billporter.info/easytransfer ... o-library/

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino to Arduino Serial Communication

Post by adafruit_support_bill »

All those numbers are in the range of 48-57 which is the decimal value of the ASCII digits '0'-'9'. http://www.cdrummond.qc.ca/cegep/inform ... /ascii.htm
Not sure why there are no CR/LF (13/10) characters in there. Are you still using 'println()'?

Aluman000
 
Posts: 27
Joined: Sun Apr 10, 2011 4:27 pm

Re: Arduino to Arduino Serial Communication

Post by Aluman000 »

I'll have to try the Bill Porter Library later.
I'm using his code to handle the PS2 Controller, and the code works great...
It's just a bugger to wire it up. Very finnicky.
Thought that might be the problem here, but this is so simple, I can't imagine how

Thanks everyone for all the help.

User avatar
zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: Arduino to Arduino Serial Communication

Post by zener »

Did you ever connect the grounds together? Very important. Don't bother doing anything else until that is done.

mobilegps
 
Posts: 3
Joined: Sun Nov 11, 2012 9:16 pm

Re: Arduino to Arduino Serial Communication

Post by mobilegps »

Hey mate,

Can you try lower baud rate like 9600 ?

Cheers

Aluman000
 
Posts: 27
Joined: Sun Apr 10, 2011 4:27 pm

Re: Arduino to Arduino Serial Communication

Post by Aluman000 »

Thanks Everyone.
The grounds worked.
My final code for direct connect is:

RECEIVING

Code: Select all

#include <SoftwareSerial.h>

SoftwareSerial DirectConnect(2,3);

int RecVar;
int input;

void setup()
{
  Serial.begin(57600);
  DirectConnect.begin(57600);  
} // Setup

void loop()
{
//  delay(50); 
    
  if(DirectConnect.available())
  {
    
    RecVar = DirectConnect.read();
    Serial.println(RecVar);
    if(isDigit(RecVar))
     { 
       input = (input * 10) + (RecVar - 48);
     }
    else
    {
      Serial.println(input);
      input = 0;
    }
   
  } 
  
} // Loop
SENDING

Code: Select all

#include <SoftwareSerial.h>

SoftwareSerial DirectConnect(2,3);

int i;

void setup()
{
  Serial.begin(57600);
  DirectConnect.begin(57600);  
} // Setup

void loop()
{
  
    
  for ( i = 1; i <= 100; i++)
  {
    
   DirectConnect.print(i);
   DirectConnect.print('T');//arbitrary non-numeric
   Serial.println(i);
   
   delay(500);
   
  } 
  
} // Loop

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”