ArduinoSerialOutput

From arrizza.org wiki

Jump to navigation Jump to search
Previous ⇦ ArduinoLedBlink Arduino ⇫ Up ArduinoSerialInput ⇨ Next

Overview

This is a simple Arduino sketch that performs the same LED blinking as ArduinoLedBlink but also outputs some data on the serial port. To see that output you need to setup a terminal emulator like Putty or screen and configure it correctly (115200, 8n1).

Git repository

https://bitbucket.org/arrizza/arduinoserialoutput

hg clone ssh://git@bitbucket.org/arrizza/arduinoserialoutput

Setup

The LED blink setup is the same as for ArduinoLedBlink#Setup

This also sets up the serial port at the 115200 baud rate. A baud is 1 bit per second, so 115200 baud rate is 115,200 bits per second. There are other bits that may get sent for each byte (a start bit, a parity bit and a stop bit).

It also sends a message "setup" to the serial port.

  Serial.begin(115200);
  Serial.println("setup");

Serial.begin() is a function that sets up the serial port to the given baud rate, in this case 115,200 bps.

The Serial.println() is a function that prints the parameter given to it, in this case the word "setup" and follows it with a newline.

A newline is a carriage return or a linefeed or both, depending on which Operating System you are on. This is equivalent to pressing the Enter key on the keyboard - it puts a new line in the output. It is sometimes abbreviated as "nl" and sometimes it is referred to as "\n" (this is because in many languages "\n" represents a newline sequence).

A carriage return, sometimes abbreviated as "cr", is a single character with hex value 0x0D.

A linefeed, sometimes abbreviated as "lf", is a single character with hex value 0x0A. This is used for most Linux or Unix based systems and for the Arduino.

On Windows, a new line is a two character combination 0x0D 0x0A (carriage return/line feed).

Main Loop

The main loop is similar to ArduinoLedBlink#Main Loop . It also prints a "1' and "0" to the serial port every second.

  Serial.print("1");
     ...
  Serial.print("0");

Thie Serial.print() is a function that is identical to the Serial.println() except it does NOT print the newline character. Because of the newline is missing, the "1" and "0" will be side-by-side on the terminal output.

   setup
   101010101010

It will continue writing the "10" sequence until you unplug the USB cable.

Notes

  • You should see a "1' when the LED is blinking quickly and you should see a "0" when the LED is off.
  • Try changing the text inside of the Serial.println() or Serial.print() statements. You should see whatever you typed in there, showing up on your terminal emulator.
  • Try changing the Serial.print() to Serial.println(). You should see a newline after each 1 and 0, something like:
 0
 1
 0
 1
 0
 ... etc ...
Personal tools