Arduino Print to Serial: Find out the functions you need. Why is the USART not called an RS232 module? How exactly does the serial link operate if its already in use for programming the Arduino? Can you use pins 0 & 1 for normal digital I/O?

To make an Arduino print to serial, there are few simple setup steps for coding, but it is important to understand how the whole system works so you can see exactly what is going on...

But if you're in a hurry here's a program that prints to serial.

Example Serial print program

// Example showing Arduino Serial print operation.

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

void loop() { 
  Serial.println("This is a repeating message, to the Serial monitor.");
  delay(500);
}

This will repeatedly print the message:

This is a repeating message, to the Serial monitor.

...every half second.

Note, to print to serial without a newline use:

    Serial.print("This does not output a newline");

Arduino Print to Serial: Programming an Arduino

When you program an Arduino board, the PC communicates with the Arduino board over the USB interface to program its internal Flash memory. This is all done in the background, so all you have to do is press the programming button!

The hardware links that allow communication back and forth from the PC to the Arduino microcontroller are already in place i.e. you connected a USB cable to the Arduino board:

Arduino Print To Serial Operational Block Diagram

A USB to RS232 serial converter is used on the Arduino board to connect from the PC USB to the RS232 serial interface present on the Arduino microcontroller.

This is why there are two ICs on the Arduino Uno board - the ATMega328p is the microcontroller you are using, usually a large DIP (Dual Inline Package) chip i.e. the one you can take out of the board, while the other, a small surface mount chip, provides the USB to serial interface (This is an ATMega16U2).

In the Arduino microcontroller there is a special bit of code called the bootloader and it listens to the serial port for serial data from PC. If it gets data within a few seconds it will start programming the ATMega328p. If it does not get the data it will start your program and in your program you can start using the serial port i.e. for the Arduino print to serial operation.

Arduino Print To Serial: Arduino Functions

There are three functions you need to know about for printing to a serial terminal:

  • begin()
  • print()
  • println()

You use begin() as follows:

    Serial.begin(<baud rate>); - Find out more here.

You use print() as follows:

    Serial.print("A message");  - prints a message with no newline.

    Serial.print(var); - prints the value of variable var with no newline.

You use println() as follows:

    Serial.println("A message");  - prints a message and appends newline.

    Serial.println(var); - prints the value of variable var and appends newline.

What is Serial communication

There are several aspects to serial communication:

Let's back up a bit and find out what a serial interface is, and why it exists in the first place.

The RS232 protocol is a serial interface communications protocol that allows two computers to communicate using two wires over long distance. Before the use of WiFi, Bluetooth or Ethernet, it provided a fairly robust method of transferring data in industrial settings and became ubiquitous on PCs. The main reason for its increasing popularity was, that you could connect a modem to the PC via the serial port to allow access to the internet!

The USART module built into most microcontrollers allows you to configure a serial interface to the RS232 protocol specification, to communicate over an RS232 serial port.

However PCs do not supply a built in serial port anymore because of the use of radio links. However the PC can emulate a serial port over the USB interface, hence the use of a USB-to-serial chip on the Arduino board.

This means that the microcontroller can use the fairly primitive RS232 protocol (but easy to implement) and you still get serial communication directly to PC software e.g. a serial terminal program.

Another reason for using a serial port on the PC is that it is well established, and quite simple, and therefore easy to use, i.e since the serial port is emulated you don't need to go and figure out complex USB software at all!

The software thinks it is communicating directly with the Arduino using an RS232 serial link and this is the primary communication method used in the Arduino IDE.

Why is it called a USART?

The reason that the serial module is a USART and not an RS232 module is that serial communication is widely used in different ways. The USART or Universal Synchronous/Asynchronous Receiver Transmitter, is configurable to many different settings.

For the RS232 protocol the USART is used in Asynchronus mode (no clock) and has a limited number of bits sent and received. You can set up the USART to generate a clock along with the data - but that would be a different protocol.

Arduino Print to Serial: Shared RX TX pins

You can see in the diagram that Arduino pins 0 and 1 are connected to the USB interface chip so you can't really use these pins as normal - you have to look at the schematic to find out what you can do.

Arduino Print To Serial Operational Block Diagram

Warning: On An Arduino Uno the serial USART is shared with the USB chip pins.

TIP: Use Arduino pins 0 & 1 as a last resort - you have to think about them!

This is a section of the schematic from the Arduino Uno R3 schematic (here) edited to show the RX TX connections:

Arduino Uno R3 schematic RX/TX connections

On the left is the USB to Serial converter (ATMega16u2) with I/O M8RXD and M8TXD are connected to the ATMega328p via 1k resistors RN4B an RN4A. The Arduino pins on the board are on the connector to the right labelled IOL.

M8RXD connects to TXD of ATMega16u2, and
M8TXD connects to RXD of ATMega16u2

...which is the standard way of crossing over (You have to connect RX to TX somewhere) - this is the pain of RS232 i.e. you could choose to use a cross over cable and then connecting TX/TX and RX/RX - this is where the RS232 standard falls down as it is up to the designer!

During programming TXD (ATMega16U2) causes output to RX (ATMega328p) - so the I/O pin 0 will transition during programming.

During programming RXD (ATMega16U2) will receive data from TX (ATMega328p) - so Arduino pin 1 will transition during programming.

Key points:

  1. If you connect 0/5V to Arduino P0 (RX) - it won't  matter too much as pin 0 is RX - an input. The resistor RN4B protects the driver (ATMega16U2) i.e. current limited to (5/1k =1mA). But think about what your circuit is doing - if transitioning or using too much current it will affect serial communications.

  2. If you connect 0/5V to Arduino P1 (TX) - it will matter as you are shorting the chip pin to GND or 5V as it is being driven by the ATMega328P - use another resistor to  limit current. Again also think about what your circuit is doing - if transitioning or using too much current it will affect serial communications.

  3. Using the ISP programming method (with no bootloader in the ATMega328p) - frees up these pins for normal use.
TIP: The easiest pin to use out of P0 or P1, is P0 (RX) as it is an input pin.

Conclusions

Printing to the serial port is very easy as the serial link is already used by the Arduino system to program your microcontroller. Once programming has finished you just use the same link to send data back and forth from Arduino to PC.

Using pins 0 (RX) and 1 (TX) as digital I/O is a pain - avoid them! You may get away with using them but there will be problems unless you program the Arduino chip using ISP (no bootloader). If you have to use them, P0 is easiest as it is an input during serial communication.

You might find these pages useful as well:

RS232

Arduino Functions

    Serial.begin();
    Serial.available();
    Serial.read();



Comments

Have your say about what you just read! Leave me a comment in the box below.

Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.



Privacy Policy | Contact | About Me

Site Map | Terms of Use