Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Arduino communications


May 15, 2021 Arduino


Table of contents


Hundreds of communication protocols have been defined to implement this data exchange. /b10> Each protocol can be divided into two categories: parallel or serial.

Parallel communication

Parallel connections between Arduino and peripherals via input/output ports are the ideal solution for short distances (up to several meters). /b10> In other cases, however, parallel connections are not possible when long-distance communication between two devices is required. /b11> The parallel interface transmits multiple bits at the same time. /b12> They usually require a data bus - transmitted over eight, sixteen or more lines. /b13> The data is transmitted in huge waveforms of 1 and 0.

Arduino communications

The advantages and disadvantages of parallel communication

Parallel communication certainly has its advantages. /b10> It is faster, more direct, and relatively easy to implement than serial. /b11> However, it requires many input/output (I/O) ports and lines. /b12> If you've ever moved a project from a basic Arduino Uno to a Mega, you know that I/O lines on microprocessors are valuable and rare. /b13> Therefore, we prefer serial communication at the expense of the potential speed of pin space.

Serial communication module

Today, most Arduino boards use several different serial communication systems as standard devices.

Which system to use depends on the following factors:

  • How many devices and data exchanges do microcontrollers have?
  • How fast is the data exchanged?
  • What is the distance between these devices?
  • Do I need to send and receive data at the same time?

One of the most important things about serial communication is the protocol, which should be strictly adhered to. I t is a set of rules that must be applied for devices to correctly interpret the data they exchange with each other. /b11> Fortunately, Arduino automatically handles this issue so that the programmer/user's work can be simplified to simple writing (sent data) and reading (receiving data).

The type of serial communication

Serial traffic can be further classified as:

  • Sync - Synchronized devices use the same clock, and their timings are synchronized with each other.

  • Asynchronous - Asynchronous devices have their own clocks and are triggered by the output of the previous state.

It's easy to find out if your devices are syncing. /b10> If the same clock is provided to all connected devices, they are synchronized. /b11> If there is no clock line, it is asynchronous.

For example, the UART (Universal Asynchronous Receiver) module is asynchronous.

Asynchronous serial protocols have some built-in rules. /b10> These rules are simply mechanisms that help ensure reliable and error-free data transfer. These mechanisms for avoiding external clock signals are:

  • Sync bits synchronization bits
  • Data bits data bits
  • Parity bits parity bits
  • Baud rate Porter rate

Synchronize bits

The synchronization bit is two or three special bits that are transmitted with each packet. T hey are the start and stop bits. As their names say, these bits mark the beginning and end of the packet, respectively.

There is always only one start bit, but the number of stop bits can be configured as one or two (although it is usually kept at 1).

The start bit is always indicated by an idle data line from 1 to 0, and the stop bit transitions back to idle by keeping the line at 1.


Arduino communications

The bit of data

The amount of data in each grouping can be set to any size of 5 to 9 bits. /b10> Of course, standard data sizes are basic 8-bit bytes, but other sizes have their uses. /b11> A 7-bit packet may be more efficient than an 8-bit packet, especially if you are only transmitting 7-bit ASCII characters.

Parity bit

The user can choose whether there should be parity bits, and if so, whether parity should be parity or even. I f the number of 1s in the data bit is even, the parity bit is 0. The parity of odd numbers is the opposite.

Porter rate

The term Baud Rate is used to represent the number of bits transmitted per second. /b10> Note that it refers to bits, not bytes. /b11> Protocols typically require each byte to be transmitted with several control bits. /b12> This means that a byte in a serial stream can include 11 bits. /b13> For example, if the Baud rate is 300bps, you can transfer up to 37 bytes per second and a minimum of 27 bytes per second.

Arduino UART

The following code will cause Arduino to send hello world at startup.

void setup() {
   Serial.begin(9600); //set up serial library baud rate to 9600
   Serial.println("hello world"); //print hello world
}

void loop() {

}

After uploading the Arduino sketch to Arduino, open the serial monitor Arduino communications search in the upper right corner of the Arduino IDE.

Type anything in the top box of the serial monitor, and then press the send key or the enter key on the keyboard. /b10> This sends a series of bytes to Arduino.

The following code returns anything it receives as input.

The following code causes Arduino to transmit the output based on the input provided.

void setup() {
   Serial.begin(9600); //set up serial library baud rate to 9600
}

void loop() {
   if(Serial.available()) //if number of bytes (characters) available for reading from { 
      serial port
      Serial.print("I received:"); //print I received
      Serial.write(Serial.read()); //send what you read
   }
}

Note that Serial.print and Serial.println will send back the actual ASCII code, while Serial.write will return the actual text. /b10> See the ASCII code for more information.