– how to operate SHIELD-LCD16x2 from Olimex with an LCD display and 4 buttons.
You can find the previous article here. It contains a description of the LCD module, shows you how to connect the display to your Arduino UNO board and describes the basic rules of operating it using the software. Still, it needs to be admitted here that there is only a limited number of applications for an LCD alone, because in the case of such a simple LCD module no settings can be entered. To do so, you need a keyboard, which we will discuss in detail in one of the future articles, but now let us use a ready-made solution from the Arduino ecosystem – a board from Olimex – SHIELDLCD16x2.
This is a shield board which extends Arduino capabilities, equipped with an LCD module (2 lines of 16 characters each), 4 buttons and 8 additional GPIO lines. On the board, there is a PIC processor which communicates with Arduino UNO via the TWI interface. With access to a library of functions, operating this module with a built-in microcontroller should not cause any major problems. Let’s start with preparing the module for operation.
Getting started – Olimex board
There are two steps to prepare the module for work: hardware and software. The first one is simply to plug the shield into the Arduino UNO board. The second one requires you to install a library of module operating functions.
To install the library, go to the module manufacturer’s website i.e. Olimex. Then, in the search box (next to the Search button) enter the part of the module name “LCD16x2”. (Figure 1). Click on the Search button. When this text was being created, the search retuned two results – please, select “SHIELD-LCD16x2”.
Figure 1: Section of the Olimex website with the search box
Under the description of the module, you can find the “SOFTWARE” section (figure 2). Each line of text in this section is also a link to a file that can be downloaded from the Olimex website. At this point, we are looking for the link saying: “OLIMEXINO-328 + SHIELD-LCD16x2 – a library and set of demo examples”.
Download the ZIP file available at this link to your computer. Of course, it is worth having a look at the available examples, but for our needs it is enough to install the library saved in the LCD16x2 catalogue.
The Arduino IDE allows for various different ways of installing libraries. In this case, the easiest way is to upload the sources to the project directory, which also contains a libraries subdirectory. The project directory is created during the installation of Arduino IDE, and Windows usually places it (in the Polish language version) in subdirectory Ten Komputer → Dokumenty → Arduino. To add the library sources to today’s example, just move the LCD16x2 directory to the libraries folder. Once you’ve done that, launch the Arduino IDE.
Figure 2: The section of example programs available for the module.
SHIELD-LCD16x2 – Button status readout
Before getting started with your own program, you should read examples of using the LCD16x2.h library of functions available in the Examples directory. It is a much more effective method of learning than reading documentation, although it is worth remembering about the latter as well.
As mentioned before, the shield communicates with the UNO board using a serial interface. It’s easy to guess that the functions described in the previous article must be modified, because they used a parallel, 4-bit interface. As you can imagine, the microcontroller on the shield board communicates with the LCD character display module in the same way, but our Arduino UNO cannot “see” the display, and the control is done indirectly. Therefore, start the program by adding libraries to support the appropriate serial interface and the display module mounted on the shield board.
#include <LCD16x2.h>#include <Wire.h>
To make it simple, in order not to use the long library name, it is recommended to assign to it the lcd alias. We will use it by writing the name of the library function after the dot.
LCD16x2 lcd;
As you remember from the previous article, programs created for Arduino are divided into two parts: the initialization function and the infinite loop. The commands of the first one are written in the void setup() function, and of the latter – in the void loop() function. The commands contained within the setup function are executed only once, while those within the infinite loop are executed throughout the program.
The initialization function starts the TWI interface, clears the LCD screen, and turns on the display backlight at the maximum LED light intensity (0 parameter turns off the backlight).
void setup(){Wire.begin(); //TWI interface initlcd.lcdClear(); //LCD screen clearlcd.lcdSetBlacklight(255); //maximum backlight on}
In the case of the Olimex board, if no button is pressed, the button readout function returns only ones, while pressing the button resets the related bit to zero.
The readButtons() function returns an “int”-type variable, and it is called as follows:
variable = lcd.readButtons();
It is possible to operate on bits that are logic zeros, but it is much more convenient and easier for further analysis, if the corresponding bit is set. This is done by the ones’ complement:
variable =~variable;
If a bit is set, it is better to test its level using the Boolean product method. It is only true if both components are true. Further bit positions can be tested by using constants: 0x01 for a bit in position 0, 0x02 – in position 1, 0x04 – in position 2, 0x08 – in position 3, etc. It’s all the same whether you test the bits using hexadecimal, binary or decimal numbers, but once you get the hang of it, hexadecimal numbers are easy to write. You can see that the subsequent hexadecimal numbers used to test the bit position are the powers of 2.
buttons = lcd.readButtons();if (buttons & 0x01) pressed = 1;else if (buttons & 0x02) pressed = 2;else if (buttons & 0x04) pressed = 3;else if (buttons & 0x08) pressed = 4;else pressed = 0;
The “buttons” variable contains information about the buttons pressed. Using “if … else” causes that the processing of the button ends when the condition is fulfilled. This reduces the program runtime, but it also has the downside – the buttons have their own hierarchy and it is not possible to read the status of two or more pressed buttons like in the case of the combination of the Shift + Ctrl buttons on a PC.
Mask 0x01 corresponds to the first button on the left, while 0x08 corresponds to the first button on the right. The program numbers the buttons by giving the pressed variable a value corresponding to the conventional button number. Then, this number is shown on the LCD screen, at the position starting in the first row and the first column. The message is displayed only if the pressed value is different from 0. Otherwise, the message “No button” is displayed, indicating that no button is pressed. The message is terminated with three spaces so that when overlaid on “1 is pressed” (which is longer) the last characters of the caption are cleared.
lcd.lcdGoToXY(1, 1);if (pressed != 0){lcd.lcdWrite(pressed);lcd.lcdGoToXY(2, 1);lcd.lcdWrite(” is pressed”);}else lcd.lcdWrite(“No button “);
The whole sketch is available in the resources attached to the article. Compile it and upload it to the Arduino UNO microcontroller memory using the Ctrl+U shortcut (Sketch → Upload).
Download resources
Text prepared by Transfer Multisort Elektronik Sp. z o.o.
The original source of text: tme.eu