Chapter 8. Communicating with Others

Until now, all our projects have been designed to be standalone and independent, but there will be occasions where we will have to integrate our project with other external devices, and this means that we will have to make our Arduino talk with these other elements. Usually, this communication is made via a very practical and extended standard called serial communication.

In this chapter, we will see how to connect and program Arduino so that it is capable of serially talking to our computer as the simplest and most common type of serial communication, but what you will learn here is directly applicable to any other connection you have to establish to another device that can talk serially.

So, let's give up talking and let's make our projects talk.

Serial communications concepts

All through this chapter, when we talk about serial communication, we will be referring to RS-232 standard protocol communication based on the Universal Asynchronous Receiver/Transmitter (UART) that the Arduino microcontroller incorporates. It is the most common communication type for most Arduino projects in comparison to other serial protocol communications that can also be established with Arduino and that we will briefly introduce in the next section.

The first thing we should know is why all these communication systems are called serial as opposed to parallel communications.

In the case of a serial communication, every bit of the data being transmitted is sent one piece at a time, through just a single line of the communication channel. In a parallel communication, data bits are sent in groups all at once, which makes it necessary to have a bigger number of communication lines in the channel.

This is perhaps the main reason to use serial communications in Arduino, due to the limited number of pins available to establish the communication channel, allowing using just two pins in the case of RS-232 communications.

If you take a closer look at your Arduino board, you should notice a pair of pins marked as TX and RX in the digital pins row, pins 0 and 1 to be precise, as you can also see in the following image:

Serial communications concepts

TX and RX pins in the digital pins row

When connecting Arduino to your computer, you don't really have to use these pins; instead, you can establish a serial connection through the USB port thanks to the UNO ATmega16U2 chip, or the FTDI chip in older boards, which tunnels the serial communication over the USB.

In case you want to connect your Arduino to any other kind of serially capable device, you should use the TX/RX pins instead.

This facility requires a little consideration when connecting external circuitry to your Arduino board through these pins. Since they are going to be used even when uploading your sketches to the microcontroller, you may have to disconnect what you have attached to these pins when uploading the code and connect them back again once the communication has finished.

My personal advice is to avoid using these pins as long as you have others available, and use them only if you don't have any more free or exclusively available when developing projects in which you have to establish serial communication with other serial devices.

All these pin considerations apart, I won't go much deeper into the technical features of a serial communication, precisely because of its implicit simplicity, but there is just one specific parameter relating to serial communications that is necessary to know: the baud rate.

The baud rate

The baud rate in a serial communication specifies the data rate in bits per second that both communicating devices must comply with to be able to understand each other. Typical values range from 300 to 115,200, depending on the connected devices.

There are also other parameters to be specified that affect the communication but are optional, like the number of data bits, parity, and stop bits.

If you are interested in the internal mechanisms that regulate a serial communication, you could visit the Wikipedia page for the serial communication at http://en.wikipedia.org/wiki/Serial_communication and from the point of view of Arduino, you could also take a look at the Serial part of the Reference section of the Arduino site at http://arduino.cc/en/Reference/Serial.

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

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