Using an Arduino Mega as a Debug Tool

Hi, not sure if this is a hardware or a programming issue, so I'll put it here for now...

I'm working with an ATMEGA328 chip on a breadboard. I'm using the Arduino library for easy coding within Atmel Studio (and an ISP MKii for programming). I have no way to easily debug my code short of flashing an LED, so I came up with the idea to use the RX and TX pins of the ATMega328 to print out feedback and plug them into my Arduino Mega, since it has multiple UARTs, which I can then listen on port 3 and output the results through 0 to the serial monitor. Only one problem... I'm only getting garbage back...

Here's my code on the Atmega328 - just send a simple hello

#include <Arduino.h>

int testLED = 2;

void setup() {
   Serial.begin(9600);
   delay(3000);
   pinMode(testLED, OUTPUT);
}

void loop() {
  Serial.println("hello");
  delay(1000);
}

...And this is the Arduino Mega - receive the hello and print it to the serial monitor

void setup() {
Serial.begin(9600);
Serial3.begin(9600);
delay(1000);
Serial3.setTimeout(500);
delay(1000);
Serial.println("Ready...");

}

void loop() {
  Serial.print("bytes available: ");
  Serial.println(Serial3.available());
    if(Serial3.available() > 0) {
      String s = Serial3.readString();
      Serial.println(s);
    }

  delay(3000);
}

The Output

Ready...
bytes available: 20
⸮TY)⸮TY)⸮TY)⸮T[)⸮T[)⸮T[)
bytes available: 20
⸮T[)⸮T[)⸮TY)⸮TY)⸮TY)⸮TY)
bytes available: 20
⸮TY)⸮T[)⸮T[)⸮T[)⸮T[)⸮TY)
bytes available: 20
⸮T[)⸮TY)⸮T[)⸮TY)⸮TY)⸮TY)
bytes available: 20
⸮T[)⸮TY)⸮T[)⸮T[)⸮T[)⸮T[)
bytes available: 20
⸮TY)⸮T[)⸮TY)⸮T[)⸮TY)⸮T[)
bytes available: 20
⸮TY)⸮T[)⸮TY)⸮T[)⸮TY)⸮TY)

etc...

Some extra details:

  • ATMega328 chip on a breadboard
  • 16MHz External Crystal
  • Fuses: Extended 0xFF, High 0xD9, Low 0xE7
  • Pin 3 of ATMega328 connected to pin 15 of Arduino Mega
  • 9600 baud rate selected on serial monitor

Things I've tried:
Connect the atmega328 chip to Serial ports 1-3 on the Mega
Other baud rates (faster and slower)
10k pullup resistors on the tx/rx lines
SoftwareSerial (On the atmega328 side)
Using the 8MHz internal osc (with and without div8)
Make sure vcc has bypass caps
Power from Arduino / Power separately (Yes ground is connected between both)

Question: Why am I receiving garbage instead of the expected 'hello'?

Seems obvious but better ask as you did not mention this : Have you crossed wires?
Rx —> Tx and Tx —> Rx

(EDIT - sorry you did say pin3 to pin 15. There is no pullup needed. Yes GND needs to be connected)

As a side note - Best way to handle Serial is not by using readString nor delay() (you might saturate your receive buffer) see Serial Input Basics

Uhg. Just figured out what was wrong after all day of messing with it. I swapped out crystals and now it's working. Turns out I had a 25MHz crystal on it. Good grief.

Lol :slight_smile:
That must have been frustrating!