Introduction: Arduino - Serial Port Read / Write

Picture of Arduino - Serial Port Read / Write

Abstract

The Universal Asynchronous Receiver / Transmitter (UART) is the main communication subsystem for the embedded system design. Asynchronous transmission allows data to be transmitted without the sender having to send a clock signal to the receiver. Instead, the sender and receiver must agree on timing parameters in advance and special bits are added to each word which are used to synchronize the sending and receiving units. The LCD display [16x2 LCD] is used for displaying the received UART bytes. This intractable is about transmitting / receiving data over UART with Arduino microcontroller.

Parts and components

Arduino Uno board

16x2 LCD

1 K ohm potentiometer

Step 1: Schematic

Picture of Schematic

The 16x2 is very common type LCD, with two rows, and each row displays 16 characters of either 5x7 or 5x8 dot matrix characters.

The LCD is available in a 16 pin package. It consists of back light and contrast adjustment function and each dot matrix has 5×8 dot resolution.

The 16x2 LCD display is connected to the Arduino (A0,A1,A2,A3,A4,A5) analog IO pins, where those pins are configured as digital in / out, where LCD operates at 4 bit data mode.

If the display is not visible, adjust the Contrast pot (1K), to make it visible.

The Arduino hardware serial port is configured for 9600, 8N1 mode / baud rate.

The Arduino serial port is connected to Proteus COMport Physical Interface Module (COMPIM) and configured for 9600, 8N1 mode / baud rate.

The com0com null modem emulator is used for creating the virtual serial ports.

COMPIM serial data is passed to com0com virtual serial ports and monitored at the PC serial terminal

Step 2: Software

The Arduino LiquidCrystal library is used for LCD device. This display will be used for displaying the received serial port string.

Every 500mS a test string is send via serial port, which will be received and printed on serial port monitoring tool running at the PC.

/*
Demonstrates the use of a serial port. Serial received bytes are displayed on 16x2 LCD screen. The Arduino circuit connection for LCD: * LCD RS pin to analog pin A0 * LCD Enable pin to analog pin A1 * LCD D4 pin to analog pin A2 * LCD D5 pin to analog pin A3 * LCD D6 pin to analog pin A4 * LCD D7 pin to analog pin A5

Name:- M.Pugazhendi Date:- 08thAug2016 Version:- V0.1 e-mail:- muthuswamy.pugazhendi@gmail.com */

// include the library code: #include <LiquidCrystal.h>

//Initialize the library with the numbers of the interface pins LiquidCrystal lcd(A0,A1,A2,A3,A4,A5);

// a string to hold incoming data String inputString = "";

// whether the string is complete boolean stringComplete = false;

void setup() { //Initialize serial port Serial.begin(9600); // set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print a message to the LCD. lcd.print("Serial TEST");

// reserve 200 bytes for the inputString: inputString.reserve(200); }

void loop() { Serial.write("This a test \n");

// print the string when a newline arrives: if (stringComplete) { //lcd clear lcd.clear(); lcd.print(inputString); // clear the string: inputString = ""; stringComplete = false; } //500smS Delay delay(500); }

/* SerialEvent occurs whenever a new data comes in the hardware serial RX. This routine is run between each time loop() runs. 500mS delay inside loop can delay response. Multiple bytes of data may be available. */ void serialEvent() { while (Serial.available()) { // get the new byte: char inChar = (char)Serial.read(); // add it to the inputString: inputString += inChar; // if the incoming character is a newline, set a flag // so the main loop can do something about it: if (inChar == '\n') { stringComplete = true; } } }

Step 3: Conclusion

Picture of Conclusion

The project is successfully simulated by using the Proteus.

The serial port can be used for many embedded projects as communication port

Comments

SoakedinVancouver (author)2016-08-13

So, the Proteus runs on a PC, to which the Arduino is connected? I am confused about this, I am obviously missing something...

PugazhM (author)2016-08-13

Proteus is a simulation tool, and com0com is virtual serial port software. For communicating from simulator to virtual com (com0com) port, Proteus provides the COMPIM. Anyway with out the com0com and COMPIM, if one program the Arduino with provided software, and connect the serial monitor tool via USB, the Arduino will send / receive the packets. Its a test program, so fixed baud rate of 9600 is chosen. But the Arduino can be programmed between 2400 to 115200 bits per second.

wstewl (author)2016-08-12

This looks interesting, but I see no information about the additional components required. What is a COMPIN ? where do you get one ? how do you configure it? (in one diagram you show Tx & Rx both in and out ports, in another there is only one set of connections) What is a Com-0-Com virtual port? Where do you get one? how do you use it? What is the "Proteus"?

Why was the port set to only 9600? What is the limiting part?

AndyR47 (author)wstewl2016-08-13

@wstewl - Yes, I'll second that!