In this lesson we will be learning how to write on an LCD module.
Liquid crystal displays
An LCD (liquid crystal display) is a thin and flat screen made up of several coloured or black and white pixels located in front of a light source or reflector. It is often used in battery-powered electronic devices, which use very small quantities of electrical energy. It allows information to be given to the user on what is happening in the program, which is very useful and can make projects even more interactive. In this lesson we will be using an I2C/SPI LCD module. With this module, only 2 signals are required to display information when controlled by I2C, or 3 if controlled by SPI. This LCD in particular has 20 columns x 4 rows, allowing 80 characters to be written on the screen (with the library that we will be using, only ASCII characters can be used).
![]() |
![]() |
Preparing the project
To program the LCD module, we will need to use a library. What is a library? Simply a series of functions that someone has written for us to make our work easier. This means that, instead of having the values in the pins, we will have high-level functions such as lcd.print(“hola”) that do the dirty work for us. The library used to control the LCD is called liquidCrystal. However, if the Arduino library is designed for LCDs, they will need 6 signals in order to be controlled. Thanks to the IIC/SPI module, the LCD in this lesson only needs 4, which is why BQ has modified the library so that it functions correctly.
Once we have our project ready with those four files, we will connect the LCD and program it.
Connecting the LCD module
The LCD module has 4 pins for connecting (in IIC mode). These pins are:
- SCL: Serial clock
- SDA: Serial data
- VCC, supply voltage
- GND or mass
This component must always be connected as shown (so that it´s compatible with the bqLiquidCrystal library – but if you use another library it doesn´t have to be like this).
NAME | PIN NUMBER | PIN COLOR |
SCL | A5 | Blue |
SDA | A4 | Blue |
VCC | A5 | Red |
GND | A5 | Black |
Hello LCD
Now we have the necessary library and everything is connected. We will start off with Hello World! (¡Hola Mundo!), displaying the text Arduino Course on the screen. Here you have the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include #include "bqLiquidCrystal.h" /*** Global variables ***/ bool firstTime=true; LiquidCrystal lcd(0); // we create an LCD object void setup() { lcd.begin(20, 4); // we start up the LCD, 20 columns, 4 rows lcd.clear(); // we delete any content we don´t need } void loop() { // we execute this only once if(firstTime){ lcd.clear(); // we clear away any surplus content lcd.setCursor(0,0); // we put the cursors in column 0, row 0 lcd.print("Curso de Arduino"); // we print this text firstTime=false; // so that it is not repeated } } |
We´ll do things bit by bit.
Declaring the LiquidCrystal object
1 2 3 4 5 6 7 |
#include #include "bqLiquidCrystal.h" /*** Global variables ***/ bool firstTime=true; LiquidCrystal lcd(0); // we create an LCD object |
The LiquidCrystal variable type will enable us to control the LCD module. So we will start off with a global variable of this type, which we have called lcd in this case. We will also start off with value 0. From now on, all operations on the LCD will be lcd.xxx type.
Starting up the LCD
1 2 3 4 5 6 7 8 9 10 11 |
void loop() { // we execute this only once if(firstTime){ lcd.clear(); // we clear away any surplus content lcd.setCursor(0,0); // we put the cursors in column 0, row 0 lcd.print("Curso de Arduino"); // we print this text (Arduino Course) firstTime=false; // so that it is not repeated } } |
As we have done before, we will use the global variable as a means to perform an action once, and once that is done, we start writing.
- lcd.clear(). We clear everything from the screen, but it´s not required as we have already done it with the setup()
- lcd.setCursor(0,0). We state which column and row we want to write in, in this case row 0 and column 0, which means the top left corner.
- lcd.print(“Curso de Arduino”). Finally, we will write the text we want to print on the screen.
If we compile and upload this program to the board, this will be the result:
Moving text across the screen
How about if we wanted “BQ” to appear moving across the screen? I will leave you the code here so that you can try it out yourself, analyse it and if you want, you can change it to make it display something else.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include #include "bqLiquidCrystal.h" /*** Global variables ***/ LiquidCrystal lcd(0); // we create an LCD object void setup() { lcd.begin(20, 4); // we start up the LCD, 20 columns, 4 rows lcd.clear(); // we delete any content we don´t need } void loop() { // loop to be iterated in the rows for(byte row=0; row < 4; row++) { // loop to be iterated in the columns for (byte column=0; column < 19 ; column++) { lcd.clear(); lcd.setCursor(column,row); lcd.print("bq"); delay(200); } } } |
I couldn’t resist commenting. Very well written!
I’d likе to find out more? I’d want to find оut some additional information.