Serial communication between NANO and DUE

Hi!
I'm trying to send data from a Nano v3.0 to a DUE.
The communication is one way only.
I know about the 5v vs 3v3 issue, so i bought this device from Sparkfun:
Sparkfun LLC

The circuit has 8 potentiometers connected to the Nano, this board has to read the values and send them to the DUE, along with an identification number for the potentiometers.

LEDs connected to PWM pins on the DUE should light up for debug purposes.

This is the wiring:

This is the code for the Nano:

#define pot1 A0
#define pot2 A1
#define pot3 A2
#define pot4 A3
#define pot5 A4
#define pot6 A5
#define pot7 A6
#define pot8 A7

int lastPot1 = 0;
int lastPot2 = 0;
int lastPot3 = 0;
int lastPot4 = 0;
int lastPot5 = 0;
int lastPot6 = 0;
int lastPot7 = 0;
int lastPot8 = 0;

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

void loop() {
  //Potentiometer 1
  int currentPot1 = analogRead(pot1);
  currentPot1 = map(currentPot1, 0, 1023, 0 ,255);

  if (currentPot1 != lastPot1) {
    lastPot1 = currentPot1;
    Serial.print("2,");
    Serial.println(currentPot1);
  }
  
  //Potentiometer 2
  int currentPot2 = analogRead(pot2);
  currentPot2 = map(currentPot2, 0, 1023, 0 ,255);

  if (currentPot2 != lastPot2) {
    lastPot2 = currentPot2;
    Serial.print("3,");
    Serial.println(currentPot2);
  }
  
  //Potentiometer 3
  int currentPot3 = analogRead(pot3);
  currentPot3 = map(currentPot3, 0, 1023, 0 ,255);

  if (currentPot3 != lastPot3) {
    lastPot3 = currentPot3;
    Serial.print("4,");
    Serial.println(currentPot3);
  }
  
  //Potentiometer 4
  int currentPot4 = analogRead(pot4);
  currentPot4 = map(currentPot4, 0, 1023, 0 ,255);

  if (currentPot4 != lastPot4) {
    lastPot4 = currentPot4;
    Serial.print("5,");
    Serial.println(currentPot4);
  }
  
  //Potentiometer 5
  int currentPot5 = analogRead(pot5);
  currentPot5 = map(currentPot5, 0, 1023, 0 ,255);

  if (currentPot5 != lastPot5) {
    lastPot5 = currentPot5;
    Serial.print("6,");
    Serial.println(currentPot5);
  }
  
  //Potentiometer 6
  int currentPot6 = analogRead(pot6);
  currentPot6 = map(currentPot6, 0, 1023, 0 ,255);

  if (currentPot6 != lastPot6) {
    lastPot6 = currentPot6;
    Serial.print("7,");
    Serial.println(currentPot6);
  }
  
  //Potentiometer 7
  int currentPot7 = analogRead(pot7);
  currentPot7 = map(currentPot7, 0, 1023, 0 ,255);

  if (currentPot7 != lastPot7) {
    lastPot7 = currentPot7;
    Serial.print("8,");
    Serial.println(currentPot7);
  }
  
  //Potentiometer 8
  int currentPot8 = analogRead(pot8);
  currentPot8 = map(currentPot8, 0, 1023, 0 ,255);

  if (currentPot8 != lastPot8) {
    lastPot8 = currentPot8;
    Serial.print("9,");
    Serial.println(currentPot8);
  }
}

And this is the code for the DUE:

void setup() {
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}

void loop() {

  while (Serial.available() > 0) {
    int pin = Serial.parseInt();
    int power = Serial.parseInt();

    if (Serial.read() == '\n') {
      analogWrite(pin, power);
    }
  }
}

The problem is that the entire thing doesn't work at all! The only thing that works is the TX LED on the Nano, which blinks as expected.

Any help appreciated. Thank you all!!!

Do you see the expected values from the pots and the mapped pot values on the Nano if you view the Serial output on the Serial monitor with the Due disconnected ?

Similarly, what values do you see on the Serial monitor if you disconnect the Nano and enter appropriate values into the Due Serial monitor input ?

Basically you should be looking to confirm that values are as expected at various parts of the program by adding Serial prints to the code to provide debug information. For instance, if you send "2" what do you receive compared with what you expect to read and are testing for ?

The output from the Nano is fine, it print just what I want:
when i move a potentiometer, the Nano print out his number and the value in a 0 to 255 range.

The problem is that the DUE doesn't receive any data at all!!!
The RX LED is supposed to blink when some data is received but it doesn't happen! There is no communication between the boards, and i can't figure out why!
Maybe I wired them up wrong, or maybe the Sparkfun LLC is not the right tool for the job.


When the Nano print "2," (for example) I expect the DUE to read it as an integer, which is the target pin for the PWM signal.

In your circuit where is the power coming from for the two Arduinos ?

Oh, I forgot to draw the power suorce!!!

They are both powered by a 12V DC power supply.

Let's see a sample of the output from the nano.

Mark

Here it is, all the potentiometers were set to 0, I moved back and forth the first potentiometers pretty fast in order to get a short sample.

This is what the nano printed out:

2,1
2,6
2,9
2,14
2,19
2,26
2,32
2,39
2,46
2,54
2,62
2,70
2,79
2,87
2,97
2,107
2,119
2,133
2,149
2,167
2,182
2,196
2,210
2,226
2,242
2,255
2,254
2,247
2,237
2,223
2,206
2,187
2,171
2,155
2,139
2,120
2,97
2,75
2,54
2,35
2,19
2,7
2,0


The problem is that the DUE doesn't receive any message....

  while (Serial.available() > 0) {
    int pin = Serial.parseInt();
    int power = Serial.parseInt();

    if (Serial.read() == '\n') {
      analogWrite(pin, power);
    }

How do you know that the DUE doesn't receive any data? The character that terminated the int is discarded. So, there is nothing left to read, because the line feed hasn't arrived yet. So, next time around loop(), when there is something to read, it's the linefeed that followed the carriage return that marked the end of the int, and you assume that it is two more ints.

Quit using parseInt(), or quit assuming that there is data still to be read.

I think that the DUE doesn't receive any data because the RX LED doesn't blink.

I'm really sorry, but I don't understand your advice due to my inexperience...
What am I supposed to do? can you post the correct code?

Hi, I think your are using the wrong part of the level changing board.
Look at the sparkfun
https://learn.sparkfun.com/tutorials/bi-directional-logic-level-converter-hookup-guide

Using the BD-LLC for Serial

Check the Rx of the UNO and where it is connected on the adapter, now check your DUE Rx and where you have it connected on the adapter.
Check the nano Tx and where it is connected to the adapter, in both cases.

Tom.... :slight_smile:

That's not the right PCB!
Click on the link in the main post, you posted the tutorial for another product!

By the way, the Uno operates at 5v, just like the Nano, so the wiring looks correct!!!

That's not the right PCB!

I think you will find that it is the same device. Open the link in your first post then click on Hookup Guide and it takes you to the page that Tom posted.

Sorry you are right!!!

Anyway, I start to think that a UART communication from a 5v Nano to a 3v3 DUE is impossible....

TheAxe:
Sorry you are right!!!

Anyway, I start to think that a UART communication from a 5v Nano to a 3v3 DUE is impossible....

Have you got the 2 Arduinos and the level shifter connected as illustrated on the Hookup Guide page or not ?

Yes, as you can see in the pic in the first post, I connected the Arduinos to channel 2 of the LLC, which is a step down channel, from the 5v signal (coming from the Nano) to the 3v3 one (for the DUE).

I followed very carefully that guide!!!

On the Due, you're using RX0 which is connected to the ATmega16U2, used for USB programming and for communicating with the PC.

Try RX1 (pin 19). Where ever you have "Serial" in your sketch, rename to Serial1.

For example:

[color=navy]Serial1.begin(9600);[/color]

Ok, I discovered something:

I noticed that if I unplug the Nano's USB cable, the Arduino doesn't send messages anymore.
The TX LED doesn't blink anymore, I tried to reset the board but the Nano doesn't want to sent messages.

So now I think that the problem is not with the RX of the DUE.

How can I test the functionality of the Nano's TX pin?

P.S.
dlloyd, i followed your advice, so now i'm using RX1 (pin 19) on the DUE

I noticed that if I unplug the Nano's USB cable, the Arduino doesn't send messages anymore.

How are you powering the Nano when not using the USB cable?

The TX LED doesn't blink anymore

Well, of course not. The TX LED is controlled by the USB-to-serial chip, NOT by the ATMega328 chip.

The wiring is the same of my original design: I use a 12V power supply in order to power both the Arduinos.

This issue is driving me crazy, I mean, both the wiring and the code looks correct to me... I really can't understand what's happening here!

I have zero experience with the nano, however I see it has only one serial port and it is also being used. In this case, pins 0 and 1 are connected to an FTDI FT232RL for serial communication over USB to the PC. Read details here under Communication. I'm quite sure you'll need to use a different pin and software serial or other method of serial communication.

Because of this, I suspect your original connections will never work.