RobotGeek 101: Serial Communication Introduction

This lesson is part of the RobotGeek 101 Learning Series. If you haven't already, please download the example sketches, and follow through the Geekduino Getting Started Guide to handle the foundations of using the Arduino IDE in conjunction with the Geekduino.

Welcome to RobotGeek 101 : Serial Communication. This lesson will show you how to start a Serial Communication session, and how to send data back and forth to/from your Geekduino and your computer.

Parts used in this lesson:

These parts and everything you need to complete chapters 1-7 in RobotGeek 101 are available in the RobotGeek Basic Experimenter's Kit.

Contents:

Introduction

Serial means "one after the other", and serial communication amounts to sending different letters and characters "one after the other" to the geekduino. The Geekduino can also send serial characters back to you /the computer. The Geekduino has all of the circuitry to easily send and get data to/from the computer. You'll use the Serial Monitor to see data and send data to the Geekduino. Using Serial Communications, you can get information from the Geekduino (like sensor values) and control devices attached to the Geekduino. You can even make you own text-only game!

Serial Communications can be very useful for debugging your code - by sending serial data at certain points in your sketch, you can figure out what's going on during the program. This is why it's so helpful to learn about serial communications early in RG101.

Back to Contents

A-serialHelloWorld

video.png Click here to expand the video

code.png Click here to expand the code

diagram.png Click here to expand the diagram

Let's Try out our first Serial Communication Sketch, showing that the Arduino can pass data to the Serial Monitor. Open the following sketch in your Arduino IDE

File > Sketchbook > RobotGeek sketches > RG101 > 00 - serialCommunication > A-serialHelloWorld

Now upload the sketch onto your Geekduino. Once the sketch is loaded, click the Serial Monitor button to open the Serial Monitor. The Autoscroll box should be checked (this will automatically scroll the window when data comes in). The first drop-down menu should be set to No line Ending (this means that no extra data is sent on the serial output). The second drop-down should be set to 9600 (this sets the port speed to 9600 bits-per-second(bps) )

The serial monitor button is located in the upper right hand corner of your Arduino Sketch. If you hover over it, you'll see the text 'Serial Monitor'.You can also go to Tools ->Serial Monitor to open the serial monitor.

In the serial monitor you should see the text

 Hello World!

This data is being transmitted from the Geekduino to your computer. In fact, if you were to plug youir Geekduino into another computer, it would transmit the same text to the new computer.

All of the code for this example is in the setup() function. First, the function Serial.begin(9600); starts the serial communication hardware at a speed of 9600 bits per second. This will do all of the behind-the-scenes work to get the hardware serial port on the Geekduino working and ready. The hardware serial port on the ATmega328 is connected to pins 0(Receive/RX) and 1(Transmit/TX) as well as to the USB-Serial Converter on your Geekduino which allows the Geekduino to transmit/receive data to/from your computer.

The second line, Serial.println("Hello World!"); sends a line of data from the Geekduino to the computer. You can change the content of the message by just changing the text in-between the quotes.

Back to Contents

B-serialPrintVariables

video.png Click here to expand the video

code.png Click here to expand the code

diagram.png Click here to expand the diagram

Let's add in variables, so we can solidify the idea that we can pass data from anywhere in the code. Open the following sketch in your Arduino IDE

File > Sketchbook > RobotGeek sketches > RG101 > 00 - serialCommunication > B-serialPrintVariables

Again, you'll open the serial monitor to see the output from the Geekduino. The output will all show up at once

This example will use variables. You can think of a variable like a box that stores a piece of data. Every variables has a type, a name and a piece of data. Our first variable looks like

int a;      //integer a, automatically set to 0
    

This variable is of type int, an 'Integer'. Integers can hold whole numbers ranging from -32768 to 32767. The variable's name is a - this name will also allow you to reference the variable later in the code. Because we are not explicuilty setting any data, the variable initially holds the value 0

The next two variables are also both integers, names b and c.

int b = 3;  //integer b, manually set to 3
int c = 0;  //intever c, manually set to 0
    

In both of these lines we're using the assingment operator = to assign a value to the variable. The value to the right of the operator gets stored into the variable - in this case b get the value 3 and c gets the value 0.

Just like in Example A, we'll use Serial.begin() and Serial.println() to start the serial port and then to print an initial message.

Afterwards we'll use the Serial.print() and the Serial.println() function to print the contents of the variables. The Serial.print() is very similar to Serial.println() in that both are used to send ASCII data over the serial port. The difference is that the Serial.println() function adds a 'return' character aftwards - this means that any data sent after this command will show up on a new line in the serial monitor. Serial.print() however will not add a 'return' character, so any data sent after this will be printed on the same line as the previous data. By mixing Serial.println() and Serial.print() we can format the output on the serial monitor.

To print a variable to the serial monitor, simply pass the variable to the Serial.print() function without quotes. The function will send the correct ASCII character to the serial monitor.

The Serial.println() and Serial.print() functions automatically format the variable data based on its type. The data in the variable can be formatted in a variety of ways. For more information check out the Serial Reference page, specifically F-serialFormatting

In the first block of prints, the Geekduino will display the initial values of the variables

 Serial.println("Print Variable Values: ");  //print static text
Serial.println("   ");                      //print empty line
Serial.println("Initial Values: ");         //print static text
Serial.print("Print # 1: ");                //print static text
Serial.print("a = ");                       //print static text
Serial.print(a);                            //print the value in variable a
Serial.print(" b = ");                      //print static text
Serial.print(b);                            //print the value in variable b
Serial.print(" c = ");                      //print static text
Serial.println(c);                          //print the value in variable c
Serial.println("   ");                      //print empty line

This code block will print out the starting values of the variables a, b and c as 0, 3 and 0 respecitvley.

The next block of text looks very similar, but before we print the contents of the a variable, we're going to use the assignment operator (=) to change its value to 2

  a = 2; 

Now the code block will print out the values of the variables a, b and c as 2, 3 and 0 respecitvley. You'll notice that only a changed and that all the other variables retained their values.

The final block of code again prints all of the variable's values, but this time we're going to modify c. Instead of using the assignment operator to set a fixed value, we're going to use the addition opeartor (+) to add the contents of a and b and place the result into c

 c = a + b;  

Now the code block will print out the values of the variables a, b and c as 2, 3 and 5 respecitvley.

Back to Contents

C-serialPrintLoop

video.png Click here to expand the video

code.png Click here to expand the code

diagram.png Click here to expand the diagram

Let's move on and start using (and counting) loops. Open the following sketch in your Arduino IDE

File > Sketchbook > RobotGeek sketches > RG101 > 00 - serialCommunication > C-serialPrintLoop

Again, you'll open the serial monitor to see the output from the Geekduino. The output will start to show up and keep printing out indefinitley

In this sketch we're going to use the integer variable count to keep track of how many times the loop() function has run.

Just like in Example A, we'll use Serial.begin() and Serial.println() to start the serial port and then to print an initial message. In this example however, we're going to put some code into the loop() function. First we'll use the assignment opertor and the addition operator to increment the count variable by one every time the loop runs.

  count = count + 1;

Next we'll use Serial.print() and Serial.println() to print a header for each line and the value of the count variable.

Serial.print("Loop # ");    //print the first part of the line
Serial.println(count);      //print the value of 'count' and a line ending (println) to show the current loop

Finally we're going to use the delay() function.

delay(1000); 

The delay() function tells the geekduino to pause what it's doing for a certain amount of time. The amount of time is determined by the number passed to the function - in this case 1000 means 1000 milliseconds, or one second. By pausing the Geekduino for one second we can limit the amount of data coming out on the serial monitor and make it easier to see. You can experiment with different values in the delay() function to increase or decrease the speed at which data shows up on the serial monitor.

Back to Contents

D-serialRead

video.png Click here to expand the video

code.png Click here to expand the code

diagram.png Click here to expand the diagram

Let's take a look at reading data on the Geekduino from the computer. Open the following sketch in your Arduino IDE

File > Sketchbook > RobotGeek sketches > RG101 > 00 - serialCommunication > D-serialRead

Again, you'll open the serial monitor to see the output from the Geekduino. There will be a single line of output with instructions. To interact with the program, put a single character in the text field and hit enter/return on your keyboard or press the 'Send' button. Data will be sent to the Geekduino, which then triggers the Geekduino to send you back information.

In this sketch we're going to use the variable incomingData to keep track of the character you sent. You'll notice that the variable is of the type char. char holds a single ASCII character. Using the char variable type will make it easy to store, use and send informatin about the character.

The char variable type is a conveient way to store characters, but at a low level the data is still stored as a number. Every ASCII character can be represented as a number. For more information check out the Serial Reference page and the ASCII table reference.

Just like in Example A, we'll use Serial.begin() and Serial.println() to start the serial port and then to print an initial message. In the loop() function you'll see the following block of code

 if(Serial.available() > 0)
{
incomingData = Serial.read(); //read one character from the serial buffer and remove that character from the buffer

 Serial.print("You Typed: ");  //print static text
 Serial.println(incomingData); //print the data that was recieved

}

The if() statement is a control structure that allows the Geekduino to make decisions based on the current state of variables and functions. If the value of the paramater passed to the if() statement evaluates to be true then all of the code between the { and } will be run. If the paramaters evalue to false then the code will not be run.

In this case, we're passing Serial.available() > 0 to the if() statement. Serial.available() is a function that will check to see if there is any incoming data on the Geekduino's serial port and return the number of characters available. If there are no characters available, then the function will return 0. If there is one character avaialble, the fucntion will return 1 and so on. > is 'greater than', a comparison operator that checks to see if the statement on the left has a larger numeric value than the value on the right. So whenever there is no data on the Serial port, Serial.available() returns 0 which is not greater than 0, so the code inbetween { and } is not run. However if there is any data on the serial port (for example if we send over a single character) then Serial.available() returns a non-zero value (in this case 1) which will be greater than 0, allowing the if() statement to run the code inbetween { and }. Let's look at the first line of that code

incomingData = Serial.read();

In this line we're going to use the Serial.read() function to get the character from the serial port. This function not only returns the data from the serial port, it also removes it from the serial port - this means that the next time we call Serial.read(), we will get back the next character that was sent to us (if one is available).

Once we get the data from Serial.read(), we use the assingment operator to store the value into incomingData. Finally we can use Serial.print() and Serial.println() to send the data back to the Serial Monitor, showing that we received the data.

Back to Contents

Serial Reference

Now that you've completed the RG101 Serial Communications Introduction, you're ready to go onto the next topic : 3.1 Digital Output .

If you are interested in learning more about serial communications, check out our Serial Communications Reference Lesson. In this lesson we will look at the remaining RG101 examples and provide you with links to advanced serial communication topics.

Back to Contents

Getting Started