ATmega328P/Talking to other parts/30. Serial on D0 and D1
Talking to other parts · 30 of 33

Serial on D0 and D1

The board has one hardware serial port, on D0 and D1, and it is the same pair of wires the CH340C uses for every upload and every line in the Serial Monitor. Whatever else you connect there shares the line: directly on the UNO, through a 1 kΩ resistor on the Nano.

Your board

One port, three users

The holes printed RX←0 and TX→1 are the ATmega328P's UART. The net list wires D0 to the CH340C's TXD pin and D1 to its RXD pin, straight, with no resistor between them. Every upload, every Serial.print and every line you type in the Serial Monitor travels on these two wires.

Each wire also carries a lamp. The TX lamp hangs from 5 V through a 1 kΩ resistor onto D1 itself, so it lights while D1 is LOW; the RX lamp does the same on D0. A serial line rests HIGH and pulls LOW for its bits, so both lamps are dark when nothing is sent and flicker while bytes go by.

Who drives D0 and D1
one driver each
What is going on
D0 driven by
CH340C
D1 driven by
the chip
Upload
works
Each wire has one driver, and both rest HIGH. A serial line idles HIGH, so both lamps, which light only while their line is LOW, are dark.

Each wire works because it has exactly one driver: the CH340C on D0, the chip on D1. Add a module's TX to D0 and the wire has two. When they disagree, the line sits wherever the stronger output pulls it, and both of them push current into each other with nothing to limit it.

Light TX by hand

Until Serial.begin runs, D1 is an ordinary pin. The sketch at the foot of the page starts there:

pinMode(1, OUTPUT);
digitalWrite(1, LOW);    // 5 V, a resistor, the TX lamp, into D1

The lamp lights and stays lit, because current now has a path from 5 V through the resistor and the lamp into the pin. Write it HIGH and the lamp goes dark. Then Serial.begin(9600) enables the transmitter, and from that moment the UART owns the pin (Microchip's datasheet: it overrides the normal port operation). This is the only sketch in the book that touches pin 1, and it ends by giving the pin back.

The CH340C is still listening on D1 while you do this, so a long LOW can arrive in an open Serial Monitor as a stray character. It is harmless.

The echo

After the blinks, the sketch waits for bytes. Type a word in the Serial Monitor, at 9600 baud, and press Enter: RX flickers as the bytes arrive on D0, and TX flickers as the capitals go back out on D1. The Serial Monitor article has the menu.

Opening the Serial Monitor restarts the board, so the three blinks run again. The CH340C's DTR line reaches RESET through a 100 nF capacitor, C6, and opening the port pulses it. That is the same pulse that starts the bootloader for an upload.

A module on D0 and D1

GPS receivers, Bluetooth serial modules and many sensors talk UART, and D0 and D1 are the obvious place to plug them. They work there while the sketch runs, but the module's TX is then a second driver on D0. An upload fails, and the Serial Monitor shows the module's bytes mixed with yours.

If a module has to use D0 and D1, unplug its TX wire for every upload. Check its supply voltage too: a 3.3 V module's RX receives the full 5 V from D1, and not every one tolerates it.

A second port: SoftwareSerial

The better place for a module is two other pins, with SoftwareSerial, which comes with the IDE:

#include <SoftwareSerial.h>

SoftwareSerial gps(8, 7);   // RX on 8, TX on 7

void setup() {
  Serial.begin(9600);       // USB, as before
  gps.begin(9600);          // the module
}

The module's TX goes to 8 and its RX to 7. On the ATmega328P any digital pin can be the RX pin, because every pin has a pin-change interrupt; Arduino's documentation lists restrictions only for boards such as the Mega and Leonardo. The library times each bit in software, so it cannot send and receive at the same time, and with two SoftwareSerial ports only one listens at once. Its source says its timing works up to 115200 baud at 16 MHz; most modules run at 9600, which is the forgiving choice.

The code

Serial_Echo.ino

Upload it with nothing on D0 or D1. The TX lamp blinks three times while D1 is still an ordinary pin, then the sketch hands the pin to Serial and sends back, in capitals, whatever you type in the Serial Monitor.

// Serial_Echo.ino — lights TX by hand, then echoes what you type
// Tools > Board: Arduino Uno, or Arduino Nano with
//   Processor ATmega328P (Old Bootloader if the upload says
//   "not in sync")
// Tools > Port: the CH340 port
// Serial Monitor: 9600 baud
// Wiring: none. Nothing on D0 or D1.

void setup() {
  // Before Serial.begin, D1 is an ordinary pin. On both boards
  // the TX lamp hangs from 5 V onto the line D1 drives, so it
  // lights while D1 is LOW.
  pinMode(1, OUTPUT);
  for (byte i = 0; i < 3; i++) {
    digitalWrite(1, LOW);    // TX lamp on
    delay(500);
    digitalWrite(1, HIGH);   // off: the line's idle level
    delay(500);
  }

  // Serial.begin hands D0 and D1 to the UART.
  Serial.begin(9600);
  Serial.println(F("Type something and press Enter."));
}

void loop() {
  if (Serial.available() > 0) {
    char c = Serial.read();            // one byte off D0
    if (c >= 'a' && c <= 'z') {
      c = c - 'a' + 'A';               // shout it back
    }
    Serial.write(c);                   // one byte out of D1
  }
}

The three blinks run again every time the board resets, which includes every time the Serial Monitor opens. That is the auto-reset, not a fault.

When it does not work

The upload fails while a module is wired to D0 and D1

The module's TX and the CH340C both drive D0, and the bootloader hears a mix of the two, or, on the Nano, where the CH340C is behind 1 kΩ, mostly the module. Unplug the module's TX wire from D0 for every upload and plug it back afterwards, or move the module to SoftwareSerial on two other pins.

The TX lamp stays lit and nothing prints

Something holds D1 LOW: a sketch that set pin 1 as an OUTPUT and never called Serial.begin, or a module pulling the line down. Serial.begin takes the pin back; a module has to come off to find out which it is.

The Serial Monitor shows the wrong characters

The baud rate in the Serial Monitor's menu is not the number in Serial.begin. Both ends must agree, and this sketch uses 9600. A stray character just after reset can be the three TX blinks, which the CH340C may read as a byte.

The board restarts every time I open the Serial Monitor

That is the auto-reset. The net list puts a 100 nF capacitor (C6 on the UNO, C14 on the Nano) between the CH340C's DTR line and RESET, so opening the port pulses RESET. Uploads depend on it. A sketch that must not restart should not have the port reopened under it.

SoftwareSerial drops or garbles characters

It cannot send and receive at the same time, and with two SoftwareSerial ports only the one that last called listen() receives. Lower the baud rate, send and receive in turns, and keep long Serial.print runs out of the moments the module is talking.

Where this goes next

Two wires, many devices, and a scanner that shows who is listening.

I2C on A4 and A5 →

Edit this page — content/books/atmega328p/uart-on-d0-and-d1.mdx

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum →