Cant get atmega328 to perform serial communication correctly

I cant make my standalone atmega328 to transmit serial messages correctly... i'll try to explain what i've tried so far.

I started following this tutorial https://www.arduino.cc/en/Tutorial/ArduinoToBreadboard, since I want to free my arduino for other stuff.
I managed to burn bootloader and to upload code through SPI.
Everything worked as expected except for the MIDI In i wanted.

At that point I started over with a simple blink sketch, to check whether the clock was cliking right...i have to say I dont have an oscilloscope, so to get an idea of it, I made a led blibk every 1 second...and that worked ok.

So I focused on making the atmega328 to transmit a simple message every 1 sec....I've uploaded a sketch again through SPI, connected the TX from the atmega to the RX of the arduino to watch the monitor log, and got only garbage.

Tried the following:

  • changed the baud rate in the serial monitor to all posible options... got different garbage options
  • changed the baud rate in the code also ... no luck
  • made a sketch that itereates from 9000baud to 1000bauds, to test if it was "near"
  • removed the 16mhz crystal and caps, and burned the bootloader to work with the internal 8mhz osc
  • added a stable power supply to the atmega instead of using the arduino 5v; and linked grounds
  • used SoftwareSerial instead of hardware serial to check if if there were bad pins or some trouble with hardware serial
  • listened from and external (working) usb-db9 cable instead of using the arduino one... read that input with 'screen' from the terminal, checking expected baudrate and some multiples of it

If I use the serial monitor with arduino itself, it works ok.

Another thing I tried was to upload to the atmega328 a sketch changes ping 4 value every 500 millis, and another sketch to arduino using interrupts to read time passed between changes and inform that to the serial monitor, wich gave me a fair precision of a steady 499928-499934 micros between changes.

....
I'm stucked...what else could I try?

Please show us a schematic of your circuit.
Please, you must show us your complete sketch. Attach your code using the </> icon on the left side of the posting menu.
Put your sketch between the code tags [code][/code]

.

this is the sketch uploaded to the atmega328 through spi

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

void loop() {
  delay(1000);
  Serial.print("test");  
}

attached is how the atmega328 is connected on my breadboard.
The tx from the atmega goes to the rx in the arduino.
I expect to see a "test" string on the serial monitor every 1 sec... but I get only garbage.
Rx led on the arduino blinks every one sec.

I forgot to mention that serial loopback seems to work ok.
Here's a sketch I uploaded to the atmega328 wich caused led connected to pin4 to blink every 1 sec.

int ledState = 0;
unsigned long previousMillis = 0;
const long interval = 1000;

void setup() {
  pinMode(4, OUTPUT);
  Serial.begin(9600);
}

void loop() {

  if (Serial.available() > 0) {
    ledState = Serial.read();
    digitalWrite(4, (ledState%2) );
  }

  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    Serial.print(ledState+1);
  }
  
}

ddorado:
this is the sketch uploaded to the atmega328 through spi

void setup() {

Serial.begin(9600);
}

void loop() {
 delay(1000);
 Serial.print("test");  
}




attached is how the atmega328 is connected on my breadboard.
The tx from the atmega goes to the rx in the arduino.
I expect to see a "test" string on the serial monitor every 1 sec... but I get only garbage.
Rx led on the arduino blinks every one sec.

you need to add 3 100nf capacitors to your circuit.

One from AVCC(20) to Gnd(22), One from VCC (7) to Gnd( 8 ),

disconnect AREF from Vcc, Connect the Third capacitor from Aref(21) to Gnd(22).

See if that doesn't make it work better.

Chuck.

OP's image:

Add a 10k resistor from pin 1 to +5volts.


.

Added 100nf caps and disconnected aref from vcc, also 10k resistor to pin1, but nothing changed

Please show your wiring as it is now.

.

these are my current connections.
crystal caps are 22pf, the others are 0.1uf.
resistor is 10k

outgoing wires are connected to the arduino

in the picture it seems that the xtal is not conected... but it is an error in my drawing. it is actually connected

The actual wiring please.

.

220pf is too big. use 22pF, can also try none at all.

You are right! it is indeed 22pf... I was confused by the fact that it has a "220" printed on it, but it meens "zero zeros" ... well..

The crystal caps are 22pf
(edited previous post)

I tried connecting this way like, the Tx of Atmega to Arduino Rx and monitor via Arduino IDE serial monitor, but I never get it worked well, always ended up getting garbage.

One thing you could try is write a new sketch that listen for the Serial communication in Arduino and if available use Serial.println from Arduino. Then check Serial monitor.

In my scenario I used a TTL to USB adapter and used CoolTerm to monitor the serial communication directly from Atmega.

Also make sure in both scenario use a common ground.

So I focused on making the atmega328 to transmit a simple message every 1 sec....I've uploaded a sketch again through SPI, connected the TX from the atmega to the RX of the arduino to watch the monitor log, and got only garbage.
<...>
If I use the serial monitor with arduino itself, it works ok.

@ddorado:

I once did some of those 8-MHz and 16-MHz bare chip circuits.
I have some pix of the layout and they all seemed to work OK.
Here

Three things come to mind... noisy +5V power, poor/no common ground, and stray capacitance.

Ray

Thanx to @sarouje comment, I realised that I was making wrong assumptions about arduino builtin usb-serial....alll garbage came because arduino was running the arduinoISP code all this time.

So I went and loaded to the arduino the code showed here: https://www.arduino.cc/en/Tutorial/SoftwareSerialExample and got TX from atmega to work..... I mean, transmit TX from atmega to arduino (receiving it with a softwareSerial instance), and then retransmit it throgh arduino HardwareSerial TX ... then I could see the data arriving normally to the serial monitor.

Right now I feel a little dizzy with all this, and i dont know why the hardwareSerial RX on the atmega isnt working.
I'm still guessing what the problem can be, but today I found that replacing HardwareSerial with SoftwareSerial, my code works ok (even using same pins D0 and D1)

So I start to suspect if my atmega UART could be burned or something....I'll keep on trying and I'll update the thread later with my results

Thank you all

I finnally got it working: apparently the rx pin needed a pullup resistor. I connected a hardware one.... but i'll try to setup the rx pin as pullup an see if it works

ddorado:
I finnally got it working: apparently the rx pin needed a pullup resistor. I connected a hardware one.... but i'll try to setup the rx pin as pullup an see if it works

You may find this codebase of some interest as it sends, receives, and correctly loops-back.

Ray