How to Use I2C LCD with ESP32 on Arduino IDE (ESP8266 compatible)
Or electronics? A specific module caught your eye, but you do not know how to use it? Do not worry, HUM is here for you! How to Use Module HUM is a blog tutorials series by e-radionica where you will find all you need in order to begin working with your favorite module. Tutorials include: technical characteristics, work principle, instructions on how to connect module with Croduino and the basic code. Everything else is left to your imagination. Introduction In this tutorial we will write about graphic display, why and when to use it, how to connect it to Croduino and how to program it to display what we want.
The reason why to use graphic display instead of a regular 16x2 LCD speaks for itself, and that is the fact that besides text, we can also display graphics, including symbols, tags, images and even the text which has a different font than the one on a regular display. Another reason why to use a graphic display is the fact that it is possible to perform a very simple GUI Graphic User Interface which makes controlling and adjusting some of your projects much more intuitive and easier.
As the first step towards that, let's learn how to control our display. How the module works In the picture you can see the back side of our graphic display. You can see that there are 20 pins used for power supplying the display, communication, adjusting contrast, and background LED lighting. But, there is another very important thing on it, which is not visible at first, and that is the selection between the parallel and serial communication.
Namely, this display more precisely, the chip, i. About the controller a bit later in the tutorial. By switching the middle pad with the pad next to the letter S, the display receives serial communication, and if the middle pad is switched with the pad next to the letter P, the display uses parallel communication.
The look of these pads JP2 and their location on the display is visible in the following picture: In order to be able to connect LCD and Croduino, it is necessary to know which function does an individual terminal on the 20 pin connector have.
The following image shows the function of each of these 20 pins. Some displays have the choice of parallel and serial communication on the connector itself PSB Pin.
In that case, it is not necessary to switch the previously mentioned pads. If you want simplicity and each used pin is important, and you can risk a bit slower display, we recommend using serial communication SPI , but if you want to connect something else besides the display to the SPI, we advise you to use display in the parallel mode it is not that the display will not work, but some modules do not like to share SPI with another module, so you should bear that in mind.
RST pin is the pin that restarts the whole display to its initial state. Some libraries use this pin to initialize i. V0 pin determines the desired contrast, but the adjusting itself is a bit different than the one of 16x2 display. With it, we have connected the variable resistor between V0 and mass GND. In this case, the variable resistor is connected between V0 and VOUT because for adjusting the contrast on this display we need a negative voltage, which can be generated by that same display.
It is mentioned that the display has a controller on it one of the three epoxy circles on the back side of the display , and it is about the ST integrated circuit, the two remaining chips are ST that increase the number of pixells the controller can manage. The controller itself has several work modes, and one of them is that it can be used as a regular alphanumeric display that has a significantly larger font than the regular 16x2 display or can even work in the extended set of commands, and work as a graphic display.
We will remain here on the extended set of commands that allows "drawing" different shapes on it. If you want to deal with the first work mode, the way you can do it can be found in the display's datasheet. Finally, we will say what types of LCD displays can be found.
Some main division is that there are three types: monochromatic black-white , grayscale can display shades of one color, e. Our display belongs to monochromatic ones because it can display only the pixel that is turned on or off.
Those who can change the intensity of the turned on pixel are grayscale displays and they are difficult to obtain and expensive, therefore they are rarely seen in some projects. And lastly, the most interesting ones are color displays, which exist in different versions serial, parallel communication or even startup from an SD card , of different resolutions and with different amounts of color bits per pixel from 8 to 24 bits , with or without touchscreen.
Such displays are easily accessible. We should note that there will be two schemes, one of them showing how to use LCD in serial communication, and the other how to use it in parallel. Scheme for parallel communication, which is used by the u8g library. Arduino code As we have already said, we will use two operation modes, parallel and serial communication.
In order to make writing of the code easier, we will use libraries one is ST GFX Library which is based on the well-known Adafruit GFX Library check the instructions for Adafruit GFX library here , only for now it does not support parallel communication, and the other is U8G library which supports both serial and parallel communication, but is somewhat more complicated to handle.
If you do not know how to install libraries, we advise you to check our tutorial about it. If everything is fine, the display will show the test program for display. If that does not happen, check the connections. Now that we know that everything is working, let us make a simple display of some image and text. Otherwise, the display will not work and nothing will be displayed on it.
In this case it is Croduino pin In this case, it is Croduino pin Ispisi novo vrijeme na display. Mala pauza izmedju podesavanja. This function works as the printf function, except that it saves everything to string which is in this case digitalniSat display.
We do the final ones using trigonometric functions. So, we multiply seconds by 6 in order to get degrees. The advantage of this library is the fact that it uses very little RAM, so it can be adapted into some more complex projects. Library's instructions along with all the functions it uses can be found here.
Building a Menu with Buttons for your Microcontroller
We use the 5V pin for the power supply of the display. The four different buttons are connected to a digital pin of the Arduino board. If you use other digital pins, make sure to change the program code as well. I do not use resistors for the connection to the buttons. Instead I use the internal pull up resistor of the Arduino board.
The function of the internal pull up is the following: If the button is not pressed, the internal pull-up resistor is connected to 5V. However if we press the button, the Arduino pin is pulled to ground and reports LOW.
We have to remember this later in the programming part of this tutorial. This script is a little bit more complicated because the difficult part is not the connection between display, buttons and microcontroller but in the script to create the logic of the menu. But do not panic, we go step by step through the script and I describe every little detail. This is the default address for this display. Either you can try the script with the same address of you can find out the I2C Hex address with a I2C Hex address scanner.
You find the script and description for the I2C Hex address scanner in this article. Now we define in total 4 different variables with the pin number to know on what signal the different buttons send their state to the Arduino. For example the down button is connected to pin 6. The buttonState variables store the current state of the button.
At the beginning of the script the buttons have the state 0. If a button is pressed it gets the state 1. With this state we know later in the script which button was pressed and can trigger the reaction. After we care about the buttons, we come to the screens. The first variable defines the number of screens we want to show. In this case we want to create 4 screens. The second variable defines the screen which is shown at the start of the program.
We define the screen 0. And here comes the first obstacle, because we now we create a two-dimensional array. In each of the 4 screens we want to save 2 variables. The 2 variables are a value and a unit and could be temperature and kelvin. The variable of the current screen is 0 because we access the first element of an array in Arduino with 0 and not 1. The following table shows the two-dimensional array.
HUM: Graphic LCD Display 128x64
A DHT 11 has four pins of which seen from the front, from left to right, Fig. Apart from that this article was written to show that readings from two completely different sensors can easily be displayed on this LCD display. Libraries U8glib. This library was apparently specifically written to display graphics.
GLCD 128×64 ST7920 interfacing with STM32
A trick to circumvent this limitation is to convert variables into character strings and display these on the LCD display. Via the one-wire protocol a sensor with a particular device address receives a call to release its data temperature reading. Puts string print given string to screen at current cursor location GLCD. It is the master configuration file. The panel configuration is used to configure things that are specific to a particular glcd panel like the geometry of the display, the chip select lines, and the low level timing for the panel.
Arduino Menu Tutorial with a Rotary Encoder and a Nokia 5110 LCD display
The pin configuration is used to assign pins. A given panel configuration will automatically determine which pin configuration file to use based on which board type is being used in the IDE.
It is suggested that you wire up the panel using the default pin assignments. Schematics The rotary encoder used is an analog device, and for that reason all its three output pins are connected to analog pins on the Arduino. To make the connections easier to follow, a pin map between the components is provided below. Code The code for this version of the tutorial is a little bit more complex compared to that of the last tutorial and you may need to replicate it yourself before you really understand the full scope of it.
Two of the libraries; the Adafruit GFX library and the Nokia LCD librarywill be used to interact with the display while the other two; the Encoder Library and the TimerOne librarywill reduce the amount of code we write to interact with the rotary encoder.
Each of the libraries can be downloaded via the links attached to them or installed via the Arduino Library Manager. To briefly explain the code; As usual, we start by including the libraries that will be used for the project.
Ensure yours is up to date Next, we write the void setup function. We also create an interrupt routine which will be used to detect button press within any of the menu pages.