Serial comm to Arduino Mega

13 Feb 2011

Hey guys, I'm having trouble getting my mbed to send ints to my arduino mega. I set up a simple code on the mbed:

#include "mbed.h"

Serial duino(p28, p27);
Serial pc(USBTX, USBRX);
Timer timer;

int main() {
    pc.baud(115200);
    duino.baud(9600);
    pc.printf("Starting...\n\r");

    timer.start();
    while (1) {
        if (timer.read() > 1) {
            pc.printf("Writing!\n\r");
            duino.putc(4);
            timer.reset();
        }
    }
}

The code on the arduino is just looking for this incoming data:

void setup()
{
  Serial.begin(115200);
  Serial2.begin(9600);
  Serial.println("Starting...");
}

void loop() {
  if(Serial2.available() > 0){
    Serial.print("Got something!  ");
    Serial.println(Serial2.read(),DEC);
  }
}

The arduino sits there and doesn't seem to receive anything. I also tried printf("4") also with no success (probably as expected...). Anybody have any idea what the problem is? I successfully implemented SPI comm between the mbed (slave) and arduino (master), I don't know why this is giving me problems...

Thanks!

13 Feb 2011

tell us about the wiring: TX to RX, RX to TX, voltage levels (Arduino/AVR may be 5V)

13 Feb 2011

Oh sorry, I should have mentioned that- I do have TX to RX and RX to TX. It may be worth mentioning that the mbed is 3.3V and it is a 5V arduino mega. However I was under the impression that the digital trigger threshold was between 2-3V for registering high even on a 5V microcontroller (and most of the mbed pins are 5V tolerant). The weird thing is that between these 2 same microcontrollers I have SPI comm working reliably- any idea why that could be?

13 Feb 2011

Have you read the stories of Arduino (5V) to XBee (3.3V) on http://forum.sparkfun.com/viewforum.php?f=13

I recall that the XBee's serial input is not 5V tolerant. A two-resistor voltage divider is used. A diode in series is used by some but the resulting logic 0 voltage has no margin with this diode approach.

13 Feb 2011

What function are you using on the Arduino to find the input? If it's expecting a CR, LF or CR & LF sequence ( line input ) then you are not issueing that.

Yes the 3.3v output to 5v input will work.

13 Feb 2011

Are you saying that the mbed serial (or p28, p27) is not 5V tolerant? Does this matter if it's only TX from mbed to arduino? http://mbed.org/forum/mbed/topic/193/?page=1#comment-794

13 Feb 2011

It's not enough just to send individual ints? That's always worked fine for comm between two arduinos... what do you mean that it would be expecting CR and/or LF? Is this something that is done automatically with the arduinos?

13 Feb 2011

Hi Jason,

Here is an example showing two serial ports communicating just sending characters (on the same chip!):

Import program

00001 // example showing sending/receiving character
00002 
00003 #include "mbed.h"
00004 
00005 // connect p9 to p14 with a wire!
00006 Serial out(p9, NC);
00007 Serial in(NC, p14);
00008 
00009 DigitalOut led(LED1);
00010 
00011 int main() {
00012     out.putc('h');
00013     out.putc('i');
00014     char c1 = in.getc();
00015     char c2 = in.getc();
00016     
00017     led = 1;
00018     printf("c1: %c, c2: %c\n", c1, c2); 
00019 }

This gives the expected results.

When communicating with another device, the sort of things I'd check are:

  • They have a shared ground (0v), so the signalling works
  • The correct pins are connected (easy to be off by one etc)
  • They are running at the same baudrate
  • They are the same signalling voltage - although the mbed is certainly 5v tolerant, that doesn't mean that something receiving 0/3.3v signalling from the mbed but expecting 0/5v signalling will behave correctly (although it often does!) - it is also more likely to work the other way around, so perhaps try that first

Hope that helps.

Simon

17 Feb 2011

Hey guys, it was a dumb wiring connection (pretty embarrassing). Thanks for all your helpful responses though; I still learned some stuff!

16 Dec 2016

Can i have your Waring diagram pls.