Hardware Reference
In-Depth Information
in the order of one megabaud (one million baud). Be aware of what the device
or computer is expecting.
Serial coni guration is normally done in setup() because devices tend to not
change the speed at which they communicate over time.
void setup()
{
Serial.begin(9600); // Opens the serial port, sets data
// rate to 9600 baud}
void loop() {}
For the Arduino Leonardo, you can detect if the USB serial communications
channel is open. The Serial class can return true or false , depending on the
communication state.
if(Serial) // Check to see if the channel is open
If you have a number of statements in your setup() that you want to send serially,
it is useful to wait until the Leonardo's serial port has initialized before proceeding.
while(!Serial){ // while there is no serial connection
;; // do nothing
}
This works on the Leonardo, Micro, Esplora, and other 32U4-based boards.
On all other boards, this function always returns true, even if the device is not
connected to USB.
Writing Data
Now that you have established a connection, your Arduino can send data to
a receiving device. For debugging, you will probably send ASCII a standard
used to transmit text using the English alphabet and some punctuation, and
use a terminal emulator for receiving messages. The Arduino IDE integrates a
terminal emulator to easily access messages and debugging data. Terminal edi-
tors are used to ASCII but will get confused if receiving a non-ASCII character.
If a terminal emulator receives a non-ASCII character, for example, something
formatted as a raw byte, it will probably produce an unintelligible mess.
Sending Text
To send ASCII data, use print() . This function sends data to the serial device
as human-readable ASCII format. The data to be printed can be in any format.
It can print a single ASCII character or a complete string.
Serial.print("Hello, world"); // Output an entire string
Serial.print('!'); // Output a single character
 
Search WWH ::




Custom Search