Communication

From robotics

Communications

There are many different communication protocols that allow electronic devices to “talk” to one another. The most common is the serial protocol that Arduino uses for printing text and values to the PC. Although every communication protocol is different they all serve the same purpose, Input and Output.


• Communications protocols supported by arduino

o Serial

o I2c

o SPI

o USB(Due and Leonardo only)


Serial

Serial is the most essential communications protocol for the Arduino user. Serial allows you to send and receive variables and messages. With the Arduino the serial port allows 2 way communications through the USB port to the PC.

Serial Output

There are 3 important steps to output from the Arduino to the PC.

Step 1
Start the Serial port in the setup function.
Serial.begin(baud rate);

The baud rate is how fast the data is being transmitted. The two most common rates are 9600 and 115200.

115200 is the fastest rate that Arduino supports. Just remember the baud rate you choose for step 3.


Step 2
Use one of the following commands to send serial messages.
Serial.write(45); 

Data is sent as a byte or series of bytes. when viewed on the PC you will see the ascii representation “-“

Serial.print(45);

Data is sent as human readable ascii. On the PC you will see “45”

Serial.println(45);

This will work exactly as serial.print but will put a new line and carriage return characters after your data. Those characters are “/n” and “/r”

Step 3
There are 2 options to view the data on the pc. The first is to use the built in serial monitor located at the top of the Arduino ide.
Make sure to set your baud rate at the bottom of the serial monitor window. You can copy paste the output with ctrl-c but it only saves a certain number of lines and cannot be copied while data is streaming.


The second option is to use a 3rd party serial monitor. We recommend putty because it’s free and works well. The most important feature is the logging feature which allows you to log the output to a text file.

There are many variations of these functions that allow you to send information anyway you would like.

Serial Input

Serial Input is how your Arduino reads data from the PC. There are also 3 steps for reading serial data.

Step 1
In setup begin serial with the

Serial.begin(9600);

Step 2
In your main loop you need to check to see if data is available.
Step 3
If data is available then read the byte and save it to a variable.
void loop() {
if (Serial.available() > 0) {  //step 2
int incomingByte = Serial.read(); 
Serial.print("I received: ");
Serial.println(incomingByte, DEC); // print what you recieved
}
}

There are a number of other functions that make reading and writing data over the serial port much easier.

Others

I2c, SPI, and USB are supported depending on the Arduino that you use but will not be covered here.