Printing From Serial Monitor To Lcd Made Easy

how to print from serial monitor to lcd

The Liquid Crystal Library allows you to control LCD displays that are compatible with the Hitachi HD44780 driver. You can print from the serial monitor to an LCD by first uploading the sketch, then opening the Serial Monitor and typing some characters before clicking Send. The text will then appear on your LCD.

Characteristics Values
Required hardware Adafruit Standard LCD - 16x2 White on Blue
Rotary potentiometer
Jumper wires
Arduino MEGA 2560 rev3
I2C 20×4 LCD Display
Arduino MEGA sensor shield
10k ohm potentiometer
Computer running Arduino Software (IDE)
Required software Arduino IDE app
Liquid Crystal Library
Steps Connect the board to your computer
Use the Arduino Software (IDE) serial monitor to communicate with it
Upload the sketch
Open the Serial Monitor and type some characters and click Send

shundigital

How to print from serial monitor to LCD using Arduino

The Liquid Crystal Library allows you to control LCD displays that are compatible with the Hitachi HD44780 driver. You can usually identify these by their 16-pin interface. To use it, you need to upload the sketch, open the Serial Monitor, type some characters, and click "Send". The text will then appear on your LCD screen.

Before wiring the LCD screen to your Arduino board, it is suggested to solder a pin header strip to the 14 or 16-pin connector of the LCD screen. To wire your LCD screen to your board, connect the following pins:

  • LCD RS pin to digital pin 12
  • LCD Enable pin to digital pin 11
  • LCD D4 pin to digital pin 5
  • LCD D5 pin to digital pin 4
  • LCD D6 pin to digital pin 3
  • LCD D7 pin to digital pin 2

Additionally, wire a 10k pot to +5V and GND, with its wiper (output) to the LCD screen's VO pin (pin 3). A 220-ohm resistor is typically used to power the backlight of the display, which is usually on pin 15 and 16 of the LCD connector.

To communicate with your Arduino board, you need to connect the board to your computer and use the Arduino Software (IDE) serial monitor.

#include

LiquidCrystal lcd(52, 50, 48, 46, 44, 42);

Int byteRead;

Void setup() {

Serial.begin(9600);

Serial1.begin(9600);

//INTIALISING THE SERIAL PORT

Lcd.begin(16, 2);

}

Void loop() {

//this is listing to print data string to Serial

DataRaspi = "$" + data_yaw + "|" + data_pitch + "|" + data_roll + "|" + data_lat + "|" + data_lon + "|" + data_airSpd + "|" + data_alt_qnh + "#";

Serial.println(dataRaspi);

Delay(1);

//this is listing to read data from Serial1

If(Serial1.available()) {

While (Serial1.available()) {

Lcd.print(Serial1.read());

}

}

In this code, `dataRaspi` is a string that contains the data to be printed to the serial monitor. The `delay(1)` function call adds a small delay between printing each character. The `if(Serial1.available())` condition checks if there is any data available to be read from `Serial1` before reading and printing it to the LCD display using the `lcd.print()` function.

To display text and numbers from your sketch on a PC or Mac via a serial link, put the `Serial.begin(9600)` statement in the setup function, and then use `Serial.print()` statements to print the text and values you want to see.

You can also use the Arduino Serial Monitor function to display serial data sent from the Arduino. To start the Serial Monitor, click the Serial Monitor toolbar icon. A new window will open for displaying output from the Arduino.

The sketch must call the `Serial.begin()` function before it can use serial input or output. This function takes a single parameter: the desired communication speed, which must be the same for both the sending and receiving sides. This example uses a speed of 9,600 baud, which is approximately 1,000 characters per second.

You can display text using the `Serial.print()` function. Strings (text within double quotes) will be printed as is (but without the quotes). For example, the following code:

Serial.print("The number is" );

The values (numbers) that you print depend on the type of variable. For instance, printing an integer will print its numeric value, so if the variable `number` is 1, the following code:

Serial.println(number);

In the example sketch, the number printed will be 0 when the loop starts and will increase by one each time through the loop. The `ln` at the end of `println` causes the next print statement to start on a new line.

You can also print data to the serial port in many different formats. Here is a sketch that demonstrates all the format options:

/*

  • SerialFormatting
  • Print values in various formats to the serial port
  • /

Char chrValue = 65; // these are the starting values to print

Int intValue = 65;

Float floatValue = 65.0;

Void setup() {

Serial.begin(9600);

}

Void loop() {

Serial.println("chrValue": );

Serial.println(chrValue);

Serial.println(chrValue, BYTE);

Serial.println(chrValue, DEC);

Serial.println("intValue": );

Serial.println(intValue);

Serial.println(intValue, BYTE);

Serial.println(intValue, DEC);

Serial.println(intValue, HEX);

Serial.println(intValue, OCT);

Serial.println(intValue, BIN);

Serial.println("floatValue": );

Serial.println(floatValue);

Delay(1000); // delay a second between numbers

ChrValue++; // to the next value

IntValue++;

}

The output of this sketch will be:

ChrValue: A

A

65

IntValue: 65

A

65

41

101

1000001

FloatValue: 65.00

ChrValue: B

B

66

IntValue: 66

B

66

42

102

1000010

Floatvalue: 66.00

Printing a text string is simple: `Serial.print("hello world");` sends the text string "hello world" to a device at the other end of the serial port. If you want your output to print a new line after the output, use `Serial.println()` instead of `Serial.print()`.

Printing numeric values can be more complicated, as the way that byte and integer values are printed depends on the type of variable and an optional formatting parameter.

Char asciiValue = 'A'; // ASCII A has a value of 65

Char chrValue = 65; // an 8-bit character, this also is ASCII 'A'

Int intValue = 65; // a

shundigital

Using LiquidCrystal_I2C.h library

Using the LiquidCrystal_I2C.h Library

The LiquidCrystal_I2C library is used to control I2C LCDs with Arduino. The library is compatible with both the ESP32 and ESP8266 boards.

To install the library, follow these steps:

  • Click here to download the LiquidCrystal_I2C library. You should have a .zip folder in your Downloads.
  • Unzip the .zip folder and you should get the LiquidCrystal_I2C-master folder.
  • Rename your folder from LiquidCrystal_I2C-master to LiquidCrystal_I2C.
  • Move the LiquidCrystal_I2C folder to your Arduino IDE installation libraries folder.
  • Finally, re-open your Arduino IDE.

To use the library, you need to include it in your code:

#include

Next, you need to create a LiquidCrystal_I2C object with the I2C address, the number of columns, and the number of rows:

LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns, 2 rows

In the setup() function, you need to initialize the LCD and turn on the backlight:

Void setup() {

Lcd.init(); // initialize the LCD

Lcd.backlight(); // turn on the backlight

}

Now, you can start printing text to the LCD. First, set the cursor to the desired position:

Lcd.setCursor(column, row);

Then, print your message:

Lcd.print("Hello, World!");

Here's an example of how to display "Hello, World!" on the first row, and then in the second row:

#include

// set the LCD number of columns and rows

Int lcdColumns = 16;

Int lcdRows = 2;

// set LCD address, number of columns and rows

// if you don't know your display address, run an I2C scanner sketch

LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);

Void setup(){

// initialize LCD

Lcd.init();

// turn on LCD backlight

Lcd.backlight();

}

Void loop(){

// set cursor to first column, first row

Lcd.setCursor(0, 0);

// print message

Lcd.print("Hello, World!");

Delay(1000);

// clears the display to print new message

Lcd.clear();

// set cursor to first column, second row

Lcd.setCursor(0,1);

Lcd.print("Hello, World!");

Delay(1000);

Lcd.clear();

}

You can also scroll text on the LCD, which is useful when you want to display messages longer than the number of columns. Here's an example of how to scroll a message longer than 16 characters:

#include

// set the LCD number of columns and rows

Int lcdColumns = 16;

Int lcdRows = 2;

// set LCD address, number of columns and rows

// if you don't know your display address, run an I2C scanner sketch

LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);

String messageToScroll = "This is a scrolling message with more than 16 characters";

// Function to scroll text

// The function accepts the following arguments:

// row: row number where the text will be displayed

// message: message to scroll

// delayTime: delay between each character shifting

// lcdColumns: number of columns of your LCD

Void scrollText(int row, String message, int delayTime, int lcdColumns) {

For (int i=0; i < lcdColumns; i++) {

Message = " " + message;

}

Message = message + " ";

For (int pos = 0; pos < message.length(); pos++) {

Lcd.setCursor(0, row);

Lcd.print(message.substring(pos, pos + lcdColumns));

Delay(delayTime);

}

}

Void setup(){

// initialize LCD

Lcd.init();

// turn on LCD backlight

Lcd.backlight();

}

Void loop(){

// set cursor to first column, first row

Lcd.setCursor(0, 0);

// print static message

Lcd.print("Static message");

// print scrolling message

ScrollText(1, messageToScroll, 250, lcdColumns);

}

You can also display custom characters on the LCD. Each character is made up of a 5x8 pixel matrix. You can use a custom character generator tool to create your custom character, which will give you the byte array for your character.

Here's an example of how to create and display a custom character:

#include

LiquidCrystal_I2C lcd(0x27, 16, 2);

Byte customChar[8] = {

0b00000, 0b01010, 0b11111, 0b11111, 0b01110, 0b00100, 0b00000, 0b00000

};

Void setup() {

Lcd.init(); // initialize the LCD

Lcd.backlight();

Lcd.createChar(0, customChar); // create a new custom character

Lcd.setCursor(2, 0); // move cursor to (2, 0)

Lcd.write((byte)0); // print the custom char at (2, 0)

}

Void loop() {

}

shundigital

Connecting an I2C LCD to a MEGA

To connect an I2C LCD to a MEGA, you will need the following:

  • Arduino MEGA 2560 rev3
  • I2C 20x4 LCD Display
  • Arduino MEGA sensor shield
  • Dupont Wires
  • Personal Computer with Arduino IDE installed

The I2C LCD is ideal for displaying text, numbers, and special characters. It has a small add-on circuit (backpack) mounted on the back of the LCD module, which includes a controller chip for I2C communication and a potentiometer for adjusting the LED backlight intensity.

Hardware Wiring Instructions: On the back of the I2C 20x4 LCD Display, there should be a daughter card with 4 I2C pins. Connect these pins to the MEGA using the following pinout:

  • I2C Daughter Card Pin 1 (GND) to MEGA GND
  • I2C Daughter Card Pin 2 (VCC) to MEGA 5V
  • I2C Daughter Card Pin 3 (SDA) to MEGA Analog Pin 4 (SDA)
  • I2C Daughter Card Pin 4 (SCL) to MEGA Analog Pin 5 (SCL)
  • Contrast Adjustment: Power your Arduino, which will also power the LCD. Locate the adjustable potentiometer on the I2C interface board and use a small screwdriver to adjust it until you see a series of rectangles on the LCD. This will allow you to see your programming results.
  • Install the LiquidCrystal_I2C Library: The Arduino module and editor do not inherently know how to communicate with the I2C interface on the LCD. You need to install the LiquidCrystal_I2C library to enable the Arduino to send commands to the LCD. You can download this library from GitHub or directly from the Craig Lyn Design Studio website. Before installing, remove any other libraries with the same name. This library works in combination with the pre-installed Wire.h library in the Arduino editor.
  • Determine the I2C Address of the LCD: Not all I2C adapters have the same address. Most use the default address of 0x27, but some may use 0x20 or other addresses. You can change the I2C address by shorting the address solder pads on the I2C module. To find the address, use a simple Arduino sketch that scans the I2C bus and displays the address in the serial monitor. You can find this sketch on the Craig Lyn Design Studio website.
  • Basic Arduino Sketch: Once you have the LCD connected and know the I2C address, you can write code to display on the screen. The code will include including the LiquidCrystal_I2C library, creating an LCD object with the LiquidCrystal_I2C class and specifying the address and dimensions, initializing the LCD and turning on the backlight, setting the cursor position, and printing text to the LCD.

For more details and example code, refer to the Craig Lyn Design Studio website and the provided links.

shundigital

Wiring an LCD screen to an Arduino board

Components Required:

  • Arduino or Genuino Board
  • LCD Screen (compatible with Hitachi HD44780 driver)
  • Pin headers to solder to the LCD display pins
  • 10k ohm potentiometer
  • Jumper wires

Wiring Instructions:

  • Solder a pin header strip to the LCD screen: Before beginning the wiring process, it is recommended to solder a pin header strip to the 14 or 16 pin count connector of the LCD screen. This will make it easier to connect the LCD screen to the Arduino board.
  • Connect the LCD to the Arduino board:
  • LCD VSS pin to Arduino GND
  • LCD VDD pin to Arduino 5V
  • LCD RS pin to digital pin 12
  • LCD RW pin to Arduino GND
  • LCD Enable pin to digital pin 11
  • LCD D4 pin to digital pin 5
  • LCD D5 pin to digital pin 4
  • LCD D6 pin to digital pin 3
  • LCD D7 pin to digital pin 2

Adjust the display contrast:

Wire a 10k ohm potentiometer to +5V and GND, with its wiper (output) connected to the LCD screen's VO pin (usually pin 3). Adjusting the potentiometer will allow you to fine-tune the display contrast to your desired level.

Power the LCD backlight:

Use a 220-ohm resistor to power the backlight of the LCD display. Connect the resistor between pin 15 (A+) and pin 16 (K-) of the LCD connector.

Additional Notes:

  • The LCD screen should have a parallel interface, meaning multiple pins need to be controlled simultaneously by the microcontroller.
  • The LiquidCrystal Library simplifies the process of controlling the LCD display by providing functions to manipulate the display.
  • The LCD screens typically used with Arduino have 16 pins and are compatible with the Hitachi HD44780 driver.
  • The LCD can be wired in 4-bit mode or 8-bit mode. 4-bit mode is usually preferred as it uses fewer wires, and there is no noticeable difference in performance between the two modes.

shundigital

Troubleshooting serial monitor issues

If you're encountering problems with your serial monitor, there are several troubleshooting steps you can take to resolve the issue. Here are some common solutions:

  • Verify the communication ports: Ensure that the Arduino and the Arduino IDE are connected to the same communication port. Unplugging and replugging the Arduino can sometimes switch the ports. A simple fix is to re-upload the sketch from the same IDE that you are using to open the Serial Monitor.
  • Match the baud rate: The Serial Monitor on the computer and the Serial Port on the Arduino must have the same data rate, also known as the baud rate. Typically, the baud rate is set to 9600, so the programming statement would be Serial.begin(9600). Ensure that the Serial Monitor is set to the same value, which can be adjusted in the dropdown box in the lower-right corner.
  • Check programming statement execution: The Serial.begin() and Serial.println() programming statements must be executed. Sometimes, these statements may be missed due to branching statements such as if and else. If these statements are not executed, the Serial Port will not function as expected.
  • Inspect for syntax errors: Examine the Serial.print() and Serial.println() statements for any syntax errors. Since print and println are common in various programming languages, the usage may differ from one language to another. Ensure that the syntax is compatible with Arduino.
  • Check wiring and digital pins: Inspect the wiring of any circuit connected to the Arduino. Digital pins 0 and 1 should not be utilised as they are dedicated to the components on the Arduino that perform serial text communication and cannot be used simultaneously for other functions.
  • Ensure proper board selection: Incorrect board selection in your Integrated Development Environment (IDE) can lead to communication issues between your computer and the board. Make sure to select the board that matches your hardware.
  • Verify the USB port: Ensure that you are using a functioning USB port to connect the board to your computer. Sometimes, a faulty or incompatible USB port can cause the Serial Monitor to malfunction. If using a USB hub, try connecting the board directly to a USB port on your computer to rule out any potential issues.
  • Update drivers: Outdated or missing drivers can prevent the Serial Monitor from functioning correctly. Check if the necessary drivers for your board are installed and up to date. You can usually find the latest drivers on the manufacturer's website.
  • Check cable integrity: Ensure the integrity of your USB cable and verify that it is securely connected to both the board and the computer's USB port. Try using a different cable to see if the problem persists.
  • Adjust screen resolution: If you are unable to view the Serial Monitor, try adjusting your screen resolution. It may be positioned off-screen, affecting its visibility.

By following these troubleshooting steps, you should be able to resolve most issues related to the serial monitor. If the problem persists, further investigation or seeking support from relevant forums or communities may be necessary.

Frequently asked questions

First, make sure you have all the necessary components and tools. Then, open the Arduino IDE app, click "Serial Monitor", and paste the code into the IDE. Next, build the project from the schematics and plug your Arduino into the computer. Now, run the program and type something into the Serial Monitor.

You can use the Liquid Crystal Library, which allows you to control LCD displays that are compatible with the Hitachi HD44780 driver. You can also use the following code:

```c++

#include

LiquidCrystal lcd(1, 2, 4, 5, 6, 7);

int x = 10;

void setup() {

lcd.begin(16, 2);

Serial.begin(9600);

}

void loop() {

x++;

lcd.setCursor(0, 0);

lcd.print(x);

Serial.print(x); // print Serial

}

```

One issue is that the serial monitor may print question marks instead of the intended output. This can be due to conflicts with the pins used by the LCD and the serial monitor. To resolve this, use different pins for the LCD and adjust the wiring accordingly.

Written by
Reviewed by
Share this post
Print
Did this article help you?

Leave a comment