Arduino Serial Communication uses the serial protocols to communicate with the computer and to receive programs, through the two digital pins 0 and 1 (TX and RX); in the printed circuit they are connected to the FTDI chip, which takes care of converting the TX / RX signal into USB protocol.

1. CONCEPTS

There are two ways to transmit data from one end to the other

a. Parallel Communication

Parallel communication is based on interfaces that are able to transfer multiple bits at the same time, using what is called a data bus. These data buses are generally transferred through bundles of 8, 16, 32, … cables on which the signals only assume binary values such as 0 and 1. We, therefore, speak of 8, 16, 32-bit buses. In addition to these cables, there is an additional one called CLK (clock) whose purpose is to mark the moment in which the bits must be read (bytes).

Figure Parallel Communication
Figure Parallel Communication

b. Serial Communication

A serial port has two lines through which current data is transmitted. These are the lines for transferring data from the terminal (PC) to the communication device and vice versa. In addition, there are several control lines. To function properly, the serial ports rely on a specific control chip called Universal Asynchronous Receiver / Transmitter (UART) which supports a very high data transfer rate, reaching 115,000 baud (bit / s). (However, it is worth noting that actual data transfer depends on the characteristics of both communication devices.) In addition, the UART controller converts the parallel code into serial and vice versa.

Figure: Serial Communication
Figure: Serial Communication

Arduino IDE has an inbuild Serial Monitor which can be used to visualise received data or to transmit data serially. This can be accessed using the magnifier icon on the top right side of Arduino IDE by pressing ctrl+shift+M (Windows). We will use this Serial Monitor to transmit data from the keyboard. Serial Communication can be started by writing Serial.begin(9600) under the void setup(). This will start Serial communication at a baud rate of 9600. You can also choose any other baud rate like 300, 1200, 2400, 2800, …, 2000000 etc.

2. COMPONENTS

3. CONNECTIONS

Figure 1: LED connections with Arduino

3. PROGRAM

Program: Control LED through Serial Communication using keyboard

/*UNCIA ROBOTICS | www.unciarobotics.com
  PROGRAM: SERIAL COMMUNICATION | CONTROL LED USING KEYBOARD
  This program reads the key press from keyboard and turn an LED ON or OFF.

  Connections:
  13    LED(+)
  GND   LED(-)
*/
char data;                    //variable to store data received
const int LED = 13;           //LED pin connection
void setup() {
  Serial.begin(9600);         // Start Serial communication at 9600 baud
  pinMode(LED, OUTPUT);       // sets the digital pin 13 as output
}

void loop() {
  data = Serial.read();       //read Serial data

  if (data == 'a')            //check for alphabet 'a'
  { Serial.println("LED ON"); //Print "LED ON" on Serial Monitor
    digitalWrite(LED, HIGH);  //Turn ON the LED
  }

  if (data == 'b')            //check for alphabet 'b'
  { Serial.println("LED OFF");//Print "LED OFF" on Serial Monitor
    digitalWrite(LED, LOW);   //Turn OFF the LED
  }
}

4. FUNCTIONS USED


const

const is an abbreviation for the word constant. It’s a Variable qualifier which makes any variable read-only. You can use it like any other variable but you cannot change its value in the program. It’s a good practice to name your pins using a constant qualifier.


Serial.begin( )

Description: Starts Serial communication at a given baud rate. To communicate with computer you can use these baud rates: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. You can however specify any other baud rate connected at TX and RX pins of Arduino

Syntax: Serial.begin(baud); or Serial.begin(baud, config);

  • Serial: which serial port you are using. Depends on the device you are using.
  • baud: speed in bits per second | Allowed data types: long
  • config: sets data, parity, and stop bits. (see list)

Returns: Nothing

Notes and Warnings:

  • Different microcontrollers can have multiple Serial ports (see list). You can use them by calling them with the given name.

Serial.println( )

Description: Prints data on Serial monitor as a human-readable form followed by a carriage return character and a newline character.

Syntax: Serial.print( (val, base);

Returns: number of bytes written, though reading that number is optional. Data typesize_t

Notes and Warnings:

  • This command takes the same forms as Serial.print( ).

pinMode( )

Description: Configure specific pin to behave either as input or output.

Syntax: pinMode (pin,MODE);

  • pin: the Arduino pin number to set the mode of.
  • MODE: INPUT, OUTPUT, or INPUT_PULLUP.

Returns: Nothing

Notes and Warnings:

  • It is only applicable to digital pins.

digitalWrite( )

Description: Write a HIGH or LOW value to a digital pin.

Syntax: digitalWrite (pin,VALUE);

  • pin: the Arduino pin number.
  • value: HIGH or LOW

Returns: Nothing

Notes and Warnings:

  • It is very important to set pinMode() as OUTPUT first before using digitalWrite() function on that pin.
  • If you do not set the pinMode() to OUTPUT, and connect an LED to a pin, when calling digitalWrite(HIGH), the LED may appear dim.
  • You can use analog Input pins (A0, A1, … etc) as digital pins. With the exception of Arduino Nano, Pro Mini, and Mini’s A6 and A7 pins.

5. FAQ’s

How to open Serial Monitor in Arduino IDE?
  • In windows you can press Ctrl+shift+M to open Serial Monitor
Figure: Arduino IDE
  • In MAC OS, Serial Monitor can be accessed by pressing Cmd+shift+M
  • When Arduino IDE is open, look for a magnifier icon on top right side of the window. Click it to open Serial Monitor.
Arduino Serial Monitor
Figure: Arduino Serial Monitor
What is the use of No line ending, Newline, Carriage return, Both NL & CR in Serial Monitor?

They do what they say.

  • “No line ending” just sends what you put.
  • “Newline” sends an ASCII newline code after what you put.
  • “Carriage return” puts an ASCII carriage return character after what you put.
  • “Both NL & CR” puts both a carriage return and a new line character after what you put.
Why we use serial begin in Arduino?

Serial.begin() doesn’t really print anything. Rather it initializes the serial connection at the mentioned baud rate inside its brackets. Both sides of the serial connection (i.e. the Arduino and your computer) need to be set to use the same speed serial connection in order to get any sort of understandable data.

Still, Having Doubts?

Ask your questions for Arduino computer keyboard LED in the comments section below or Contact Us.

Leave a Reply