Source Code. Projects. Nerd Stuff. Art Stuff.

Serial Communication

From Tom Igoe’s blog.

Serial communication is the process of sending data sequentially between two devices, whether they are computers, microcontrollers, or some other form of integrated circuit. This is in contrast to parallel communication, where several bits are sent as a whole, on a link with several parallel channels. Communicating serially involves sending a series of digital pulses back and forth between devices at a mutually agreed-upon rate. A start signal is sent prior to each byte, character or code word to prepare the other device for receiving it. After the transmission, a stop signal is sent to notify the receiving device to rest and prepare for the next pulse. This common type of start-stop transmission is called asynchronous serial communication. The sender sends pulses representing the data to be sent at the agreed-upon data rate, and the receiver listens for pulses at that same rate. In this type of communication, there isn’t one common clock; instead, both devices have their own clock and agree on a rate to which to set their clocks.

For example, if two devices are to exchange data at a rate of 9600 bits per second (sometimes called baud rate or baud), we would make three connections between the two devices:

  • a common ground connection, so both devices have a common reference point to measure voltage by
  • one wire for the sender to send data to the receiver on (transmit line for the sender)
  • one wire for the receiver to send date to the sender on (receive line for the sender)

The receiver will continually read the voltage that the sender is putting out, and at 9600 baud, it will will interpret that voltage as a new bit of data every 1/9600th of a second. If the voltage is high, it will interpret that bit of data as a 1. If it is low, it will interpret that bit of data as a 0. By interpreting several bits of data over time, the receiver can get a detailed message from the sender.

Say we want to transmit some data serially:
At the first instance of the clock pulse, the data is read as LOW, at the second, it is read as HIGH, at the third, HIGH as well and so on. The clock rate is set to run at a continuous interval, and both the sending device and receiving device are listening for information at each interval of the clock pulse agreed upon beforehand.
In order for the message to be delivered accurately, both devices have to agree on the order of the bits. Usually the sender sends the highest bit (or most significant bit) first in time, and the lowest (or least significant bit) last in time. As long as we have an agreed upon voltage, data rate, and order of interpretation of bits, we can exchange any data we want serially.

 

Serial Output from the Microcontroller

Arduino boards have a built-in USB-to-serial converter, so they communicate with your computer serially using the USB port. Once the two are connected, we’ll need to write a program to address the serial ports.

Desktop computers have a place they store incoming data from the serial port, called a serial buffer. It’s a little area of memory to store whatever comes in the serial port. Because of this, they can do other tasks while waiting for data to come in, and act on the data from the buffer. Similarly, Arduino has a small serial buffer, although there are many microcontrollers that do not. Additionally, Arduino’s serial buffer is automatically set up, so no additional steps are required.

On the Arduino, the serial pins are fixed, and you can’t change them. You can, however, use the software serial library for Arduino if you need to use different pins, or if you need more than one serial port.

If you’re sending a single byte out serially, it’s easy for the receiving device to know what’s coming. It will read a byte and know the value. Sometimes, however, you have to send multiple bytes to get a message across. Those bytes have to be reassembled on the receiving side. More on that in the serial data interpretation notes.