Arduino Tutorial

By LiterallyTheOne

4: Serial Communication

Arduino Tutorial, Serial Communication

Introduction

  • Previous tutorial: LCD and Keypad
  • This tutorial: Serial Communicaion
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Serial Communication

Serial Communication

  • Send
  • Recive
  • One bit
  • One of the most important concept in microcontrollers
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Serial Communication

Usecase

  • Send data to:
    • Computer
    • Microcontroller
    • modules (e.g., Blutooth, GPS)
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Serial Communication

UART

  • Universal Asynchronous Receiver-Transmitter
  • The way Arduino handles serial Communication
  • Needs two pins:
    • Recieve data (RX)
    • Transmit data (TX)
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Serial Communication

UART pins in Arduino

  • RX: pin 0
  • TX: pin 1
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Serial Communication

Baud rate

  • Set baud rate correctly to have a communication
  • Speed of data transfer
  • Asynchronous
  • Has a start and end bit
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Serial Communication

Serial Terminal on SimulIDE

  • TX of Arduino -> RX of Serial Terminal
  • RX of Arduino -> TX of Serial Terminal
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Serial Communication

Serial Hello World

  • Set a baud rate and intitialize
Serial.begin(9600);
  • Write on Serial Terminal
Serial.println("Hello World");
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Serial Communication

Full code

#include <Arduino.h>

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.println("Hello World");
  delay(1000);
}
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Serial Communication

Output Hello World

hello world

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Serial Communication

Read from Serial Termianl

if (Serial.available())
{
    char ch = Serial.read();
    Serial.println(ch);
}
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Serial Communication

Output of reading from terminal

Read serial

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Serial Communication

Read until new line

if (Serial.available())
{
    String result = Serial.readStringUntil('\n');
    Serial.println(result);
}
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Serial Communication

Output of read until new line

Read until new line

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Serial Communication

Built-in serial monitor in SimulIDE

  • mega328/Open Serial Monitor/USart
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Serial Communication

Software Serial

  • Hardware serial communication:
    • 0
    • 1
  • If we want to use other pins for serial communication
    • We use Software Serial
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Serial Communication

Software Serial usage

  • Include
#include <SoftwareSerial.h>
  • Make a serial object
SoftwareSerial mySerial(10, 11); // RX, TX
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Serial Communication

Full code Software Serial

#include <Arduino.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

void setup()
{
  mySerial.begin(9600);
}

void loop()
{
  mySerial.println("Hello SoftwareSerial!");
  delay(1000);
}
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Serial Communication

Output of Software Serial

Software Serial

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Serial Communication

Two Arduinos

Two Arduinos

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Serial Communication

By Ramin Zarebidoky (LiterallyTheOne)