Hardware Reference
In-Depth Information
NOTE Serial connections depend on correct parameters. If the speed parameter
is wrong, the receiving UART device will receive garbled data. You will not get small
portions of cleartext with a few wrong characters; the entire text will be unreadable. If
your terminal is showing corrupted data, check your settings.
Starting a Serial Connection
All Arduinos have at least one serial port to communicate with a PC called
Serial . Some boards have several UART devices. The Arduino Mega, for example,
has three additional UART controllers called Serial1, Serial2 , and Serial3 .
The Arduino Leonardo's microcontroller has a built-in USB communication
device, separating USB and Serial communication. On the Leonardo, the Serial
class refers to the virtual serial driver, not the serial device on pins 0 and 1.
These pins are connected to Serial1 .
To do anything with a serial port, you must use the functions available to
the Serial class.
To begin using a UART device, you must i rst do some basic coni guration. You
need to set at least one parameter; the baud rate, or speed. Optionally, you can
set the data bits, parity, and stop bits if required. Arduinos, by default, require
you to set the speed and set 8N1 as a default coni guration. To do this, you use
the begin function of the Serial object.
Serial.begin(speed);
Serial.begin(speed, config);
For Arduino Megas, you can also use the other serial objects (note that these
are not connected to the USB port through the 16U2):
Serial1.begin(speed);
Serial1.begin(speed, config);
Serial2.begin(speed);
Serial2.begin(speed, config);
Serial3.begin(speed);
Serial3.begin(speed, config);
The speed parameter is a long and indicates the baud rate. To communicate
with a PC, use one of the following: 300, 600, 1,200, 2,400, 4,800, 9,600, 14,400,
19,200, 28,800, 38,400, 57,600, or 115,200. Typically, 9,600 is an appropriate speed
for communicating debug information. You are free to use just about any speed
you want as long as both devices are operating at the same speed. For example,
some Bluetooth devices can send serial data at speeds much faster than 115,200,
 
Search WWH ::




Custom Search