I’m working in a project to control Arduino Uno over my PC (already accomplished); and stumbled into a problem. Which is: How to be able for Arduino Uno to communicate with Arduino Nano.
My PC is sending the value to Uno [ port.Write(string value); - on C# Application ] and need Nano to receive Uno information.
Initially I thought that connecting RX/TX wires would do the trick; but I was proven to be wrong.
I’m unable to communicate at all.
I’ve been searching the web on the last few days for a similar project / information on how to achieve this; but without success.
Can anyone point me out in the right direction?
Links; info; and / or how the procedure should be done would be extremely appreciated.
The first thing you need to understand is that the RX and TX pins (pins 0 and 1) on your Uno and Nano are the "Serial" port, used for communication with your computer over the USB cable, including printing to Serial Monitor and uploading sketches to the board. You may find you are unable to upload to the board when you connect things to those pins. So my advice is to avoid connecting anything to those pins. What you can do instead is create a software serial port on any pins using the SoftwareSerial library:
The second thing to know is that with serial you need to make the connection RX-TX, TX-RX. The reason is that RX stands for "receive" and TX stands for "transmit". You also need to connect the ground pins of the two boards.
So, given this information and the information at the SoftwareSerial reference pages I linked above, have another try. If it still doesn't work, come back here and post your full sketch, description of the wiring, and a complete explanation of the problem you are having.
Thank you for your prompt reply.
I'm already working on it.
So far (aside from what you've proposed and before your answer); I was able to manage to communicate between the Controllers (but not using the PC / connected to the PC). PC Application simply stays in "Connecting" mode. Which I could easily understand that the Port is being used.
Was attempting this using library with some help from a project found over the web.
After reading your advice (using library) I could understand that only 1 Port can communicate at a time. See: port.listen();
Thus writing this reply to enlighten myself a little bit before posting lots of possible code smell in this thread.
I got myself a couple questions:
[1]: The example ("Two Port Receive") declares 2 Serial ports (portOne and portTwo).
SoftwareSerial: port.listen() - Selects the port to listen to.
From what I understand the tutorial is aiming to communicate between 2 controllers; thus referencing pins for that communication.
If I want to create a port for USB Serial Port which communicates with the Computer; am I required to assign pins as well? (Which Pins to assign : 0 [RX] and 1 [TX]?)
[2]: I'm currently using Pins [A4] and [A5] (SDA and SDL) which (according to previous tutorial I was running) serve as RX and TX pins; and from what I could understand this is called I2C ("I Squared C") communication.
Can I still use these pins; or I must switch back to specified digital pins? Note: While Checking the Controller Pin Diagram I noticed the 8, 9, 10 and 11 pins don't include SDA/SCL but "OC"x. Not sure if this will influence or not (couldn't find references or meanings for it in order to understand the difference between the Pins used for communication).
"Initially I thought that connecting RX/TX wires would do the trick; but I was proven to be wrong."
A concept to consider is that the uno tx pin is also the computer (serial monitor) rx pin, and the uno rx pin is also the computer tx pin. You might make a very simple in/out echo programs for the uno and nano to see if you can make the echo loop serial monitor > uno > nano > uno > serial monitor.
rgxHost:
If I want to create a port for USB Serial Port which communicates with the Computer; am I required to assign pins as well? (Which Pins to assign : 0 [RX] and 1 [TX]?)
No. You already have a hardware serial port, Serial, to communicate with the computer. So you don't need to use the SoftwareSerial library for that. To understand how to use the hardware serial port, please read these documentation pages:
rgxHost:
Can I still use these pins
Use them for what?
rgxHost:
While Checking the Controller Pin Diagram I noticed the 8, 9, 10 and 11 pins don't include SDA/SCL but "OC"x.
If you're using software serial, stop worrying about I2C. That's a completely different communications protocol and has absolutely nothing to do with SoftwareSerial.
rgxHost:
Not sure if this will influence or not (couldn't find references or meanings for it in order to understand the difference between the Pins used for communication).
If you're communicating with hardware serial, then you can find the pins listed on the Serial reference page:
If you're communicating with the SoftwareSerial library, then you can find the pins listed on the SoftwareSerial library's reference page: https://www.arduino.cc/en/Reference/SoftwareSerial
If you're communicating with I2C, then you can find the pins listed on the Wire library reference page:
Don't get mixed up between them. They are each completely different things. There is good documentation available for each one.
Why should SoftwareSerial be used for inter-controller communication? For short distances I2C and otherwise SPI are better suited and hardware supported for such communication.
After the entire day killing myself to accomplish something close to a working project; I still have some problems.
Problems
My Application: I can (finally) connect to "Arduino Uno" (Sender Module).
There are some things to work on regarding Serial Communication Status (but works fine).
Since this is a problem slightly aside of this thread I have to skip this part for now.
"Arduino Uno" (Sender Module): I am able to write (send) to "Arduino Nano" (Receiver Module).
Even though the "Arduino Nano" (Receiver Module) prints the message sent from "Arduino Uno"; it won't turn on a Led Attached to it; after receiving the value. I couldn't understand why exactly.
Feels like the value in the variable is empty.
Unfortunately Arduino IDE isn't powerful enough for debugging this problem.
Thus some help here would be appreciated (Code Bellow).
Quick Info for people researching
Note: Please don't take all the information bellow as granted. I may be wrong (I am learning after all). The information bellow is regarding of what I could understand and learn from all I've been learning and testing.
Arduino Can have only 1 Hardware Serial Port active (open / in use) (besides the Serial method that configures the unit). And this one is used whenever you connect your Arduino to your computer.
USB uses Hardware Serial and this one has only 1 port (in use when connected to Computer).
This means that; if this is used by the application on your computer (your application; Arduino IDE; etc); you won't be able to communicate to another unit using the Hardware Serial.
Hence you are required to search for an alternative.
In this case I'm attempting to use "SoftwareSerial" Library; to Control "Arduino Nano" (Receiver Module) from my PC (also my application) using "Arduino Uno" (Sender Module).
Simplyfying:
Communicate from my Computer (via my application) to "Arduino Uno" (Sender Module) and from "Arduino Uno" (Sender Module) to Arduino Nano (Receiver Module).
Note(s): - Arduino Nano has a led attached to pin 3 for testing.
Wire Connection
Note(s):
USB pins are 0 and 1 (RX and TX) and Refer to Hardware Serial.
On "Arduino Uno" (Sender Module): I have RX/TX Wires on Pin 8 (RX) and 9 (TX).
These wires connect to "Arduino Nano" RX0/TX0 Pins (Any TX/RX Pins will Work).
Code:
/* Arduino Nano : Sender */
// Include "SoftwareSerial" Library
#include <SoftwareSerial.h>
// Define "SoftwareSerial" object and the used pins.
/* [Note]: The Pins are refered to the Pins used on this Module Only.
* [Note]: The Receiver should have these RX/TX wires (Pins: 8 and 9)
* connected to RX0/TX0 Pins.
*/
SoftwareSerial softSerial(8, 9); /* RX, TX */
void setup()
{
Serial.begin(9600);
softSerial.begin(9600);
Serial.println("Arduino Connected");
}
void loop()
{
// Check received characters from the Computer or Console Serial Monitor.
if (Serial.available() > 0)
{
// Write (send) the input value.
// The value that you Enter from Computer or Console Serial Monitor .
// This value is received by the SoftwareSerial.
softSerial.write(Serial.read());
}
}
/* Arduino Nano : Receiver */
// Include "SoftwareSerial" Library
#include <SoftwareSerial.h>
// Define "SoftwareSerial" object and used pins.
/* [Note]: The Pins are refered to the Pins used on Sender.
* [Note]: The Receiver Should Have RX/TX Wires to RX0/TX0 Pins.
*/
SoftwareSerial softSerial(8, 9); /* RX, TX */
// Led Pin
int ledPin = 3;
void setup()
{
softSerial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop()
{
/* Check if data was received from SoftwareSerial Buffer */
if (softSerial.available() > 0)
{
/* Read SoftSerial Buffer and the value into variable */
char dataReceived = softSerial.read();
LedMode(softSerial.read());
}
}
/* LEDS */
void LedMode(char ledState)
{
switch (ledState)
{
case '1': LedOn(); break;
case '2': LedOff();break;
}
}
void LedOn() { digitalWrite(ledPin, HIGH); }
void LedOff() { digitalWrite(ledPin, LOW); }
Tomorrow will continue this battle.
Please feel free to test and express your thoughts regarding the problems I'm facing.
To my mind the simplest way to do what you want is with a system like this
PC -----> USB cable ----> Uno Pins0 and 1(Rx and Tx)
Uno receives a message from the PC and saves it
Uno pins 6 and 7 (any except 0 and 1) with SoftwareSerial -----> wires ------> Nano Pins 7 and 6 with SoftwareSerial
Uno sends message to Nano
With this system you can use the 2nd or 3rd example in Serial Input Basics to receive the message on the Uno and on the Nano. Obviously, on Nano it would have to use SoftwareSerial.
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)
Another thought. If you are prepared to disconnect the wires when uploading programs (to either the Uno or the Nano) you could connect the Uno Rx pin 0 to the Nano Rx pin 0 and connect the GNDs and that way anything that the Uno receives from the PC will also be received by the Nano on Serial without any need for SoftwareSerial. However there will be no means for the Nano to respond either to the Uno or to the PC.
Another thought. If you are prepared to disconnect the wires when uploading programs (to either the Uno or the Nano) you could connect the Uno Rx pin 0 to the Nano Rx pin 0 and connect the GNDs and that way anything that the Uno receives from the PC will also be received by the Nano on Serial without any need for SoftwareSerial. However there will be no means for the Nano to respond either to the Uno or to the PC.
...R
So... This might be what is happening.
I actually have "GND's" connected to each controller. However the Arduino Uno pins (for now 8 and 9) connect to Nano RX/TX.
And what happens is exatcly that. Nano wont respond to anything. Just noticed that it receives the information directly without actually receiving a value. The variable is definitely empty.
Thanks for your insight on this matter.
Just gave it a try. Example 3:
Even though it didn't work for me (at least as is); it has some really nice features and they are not that difficult to work with. Will definitely consider using them after managing to make this (psudo simple) project work.
Connecting the RX pins directly:
Was also a no go. Nothing works actually. Sender Module simply gets stuck. Nothing sent and nothing received.
Another Fail: Nothing is received in "Arduino Nano" Receiver Module. (Awkward).
Just as a functional test, you might put the below echo code on the uno and the nano, then connect the board's hardware tx pins together, the rx pins together and a ground between the boards. Open the serial monitor and send something to the uno and see if it gets echoed back from both boards. The nano code has a delay added so it won't send at the same time as the uno.
uno
//zoomkat 6-29-14 Simple serial echo test
//type or paste text in serial monitor and send
String readString;
void setup() {
Serial.begin(9600);
Serial.println("Simple serial echo test"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the String readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured String
readString="";
}
}
nano
//zoomkat 6-29-14 Simple serial echo test
//type or paste text in serial monitor and send
String readString;
void setup() {
Serial.begin(9600);
Serial.println("Simple serial echo test"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the String readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
delay(200);
Serial.print("nano also received ");
Serial.println(readString); //so you can see the captured String
readString="";
}
}
rgxHost:
Just gave it a try. Example 3:
Even though it didn't work for me (at least as is); it has some really nice features and they are not that difficult to work with. Will definitely consider using them after managing to make this (psudo simple) project work.
Connecting the RX pins directly:
Was also a no go. Nothing works actually. Sender Module simply gets stuck. Nothing sent and nothing received.
Another Fail: Nothing is received in "Arduino Nano" Receiver Module. (Awkward).
As you have not provided the programs and the wiring diagrams that you used in those two tests it is impossible to comment.
Yes; the project was very messy to post whatever in here.
Rather try to post the most clean as possible.
I was able to manage to connect from Uno to Nano properly but; now I'm stuck into getting a Serial Data Exchange to Uno from my Application.
I can receive status data (i.e: "" message from Arduino Uno)
However when I send Arduino Uno won't answer (was peeking the communication led on it and no blink)
Issues so far:
At this point I'm completely clueless on how to get the application send data to Arduino working.
Can this be due to the Arduino Code?
Arduino Uno (Sender): Prints Input Data (Sent Data) on its monitor with 3 lines for some reason.
example:
Wire Connection and Updated Code for both Modules Bellow
Image:
/* Arduino Uno : Sender */
/*
* Serial Communication Notes:
* HW & SW Serial:
* - Each Serial (Harware and Software Serial) has 1 Port Available Only.
* - Once the Port is Open by some device it will become unusable to other devices.
* - "Harware Serial" will be used to Communicate this Module with PC.
* - "Software Serial" will be used to Communicate between 2 Arduino Modules.
*
* Wire Connection : Logic
* - In order to Transmit; something has to be expecting to Receive.
* - In order to Receive; you must expect something to be Transmitting.
* - Therefore the wires are cross connected ("Cross Link").
* - See picture attached to this project folder.
* - You need to remove wires before uploading the code.
*
* Uno RX [Connects To] Nano TX : Uno Pin A4 (RX) to Nano TX Pin
* Uno TX [Connects To] Nano RX : Uno Pin A4 (TX) to Nano RX Pin
*
*/
// Include "SoftwareSerial" Library
#include <SoftwareSerial.h>
// Define "SoftwareSerial" object and the used pins.
SoftwareSerial softSerial(A5, A4); /* RX, TX */
void setup()
{
/* Start Harware Serial : To be able to Communicate with Computer */
Serial.begin(9600);
/* Start Software Serial : To be able to Communicate Arduino Nano (Receiver) */
softSerial.begin(9600);
/* Print Status Message */
Serial.println("<Uno Ready>");
}
void loop()
{
/* Check received characters from the Computer Apllication. */
if (Serial.available() > 0)
{
// Write (send) the received value.
// The value that you Enter from Computer or Console Serial Monitor .
// This value is received by the SoftwareSerial.
/* Create a variable to store the value */
char usrInput = Serial.read();
/* Print value to be sent */
Serial.println(usrInput);
/* Send the value */
softSerial.write(usrInput);
}
}
/* Arduino Nano : Receiver */
/*
* Serial Communication Notes:
* HW & SW Serial:
* - Each Serial (Harware and Software Serial) has 1 Port Available Only.
* - Once the Port is Open by some device it will become unusable to other devices.
* - "Harware Serial" will be used to Communicate this Module with PC (Not used in this module).
* - "Software Serial" will be used to Communicate between 2 Arduino Modules.
*
* Wire Connection : Logic
* - In order to Transmit; something has to be expecting to Receive.
* - In order to Receive; you must expect something to be Transmitting.
* - Therefore the wires are cross connected ("Cross Link").
* - You need to remove wires before uploading the code.
* - See picture attached to this project folder.
*
* Uno RX [Connects To] Nano TX : Uno Pin A4 (RX) to Nano TX Pin
* Uno TX [Connects To] Nano RX : Uno Pin A4 (TX) to Nano RX Pin
*
*/
/// Include "SoftwareSerial" Library
#include <SoftwareSerial.h>
// Define "SoftwareSerial" object and used pins.
SoftwareSerial softSerial(1, 0); /* TX, RX ("Cross Link") */
// Led Pin
int ledPin = 3;
void setup()
{
/* Start Software Serial : To be able to Communicate Arduino Uno (Sender) */
softSerial.begin(9600);
/* Configure LED Pin*/
pinMode(ledPin, OUTPUT);
/* Send Status Message Back to Arduino Uno (Sender) */
softSerial.println("<Nano Ready>");
}
void loop()
{
/* Check if data was received from SoftwareSerial Buffer */
if (softSerial.available() > 0)
{
/* Read SoftSerial Buffer and Apply Option*/
LedMode(softSerial.read());
}
}
/* LED Control */
void LedMode(char ledState)
{
switch (ledState)
{
case '1': LedOn(); break;
case '2': LedOff();break;
}
}
void LedOn() { digitalWrite(ledPin, HIGH); }
void LedOff() { digitalWrite(ledPin, LOW); }
Both programs are closely based on the second example in Serial Input Basics
If you were to use SoftwareSerial on the nano and leave pin 0 and 1 free for communication with the PC then you could send debug messages to the Serial Monitor.
Everything is working well so far (in a simple manner). I'm going to take a look at your sketch and attempt to improve what I have. I definitely need lots of improvement (see: Things to Implement bellow).
I am able to receive Uno Output to my Application Console. However from Nano to Uno; I'm not sure what I am receiving. Bytes - I guess?
Have tried last night converting the values received. Returns one char at a time (1 Char per Line).
Char Arrays in C++ seem to be implemented differently unlike C#. I'll have to figure it out as well.
Regarding your last comment:
I wasn't able to use another Pins besides 0 and 1 on Nano. Simply nothing happens (No Comms).
You Code doesn't start SoftwareSerial [ softSerial.begin(baudRate); ]; for any specific reason?
Things to Implement
Some sort of Handshake (to Ping Devices)
The Nano will be replaced with Nano RF (Nano with built in NRF24L01).
This one is really tough. I mean: There are separate classes for the NRF24L01 and as far I could read, the HW Serial gets mixed in the way with the nRF (And my experience on C++ ain't the same as with C#).