Serial Communication in Arduino Uno

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3
At a glance
Powered by AI
The key takeaways are that serial communication allows data transmission between Arduino and PC over USB and it enables monitoring and control of Arduino projects from a computer.

Serial communication in Arduino works by transmitting data through the TX and RX pins to a computer or other device. The Arduino IDE serial monitor displays data sent from Arduino and allows sending data to Arduino.

Hardware serial uses dedicated RX and TX pins while software serial emulates serial communication on any digital pins using a library. Software serial is used when more serial ports are needed or to use non-standard pins.

Serial Communication in Arduino Uno

In this tutorial, we will explore the use of Serial Communication in Arduino Uno. Arduino has built-in support for UART which enable serial communication. UART as a

serial protocol is most useful and famous protocol. The Arduino can transmit and receive data to the PC over USB Cable. This comes handy when we want to

send the sensor data from microcontroller to PC.  The Arduino IDE has built-in Serial Monitor window, which displays the data sent from Arduino to PC. The same

way we can send data/command from Serial Monitor to Arduino. The serial communication enables us to control electronic devices connected to Arduino board from

PC. When comes to interfacing more complicated devices such as LCD, RTC, EEPROM etc. We can use serial communication to debug the code and track errors to

interface those devices.

Example Program 1: In this example, we will send a string from Arduino to PC. Once we upload the sketch into Arduino. We will see this string will be printing

on Serial Monitor Window of Arduino IDE.

1. void setup()
2. {
3. Serial.begin(9600);
4. }
5.
6. void loop()
7. {
8. Serial.println("Hello from Umesh");
9. delay(1000);
10. }

 Hardware Setup: We only need to connect Arduino Uno to PC over standard USB Cable. Once

we have done connection we are ready to upload the sketch to Arduino and open serial
monitor window to display data sent from Arduino. As we been transmitting string from

Arduino to PC. We will observe that LED connected to TX Pin on Arduino will light up.


Open Serial Monitor of Arduino IDE

Serial Monitor of Arduino Printing String


Example Program 2: In this example, we will transmit as well as receive data using Arduino. This program receives data from PC and then send it back to PC.

This way we can perform transmit and receive data using Arduino board.

1. int inByte; // Stores incoming command


2.
3. void setup()
4. {
5. Serial.begin(9600);
6. pinMode(13, OUTPUT); // Led pin
7. Serial.println("Type 1: LED ON, 0: LED OFF "); // Ready to receive commands
8. }
9.
10. void loop()
11. {
12. if (Serial.available() > 0)
13. { // A byte is ready to receive
14. inByte = Serial.read();
15. if (inByte == '1') { // byte is '1'
16. digitalWrite(13, HIGH);
17. Serial.println("LED - On");
18. } else { // byte isn't '1'
19. digitalWrite(13, LOW);
20. Serial.println("LED - off");
21. }
22. }
23. }

Now just verify and upload the sketch to Arduino and open up Serial Monitor. We are ready to control LED connected PIN 13 of Arduino. Now just enter ‘1’ and press

Send button. We will see on board orange LED will turn ON. When we enter ‘0’ and hit Send button LED will turn OFF. This is how we can control devices connected to

Arduino using a serial interface.


Now you might be thinking what if we need more serial ports in our project. In that case, we will use

Software Serial Library. Also, SoftwareSerial will be used when we want to connect via serial

communication on some other digital pins of Arduino. It allows our program to emulate serial
input/output in software instead of dedicated hardware pins.

Software Serial Example: In this example, we’ll be using PIN 10 and 11 as Rx, Tx respectively. Whereas Pin No. 0 and 1 which are default serial pins on

Arduino hardware.

1. #include <SoftwareSerial.h> /* Connect device Rx - 10, Tx - 11 */


2.
3. SoftwareSerial mySerial(10, 11);
4.
5. void setup()
6. {
7. Serial.begin(9600); // Open serial communications and wait for port to open:
8. mySerial.begin(4800); // set the data rate for the SoftwareSerial port
9. }
10.
11. void loop()
12. {
13. Serial.println("HELLO"); // Sends string to PC to Serial Monitor
14. mySerial.println("HELLO"); // Sends string to device
15. delay(1000); // Waits here 1000ms and then goes on
16. }

This is how Serial Communication in Arduino works. We hope this tutorial will help you to understand serial communication. There will be several projects where

we will use this feature serial UART in Arduino to debug the code. We recommend you to play around code and see how it works. If you have any suggestions, feel

free to leave a comment. Thanks.

Get Free Courses & Webinars


You'll receive only high quality learning material, tips & tricks

Email Address

Get FREE Access

I agree to have my personal information transfered to MailChimp ( more information )

We respect your privacy

Login
Username

Password

Remember Me
Log In

Register | Lost your password?


| Back to Login

You might also like