# Serial Communications With Arduino

79,918

92

4

At this point you should have a robot that can think on its own, we need to be able to tap into its thought process. See what he sees. Make some of his decisions for him. in essence, we need to be able to guage his intelligence to make him more intelligent and increase his efficiency.
With serial communications we can do two things: We can either send commands or receive data. Here are some examples to
use with Serial communications:

1. Find out what values your sensors are outputting.
2. Find out if your robot is making the right decisions (Drugs = FALSE;)
3. Change the value of a variable.

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: A BIT of Information.

Serial communication works on 1s and 0s. Also known as binary, the Arduino sends these 1s and 0s (bits) one by one, or Serially. These bits are sent in the form of Highs(1) and Lows(0). These bits form together and turn into bytes. A byte is composed of 8 bits. Here's a little tutorial on binary:
Each bit represents a certain number to add. the first bit (Far right) represents the ones place, similar to the common decimal system.
1 = 1 and 0 = 0.
the next bit's value is multiplied by two, so:
10 = 2, 11 = 3, 00 = 0 and 01 = 1.

similarly:
100 = 4,
1000 = 8,
10000 = 16,
100000 = 32,
1000000 = 64
10000000 = 128.

Now these bits can form a value up to 255 (11111111).

This value can be turned into ASCII encoded symbols and letters. Here's a list of them.

## Step 2: Serial Output Code

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

void loop(){
Serial.print("The analog value is: ");
delay(100);
}

Serial Print is a function that prints whatever you specify on the Serial Monitor. In this code, we are going to print the analog value of whatever is in Analog Pin 0.

In the void setup, we begin the serial monitor at a baud rate of 9600.

In the void loop, we are using the Serial.println function and what we are printing is the value found in A0 by the Analog Read.

## Step 3: How It Is Used

Serial communications can be used in many ways, but in this situation we will use it to turn off and on an LED.
Just add an LED to pin 13 and ground. Then connect your USB cable.

Create an integer to store the incoming serial byte. I decided to name it "inByte"

In the void setup, set pin 13 as an output and use "Serial.begin(< baud rate >);" to initiate the serial communication. We used 9600bits per second, so we added - Serial.begin(9600);

in the void loop we first need to know when a byte is available to be read. So, we use "Serial.available()" This function returns the number of bytes that are available to be read. So, if we use "if(Serial.available() > 0)", we know there is at least one byte that we can read.

inside that if statement, we need to read the byte and store it for later use. we will use "Serial.read()", which will read the byte. then we will save it to inByte. In total, this line read: inByte = Serial.read();

While still being inside the main if statement (<"if(Serial.available() > 0)>) we add if statements. To read or write a letter to a variable, we will put the letter in single quotation marks. We wanted our LED to turn on if we sent an 'a' to the Arduino. Thus, our if statement read: "if(inByte == 'a')" inside the if statement we added set the pin to HIGH.

Also, we used "Serial.println()" so that the arduino will notify us, right within the Serial monitor, that it has read an 'a' and has turned on the LED.
This if statement looked like this:
if(inByte == 'a') { // byte is 'a'
digitalWrite(13, HIGH);
Serial.println("LED - On");
}

We also wanted the LED to turn off if any other letter was read. so we added an else statement which turned the LED off.

This all may seem confusing, but it is easier to understand if you look at the final code on the next page.

## Step 4: Serial LED Code

int inByte; // Stores incoming command

void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT); // Led pin
}

void loop() {

if(inByte == 'a') { // byte is 'a'
digitalWrite(13, HIGH);
Serial.println("LED - On");
}
else { // byte isn't 'a'
digitalWrite(13, LOW);
Serial.println("LED - off");
}
}
}

Participated in the
Pocket-Sized Contest

Participated in the

Participated in the
Make It Glow Challenge

## Recommendations

• ### Internet of Things Class

22,710 Enrolled

## 4 Discussions

great, but can you give me any information about XBee communication and how I can send an analog signal via XBee, thanks in advance...

I have to say that Ardunio makes it much easier to would with serial communication. Nice work !