Displaying Text on 16×2 LCD With Arduino
by GrayCode in Circuits > Arduino
659 Views, 0 Favorites, 0 Comments
Displaying Text on 16×2 LCD With Arduino
How to Display Text on a 16×2 LCD Screen
Here we are going to use a 16×2 LCD screen and our Arduino Microcontroller to display some text.
I’m going to give two examples of code to try, one show how to display some static text and the other shows how to change the text over a certain set time.
Always try to be creative and do a little but more than what the tutorial shows you. Play with the code and maybe add another few components, this will help you more than just copying the code.
View this project and more at my website Gray Code!
Supplies
- 1x Breadboard
- 1x Arduino Uno
- 1x 16×2 LCD
- 9x Jumper Wires
- 1x USB-A Cable
Let's Construct the Circuit!
How do I start?
The first thing to do is to take a look at our photos above to see how the circuit is constructed, and then have a go building it yourself! Hint: Use the circuit diagram when building it, it’s there for a reason.
We use a potentiometer to control the contrast of the LCD screen. The left pin of the potentiometer is the VCC in the middle pin is Voltage Out and the right pin is the Ground pin.
Connect the positive rail of the breadboard to the 5V pin on the Arduino, and connect the ground rail to the GND pin.
Video below shows Arduino running the second example code, where the text appears after a given time period.
A quick summary of the circuit can be seen below:
LCD Pin VSS > Ground
LCD Pin VDD > +5V
LCD Pin V0 > V Out from Pot
LCD Pin RS > Pin 9
LCD Pin RW > Ground
LCD Pin E > Pin 8
LCD Pin D4 > Pin 5
LCD Pin D5 > Pin 4
LCD Pin D6 > Pin 3
LCD Pin D7 > Pin 2
LCD Light + > 5V
LCD Light – > Ground
Pot Vcc > 5V
Pot Ground > Ground
Now the Code:
Let’s run through what the code is doing
The following code is used to implement the project by uploading it to the Arduino Microcontroller.
We use #include to include the required libraries in the project. Then we assign the Arduino pins to their respective variables named after the pin on the LCD to which they are connected . We then pass these variables into the Liquid Crystal library function.
Inside the Setup function we use the lcd.begin() library specific function to allow the LCD screen to start receiving data and to operate properly. If we want to show the same text at all times we will enter our lcd.print() functions in the setup function also.
Then inside the main loop function we include nothing IF we are showing the same text at all times. However if we want to alter our text then we will use the lcd.print() function here and using the delay() function we can change the text after a certain period of time.
Check out the information given on Arduino documentation to learn more.
LCD WITH STATIC TEXT
#include <LiquidCrystal.h> //including library for LCD (16,2)const int rs = 9, en = 8, d4 = 5, d5 = 4, d6 = 3, d7 = 2; //LCD pinout declarationLiquidCrystal lcd(rs, en, d4, d5, d6, d7);void setup() {// put your setup code here, to run once:lcd.begin(16,2); //begin LCD screen, defining the size of the screenlcd.setCursor(0,0); //set LCD cursor to start of 1st line to get ready to print temperaturelcd.print("Welcome :)"); //print the aforementioned data from the array to the LCDlcd.setCursor(0,1); //set LCD cursor to start of 2nd line to get ready to print humiditylcd.print("www.graycode.ie"); //print the aforementioned data from the array to the LCD}void loop() {// No code to be repeated}
LCD WITH CHANGING TEXT
#include <LiquidCrystal.h> //including library for LCD (16,2)const int rs = 9, en = 8, d4 = 5, d5 = 4, d6 = 3, d7 = 2; //LCD pinout declarationLiquidCrystal lcd(rs, en, d4, d5, d6, d7);void setup() {// put your setup code here, to run once:lcd.begin(16,2); //begin LCD screen, defining the size of the screen}void loop() {// put your main code here, to run repeatedly:lcd.setCursor(0,0); //set LCD cursor to start of 1st line to get ready to print temperaturelcd.print("Welcome :)"); //print the aforementioned data from the array to the LCDdelay(5000);lcd.setCursor(0,1); //set LCD cursor to start of 2nd line to get ready to print humiditylcd.print("www.graycode.ie"); //print the aforementioned data from the array to the LCDdelay(5000);lcd.clear();}
Upload the Code and Watch the Magic!
Upload the code from the Arduino IDE and enjoy your LCD displaying text!!
Take some time and play around, add extra LEDs or change the timing and have fun!
View this project and more at my website Gray Code!