Introduction: 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
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 A5Name:- 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
The project is successfully simulated by using the Proteus.
The serial port can be used for many embedded projects as communication port