Lesson-06 (UART) Serial Communication

Definition

UART (Universal Asynchronous Receiver-Transmitter) is a hardware communication protocol that is commonly used for serial communication between microcontrollers, computers, and other devices. It provides a simple and reliable way to transmit and receive data over a serial connection. The UART protocol is implemented on many microcontrollers, including those used in Arduino boards.

UART in Arduino

UART is a standard communication protocol that is available in most microcontrollers, including those used in Arduino boards. While the basic concept of UART remains the same across different boards, there may be some variations in the specifics of its implementation, such as the number of hardware UART interfaces available on the board.

Here are some examples of how UART is implemented in different Arduino boards:

  1. Arduino Uno: The Arduino Uno has one hardware UART interface, which is connected to pins 0 (RX) and 1 (TX) of the board. This interface can be accessed using the Serial library in the Arduino IDE.
  2. Arduino Mega: The Arduino Mega has four hardware UART interfaces, which are connected to pins 0/1, 19/18, 17/16, and 15/14 of the board. These interfaces can be accessed using the Serial, Serial1, Serial2, and Serial3 libraries in the Arduino IDE.
  3. Arduino Nano: The Arduino Nano has one hardware UART interface, which is connected to pins D0 (RX) and D1 (TX) of the board. This interface can be accessed using the Serial library in the Arduino IDE.

Overall, the UART implementation in different Arduino boards may vary, but the basic functionality remains the same.

Example

To use the UART interface on an Arduino board, you can use the Serial library, which provides functions for initializing and using the UART interface. The library provides functions for setting the baud rate, transmitting and receiving data, and checking the status of the UART interface.

Here is an example of using the Serial library to send and receive data over the UART interface:

void setup() {
  Serial.begin(9600); // initialize serial communication at 9600 baud rate
}

void loop() {
  if (Serial.available() > 0) { // check if there is data available to read
    char data = Serial.read(); // read the incoming data
    Serial.print("Received data: ");
    Serial.println(data); // print the received data
  }

  Serial.println("Hello, world!"); // send a message over the serial interface
  delay(1000); // wait for a second before sending the next message
}

Explanation

Here’s a breakdown of what this code does:

  1. void setup() – this function runs only once during startup and is used to initialize settings for the program.
  2. Serial.begin(9600); – initializes the serial communication with the computer at a baud rate of 9600 bits per second.
  3. void loop() – this function runs repeatedly as long as the board is powered on.
  4. if (Serial.available() > 0) – checks if there is any data available to read from the serial port.
  5. char data = Serial.read(); – reads the incoming data from the serial port and stores it in a variable named data.
  6. Serial.print("Received data: "); – prints a message indicating that data has been received.
  7. Serial.println(data); – prints the received data to the serial monitor.
  8. Serial.println("Hello, world!"); – sends the message “Hello, world!” over the serial interface to the computer.
  9. delay(1000); – adds a delay of 1 second before sending the next message.

Inbuilt Functions related to Serial Communication

It is good to keep in mind all the inbuilt functions related to serial communication

  1. Serial.begin(baudrate): Initializes the serial communication with a specified baud rate.
  2. Serial.print(data): Prints data to the serial port as human-readable ASCII text.
  3. Serial.println(data): Prints data to the serial port as human-readable ASCII text and adds a newline character at the end.
  4. Serial.print(data, format): Prints data to the serial port using the specified format.
  5. Serial.read(): Reads a single byte of data from the serial port.
  6. Serial.available():Returns the number of bytes available to read from the serial port.
  7. Serial.write(data): Writes a single byte of data to the serial port.
  8. Serial.write(data, length): Writes a specified number of bytes to the serial port.
  9. Serial.flush(): Waits for the transmission of outgoing serial data to complete.
  10. Serial.setTimeout(timeout): Sets the maximum time to wait for incoming serial data in milliseconds.
  11. Serial.end(): Stops serial communication.
  12. Serial.find(): Reads data from the serial buffer until the specified string is found.
  13. Serial.findUntil(): Reads data from the serial buffer until the specified string is found or until the specified termination string is found.
  14. Serial.parseFloat(): Reads a floating point number from the serial buffer.
  15. Serial.parseInt(): Reads an integer from the serial buffer.
  16. Serial.peek(): Returns the next byte of incoming serial data without removing it from the buffer.
  17. Serial.readString(): Reads characters from the serial buffer into a string until a specified terminator character is found.
  18. Serial.readStringUntil(): Reads characters from the serial buffer into a string until a specified terminator string is found.

    Leave a Reply

    Your email address will not be published.

    Need Help?