Hi Computer, I’m Arduino! - Using serial communication to make your Arduino talk

The Arduino is no baby that just crawls around and blinks LED’s. You can program it to speak!

We will see how to use the Arduino’s Serial library to have the Arduino talk to the computer over the USB port.

Let’s begin by making it clear that we aren’t talking about tasty breakfast while talking about Serial. Serial communication is a type of “protocol” or a procedure by which information and data is sent one after another “bit by bit” instead of all at once. A bit is the fundamental building block of digital data, consisting of a 0 or a 1.

You need not to worry about the inner working of the protocol itself, but the end-result it can accomplish: Sending messages and data back and forth from the Arduino to a computer or even other hardware and Arduinos. Let us consider communicating between a computer and Arduino for now since we will be using it extensively in the book.

Let us understand Serial Communication Library by directly jumping into the code. Don’t worry if you don’t know what libraries are yet. You are correct, in guessing that we aren’t talking about the libraries which have books and strict librarians with large spectacles.

Libraries will be explained in later chapters. For now, just think of a library as a collection of procedures or tasks that tell the Arduino to do certain things:

void setup()         // run once, when the sketch starts
{
  Serial.begin(9600);  // set up Serial library at 9600 bps
  Serial.println("Hello world!");  // prints hello
}
void loop()                       // run over and over again
{
                                  // do nothing!
}

Let us see the working of this code snippet:

void setup()

This part of the code, and everything between the { and } brackets runs exactly once when the Arduino runs.

The following procedure is part of the serial library, and tells the Arduino the speed at which communication should take place:

Serial.begin(9600);

The following table would give you an idea of how this line can be further broken up:

Library Name

.

Procedure Name

(Value)

;

Serial

.

begin

9600

;

This line of code says: begin the serial communication at the speed of 9600 bits per second (remember the 0’s and 1’s talked about earlier). This speed is sometimes called the “Baud Rate” of communication. The . Specifies that this procedure belongs to the Serial library.

Serial.println("Hello world!");

Similar to the previous command, the println procedure tells the Arduino to send data to the serial port. The data that will be sent is Hello World which is the value given to the println procedure. The following part of the code runs forever in a loop:

void loop()

We haven’t put anything inside this because we want to send “Hello World” only once to the computer.

We now upload the code to our Arduino using the Arduino IDE. After uploading, we use the Arduino IDE’s Serial Monitor to see what the Arduino is sending:

Hi Computer, I’m Arduino! - Using serial communication to make your Arduino talk

We make sure the baud rate in the Serial Monitor is set to the value we set on our Arduino. This is done so that our computer listens to the Arduino at the same rate at which the Arduino sends data, otherwise, the Serial Monitor will output gibberish.

You can see the the Arduino send “Hello World!”. How exciting, your Arduino can talk now!

Try putting the Serial.println("Hello world!"); inside the void loop(){} instead of void setup(){} and see what happens.

We will be using the Arduino’s serial monitor to output values of sensors to our computer so we have a better understanding of what’s going. The Serial Monitor and Serial Communication are great ways to debug (meaning fix, in code-speak) sensors, connections and even code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset