chipKIT® Development Platform

Inspired by Arduino™

Try to get Arduino MEGA and MAX32 to comunicate via serial

Created Fri, 31 May 2019 13:10:15 +0000 by aldoz


aldoz

Fri, 31 May 2019 13:10:15 +0000

Hi everyone. I'm trying to get an Arduino MEGA2560 to communicate with a Max32 via serial. I connected the Arduino MEGA TX (pin 18) with the Max32 RX (pin 19); the RX of the Arduino MEGA (pin 19) with the TX of the Max32 (pin 18). I do not use any other electrical components.

The MEGA code allows me to enter a character from the keyboard; when I press enter so this character is sent to MAX32 which will read it and send it back to the MEGA which, at this point, will display it on the serial monitor.

Here is the code that is executed by the MEGA:

//CODE A (Arduino MEGA)
void setup() {
  Serial.begin(115200);
  Serial1.begin(115200);
}
void loop() 
{
  if(Serial.available()) 
  {
    char a = Serial.read();
    delay(10);
    Serial1.write(a);

    while(!Serial.available()) 
    { }
    char b = Serial1.read();
    Serial.print("MAX32 answer this character : ");
    Serial.println(b);
    delay(100);
  }
}

while this is the code that is executed by the MAX32:

//CODE B (Chipkit MAX32)
void setup() {
  Serial.begin(115200);
}

void loop() {
  if(Serial.available()) {
    char a = Serial.read();
    delay(100);
    Serial.write(a);
  }
  delay(50);
}

Here is the procedure I'm using:

  1. I connect the max32 to the PC via USB (configuring the IDE to use the MAX32)

  2. load the sketch onto the Max32

  3. detach the max32 from the pc

  4. I connect the MEGA arduino to the pc via usb (configuring the IDE to use the MEGA)

  5. Load the sketch of the MEGA

  6. I reconnect the max32 to the PC via USB (the MEGA arduino is already connected and the IDE settings remain those for the MEGA)

  7. I open the serial monitor of the MEGA sketch

  8. I press a key on the PC keyboard (any letter) and I press enter

  9. the serial monitor shows me the character that MAX32 has read and that has returned to the MEGA but that character is always "?"

What happens? what I'm forgetting?

During work, the MEGA turns the TX and RX lights on and off while the Max32 does not illuminate any light. Help!


majenko

Fri, 31 May 2019 14:08:27 +0000

It could be because the MAX32 is a 3.3V device and the Mega is a 5V device. The 3.3V is right on the cusp of being a valid logic signal, and any slight variation will cause it to fail to register properly. You may need a logic level translator between the two.

Also, you did connect the ground pins together between the two boards, didn't you?

Another thing: Serial on the MAX32 is pins 0/1. You should be using Serial1 for TX1 and RX1.


aldoz

Fri, 31 May 2019 16:27:15 +0000

It could be because the MAX32 is a 3.3V device and the Mega is a 5V device. The 3.3V is right on the cusp of being a valid logic signal, and any slight variation will cause it to fail to register properly. You may need a logic level translator between the two.

Hi majenko, So I need to use a level-shifter between the RX-TX TX-RX of the 2 boards?

Also, you did connect the ground pins together between the two boards, didn't you?

I no using a ground wire between the 2 boards because both the MEGA and the MAX32 are USB plugged (so they using the PC USB ground) do I need that cable anyway?

Another thing: Serial on the MAX32 is pins 0/1. You should be using Serial1 for TX1 and RX1.

Do you mean the PWM/DIGITAL pin number 0 (0RX) and pin number 1 (1TX)?

Currently I am using pin number 19 (RX1) and pin number 18 (TX1) on the MAX32 and same pins (18 and 19) in MEGA too


majenko

Fri, 31 May 2019 16:57:07 +0000

You must have a ground connection directly between the boards. The USB connection will work - kind of - but it will be really noisy.

If you use "Serial" on the MAX32 then you need to use digital/PWM pins 0 and 1 (RX0 and TX0). If you want to use COMMUNICATION pins 18 and 19 (TX1 and RX1) then you must use "Serial1" not "Serial".

Just like on the Mega, where you are using Serial1 for the communication through those pins.


aldoz

Fri, 31 May 2019 17:04:11 +0000

Wow great advices!

You must have a ground connection directly between the boards. The USB connection will work - kind of - but it will be really noisy.

Ah! so I will use a ground wire between Max32 GND pin and MEGA GND pin!

If you use "Serial" on the MAX32 then you need to use digital/PWM pins 0 and 1 (RX0 and TX0). If you want to use COMMUNICATION pins 18 and 19 (TX1 and RX1) then you must use "Serial1" not "Serial".

So I MUST to use digital/PWM pins 0 and 1 (RX0 and TX0) !! And, in the MEGA, can I continue to use pins 18 and 19?

and about the level shifter? I really need to use it?


majenko

Fri, 31 May 2019 17:22:21 +0000

No, you don't have to use RX0 and TX0. you only have to use those if you continue to use Serial. Instead just change Serial to Serial1 and then you use the pins you are already using - pins 18 and 19.

You should use a level shifter, but once you start using the right Serial1 object and have a proper ground connection you may find you don't need it.


aldoz

Fri, 31 May 2019 19:32:25 +0000

No, you don't have to use RX0 and TX0. you only have to use those if you continue to use Serial. Instead just change Serial to Serial1 and then you use the pins you are already using - pins 18 and 19. You should use a level shifter, but once you start using the right Serial1 object and have a proper ground connection you may find you don't need it.

Majenko really sorry for my poor english..

So I need to use PWM/Digital pins 0 and 1 just because the MAX 32 sketch using the command "Serial" So I will to use these 0 and 1 pins. IF I would to use pins 18 and 19 I need to use Serial1 (like is written in the Arduino MEGA sketch) But ok, no problems, I will NOT to change the MAX32 sketch so I will use pins 0 and 1. Ok I start some tests!


aldoz

Fri, 31 May 2019 20:34:32 +0000

Ok, using pins 0 and 1 in the MAX32 using pins 19 and 18 in Arduino MEGA added a ground wire from the Mega GND pin to the MAX 32 GND pin Modified both sketches removing the delay(100) line.

Now I start serial monitor on my MEGA and I push a key (for example the character Z) so I get "MAX32 answer this character : ?" if I re-enter the character Z a second time so I finally get "MAX32 answer this character : Z"

So I need to enter 2 times in a row the character to let the MAX32 to return it correctly!

Maybe wrong baud rate?


majenko

Fri, 31 May 2019 21:06:39 +0000

More likely the lack of a logic level translator.


aldoz

Fri, 31 May 2019 21:11:13 +0000

More likely the lack of a logic level translator.

So I need to use the level shifter between Max32 RX/TX and Mega RX/TX


majenko

Fri, 31 May 2019 22:50:13 +0000

I would certainly recommend it, yes. The HIGH logic level of CMOS (VIH) is 0.6 x VDD. Since the Arduino is 5V that's 0.6 x 5V = 3V.

If you are powering from USB that 5V can be as high as 5.25V and still be in spec - which means 3.15V for VIH.

The PIC32 is 3.3V, and the output can be slightly below that, say 3.2V, when HIGH. As you can see, the two numbers are close. Any noise in the circuit will cause problems.

By using a logic level translator you convert that ~3.2V into something nearer 5V which is far more reliable.

However, I just spotted an error in your Mega code:

while(!Serial.available()) 
    { }
    char b = Serial1.read();

I think that should be "while(!Serial1.available())". You are:

  1. Sending 2 or 3 characters to the Mega ("X\r\n")
  2. Sending the first to the MAX32 ("X")
  3. Waiting for there to be something available on Serial (which has "\r\n" still buffered, so always passes)
  4. Reading a character from Serial1, which hasn't had anything arrive yet
  5. Printing that nothing.

So it's only at some point in the future that you will get any response. When you use Serial1.read() and there is nothing there yet it returns -1. That equates to character 255, which is unprintable, so prints "?"


aldoz

Sat, 01 Jun 2019 07:22:57 +0000

Majenko thank you so mutch for help! It's all really complex for my knowledge level but I start to understand the logic behind the use of a level-shifter for this serial comunication.

I will change the code using the following :

while(!Serial1.available()) 
    { }
    char b = Serial1.read();

and please check my schema, can you tell me if it's correct?


majenko

Sat, 01 Jun 2019 10:55:54 +0000

Yep. looks right to me.


aldoz

Sat, 01 Jun 2019 12:28:03 +0000

Yep. looks right to me.

Majenko, again and again thank you! I will test the new configuration in 1 or 2 days! and I will put here the results!


aldoz

Sun, 02 Jun 2019 14:18:28 +0000

Ok I made all the tests!

So,

  1. I am using a level-shifter but it don't change the result; If I use it or not, the final result not change.

  2. The error sicovered by you majenko (the error in the code, serial1 instead serial) was the pretty one problem!: The result is not 100% perfect but almost! Now when I insert a character (by the PC keyboard, for example "Z") and I press enter so I get :

"MAX32 answer this character : Z"
"MAX32 answer this character :  "

So, Max 32 answer the right character but in the same answer give another character to MEGA and this character is " " (space). If I restart and I insert a sentence instead of a single character (for example "aldoz go") ,when I press enter so I get:

"MAX32 answer this character : a"
"MAX32 answer this character : l"
"MAX32 answer this character : d"
"MAX32 answer this character : o"
"MAX32 answer this character : z"
"MAX32 answer this character :  "
"MAX32 answer this character : g"
"MAX32 answer this character : o"
"MAX32 answer this character :  "

Note the last character, it's always present in the result and this is again a space character! Then, if I insert a single character or a word or a phrase so I get always an added space character.

Honestly it's not a big deal for me! my first objective is to get an immediate/precise answer by MAX32, I simply will not consider this last added character in my code.

As I write at the start of this last post, I am using the level-shifter but no differences when I use it or not. just a detail : the level shifter I am using is not like the one in the attched pic; here the pins of my level-shifter:

mega side: AVIN ASCL ASDA AGND

max32 side: BVIN BSCL BSDA BGND

So not generic inputs but is specified SCL and SDA.. and instead I attacked TX and RX! I don't know if it work only when shifting level for SCL and SDA pins or not! but sure is that if I use it or not, nothing changes!


majenko

Sun, 02 Jun 2019 18:22:38 +0000

That extra character is the "line ending" that you are sending. If you print the character as a decimal rather than a letter (Serial.print(x, DEC)) you will see it's either 10 or 13, depending on what you have your line ending set to in the serial monitor.


aldoz

Sun, 02 Jun 2019 19:19:01 +0000

That extra character is the "line ending" that you are sending. If you print the character as a decimal rather than a letter (Serial.print(x, DEC)) you will see it's either 10 or 13, depending on what you have your line ending set to in the serial monitor.

So this added space character seems normal! I mean ,this is not avoidable, is the correct logic of the code right?

Thank you so mutch for all your help majenko! again and again!


majenko

Sun, 02 Jun 2019 20:04:51 +0000

You should read my tutorial on reading serial:


aldoz

Mon, 03 Jun 2019 09:03:40 +0000

You should read my tutorial on reading serial:

Reading right now and it is really interesting! (like interesting how I am far light years to a fully knowledge of this matter..) Sure now I know more and I start to understand the REAL logic behind serial comunications. And a lot of other interesting stuff there! a lot of cool reading

PS: I know this thing is out of this thread but to avoid reopening the post on the laser, I ask you: since MAX32 is 3.3v and the laser works at 5v, could I try to use the level-shifter to avoid wire blocking when I use the laser?