Learn to code

ARDUINO & CODES

Previous Post
Controlling a TM1637 4 Digit 7 segment display with Arduino
Next Post
Use the millis() function instead of delay() function for Arduino

Creating Custom Characters on a 16×2 LCD with Arduino

in Arduino Displays

In this Arduino Tutorial, you will learn how to create custom characters for a 16×2 LCD Display with Arduino. With the function createChar() it is possible to create and display custom characters on the LCD.

createChar function, With the function createChar() it is possible to create and display custom characters on the LCD. This is especially useful if you want to display a character that is not part of the standard ASCII character set.

What you will need to know?

Before continuing with this guide, you may want to check out any topics from the list below that sound unfamiliar.

How to create characters with code?

To be able to show custom characters on the LCD Display we are going to use the command createChar(). With this command it is possible to display custom characters on the LCD. This will be especially in the case if the character that you would like to make is not not part of the ASCII characters.

The LiquidCrystal library that we are going to use enables you to set up eight custom characters. These characters are drawn on your display with a 5×8 pixel grid.

To define a character, we are going to create an array of eight bytes. Each byte defines one of the rows of the character. The number 1 indicates a pixel is on while the 0 indicates that the pixel is off.

You can easily change this by visiting the following website: Character Code Website.

BreadBoard Layout

BreadBoard Layout – Connect power and ground to one side of your breadboard.

Place the push button switch on the breadboard and attach one lead to 5V. Attach the other side to ground through a 10-kilohm resistor, and to your Arduino’s pin 6. You’re wiring this as a digital input, just as you’ve done in several other tutorials on this website.

The register select (RS) pin controls where the characters will appear on screen. The read/write pin (R/W) puts the screen in reading or write mode. You’ll be using the write mode in this project. The enable (EN) tells the LCD that it will be receiving a command. The data pins (D0-D7) are used to send character data to the screen. You’ll only be using 4 of these (D4-D7). Finally, there’s a connection for adjusting the contrast of the display. You’ll use a potentiometer to control this.

The LiquidCrystal library that comes with the Arduino software handles all the writing to these pins, and simplifies the process of writing software to display characters. The two outside pins of the LCD (Vss and LED-) need to be connected to the ground. Also, connect the R/W pin to ground. This places the screen in write mode. The LCD power supply (Vcc) should connect directly to 5V. The LED+ pin on the screen connects to power through a 220-ohm resistor.

Connect: Arduino Digital pin 2 to LCD D7, Arduino Digital pin 3 to LCD D6, Arduino Digital pin 4 to LCD D5, Arduino Digital pin 5 to LCD D4. These are the data pins that tell the screen what character to display.

Connect EN on the screen to pin 11 on your Arduino. RS on the LCD connects to pin 12. This pin enables writing to the LCD.

Place the potentiometer on the breadboard, connecting one endpin to power and the other to the ground. The center pin should connect to V0 on the LCD. This will allow you to change the contrast of the screen.

16×2 LCD Display Connections

Pin Number on DisplaySymbol Connection
1VSSGND Arduino
2VDD5V Arduino
3V010K Potentiometer
4RSPin 2 Arduino
5R/W GND Arduino
6EPin 3 Arduino
7-14D0 – D7- (not all are used in this tutorial)
11-14D4 – D7 Pin 4 – 7 Arduino
15A (LED+) 5V Arduino
16K (LED-) Ground Arduino

The Code

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int switchPin = 6;
const int numRows = 2;
const int numCols = 16;
int switchState = 0;
int prevSwitchState = 0;
int reply;

byte Heart[] = {
  B00000,
  B01010,
  B11111,
  B11111,
  B01110,
  B00100,
  B00000,
  B00000
};
byte Sound[] = {
  B00001,
  B00011,
  B00101,
  B01001,
  B01001,
  B01011,
  B11011,

};

byte Lock[] = {
  B01110,
  B10001,
  B10001,
  B11111,
  B11011,
  B11011,
  B11111,
  B00000
};

void setup() {
  lcd.begin(numRows, numCols);
  pinMode(switchPin, INPUT);
  lcd.print("ArduinoPlatform says: ");
  lcd.setCursor(0, 1);
  lcd.print("Push the Button!");
  lcd.createChar(0, Heart);
  lcd.createChar(1, Sound);
  lcd.createChar(2, Lock);
}

void loop() {
  switchState = digitalRead(switchPin);
  if (switchState != prevSwitchState) {
    if (switchState == LOW) {
      reply = random(3);
      lcd.clear();

      switch (reply) {
        case 0:
          hearts();
          break;
        case 1:
          sound();
          break;
        case 2:
          lock();
          break;
      }
    }
  }
  prevSwitchState = switchState;
}

void hearts() {
  lcd.setCursor(0, 0);
  lcd.print("Love More !!");
  lcd.setCursor(0, 1);
  lcd.write(byte(0));
  lcd.setCursor(2, 1);
  lcd.write(byte(0));
  lcd.setCursor(4, 1);
  lcd.write(byte(0));
  lcd.setCursor(6, 1);
  lcd.write(byte(0));
  lcd.setCursor(8, 1);
  lcd.write(byte(0));
  lcd.setCursor(10, 1);
  lcd.write(byte(0));
  lcd.setCursor(12, 1);
  lcd.write(byte(0));
  lcd.setCursor(14, 1);
  lcd.write(byte(0));
}

void sound() {
  lcd.setCursor(0, 0);
  lcd.print("Hear the tune !!");
  lcd.setCursor(0, 1);
  lcd.write(byte(1));
  lcd.setCursor(2, 1);
  lcd.write(byte(1));
  lcd.setCursor(4, 1);
  lcd.write(byte(1));
  lcd.setCursor(6, 1);
  lcd.write(byte(1));
  lcd.setCursor(8, 1);
  lcd.write(byte(1));
  lcd.setCursor(10, 1);
  lcd.write(byte(1));
  lcd.setCursor(12, 1);
  lcd.write(byte(1));
  lcd.setCursor(14, 1);
  lcd.write(byte(1));
}

void lock() {
  lcd.setCursor(0, 0);
  lcd.print("Is it locked?");
  lcd.setCursor(0, 1);
  lcd.write(byte(2));
  lcd.setCursor(2, 1);
  lcd.write(byte(2));
  lcd.setCursor(4, 1);
  lcd.write(byte(2));
  lcd.setCursor(6, 1);
  lcd.write(byte(2));
  lcd.setCursor(8, 1);
  lcd.write(byte(2));
  lcd.setCursor(10, 1);
  lcd.write(byte(2));
  lcd.setCursor(12, 1);
  lcd.write(byte(2));
  lcd.setCursor(14, 1);
  lcd.write(byte(2));
}




Code Explanation

Declare Variables

The first part of the sketch includes declaring variables for the LCD Display and assigning variables to the tactile switch. If you want to use the tactile switch button as a switch you will need to know the state of the button. In other words, we need to figure out how we can know when the button is pressed and when it is released. Therefore, two integer variables have defined that store the state of the button. After the button is pressed a random number of 0,1 or 2 will be replied in the code. To pass this reply to functions in the code we need to define a variable that stores this, int reply.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int switchPin = 6;
const int numRows = 2;
const int numCols = 16;
int switchState = 0;
int prevSwitchState = 0;
int reply;

 

Custom Characters

The next part in this sketch is creating the custom characters. We are going to display a heart, sound and lock character on the display. Remember, each character is drawn on a 5×8 pixel grid. As you can see in the code below the code for each character consists of 5 columns and 8 rows where 0 means that the pixel is off and 1 means that the pixel is on.

byte Heart[] = {
  B00000,
  B01010,
  B11111,
  B11111,
  B01110,
  B00100,
  B00000,
  B00000
};
byte Sound[] = {
  B00001,
  B00011,
  B00101,
  B01001,
  B01001,
  B01011,
  B11011,

};

byte Lock[] = {
  B01110,
  B10001,
  B10001,
  B11111,
  B11011,
  B11011,
  B11111,
  B00000
};
Void Setup() part

In the setup part we are going to begin the connection between the LCD and the Arduino. Furthermore, the switchpin is set as an input.

Next, we will print a first message tot he LCD Display. By setting the cursor on the second row we will print the next message. To be able to display the custom char characters we will need to create them with the createChar function.

void setup() {
  lcd.begin(numRows, numCols); 
  pinMode(switchPin, INPUT);
  lcd.print("ArduinoPlatform says: ");
  lcd.setCursor(0, 1);
  lcd.print("Push the Button!");
  lcd.createChar(0, Heart);
  lcd.createChar(1, Sound);
  lcd.createChar(2, Lock);
}
Pushing the Button

In the void loop() part of the code we want to detect if the tactile push-button is pushed. To know the state of the button we need to read its value with the command digitalRead. Rember, if the button is pressed the state will be HIGH. When the sketch begins 0 is assigned to the integer prevSwitchState (LOW).

Next we check whether or not the current state of the button is not equal tot he previous state.

The if statement checks is the button is released and will then execute a code.

Remember, we defined an integer for the random number. So after the button is released a random number between 0-2 will be chosen.

We are clearing the display before executing the functions. The switch statement corresponds to the random numbers that are chosen. Each random number will send a unique character to the LCD Display. For this, we will use three functions, hearts sound and lock.

After the custom character is send the the DISPLAY the previous switch state is updated with the current switchstate.

 void loop() {
  switchState = digitalRead(switchPin);
  if (switchState != prevSwitchState) {
    if (switchState == LOW) {
      reply = random(3);
      lcd.clear();

      
switch (reply) {
        case 0:
          hearts();
          break;
        case 1:
          sound();
          break;
        case 2:
          lock();
          break;
      }
    }
  }
  prevSwitchState = switchState;
}
Character Functions

I will highlight one of the three-character functions that we are using in this sketch. First, we are going to send some text fort he first row. In this case, Love More!!. Next we are going to print 8 times the hear symbol by setting the cursor at the right spot and then sending the number of the array that corresponds with the heart symbol. The text is printed on the first row while the character is printed on the second row.

void hearts() {
  lcd.setCursor(0, 0);
  lcd.print("Love More !!");
  lcd.setCursor(0, 1);
  lcd.write(byte(0));
  lcd.setCursor(2, 1);
  lcd.write(byte(0));
  lcd.setCursor(4, 1);
  lcd.write(byte(0));
  lcd.setCursor(6, 1);
  lcd.write(byte(0));
  lcd.setCursor(8, 1);
  lcd.write(byte(0));
  lcd.setCursor(10, 1);
  lcd.write(byte(0));
  lcd.setCursor(12, 1);
  lcd.write(byte(0));
  lcd.setCursor(14, 1);
  lcd.write(byte(0));
}

In this tutorial I have demonstrated how you can create custom characters for a 16×2 LCD Display. We used the command createChar() and also applied a switch statement in the code that is activated by a button.

I hope that you enjoyed reading the tutorial and fount is useful and informative. If you did please subscribe to my Instagram or Pinterest account found at the top of this page.

In any case that you have questions, feel free to ask them in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu