There are many ways of connecting your IoT devices for the purpose of communication. Each more optimized for a peculiar situation, some easier to deal with than others. In this article, we would look at the most common methods for connecting our IoT devices for the purpose of communication. There are two ways we can make our devices communicate; either synchronously or asynchronously. When communicating over an asynchronous protocol, the sender of the data does not need to tell the receiver the state of its clock. The data is only sent without recourse to the state of the receiver's clock state. However, the data sent has to include other bits in the sent packet to help the receiver determine the start and stop bits of the data sent.
In synchronous protocols, the receive clock is linked to the send clock. This is usually done by adding an additional line between sender and receiver over which clock signals will be communicated. In this method, there is no need for start and stop bits; hence less overheads are incurred.
Data can also be communicated in parallel or serially. Communicating parallelly is expensive since there has to be a lot of lines to communicate. On the plus side, it is mostly faster. For example, if we have to send a series of 8-bit numbers we need 8 parallel data lines in addition to the control lines. Though we send those 8 bits in one burst, we needed one line for each bit - darn expensive!
If we communicate serially, we need fewer lines. Although, both means have their pros and cons, serial communication has proven to be more effective and cost effective. Hence, serial communication is favored over parallel most times.
Some serial protocols for connecting IoT devices include: SPI, I2C, USART, UART(SCI).
Serial Communication Interface (SCI) provides an asynchronous communications interface. Also called Universal Asynchronous Receiver Transmitter (UART), the SCI is a protocol (in some sense) but more appropriately it is a module that allows for asynchronous communication. UART modules are usually built into microcontrollers. The Atmega 328 has one module while the Atmega2560 has 4 of those.
Since, UART is somewhat a very generic protocol, applications have the chance to implement exactly how it behaves. Applications can specify the number of data bits used for communication. Parity bit can be set. This bit helps in error checking. It can be set to 0 or 1 depending on even, odd or no parity at all. Stop bits indicate the end of the data stream. Together with the start bit which is always 1, the stop bit helps synchronize data being transmitted between transmitter and receiver.
The Baud rate is the rate of sending data over the connection lines. These are measured in bit per second(bps) and is controlled by the application. Typical baud rates in 9600bps and 115200 bps. These values are frequency dependent hence are peculiar to the communicating devices. UART uses 2 wires - TXD (transmit) and RXD (receive) for full or half-duplex communication. In duplex communication, both the receiver and transmitter get the chance to send data. In full duplex, both sides can communicate simultaneously while in half-duplex, they take turns.
Messages are transmitted using an encoding system called Non-Return to Zero (the voltage levels do not return to 0V). In this scheme, 1s are represented by the more positive voltage and 0s correspond to the more negative voltage. Negative logic can also be employed. In that case, 1s are represented by more negative voltages and 0s by more positive voltages. Since communication is asynchronous, data has to be enveloped by a frame consisting of a start bit and a stop bit.
In the idle state(no data is being transmitted), the line is held HIGH. It goes LOW with a start bit which start a UART frame. After the start bit, data bits are transmitted beginning with the LSB(least significant bit). After the data bits, there may follow 1 parity bit. The frame is completed with 1 or 2 stop bits. Stop bits send the line back to a HIGH state.
Because UART is asynchronous, the receiver and the sender's clock are not in sync; hence there has to be an alternate way of syncing the data being transmitted. The receiver knows what bitrate to expect with the set baud rate. However, it does not know where a frame starts from. Hence, it synchronizes to the falling edge of the start bit.
Clock oscillators tend to have a non-zero drift (they drift from their nominal frequency). So, even if they sync. to the sender's clock at the start of the message, they might drift away during transmission.
To regain initial sync., the receiver uses oversampling this means that RXD line is sampled S times per bit. S is typically 16 bits. When receiver detects a falling edge, it assumes the start of a start bit and begins counting the samples.
Errors include parity and frame error. There can also be data overruns.
General UART Frame Format
There is a synchronized version of UART called USART. The basic difference here is the addition of a dedicated line carry clock signal. This is used for synchronization. As can be inferred from the earlier paragraphs, the presence of the clock line reduces the overhead incurred in UART.
This is but the basic stuff about UART, in subsequent articles (which will be practice based) we will use UART for connecting stuff. In those ones we will go deeper as required.
Rene Novor X.K
At Mor-Lan Technologies, we aim at developing the best solution to the most pressing problems we face here in Ghana and Africa at large when it comes to information technologies. Producing high quality products is our focus here!