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
Arduino has built-in UART communication on pins
0
(RX) and1
(TX).Serial communication is used to send data serially, byte-to-byte.
for Arduino is
9600
or115200
. (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);
}
Last updated