Serial Communication - Sketch Programming - Arduino Development for OSX and iOS (2015)

Arduino Development for OSX and iOS (2015)

2. Sketch Programming

2.6 Serial Communication

Arduino Uno has one UART which is represented as RX (digital pins 0) and TX (digital pins 1). It’s used for communication between the Arduino board and a computer or other devices. Arduino Mega has three additional serial ports: Serial1 on pins 19 (RX) and 18 (TX), Serial2 on pins 17 (RX) and 16 (TX), Serial3 on pins 15 (RX) and 14 (TX).

To access UART on Arduino board, we can use Serial object, https://www.arduino.cc/en/Reference/Serial .

int led = 13;

void setup() {

Serial.begin(9600);

pinMode(led, OUTPUT);

}

void loop() {

Serial.println("LED: HIGH");

digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)

delay(1000); // wait for a second

Serial.println("LED: LOW");

digitalWrite(led, LOW); // turn the LED off by making the voltage LOW

delay(1000); // wait for a second

}

Save this code, called SerialDemo.ino.

Compile and upload the program. To see program output, open Serial Monitor tool. Click menu Tools -> Serial Monitor.

ch2-9

After clicked, you should see the program output.

ch2-10