Serial Communication

Introduction

Serial communication in Arduino is a way to send and receive data between the Arduino board and other devices such as computers, sensors, or other microcontroller. It is usually used for debugging and monitoring.

Characteristics

  1. Arduino has built-in UART communication on pins 0 (RX) and 1 (TX).

  2. Serial communication is used to send data serially, byte-to-byte.

  3. for Arduino is 9600 or 115200. (9600 is default and most commonly used)

Usage

// In setup()
void Serial.begin(baudrate);

// In loop()
void Serial.print(string);
void Serial.println(string);

Examples

Timer Demo

This is a 2-second cycle. The LED will turn ON for the first 1 second of the cycle and OFF for the second second.

void setup() {
    pinMode(13, OUTPUT);
    Serial.begin(9600);
}

int count = 0;
void loop() {
    Serial.println(count);
    if ((millis() - count) < 1000) {
        digitalWrite(13, HIGH);
    } else {
        digitalWrite(13, LOW);
    }
    if ((millis() - count) >= 2000) {
        count = millis();
    }
    delay(10);
}

The delay(10) at the end of the code helps to avoid rapid cycling and ensures stable behavior.

Last updated