Nokia 5110 LCD, show text with Arduino (Part 2)

Hello, after bringing you the basic part of how to use the LCD display of the Nokia 5110 on Arduino, today I bring you a second part, in which I will teach you several more functions of the Adafruit library, with which we can display on the Nokia 5110 LCD some text.

If you don’t know how to connect the display, I recommend you go through part one, which you can access with the link above.

If, on the other hand, you want to see more screen functions, do not hesitate to go to the third part: Nokia 5110 LCD, show graphics with Arduino (Part 3)

Show Text

Something I talked about in part one is how to display basic text on the display, however in this case I am going to show you how to do more things like:

Invert text

The first thing we are going to do is show an inverted text, for which we can use this code:

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);

void setup()   {
  display.begin();
  // init done
  display.clearDisplay();
  display.display();
  display.setContrast(50);
  
  // Show inverted text
  display.setCursor(0,0);
  display.setTextColor(WHITE, BLACK);
  display.print("Hola Mundo!");
  display.display();
}

void loop() {
}

As we can see, after initializing the display what we do is change the color of the text with the function display.setTextColor, after which we write the text. This function accepts the text color as the first parameter, and the background color as the second parameter, and of course, we can mix texts of different colors by inserting the setTextColor function between them.

Show numbers

Showing numbers on our Nokia 5110 LCD display is no more complex than displaying text, in fact, it is exactly the same. The only thing we have to take into account is that the library, like Serial.print, adjusts the float variables to the second decimal (or includes zeros if there are no decimals), so to show more decimals we will have to indicate it in the function.

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);

void setup()   {
  display.begin();
  // init done
  display.clearDisplay();
  display.display();
  display.setContrast(50);
  
  // Show number
  display.setCursor(0,0);
  display.println(1234567890);
  // Show float (cutted to two decimals)
  float number = 15.005;
  display.println(number);
  // Set the decimal places to show
  display.println(number,3);
  display.display();
}

void loop() {
}

Convert numbers to different notations

Another of the things that we can do with our display is to show numbers in different notations, such as decimal, hexadecimal … among which are the following:

  • Decimal
  • Hexadecimal
  • Binary
  • Octal
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);

void setup()   {
  display.begin();
  // init done
  display.clearDisplay();
  display.display();
  display.setContrast(50);
  
  // Show hexadecimal number
  display.setCursor(0,0);
  int number = 124;
  display.print("HEX: ");
  display.println(number, HEX);
  // Show decimal number
  display.print("DEC: ");
  display.println(number, DEC);
  // Show octadecimal number
  display.print("OCT: ");
  display.println(number, OCT);
  // Show binary number
  display.print("BIN: ");
  display.println(number, BIN);
  display.display();
}

void loop() {
}

As you can see, just by indicating what notation you want the number in, he is in charge of converting it to that notation. It can also be used to convert numbers between different notations, such as hex to dec, hex to bin …

Show ASCII characters

Continuing with the things we can do with our Nokia 5110 LCD display, we find one such useful as displaying ASCII characters. For this we will not use any display.print function, but we will use the display.write function. This is because the print functions send the text to the display in readable format, while the display.write function sends it in binary format, which gives it more flexibility.

For those who don’t know, an ASCII character is a standard type of information exchange character, so it is usually available on all devices. A list of the characters that we have would be the following.

As you can see, we have a list with all the letters of the alphabet, in addition to some special characters. These special characters can serve us for multiple things such as making us a solitaire.

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);

void setup()   {
  display.begin();
  // init done
  display.clearDisplay();
  display.display();
  display.setContrast(50);
  display.setCursor(0,0);
  // Mostrar corazón en pantalla
  display.write(3);
  display.display();
}

void loop() {
}

Rotate the text

Imagine that instead of putting your display in a horizontal position, you want to do it in a vertical position, or you just want part of the text to be shown in another direction. Well with this library you can do it without problem. For this we have the function display.setRotation.

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);

void setup()   {
  display.begin();
  // init done
  display.clearDisplay();
  display.display();
  display.setContrast(50);
  
  // Original posición
  display.setRotation(0);
  display.setCursor(0,0);
  display.print("Arriba");
  
  // Left position
  display.setRotation(1);
  display.setCursor(0,0);
  display.print("Izq");
  
  // Bottom position
  display.setRotation(2);
  display.setCursor(0,0);
  display.print("Abajo");
  
  // Right position
  display.setRotation(3);
  display.setCursor(0,0);
  display.print("Drcha");
  
  display.display();
}

void loop() {
}

As you can see, it is as easy as telling it in what orientation we want the text, with 0 being the default orientation, 1 for the left (270º), 2 for the bottom (180º), and 3 for the right (90º). As you can see, the turn is counterclockwise.

Change the size

Of course, changing the font size is not a complicated thing to do either, and it’s as simple as telling you what size you want the text to be. For that we will use the function display.setTextSize.

Since the letters are displayed in a space of 5×7 pixels, indicating a size of 2 will create letters of 10×14 pixels, size 3 will create letters of 15×21, … and so on.

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);

void setup()   {
  display.begin();
  // init done
  display.clearDisplay();
  display.display();
  display.setContrast(50);
  
  display.print("A");
  display.setTextSize(2);
  display.print("B");
  display.setTextSize(3);
  display.print("C");
  display.setTextSize(4);
  display.print("D");
  display.display();
}

void loop() {
}

Put the cursor on a coordinate

We have already used this function throughout this tutorial, but I didn’t want to finish it without talking about it. This is the display.setCursor function, which will position you at the pixel you indicate. For this we have to take into account that the display has a resolution of 84×48 pixels, so the center, for example, would be the 42×24 coordinate.

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);

void setup()   {
  display.begin();
  // init done
  display.clearDisplay();
  display.display();
  display.setContrast(50);
  
  // Show a letter centered, taking care of its size
  display.setCursor(37, 17);
  display.setTextSize(2);
  display.print("B");
  display.display();
}

void loop() {
}

When positioning an object we have to take into account its size. That is why we have to position the cursor a few pixels before, as can be seen in the example. The letter has a size of 10×14, so the center will be:

x = (<display-x-size> - <object-x-size>)/2
y = (<display-y-size> - <object-y-size>)/2

With this formula we can center the letter with the following formula:

x = (84 - 10)/2 = 74 / 2 = 37
y = (48 - 14)/2 = 34 / 2 = 17

And so far the second part of the tutorial about how to show text on a Nokia 5110 LCD using Arduino. I hope it has served you and as always, comments are welcome;)

Greetings!

2 thoughts on “Nokia 5110 LCD, show text with Arduino (Part 2)”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.