Can't get serial communication working on Atmega328

Hello,

I have a fairly complex project developed on an Arduino Mega, and now I port it to a standalone Atmega328 (P-PU). I can program it via Arduino ISP but can't really get serial communication working. First I used a MAX232 and an RS232-USB cable on my own board which sometimes worked, sometimes not, probably due to wiring issues. I got tired of it so I bought a TTL-USB cable (PL2303 based), but without success. The cable itself should work, I could echo my message by connecting RX and TX.

Now, to try to isolate the issue, I removed the Atmega from my board and put it in a breadboard with minimal config (crystal, power, pullup resistor on reset). I tried both the TTL cable and a breadboarded MAX232, but all I could get is random characters or squares or whitespaces. Or nothing at all. My final test was using my Arduino Mega to check both the TTL cable and the RS232.

My results: if I use Arduino's built in serial comm (COM3), it works. If I use pin 0,1 with the MAX232 (COM5), it receives data, but only randomness (both on COM3 and COM5 ports). If I use Serial1 (pin 18,19), Serial on COM3 works fine, Serial1 on COM5 gets randomness. The TTL cable only reads whitespaces, if anything, under every circumstance.

Wiring is (Arduino)TX1 ->R1OUT (MAX232)
RX1->T1IN

The software is simple:

void setup() {
Serial.begin(9600);
Serial.println("start");
Serial1.begin(9600);
Serial1.println("start1");
}

void loop() {
Serial.println("test");
Serial1.println("test1");
delay(5);
}

What do you think?

Please post a schematic diagram (not Fritzing), and link to the Max232 module.

Did you connect the grounds?

a breadboard with minimal config with minimal config (crystal, power, pullup resistor on reset)

That is not a minimal configuration. You need decoupling and crystal capacitors.

balazs:
Wiring is (Arduino)TX1 ->R1OUT (MAX232)
RX1->T1IN

Did you also connect ground on the MAX232 to ground on the Uno?

Grounds are connected of course. I will make a schematic a bit later.

328P does not have Serial1. You will need one of the Software Serial libraries for that.
18/19 are also A4/A5, which are the I2C pins. Will your program be using I2C later, as you mentioned it is fairly complex?

jremington:
Please post a schematic diagram (not Fritzing), and link to the Max232 module.

Did you connect the grounds?
That is not a minimal configuration. You need decoupling and crystal capacitors.

The breadboard circuit schematic is attached.

With this configuration, I get "QStT⸮" instead of "test". I use a MAX232N .

Note that I use a 15pF and 18pF caps for the quartz for improved noise protection (according to an Atmel hardware design documentation).

Also I could make the TTL work. Pin 2(RX) of the Atmega328 to TX wire
and pin 3(TX) to the RX wire, GND to black and the MAX232 is totally unconnected (I left it connected in the previous experiments, maybe it affected the communication).

However, I still can't get the MAX232 to work.

CrossRoads:
328P does not have Serial1. You will need one of the Software Serial libraries for that.
18/19 are also A4/A5, which are the I2C pins. Will your program be using I2C later, as you mentioned it is fairly complex?

I know, the code I posted runs on an Arduino Mega, which does have Serial1. The same code is on the 328, with only the Serial part. I just can't get it work with an independent MAX232. TTL level comm now works.

On the separate 328 I use pin 2 and 3 for serial comm. The final version will use I2C with pins 27 and 28.

edit

I reuploaded the corrected schematics.

Vcc, Avcc, need to connect to 5V directly, with the pins connected to caps going to Gnd in parallel, the same way Vcc is connected on the Max232. You do not have Avcc connected to 5V at all, and you have Vcc going thru a cap to 5V. I am surprised the chip is getting power at all.

Also, the crystal caps should both be the same value. From the Nov 2016 datasheet:

13.2.3. Low Power Crystal Oscillator
Pins XTAL1 and XTAL2 are input and output, respectively, of an inverting amplifier which can be
configured for use as an On-chip Oscillator, as shown in the Figure below. Either a quartz crystal or a
ceramic resonator may be used.
C1 and C2 should always be equal for both crystals and resonators.

What do you have that shows different values should be used? I have used 22pF in all my applications without issue.

Oh crap. Of course it's connected to 5V, I just messed up the schematic I drew, I guess I'm not yet awake fully. I also switched up the caps of the quartz, the bigger value should be on XTAL1.

The 5V is connected on the breadboard power line, the 100nF caps are placed right next to the IC pins.

So, here it is. The reason I used different cap values is here. Unbalanced external capacitors part.

Are you referring to this section?

5.4 Unbalanced External Capacitors
In noisy environments the oscillator can be crucially affected. If the noise is strong enough, the oscillator
can “lock up” and stop oscillating. To reduce the sensitivity of the oscillator to noise, the size of the
capacitor at the high-impedance input of the oscillator circuit, XTAL1, can be slightly increased.
Increasing only one of the capacitors does not affect the total capacitive load much, but unbalanced
capacitors can affect the resonant frequency to a higher degree than the change of the total capacitive
load. However, unbalanced capacitive loads will affect the duty cycle of the oscillation and should not be
used. This is especially critical if the AVR device is utilized close to its maximum speed limit.

If so, I can see that. I wouldn't call a breadboard a particularly noise environment.

Wiring: PD0 pin 2 is Rx, PD1 pin 3 is Tx. Try swapping those.

Similarly, on the DB9, 3 is Tx and 2 is Rx, that looks ok
https://www.db9-pinout.com/
The device on the other end of the cable needs Rx on 3 and Tx on 2; or the cable needs to have 2 and 3 swapped on one end only so that Rx goes to Tx, and Tx to Rx.

CrossRoads:
Are you referring to this section?

5.4 Unbalanced External Capacitors

If so, I can see that. I wouldn't call a breadboard a particularly noise environment.

Wiring: PD0 pin 2 is Rx, PD1 pin 3 is Tx. Try swapping those.

Similarly, on the DB9, 3 is Tx and 2 is Rx, that looks ok
https://www.db9-pinout.com/
The device on the other end of the cable needs Rx on 3 and Tx on 2; or the cable needs to have 2 and 3 swapped on one end only so that Rx goes to Tx, and Tx to Rx.

Yes, I meant that part. I know it's not very noisy, but I want to test if this works.

However, I tried swapping PD0 and PD1 cables. If nothing else is modified, that I don't get anything on serial.
But I also tried swapping the wires going to the DB9, so DB9 pin 2 to MAX232 pin 14 ands DB9 pin 3 to MAX232 pin13, then I get some random stuff.

Interesting, because to my logic, this would be the good setup. Atmega RX(2)->MAX232 TX(12), and MAX232 RS232 side RX(13)->DB9 pin3->USB-RS232 TX(3).
On the other side Atmega TX(3)->MAX232 RX(11), and MAX232 RS232 side TX(14)->DB9 pin2->USB-RS232 RX(2). It only gives 8s and squares.

@OP

Why is your following schematic does not agree with the pins of the ATmega328P MCU?


Figure-1:

atmega328pPin.png
Figure-2:

In Fig-2, it is observed that PD0 is the RX-pin for the UART Port. If so, in Fig-1, we expect to see that PD0-pin (RX-pin) should be connected with output of one of the gates of MX232. Your circuit of Fig-1 shows that the RX-pin is connected with the input of a gate of MAX232. Similarly, PD1-pin(TX-pin) is also has wrong connection.

This is the official schematic of MAX232 (TTL<---->RS232) chip:

This is the schematic of a working UART Commutation system using MAX232 logic converter chip:
uart-1.png

atmega328pPin.png

uart-1.png

GolamMostafa:
@OP

Why is your following schematic does not agree with the pins of the ATmega328P MCU?


Figure-1:

atmega328pPin.png
Figure-2:

In Fig-2, it is observed that PD0 is the RX-pin for the UART Port. If so, in Fig-1, we expect to see that PD0-pin (RX-pin) should be connected with output of one of the gates of MX232. Your circuit of Fig-1 shows that the RX-pin is connected with the input of a gate of MAX232.

This is the schematic of a working UART Commutation system using MAX232 logic converter chip:
uart-1.png

Yes, we already figured that out, see my previous answer. Still, I get random output, so something is still not right.

balazs:
Yes, we already figured that out, see my previous answer. Still, I get random output, so something is still not right.

Then you should have removed that schematic from the post and then place the corrected one in order to give us easy life.

Please, double check your connections between MAX232 and DB9 connector in respect of the following schematic.
db9x.png

db9x.png

Problem solved, it was a faulty ground line. Thanks everyone.

I'm glad to hear you found the problem. Enjoy!