Introduction: Serial Communications With Arduino

About: 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 and I plan to use it to make more things and bring more instructables.

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");
}
}
}

Make It Glow Challenge

Participated in the
Make It Glow Challenge

Pocket-Sized Contest

Participated in the
Pocket-Sized Contest

The Mad Science Fair

Participated in the
The Mad Science Fair