How to use The Basic 16x2 Character LCD
The Basic 16x2 Character LCD (Black on Green, Yellow Backlight 5V) is a basic 16 characters by 2 lines display with a black text on green background display. In this guide, we will be using the Basic 16x2 Character LCD (Black on Green, Yellow Backlight 5V) in our shop.
Parts
- Basic 16x2 Character LCD (Black on Green, Yellow Backlight 5V)
- M/F Jumper wires
- Arduino Uno R3
- Arduino IDE
- Soldering Iron
- Lead Free Solder
- 16 Pin Header
Before we start the wiring, you will need to solder the shorter end of the 16 pin header onto the LCD, like so:
Wiring Guide
(Note: It is best to wire these on a breadboard)
LCD | Arduino |
---|---|
VSS | GND |
VDD | 5V |
V0 | Potentiometer |
RS | Pin 12 |
RW | GND |
E | Pin 11 |
D4 | Pin 5 |
D5 | Pin 4 |
D6 | Pin 3 |
D7 | Pin 2 |
A | 5V (with a 220Ω resistor) |
K | GND |
The wiring for the VSS, VDD and V0 should look something like this:
Programming
Step 1: Before we start coding, we first have to download the needed libraries.
Step 2: Enter LiquidCrystal
and install the highlighted search.
Step 3: Declare your libraries
#include "LiquidCrystal.h"
Step 4: Initialize the library with the connected digital pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Step 5: Begin the code by setting up the LCD's number of columns and rows
void setup(){
lcd.begin(16, 2);
}
The setup is now done.
Full Code
#include "LiquidCrystal.h"
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup(){
lcd.begin(16, 2);
}
Example
Here are all the commands for <LiquidCrystal.h>
.
The commands we will use in this example are:
lcd.print("text"); // prints text
lcd.setCursor(column, row); // sets where the text will start
lcd.clear(); // clear all text and reset cursor back to top left
millis() // counts the milliseconds that have passed since the code was activated
For this example, we will program the LCD to have a timer counting up, as well as the top row telling us if the current second is in the 10s, 20s, 30s, 40s, 50s and stop when it reaches 60.
Step 1: First of all, let's setup the code.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup(){
lcd.begin(16, 2);
}
void loop(){
}
Step 2: Let's create an If Statement to make sure that the timer does not keep counting after 60, we will also declare an int variable for the amount of seconds past.
void loop()[
int sec = millis()/1000;
if(sec < 61){
}
]
Step 3: Now we can add in what the timer will show depending on the second, you can use arrays to make the code more tidy but I will just use numbers.
void loop(){
int sec = millis()/1000;
if(sec < 61){
if(sec > 9 && sec < 20){
lcd.setCursor(0, 0);
lcd.print("Tens ");
}
if(sec > 19 && sec < 30){
lcd.setCursor(0, 0);
lcd.print("Twenties");
}
if(sec > 29 && sec < 40){
lcd.setCursor(0, 0);
lcd.print("Thirties");
}
if(sec > 39 && sec < 50){
lcd.setCursor(0, 0);
lcd.print("Forties ");
}
if(sec > 49 && sec < 60){
lcd.setCursor(0, 0);
lcd.print("Fifties ");
}
if(sec == 60){
lcd.setCursor(0, 0);
lcd.print("One Minute");
}
}
}
Step 4: Lastly, let's create the timer.
lcd.setCursor(0, 1); //The column is set as 1 because the LCD counts the columns starting from 0, which means 0=1, 1=2
lcd.print(millis()/1000); //prints every second
delay(50); //short delay to make sure the LCD doesn't lag
Full Code
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup(){
lcd.begin(16, 2);
lcd.print("Singles");
}
void loop(){
int sec = millis()/1000;
if(sec < 61){
if(sec > 9 && sec < 20){
lcd.setCursor(0, 0);
lcd.print("Tens ");
}
if(sec > 19 && sec < 30){
lcd.setCursor(0, 0);
lcd.print("Twenties");
}
if(sec > 29 && sec < 40){
lcd.setCursor(0, 0);
lcd.print("Thirties");
}
if(sec > 39 && sec < 50){
lcd.setCursor(0, 0);
lcd.print("Forties ");
}
if(sec > 49 && sec < 60){
lcd.setCursor(0, 0);
lcd.print("Fifties ");
}
if(sec == 60){
lcd.setCursor(0, 0);
lcd.print("One Minute");
}
lcd.setCursor(0, 1); //the reason why the cursor is set to the 2nd row is because the LCD counts from 0
lcd.print(millis()/1000); //prints every second
delay(50); //delay to make sure the LCD doesn't lag
}
}
Output