In my project, I have to send a string from one arduino to a second arduino, and make it send the same string to a third arduino. I used UART and SoftwareSerial library, and connected the arduinos like that:
In all the arduinos: Pin 10 - RX port, pin 11 - TX port enter image description here Arduino 1 pin 11 ----> Arduino 2 pin 10 Arduino 2 pin 11 ----> Arduino 3 pin 10 Arduino 3 ----> PC (by hardware Serial)
By theory, Arduino 1 sends string "CA" to arduino 2, which resends that same string to arduino 3, that prints it back on Serial.
The problem is that I get the string "C�" instead of "CA" (that weird "C�" is equal to "067 255" on ASCII). Does anyone knows why it happens? Maybe there's something wrong with my code?
Here's the code for all the three Arduinos:
Arduino No. 1:
#include <SoftwareSerial.h>
#define ADDR "CA"
SoftwareSerial tiny(10, 11); //TBD
void setup() {
Serial.begin(9600); //debug only
tiny.begin(9600);
delay(2000);
tiny.write(ADDR);
}
void loop() {
//nothing here
}
Arduino No. 2:
#include <SoftwareSerial.h>
SoftwareSerial tiny(10, 11); //TBD
void setup() {
Serial.begin(9600); //debug only
tiny.begin(9600);
}
void loop() {
if(tiny.available() > 0) {
tiny.write(tiny.read());
}
}
Arduino No. 3:
#include <SoftwareSerial.h>
SoftwareSerial tiny(10, 11); //TBD
void setup() {
Serial.begin(9600); //debug only
tiny.begin(9600);
}
void loop() {
if(tiny.available() > 0) {
Serial.write(tiny.read());
}
}
Any idea on what went wrong?
-
Where are your ground connections? I don't see them there.Majenko– Majenko2016年09月19日 14:45:22 +00:00Commented Sep 19, 2016 at 14:45
-
Where is it being modified / becoming corrupt ? Have you printed to debug serial on Arduino 2 to see if it is receiving corrupt data, or if the data is being corrupted on the 2-> leg ? Also have you swapped them around (Arduino 2 becomes Arduino 3 etc) ?KennetRunner– KennetRunner2016年09月19日 14:57:38 +00:00Commented Sep 19, 2016 at 14:57
-
@Majenko They are all connected to the PC, so their grounds are connected (nevertheless, I tried to connect their grounds together and it didn't help).Ido Daniel– Ido Daniel2016年09月19日 15:01:53 +00:00Commented Sep 19, 2016 at 15:01
-
@KennetRunner It is being corrupted when it gets to Arduino 2 (gives same results). I tried to swap arduino 2 and 3 but it didn't help.Ido Daniel– Ido Daniel2016年09月19日 15:03:15 +00:00Commented Sep 19, 2016 at 15:03
-
1Connecting the grounds through the USB is not adequate. You need a direct connection between all the boards' grounds.Majenko– Majenko2016年09月19日 15:05:04 +00:00Commented Sep 19, 2016 at 15:05
1 Answer 1
Arduino #2 should have two SoftwareSerial instances, one to listen to Arduino #1 and another one to talk to Arduino #3:
// In Arduino #2
SoftwareSerial tiny1(10, 11); // Listen to Arduino #1 on pin 10
SoftwareSerial tiny2(8, 9); // Talk to Arduino #3 on pin 9
You may not need to wire unused signals (11 and 8).
There is an example here : https://www.arduino.cc/en/Tutorial/TwoPortReceive
Note that what you are trying to do is a bus. Serial in Arduino is RS232 based and that is not a bus but a point to point protocol. You should consider using I2C or SPI.