Serial Communication in Arduino Uno

Diazonic Labs
3 min readJul 6, 2020

Serial Communication is the process of sending or receiving information between data processing equipment. It sends/receives the data one bit at a time. Data is exchanged between laptop and Arduino Uno through the process of Serial communication.

This happens

  1. When the code is getting uploaded from laptop to the Arduino Microcontroller.
  2. When we want to print something on the Serial Monitor

With Serial Communication, 2 Arduinos can communicate with each other. Arduino can also communicate with Bluetooth module or Wifi Module. Most of the times Arduino uses Hardware Serial Pins (Pin 0 and Pin 1) for Serial communication and that’s the reason both this pins should be left empty without any contact while uploading the code.

However we can use any digital pin in Arduino as Rx and Tx pin using Software Serial library.

Now lets see a simple working of Serial Communication by manually controlling RGB LED

There are multiple Serial functions available in Arduino software. Let’s see a couple of them which will be used in the project.

  1. Serial.begin() — Sets the Baud rate for beginning of Serial Communication. By default, we keep 9600 as Baud Rate

More on Baud Rate : https://www.arduino.cc/en/Serial.Begin#:~:text=Sets%20the%20data%20rate%20in,38400%2C%2057600%2C%20or%20115200.

2. Serial.available() — Get the number of bytes (characters) available for reading from the serial port.

3. Serial.read() — Reads incoming serial data

Circuit Diagram :

Hardware components used:

  1. Arduino Uno
  2. RGB LED( Common Cathode)
  3. Resistors( 3)
  4. Arduino Cable

We can program this via Arduino IDE . Anyways I have simulated this on Tinkercad.

void setup()
{
pinMode(9, OUTPUT);//Green
pinMode(10, OUTPUT);//Blue
pinMode(11, OUTPUT);// Red
Serial.begin(9600);
}

Any Arduino program contains 2 main functions. void setup() and void loop().

RGB LED consists of 4 pins. I have connected 3 positive pins to digital pins 9,10,11 of Arduino Uno. Using pinMode we can define the GPIO pins to be OUTPUT pins

if ( Serial.available())
{
char state = Serial.read();

Serial. available() returns the number of characters (i.e. bytes of data) which have arrived in the serial buffer and that are ready to be read.

The value which is read from Serial is stored in a variable named state.

To get Green color, voltage has to be supplied to only one pin and hence it is turned HIGH. Rest all the other pins are turned LOW.

if(state == 'G')
{
digitalWrite(9,1);
digitalWrite(10,0);
digitalWrite(11,0);
}

The similar procedure can be followed for getting other colors as well.

For the complete code, please visit:

https://github.com/diazonic/Arduino-and-Sensors-Interfacing/blob/master/Serial%20Communication%20-%20RGB.ino

--

--

Diazonic Labs

Internet of Things, Cloud Computing, Edge Computing, Artificial Intelligence