{"id":9469,"date":"2023-05-09T17:26:08","date_gmt":"2023-05-09T15:26:08","guid":{"rendered":"https:\/\/deepbluembedded.com\/?p=9469"},"modified":"2023-08-17T23:50:04","modified_gmt":"2023-08-17T20:50:04","slug":"arduino-serial-communication","status":"publish","type":"post","link":"https:\/\/deepbluembedded.com\/arduino-serial-communication\/","title":{"rendered":"Arduino Serial Communication Tutorial"},"content":{"rendered":"\n
In this tutorial, we’ll discuss the Arduino Serial Communication Ports (Protocols)<\/strong> that we can use for communicating with other sensors, modules, and microcontrollers. The Arduino’s microcontroller supports 3 serial communication ports (UART<\/strong>, SPI<\/strong>, and I2C<\/strong>).<\/p>\n\n\n\n We’ll get an overview of the serial communication ports (protocols) available in the Arduino, their IO pins, specifications, differences, use cases, and applications. Each serial communication port can be used to establish a communication channel between two Arduino boards, an Arduino with another microcontroller, or between Arduino and various sensors & modules. Which is the most common use case as we’ll see hereafter.<\/p>\n\n\n In Embedded Systems, Telecommunication, and Data Transmission applications, Serial Communication<\/strong> is known to be the process of sending data one bit at a time (bit-by-bit) sequentially, over the serial bus. It takes a complete clock cycle in order to transfer each bit from one end to the other. <\/p>\n\n\n\n Conversely, parallel communication is known to be the process of sending several bits, even bytes, as a whole only in a single clock cycle. However, even if you transfer fewer data per cycle with a serial transmission, you can do it at much higher frequencies which results in higher net transfer rates than parallel communication.<\/p>\n\n\n\n There are so many different serial communication protocols, each of which is working in a unique way. However, they all do share one thing in common. Which is having Shift Registers somehow\/somewhere in their hardware implementation as the working horse (core). <\/p>\n\n\n\n Shift Registers<\/strong> are used to shift out the data to be transmitted bit-by-bit each clock cycle. Shift registers are basically some D-Flip-Flops serially connected while sharing the same clock line. Here is a graphical animation that demonstrates how a shift register works.<\/p>\n\n\n\n <\/p>\n\n\n\n Generally speaking, serial communication at the end of the day comes down to connecting a couple of shift registers together! We just connect the data output of a shift register to the data input of the other shift register enables us of sending digital data serially from one end to another.<\/p>\n\n\n\n In the following animation, I\u2019m connecting a couple of 4-Bit shift registers<\/strong>. One at the transmitter device<\/strong> and the other at the receiver device<\/strong>. The serial bus consists of a couple of wires (data line, and clock line). Each clock, a bit is sent from the transmitter’s TX pin and received by the receiver\u2019s RX pin.<\/p>\n\n\n\n <\/p>\n\n\n\n As you might have noticed, it takes 4 clocks<\/strong> to send the 4-Bit data from the transmitter to the receiver. This is simply the short answer to the \u201cHow does Serial Communication Work?\u201d. This is the basic idea behind serial communication that you need to know first to start building up your understanding of the topic.<\/p>\n\n\n\n In fact, there are so many options and configurations for each serial communication protocol that sets it apart from the others. This includes various data transmission rates, operating modes, error detection & correction mechanisms, data flow control, and much more that adds to the overall complexity associated with each protocol. <\/p>\n\n\n\n This is more than enough for the scope of this tutorial, but if you want to learn more about serial communication fundamentals, and its underlying hardware, and to go deeper into the topic, then the tutorial linked below is a very good starting point for you.<\/p>\n\n\n This article will give more in-depth information about serial communication fundamentals and UART serial communication ports hardware & software.<\/p>\n\n<\/div><\/div><\/div>\n<\/div>\n<\/div><\/div>\n\n\n UART<\/strong> (U<\/strong>niversal A<\/strong>synchronous R<\/strong>eceiver-T<\/strong>ransmitter) is the most popular serial communication protocol in embedded microcontrollers. In Arduino, we typically use the UART module for serial communication with the PC via a USB-TTL converter to print serial messages on the serial monitor.<\/p>\n\n\n\n There are actually two forms of UART Hardware as follows:<\/p>\n\n\n\n The Synchronous type of transmitter generates the data clock and sends it to the receiver which works accordingly in a synchronized manner. On the other hand, the Asynchronous type of transmitter generates the data clock internally. There is no incoming serial clock signal, so in order to achieve proper communication between the two ends, both of them must be using the same baud rate. The baud rate is the rate at which bits are being sent bps (bits per second).<\/p>\n\n\n The Arduino UNO has only one UART module which has two pins (RX for receiving serial data and TX for transmitting serial data). The UART pins’ mapping is as follows: RX<\/strong> is Arduino pin 0<\/strong>, and TX<\/strong> is Arduino pin 1<\/strong> respectively.<\/p>\n\n\n\n <\/p>\n\n\n Besides serial communication with your PC for debugging and such, the UART is also used for interfacing various sensors and modules such as:<\/p>\n\n\n\n <\/p>\n\n\n If you need an extra UART module in your application, you can use the This article will give more in-depth information about Arduino UART serial communication, and Arduino UART code examples for communication with a PC and between 2 Arduino boards.<\/p>\n\n<\/div><\/div><\/div>\n<\/div>\n<\/div><\/div>\n\n\n SPI<\/strong> (S<\/strong>erial P<\/strong>eripheral I<\/strong>nterface) is pronounced as \u201cS-P-I\u201d or \u201cSpy\u201d. Which is a serial, synchronous, single-master, multi-slave, full-duplex interface bus typically used for serial communication between microcomputer systems and other devices, memories, and sensors. Usually used to interface Flash Memories, ADC, DAC, RTC, LCD, SDcards, and much more.<\/p>\n\n\n\n In typical SPI communication, there should be at least 2 devices attached to the SPI bus. One of them should be the master and the other will essentially be a slave. The master initiates communication by generating a serial clock signal to shift a data frame out, at the same time serial data is being shifted into the master. This process is the same whether it\u2019s a read or write operation.<\/p>\n\n\n The SPI communication bus consists of 3 main lines (Serial Data Out, Serial Data In, and Serial Clock). The SPI master device needs also an extra IO pin to select the slave to which it’d like to communicate. This means we need an extra IO pin for each slave device added to the bus which is definitely going to limit the maximum addressable devices on the bus.<\/p>\n\n\n\n The SPI bus lines are named according to a conversion that eases the wiring and usage for users, which goes as follows:<\/p>\n\n\n\n <\/p>\n\n\n There are so many applications in which you’d need to use the Arduino SPI bus to interface various sensors and modules which include, but are not limited to, the following examples:<\/p>\n\n\n\n <\/p>\n\n\n\n I2<\/sup>C<\/strong>, I2C<\/strong>, or IIC<\/strong> (I<\/strong>nter-I<\/strong>ntegrated C<\/strong>ircuit) is also a very popular serial communication protocol that’s widely used by different sensors and modules in embedded systems. It consists of 2 pins only (one for serial data and one for the serial clock). <\/p>\n\n\n\n The I2C is a multi-master multi-slave protocol that supports a large number of devices on the same 2-wire bus. Each device has a unique ID that others can use to directly address that specific device. And it’s considered the most efficient serial communication bus in terms of the number of IO pins needed to establish a communication network of multiple devices.<\/p>\n\n\n The Arduino UNO (Atmega328p) microcontroller has only one I2C module, which has 2 pins: (SDA and SCL) as shown below.<\/p>\n\n\n\n <\/p>\n\n\n There are so many modules and sensors that we can interface with Arduino using the I2C bus, which include:<\/p>\n\n\n\n Typically, you won’t need more than one I2C module on your Arduino microcontroller. Because if you need to add a new extra device to the I2C bus, it’s as simple as hooking it up to the SCL and SDA lines and you’re good to go. Unlike the UART which is a single master single slave communication port.<\/p>\n\n\n The I2C Pins (SDA<\/strong> and SCL<\/strong>) are the Arduino analog input pins A4<\/strong> and A5<\/strong> respectively. If you’re going to use the Arduino I2C communication, you’ll not be able to use those 2 pins as analog input channels for the ADC or whatever.<\/p>\n\n<\/div><\/div>\n\n\n And now let’s summarize and compare the technical features and specifications for the 3 serial communication ports (protocols) that we’ve discussed in this tutorial (UART, I2C, and SPI).<\/p>\n\n\n\nTable of Contents<\/h2>\n
\n
\n\n\nWhat is Serial Communication?<\/strong><\/h2>\n\n\n
\n\n\nArduino Serial Communication<\/strong><\/strong> – UART<\/strong><\/h2>\n\n\n
\n
Arduino UART Pins<\/strong><\/h3>\n\n\n
Arduino UART Applications<\/strong><\/h3>\n\n\n
\n
SoftwareSerial<\/code> library which is a software-emulated UART module. It’ll bit-bang the UART serial pattern on any 2 pins of your choice. But note that it’ll cost you a lot of CPU load and it’s not going to be reliable at high speeds. Because at the end of the day, it’s not a real UART hardware module.<\/p>\n\n<\/div><\/div>\n\n
\n\n\nArduino Serial Communication<\/strong><\/strong> – SPI<\/strong><\/h2>\n\n\n
Arduino SPI Pins<\/strong><\/h3>\n\n\n
\n
Arduino SPI Applications<\/strong><\/h3>\n\n\n
\n
\n\n\nArduino Serial Communication<\/strong><\/strong> – I2C<\/strong><\/h2>\n\n\n
Arduino I2C Pins<\/strong><\/h3>\n\n\n
Arduino I2C Applications<\/strong><\/h3>\n\n\n
\n
\n\n\nArduino Serial Communication Ports Comparison<\/strong><\/h2>\n\n\n