Communicating using the USB cable

Sending information using Morse code would be tedious. Sure, if all we had was a single LED, it might get the job done, but we have a USB cable connecting our tiny computer to a host computer that has a screen and a keyboard. We can have our tiny computer send words over the cable, and have the host computer print them on the screen.

To do this, we use the built-in Serial class.

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

void
loop()
{
  Serial.println( "I will not talk in class." );
}

There are only two new lines in this tiny program. The first new line tells the tiny computer how fast to send the bits to the other computer. In this case, we told it to send at 9600 bits per second. Since there are ten bits in a character like the letter 'A', this means we can send 960 letters per second.

The second new line tells the tiny computer to send a sentence to the host computer.

Copy and paste this program into the Arduino IDE, and run it on our little computer. When we do, nothing seems to happen. The lights aren't flashing -- it just sits there.

To see what the Arduino is sending, we need to bring up the Serial Monitor.

Click on the Tools|Serial Monitor menu item, or hold down the Control and Shift keys and hit the M key (for monitor).

Up pops the Serial Monitor window, and we can see our text whizzing by at 960 characters per second (35 lines of text scream by every second).

In the lower right corner of the Serial Monitor window is a box with the words 9600 baud in it. If your window has some other number, then you might be seeing no text at all, or some nonsense letters. That is because you are listening at the wrong speed.

We can slow down the rate at which we print things by adding delay functions. For example, putting

delay( 1000 );

after the call to the println() method would send a line per second. But we can't speed it up (the delay() function does not take negative numbers!).

We can speed it up by changing the rate that the bits are sent, however. We need to change two things. First, we change the program to send at 115,200 bits per second instead of 9600:

Serial.begin( 115200 );

(Notice that we don't put commas in numbers.)

Now we compile the program and run it on the tiny computer.

The serial monitor window disappears, since the host computer needs to use the USB cable to download the program into the tiny computer. So we need to open the Serial Monitor window again. Now we see a lot of nonsense being printed, since it is still set for 9600 bits per second (bits per second is shortened to 'baud' when we talk about serial ports). So we need to do one more thing -- set the Serial Monitor window to use 115200 baud. Now we get readable text, whizzing by at 11,520 characters per second.

Who can read that fast? Why would we ever want to send data faster than we can read it?

Well, maybe a computer is reading it, instead of a human. Computers can read very fast.