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 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");
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:
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.
There are three functions you need to know about for printing to a serial terminal:
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.
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.
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.
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.
This is a section of the schematic from the Arduino Uno R3 schematic (here) edited to show the 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:
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.