Go Down

Topic: Serial Connection between two Arduinos (Read 342 times) previous topic - next topic

Rodrigo_Galvao_BS

So, I tried to make communication between two Arduinos, but it didn't work. I searched a lot about it but nothing worked.

The receiver Arduino is an Arduino Uno and the transmissor Arduino is an Arduino Pro Mini. I have uploaded a different code for each.

Connection Ports:

Ard. Uno Tx -----> Ard. ProMini Rx
Ard. Uno Rx -----> Ard. ProMini Tx
Ard. Uno GND -----> Ard. ProMini GND
Ard. Uno 5v -----> Ard. ProMini Vcc

and the codes:

Arduino ProMini:

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

Serial.write('A');
delay(2000);
Serial.write('a');
delay(2000);

}


Arduino UNO:

void setup() {
   
  pinMode(13, OUTPUT);
  Serial.begin(9600);

}
 
void loop() {
 
  if (Serial.available()>0) {
       
       char recebido = Serial.read();

       if(recebido=='A'){digitalWrite(13, HIGH);}
       if(recebido=='a'){digitalWrite(13, LOW); }
     
     }

}

J-M-L

#1
Apr 20, 2019, 04:28 pm Last Edit: Apr 20, 2019, 04:28 pm by J-M-L
do you have USB cables connected to your 2 arduinos?

I would not do that BTW, have a clean power supply for each arduino...
Quote
Ard. Uno 5v -----> Ard. ProMini Vcc
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Robin2

I suggest you use SoftwareSerial to create an additional serial port on each Arduino so as to leave the HardwareSerial port free for uploading programs and debug messages.


Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)
Code: [Select]
Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Rodrigo_Galvao_BS

do you have USB cables connected to your 2 arduinos?

I would not do that BTW, have a clean power supply for each arduino...

I also tried with power supply separated, but nothing has changed. Thank you.

J-M-L

do you have USB cables connected to your 2 arduinos?
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Rodrigo_Galvao_BS

#5
Apr 20, 2019, 07:03 pm Last Edit: Apr 20, 2019, 07:28 pm by Rodrigo_Galvao_BS
I suggest you use SoftwareSerial to create an additional serial port on each Arduino so as to leave the HardwareSerial port free for uploading programs and debug messages.


Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)
Code: [Select]
Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker


...R
I suggest you use SoftwareSerial to create an additional serial port on each Arduino so as to leave the HardwareSerial port free for uploading programs and debug messages.


Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)
Code: [Select]
Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker


...R
So, first of all, my real problem is between an Arduino ProMini and an ESP8266 12-E. So after I read your link and your SoftwareSerial recommendation I went right in to try to make communication between those.

I tried to use the SoftwareSerial between ESP8266 and Arduino UNO, after ESp8266 and Arduino ProMini. The first try was successful, I could send and receive data from both.
The second one, when I send a data to Arduino ProMini, the message received was like in the wrong Baud Rate, even I have configurated both codes in the same Rate, 9600. So, I needed to change to a slower rate, 4800, and something showed up but deconfigured.

The messages I sent from Arduino ProMini to ESP8266 were:

Code: [Select]

mySerial.println("Hello, world?");
mySerial.write("jjjjjjjjjj");


And what showed up in the ESP's SerialMonitor were:

Code: [Select]

H⸮llo,8ozl|?⸮
jjjjjjjjjj




The Code from Arduino ProMini:

Code: [Select]

#include <SoftwareSerial.h>
int cont = 0;
SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
  ; // wait for serial port to connect. Needed for native USB port only
}


Serial.println("Goodnight moon!");

// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println("Hello, world?");
}

void loop() { // run over and over

if(cont == 0){
mySerial.write("jjjjjjjjjj");

cont++;
}


if (mySerial.available()>0) {
 
  Serial.write(mySerial.read());
}
if (Serial.available()>0) {
  mySerial.write(Serial.read());
}
}


Thank you for your help.

Rodrigo_Galvao_BS

do you have USB cables connected to your 2 arduinos?

Yes, exactly. Two cables. I wrote a little more about the problem in the last answer that I made. May you read to help me out?

Thank you.

J-M-L

#7
Apr 20, 2019, 07:12 pm Last Edit: Apr 20, 2019, 07:13 pm by J-M-L
careful, one of those Arduino might be 5V and the other 3.3V.. connecting Tx at 5V to Rx which only supports 3.3V is a risk

Yes, exactly. Two cables. I wrote a little more about the problem in the last answer that I made. May you read to help me out?
Then you have too many people talking on the same serial line... your computer and the arduino amongst themselves. Hence the suggestion to go Software Serial for example


Please correct your post above and add code tags around your code:

[code] // your code is here [/code]
.

It should look like this:
Code: [Select]
// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Rodrigo_Galvao_BS

careful, one of those Arduino might be 5V and the other 3.3V.. connecting Tx at 5V to Rx which only supports 3.3V is a risk
Then you have too many people talking on the same serial line... your computer and the arduino amongst themselves. Hence the suggestion to go Software Serial for example


Please correct your post above and add code tags around your code:

[code] // your code is here [/code]
.

It should look like this:
Code: [Select]
// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)


Yes, I know about the risk, I will take care of it. I did make the Software Serial suggestion, I don't know why it still not working. What would you say?

Thank you.

J-M-L

use slower baud rate - like 9600 to test, ensure grounds are properly joined and take care of the voltage Level translation
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Rodrigo_Galvao_BS


Robin2

I tried to use the SoftwareSerial between ESP8266 and Arduino UNO, after ESp8266 and Arduino ProMini. The first try was successful, I could send and receive data from both.
The second one, when I send a data to Arduino ProMini, the message received was like in the wrong Baud Rate, even I have configurated both codes in the same Rate, 9600. So, I needed to change to a slower rate, 4800, and something showed up but deconfigured.
I don't find that as clear as you may think.

Do you mean that you can communicate between your ESP8266 and your Uno but you cannot communicate between your ESP8266 and your ProMini using the exact same program on the ProMini as on the Uno?

If the baud rate seems wrong by a factor of 2 it suggests that the ProMini is programmed for 8MHz but is actually running at 16Mhz.  If your ProMini works at 3.3v then it should be running at 8Mhz as it would not be reliable at 16MHz. If you think the clock speed is wrong you need to burn the bootloader for the correct clockspeed as that sets the fuse bits for the appropriate clock speed.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Rodrigo_Galvao_BS

I don't find that as clear as you may think.

Do you mean that you can communicate between your ESP8266 and your Uno but you cannot communicate between your ESP8266 and your ProMini using the exact same program on the ProMini as on the Uno?

Exactly!
I will try those suggestions, it seems to make sense.

Thank you.

Rodrigo_Galvao_BS

If the baud rate seems wrong by a factor of 2 it suggests that the ProMini is programmed for 8MHz but is actually running at 16Mhz.  If your ProMini works at 3.3v then it should be running at 8Mhz as it would not be reliable at 16MHz. If you think the clock speed is wrong you need to burn the bootloader for the correct clockspeed as that sets the fuse bits for the appropriate clock speed.

...R
It worked!!!! Thank you very much!!! Could you explain more about it? Or if you know some post related to it send the link, please.

Thank you!

Robin2

It worked!!!! Thank you very much!!! Could you explain more about it?
Good to hear that it worked. However I don't know what you want more explanation about?

When you compile a program for an Arduino it will be compiled for a particular microprocessor and a specific clock speed. The speed is relevant for a lot of timing things including millis() and (as you saw) the baud rate. If the microprocessor is not running at the expected speed then all the speed calculations will be wrong.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up