Introduction: Serial Communications With Arduino

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.
4. Control your robot's movement or other things in your robot


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: ");
  Serial.println(analogRead(A0));
  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
Serial.println("Ready"); // Ready to receive commands
}

void loop() {
if(Serial.available() > 0) { // A byte is ready to receive

inByte = Serial.read();
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");
}
}
}

Comments

author
aslan0490 (author)2012-04-08

hey cool proj. any details fr comm with xbee?? thnks in advance

author
the-new-x (author)2011-12-21

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

author
AvrDon (author)2011-11-15

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

author
Computothought (author)2011-11-14

Cool!

About This Instructable

69,433views

88favorites

License:

Bio: I am a young engineer who likes to build things. My motto is Why buy, when you can Make! in 2010 I got an Arduino ... More »
More by Chowmix12:Graphing on an Arduino, the EASY WAY!Graphing values in Arduino, the EASY way!Never lose an RC BodyClip again!
Add instructable to: