Friday 27 December 2013

Arduino Serial Library

                                  Arduino Serial Library

Introduction

In this lesson we'll learn how to use the Serial Library to communicate from the Arduino board back to the computer over the USB port. Then we'll learn how to manipulate numbers and data.

What is a Library?

A library is a big collection of procedures, where all the procedures are related! If you, say, want to control a motor, you may want to find a Motor Control Library: a collection of procedures that have already been written for you that you can use without having to do the dirty work of learning the nuances of motors.The library we will be using is the Serial Library, which allows the Arduino to send data back to the computer:


What is Serial?
The word serial means "one after the other." For example, a serial killer doesn't stop with one murder, but stabs many people one after the other. Serial data transfer is when we transfer data one bit at a time, one right after the other.
Information is passed back & forth between the computer and Arduino by, essentially, setting a pin high or low. Just like we used that technique to turn an LED on and off, we can also send data. One side sets the pin and the other reads it.
(Now, people who are all geeked-out will probably get angry at this point because I'm simplifying things. 
Well guess what, its an Arduino tutorial, not a OSI Physical Network Architecture tutorial.)
Bits & Bytes
Now is a good time to review how data is measured. For example, we measure weight with "ounces" and "pounds" (or grams and kilograms) and distances with "inches," "feet," and "miles" (or centimeters, meters and kilometers). Information has its own system of measurements:
single bit is either a zero or a one
You can group bits together into 8 bits which is 1 byte
1024 bytes (8192 bits) is one Kilobyte (sometimes written KB).
1024 KB (1048576 bytes) is one Megabyte (MB)
1024 MB is 1 Gigabyte (GB)
That is, a 1.0 Kilobyte file on your computer is 1024 bytes:
About 4.6% less than you'd expect 
How Arduino Works:
We've actually used the Serial communications capability already quite a bit...that's how we send sketches to the Arduino! When you Compile/Verify what you're really doing is turning the sketch into binary data (ones and zeros). When you Upload it to the Arduino, the bits are shoved out one at a time through the USB cable to the Arduino where they are stored in the main chip.
Next time you upload a sketch, look carefully at the two LEDs near the USB connector, they'll blink when data is being transmitted. One blinks when the Arduino is receiving data (RX) and one blinks when the Arduino is transmitting data (TX)
Our first sketch
Our first sketch is going to be the hello world! program. When it starts up, it will say "hello world!"
Create a New Sketch... and save it as HelloWorld
Into the new sketch, copy and paste the following source code, then save it
/*
 * Hello World!
 *
 * This is the Hello World! for Arduino. 
 * It shows how to send data to the computer
 */


void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  
  Serial.println("Hello world!");  // prints hello with ending line break 
}

void loop()                       // run over and over again
{
                                  // do nothing!
}

Copy and paste the sketch into your Arduino IDE and compile and verify code,and you can see the output at Serial Monitor(Upper Right corner of IDE).

We definately see that there is a Serial thing going on, and it looks like there is a procedure call as well. This is a library procedure call. The library is called Serial and inside the library is a procedure called begin.
library name
.
procedure name
(input values)
;
Serial
.
begin
(9600)
;
So there's some mystery procedure that's called begin, well it's not too tough to figure out what it might do. It's the procedure that gets the Serial stuff ready. But what's the 9600 about? 
The comment says 9600 bps, and just so you know bps stands for bits-per-second (we will refer to this as the baud rate)
If you have broadband connection, you may remember reading somewhere that it has, say 350 kbps download rate. This is how fast the connection can read and write bits on the wire. (Needless to say, your broadband connection can transfer data a lot faster than an Arduino!)
OK so Serial.begin sets up the Arduino with the transfer rate we want, in this case 9600 bits per second.
Lets move on to the next line.
Serial.println("Hello world!");  // prints hello with ending line break 
This line also uses the Serial library, this time it's calling a procedure called println which is just a shorthand for "print line". Note that the 6th letter in println is the letter L not the number 1. This time the input is a quotation, the line of text we would like it to print. We use two "'s (double quotes) to indicate the beginning and end of a line of text.is is twice as fast as before, so it will take half the time, about 1/200th of a second.
Now compile the sketch and upload it to your Arduino....
and then there's a serial monitor built into the Arduino software where you can see the output of your code!

No comments:

Post a Comment