Go Down

Topic: Problems reading serial (Read 979 times) previous topic - next topic

thchittenden

Hi all,

So I'm having some problems reading serial data from an Atmega328 (from Sparkfun's Razor 9DoF IMU) through a logic level converter into an Arduino Mega. I loaded the Atmega328 with some basic code to continuously print 1 through 100 over serial (I hooked it directly up to the computer and the data looks fine, I don't think the problem is here).

Code: [Select]
void setup(){
  Serial.begin(19200);
}
void loop(){
  for(int i = 0; i<100; i++){
      Serial.println(i);
  }
}


The Atmega328 runs at 3.3v so I route the serial data through Sparkfun's logic level converter (http://www.sparkfun.com/products/8745). On the logic level converter I connected the grounds together (everything is on a common ground), the LV pin to the 3.3v output on the Arduino Mega, the HV pin to the 5v output on the Arduino Mega, and I connected all the TX/RX pins up to their respective ports (everything is connected correctly on this board so I don't think the problem is here either).

On the Arduino Mega I put code to just retransmit all the data from Serial3 to Serial:

Code: [Select]
void setup(){
Serial3.begin(19200);
Serial.begin(57600);
}

void loop(){
if(Serial3.available() > 0)
{
Serial.write(Serial3.read());
}
if(Serial.available() > 0)
{
Serial3.write(Serial.read());
}
}


I originally had the Atmega328 transmitting at 57600 baud but all I got was a bunch of garbage (and a few numbers). In the comments on the logic level converter product page, someone said something about it attenuating signals so I lowered the baud to 19200 and now the data looks alright, except, approximately every 195 numbers (565 bytes with the newline characters?) I get a load of garbage:

Code: [Select]
76
77
78
79
80
x×ô?Þ}_¼¾lòæ·?Ý­?ÿù¿úúþ:Kû¿r÷ñ
総ç¾ûãýû¼öëÚ¿Mß¿?~N~sû®ÏÇ¿~ãçýî=]?î~ôë¯?w³ny¿¿Ýiá­áõ{wzó|Ïçç³Ï­ÏÝt~ýï&w¯=Oý¶n~\þý2ÿüÓ?~?g¿~ôûî?6ÿßùç}éÛ~ú÷nùïûGæÏn­Ãç×îv§ëÇ÷¯·ßÓöÏõîm·~wÜþxï?×÷??w­®/íÓ÷Þ]ûiEÕ}-÷Ï'ÿÏwߨý}ùþ=«çå^·òîûGíîxûã7£ÝC¡?SH¨ºj
58
59
60
61
62


It's odd because it does this really consistently at around every 195 numbers. I don't think i'm overrunning the buffers in the Arduino Mega because I'm printing to the computer at 57600 baud and the Atmega328 is only printing at 19200 baud.
If anyone's had experience with logic level converters, is there a maximum baud i can transmit at over them? I can't really pinpoint the problem to hardware or software so if anyone has any ideas, they'd be much appreciated!

Thanks,

PaulS

Quote
I don't think i'm overrunning the buffers in the Arduino Mega because I'm printing to the computer at 57600 baud and the Atmega328 is only printing at 19200 baud.

The baud rate refers to how fast the RX pin is toggled to transmit a bit. It is still possible to overrun the receive buffer.

Try adding a small delay in the sender code, after each value is sent. 50 milliseconds should be good. Does that affect where the bad data appears?

Quote
If anyone's had experience with logic level converters, is there a maximum baud i can transmit at over them?

The logic level converter is NOT the source of your problem.

Code: [Select]
void loop(){
if(Serial3.available() > 0)
{
Serial.write(Serial3.read());
}
if(Serial.available() > 0)
{
Serial3.write(Serial.read());
}
}

What is the Razor doing with the data you send it?

thchittenden

Quote
The baud rate refers to how fast the RX pin is toggled to transmit a bit. It is still possible to overrun the receive buffer.

Try adding a small delay in the sender code, after each value is sent. 50 milliseconds should be good. Does that affect where the bad data appears?


I added a 50 ms delay after every 100 numbers written but the garbage still appeared in the same place. When I added a 10 ms delay after every number written, however, the garbage occurred more often (about every 140 bytes written) and there was less of it (around 40-48 bytes of garbage). I increased the delay between every number to 50 ms and this was the output:

Code: [Select]
[...]71
72
73
74
êùÿ÷öÃû77
78
79
80
81
82
83
ý}çÝM¦ÿ86
87
88
89
90
91
92
93
_zç·}ù¾½^97
98
99
0
1[...]


It seems to be REALLY consistent at outputting 7 good numbers (regardless of whether they're 1 or 2 character numbers), some garbage, 8 good numbers, some garbage, and then back to 7 good numbers...

Also, I'm still a bit confused about how I could be overrunning the buffer. If the Arduino Mega is talking with the computer at 57600 baud and receiving from the Razor at 19200 baud and if the main loop is doing nothing more than relaying the signals i.e. going plenty fast enough to keep up with these baud rates, shouldn't it take 1/3 the time to write the byte from the Arduino to the computer that it took for the Razor to write the byte to the Arduino?

Quote
What is the Razor doing with the data you send it?


This is just my generic serial testing program so the relaying the messages from the computer to the razor is just leftover. I'm not sending anything from the computer to the Arduino but just in case, I commented it out.

PaulS

Code: [Select]
Serial.write(Serial3.read());
Does Serial.print() do any better?

If not, there is something causing interference (noise) in your system. You need to figure out what that is, and stop it.

thchittenden

Serial.print() made the noise look neater, but it was still there.

I think I found what the problem is though. When I downloaded some code to the Razor and opened up the serial monitor to verify it, I saw an NMEA string  XD I've got a GPS right next to the Razor so I must have some wires touching or something.

Thanks for all the help.

thchittenden

So I have both the GPS and the Razor hooked up to the same logic level converter, seeing as it had four channels I figured it would be fine to use two devices on it. I'm only seeing 20 kOhm resistance between the two Rx pins on the Arduino with the LLC attached though. This is really my first big Arduino project using multiple devices on a LLC so I'm not sure if that is low enough to allow interference between the GPS and the Razor. I have more LLCs so I guess I could use two different ones, it just seemed convenient to use a single one.

PaulS

If the Mega is not talking to the Razor, listening only, the 3.3V signal from the Razor should be sufficient to ensure reliable communication. What happens if you simply don't use the LLC between the razor and the Mega?

It's likely that the GPS doesn't need the LLC, either.

zoomkat

It just seems like from times past that ~2.1v was a go/no go transision point for high/low detection. A simple setup with a pot probably could easily determine what the point is with the current chips.
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

PaulS

Quote
It just seems like from times past that ~2.1v was a go/no go transision point for high/low detection.

The Arduino site says 60% of the board's operating voltage or above is considered HIGH. For a 5V board, that would be 3V for HIGH. A 3.3V board's output should be plenty.

Go Up