• Connecting LCD to Arduino. Character LCD and Arduino

    Every radio amateur, after a certain number of simple homemade projects, comes to the goal of constructing something grandiose using sensors and buttons. After all, it is much more interesting to display data on a display rather than on a port monitor. But then the question arises: which display to choose? And in general, how to connect it, what is needed to connect? The answers to these questions will be discussed in this article.

    LCD 1602

    Among the many options among displays, I would like to specifically mention the LCD1602 display based on the HD4478 controller. This display is available in two colors: white letters on a blue background, black letters on a yellow background. Connecting the LCD 1602 to Arduino will also not cause any problems, since there is a built-in library and there is no need to download anything additional. Displays differ not only in price, but also in size. Often, radio amateurs use 16 x 2, that is, 2 lines of 16 characters. But there is also 20 x 4, where there are 4 lines of 20 characters. Dimensions and color do not play any role in connecting the lcd 1602 display to Arduno; they are connected in the same way. The viewing angle is 35 degrees, the display response time is 250 ms. It can work at temperatures from -20 to 70 degrees Celsius. During operation, it uses 4 mA for the screen and 120 mA for the backlight.

    Where is it used?

    This display has its popularity not only among radio amateurs, but also among large manufacturers. For example, printers and coffee machines also use LCD1602. This is due to its low price; this display costs 200-300 rubles on Chinese sites. It’s worth buying there, since in our stores the markups for this display are very high.

    Connecting to Arduino

    Connecting the LCD 1602 to Arduino Nano and Uno is no different. You can work with the display in two modes: 4 bits and 8. When working with 8-bit, both the low-order and high-order bits are used, and with 4-bit, only the low-order ones. There is no particular point in working with 8-bit, since it will add 4 more contacts for connection, which is not advisable, because the speed will not be higher, the limit for display updates is 10 times per second. In general, to connect the lcd 1602 to the Arduino, a lot of wires are used, which causes some inconvenience, but there are special shields, but more on that later. The photo shows the connection of the display to Arduino Uno:

    Example program code:

    #include // Add the necessary LiquidCrystal library lcd(7, 6, 5, 4, 3, 2); // (RS, E, DB4, DB5, DB6, DB7) void setup())( lcd.begin(16, 2); // Set the screen size lcd.setCursor(0, 0); // Set the cursor to the beginning 1 lines lcd.print("Hello, world!"); // Print the text lcd.setCursor(0, 1); // Set the cursor to the beginning of line 2 lcd.print("site"); // Print the text ) void loop ()( )

    What does the code do? The first step is to connect the library for working with the display. As mentioned above, this library is already included in Arduino IDE and there is no additional need to download and install it. Next, the contacts that are connected to the pins are determined: RS, E, DB4, DB5, DB6, DB7, respectively. Then the screen size is set. Since we are working with a version with 16 characters and 2 lines, we write the following values. We place the cursor at the beginning of the first line and display our first text Hello World. Next, place the cursor on the second line and display the name of the site. That's it! Connecting lcd 1602 to Arduino Uno was considered.

    What is I2C and why is it needed?

    As mentioned above, connecting the display takes up a lot of contacts. For example, when working with multiple sensors and an LCD display, 1602 pins may simply not be enough. Often, radio amateurs use the Uno or Nano versions, which do not have many contacts. Then people came up with special shields. For example, I2C. It allows you to connect a display with only 4 pins. This is twice as much. The I2C module is sold both separately, where you need to solder it yourself, and already soldered to the LCD 1602 display.

    Connection using I2C module

    Connecting LCD 1602 to Arduino Nano with I2C takes up little space, only 4 pins: ground, power and 2 data outputs. We connect power and ground to 5V and GND on the Arduino, respectively. We connect the remaining two contacts: SCL and SDA to any analog pins. In the photo you can see an example of connecting an lcd 1602 to an arduino with an I2C module:

    Program code

    If to work with a display without a module it was necessary to use only one library, then to work with a module you need two libraries. One of them is already included in the Arduino IDE - Wire. Another library, LiquidCrystal I2C, must be downloaded separately and installed. To install the library in Arduino, the contents of the downloaded archive must be loaded into the root Libraries folder. Example program code using I2C:

    #include #include LiquidCrystal_I2C lcd(0x27,16,2); // Set the display void setup() ( lcd.init(); lcd.backlight(); // Turn on the display backlight lcd..setCursor(8, 1); lcd.print("LCD 1602"); ) void loop( ) ( // Set the cursor to the second line and the zero character. lcd.setCursor(0, 1); // Display the number of seconds since the Arduino started lcd.print(millis()/1000); )

    As you can see, the code is almost the same.

    How to add your own symbol?

    The problem with these displays is that there is no support for Cyrillic alphabet and symbols. For example, you need to load some symbol into the display so that it can reflect it. To do this, the display allows you to create up to 7 of your own symbols. Imagine the table:

    0 0 0 1 0
    0 0 0 0 1
    1 1 0 0 1
    0 0 0 0 1
    1 1 0 0 1
    0 0 0 0 1
    0 0 0 1 0
    0 0 0 0 0

    If 0 - there is nothing there, if 1 - this is a painted area. In the example above you can see the creation of the "smiling smiley" symbol. Using an example program in Arduino it would look like this:

    #include #include // Add the necessary library // Bit mask of the smile symbol byte smile = ( B00010, B00001, B11001, B00001, B11001, B00001, B00010, ); LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // (RS, E, DB4, DB5, DB6, DB7) void setup())( lcd.begin(16, 2); // Set the screen size lcd.createChar(1, smile); // Create character number 1 lcd.setCursor(0, 0); // Set the cursor to the beginning of line 1 lcd.print("\1"); // Print a smiley (character number 1) - "\1" ) void loop() ( )

    As you can see, a bitmask was created the same as the table. Once created, it can be displayed as a variable on the display. Remember that you can only store 7 characters in memory. In principle, this is enough. For example, if you need to show a degree symbol.

    Problems in which the display may not work

    There are times when the display does not work. For example, it turns on, but does not show characters. Or it doesn't turn on at all. First, check if you have connected the pins correctly. If you used to connect lcd 1202 to Arduino without I2C, then it is very easy to get tangled in the wires, which may cause incorrect operation display. You should also make sure that the display contrast is increased, since with the minimum contrast it is not even visible whether the LCD 1602 is turned on or not. If this does not help, then perhaps the problem may lie in the soldering of the contacts, this is when using an I2C module. Also common cause, at which the display may not work is incorrect installation I2C addresses. The fact is that there are many manufacturers, and they can put a different address, you need to correct it here:

    LiquidCrystal_I2C lcd(0x27,16,2);

    In parentheses you can see two values, 0x27 and 16.2 (16.2 is the display size, and 0x27 is the I2C address). Instead of these values, you can try setting 0x37 or 0x3F. Well, another reason is simply a faulty LCD 1602. Considering that almost everything for Arduino is made in China, you cannot be 100% sure that the purchased product is not defective.

    Pros and cons of LCD 1602

    Let's look at the pros and cons of the LCD 1602 display.

    • Price. This module can be purchased at a very reasonable price in Chinese stores. The price is 200-300 rubles. Sometimes it is even sold together with an I2C module.
    • Easy to connect. Probably no one is connecting LCD 1602 without I2C these days. And with this module, the connection takes only 4 contacts, there will be no “webs” of wires.
    • Programming. Thanks to ready-made libraries, working with this module is easy; all functions are already written. And if you need to add your own symbol, it only takes a couple of minutes.
    • During its use by thousands of radio amateurs, no major disadvantages have been identified, only there are cases of defective purchases, since Chinese versions of displays are mainly used.

    This article looked at connecting the 1602 to Arduino, and also provided examples of programs for working with this display. It really is one of the best in its category; it’s not for nothing that thousands of radio amateurs choose it for their projects!

    Instructions

    The operation of the ultrasonic rangefinder HC-SR04 is based on the principle of echolocation. It emits sound pulses into space and receives a signal reflected from an obstacle. The distance to the object is determined by the time of propagation of the sound wave to the obstacle and back.
    The sound wave is triggered by applying a positive pulse lasting at least 10 microseconds to the TRIG leg of the rangefinder. As soon as the pulse ends, the rangefinder emits a packet of sound pulses with a frequency of 40 kHz into the space in front of it. At the same time, the algorithm for determining the delay time of the reflected signal is launched, and a logical unit appears on the ECHO rangefinder leg. As soon as the sensor detects the reflected signal, a logical zero appears at the ECHO pin. The duration of this signal (“Echo Delay” in the figure) determines the distance to the object.
    The distance measurement range of the HC-SR04 rangefinder is up to 4 meters with a resolution of 0.3 cm. The viewing angle is 30 degrees, the effective angle is 15 degrees. Current consumption in standby mode is 2 mA, during operation - 15 mA.

    The ultrasonic rangefinder is powered by a voltage of +5 V. The other two pins are connected to any digital ports Arduino, we will connect to 11 and 12.

    Now let's write a sketch that determines the distance to the obstacle and outputs it to the serial port. First, we set the numbers of the TRIG and ECHO pins - these are pins 12 and 11. Then we declare the trigger as an output and the echo as an input. Initialize the serial port at 9600 baud. Each repetition of the cycle loop() We read the distance and output it to the port.
    Function getEchoTiming() generates a trigger pulse. It just creates a current of 10 microseconds, a pulse, which is a trigger for the rangefinder to start emitting a sound packet into space. Next, it remembers the time from the beginning of the transmission of the sound wave until the arrival of the echo.
    Function getDistance() calculates the distance to the object. We remember from the school physics course that distance equals speed times time: S = V*t. The speed of sound in air is 340 m/sec, we know the time in microseconds, this is “duratuion”. To get the time in seconds, divide by 1,000,000. Since sound travels twice the distance - to the object and back - you need to divide the distance in half. So it turns out that the distance to the object is S = 34000 cm/sec * duration / 1,000,000 sec / 2 = 1.7 cm/sec / 100, which is what we wrote in the sketch. The microcontroller performs the multiplication operation faster than division, so I replaced “/ 100” with the equivalent “* 0.01”.

    Also, many libraries have been written to work with ultrasonic rangefinders. For example, this one: http://robocraft.ru/files/sensors/Ultrasonic/HC-SR04/ultrasonic-HC-SR04.zip. The library is installed as standard: download, unzip to a directory libraries, which is located in the Arduino IDE folder. After this, the library can be used.
    Having installed the library, let's write a new sketch. The result of its work is the same - in the monitor serial port The distance to the object is displayed in centimeters. If you write in a sketch float dist_cm = ultrasonic.Ranging(INC);, the distance will be displayed in inches.

    So, we connected the HC-SR04 ultrasonic rangefinder to Arduino and received data from it in two in different ways: with and without using a special library.
    The advantage of using the library is that the amount of code is significantly reduced and the readability of the program is improved; you do not have to delve into the intricacies of the device and you can use it immediately. But therein lies a drawback: you have a worse understanding of how the device works and what processes take place in it. In any case, which method to use is up to you.

    LCD displays of 1602 sizes, created on the basis of the HD44780 controller, today still remain one of the most affordable, simple and in demand for developing any kind of electronic devices. It is not surprising that they can be seen both in simple units assembled literally on the knee, and in more serious industrial ones, for example, coffee machines. It is with this display that the most popular Arduino-related modules and shields are assembled, for example LCD I2C module and LCD Keypad Shield.

    This article explains in detail with pictures how to connect an LCD to Arduino and display information.

    1602 displays have two different versions:

    Yellow backlight with black letters
    - or (this happens much more often) blue backlight with white ones.

    The size of the displays on the HD44780 controller can be very different, but they are controlled in the same way. The most common dimensions are 16 by 02 (that is, 16 characters in two lines) or 20 by 04. The characters themselves have a resolution of 5 by 8 pixels.

    Most displays do not support Cyrillic alphabet (with the exception of CTK-marked displays). But this problem is partially solvable, and the article further describes in detail how to do this.

    The display has a 16-PIN connector for connection. The conclusions have markings on the back of the board, it is as follows:

    1 (VSS) – negative power supply for the controller.
    2 (VDD) – positive power supply for the controller.
    3 (VO) – contrast control settings.
    4 (RS) – register selection.
    5 (R/W) – reading and writing, in particular, writing when connected to ground.
    6 (E) – activation (enable).
    7–10 (DB0-DB3) – low-order bits from the eight-bit interface.
    11–14 (DB4-DB7) – most significant bits from the interface
    15 (A) – positive anode for backlight power supply.
    16 (K) – negative cathode for backlight power supply.

    Step 2: Connect the LCD Display

    Before connecting the display and transferring information to it, it is worth checking its functionality. First, apply voltage to the VSS and VDD controller, power up the backlight (A, K), then adjust the contrast. For such settings, a 10 kOhm potentiometer is suitable; its shape is not important. +5V and GND are supplied to the outer legs, and the leg in the center is connected to the VO pin.

    When power is supplied to the circuit, you need to achieve the necessary contrast; if it is adjusted incorrectly, then the image on the screen will not be visible. To adjust the contrast, you need to “play” with the potentiometer. When the circuit is assembled correctly and the contrast is adjusted correctly, top line the screen should be filled with rectangles.

    To make the display work, a special library, LiquidCrystal.h, built into the Arduino IDE, is used, which I will write about below. It can operate in 8-bit and 4-bit mode. In the first option, only the least significant and most significant bits (BB0-DB7) are used, in the second - only the least significant (BB4-DB7).

    But using 8-bit mode in this display - wrong decision, there is almost no speed advantage, since its update rate is always less than 10 times per second. To display text, you need to connect pins DB7, DB6, DB5, DB4, E and RS to the controller pins. They can be connected to any Arduino pins, the main thing is to set the correct sequence in the code.

    If the required symbol is not yet in the controller’s memory, you can define it manually (up to seven symbols in total). The cell in the displays under consideration has an extension of five by eight pixels. The task of creating a symbol is to write a bit mask and place ones in places where the dots should be lit, and zeros where they shouldn’t.

    The connection diagram discussed above is not always good, since at least six digital outputs are used on the Arduino.

    Step 3: Workaround

    Let's explore an option to get around this and get by with only two. We need an additional converter module for LCD to IIC/I2C. How it is soldered to the display and connected to the Arduino can be seen in the images below.

    But this connection option only works with a special library, LiquidCrystal_I2C1602V1, which, however, is easy to find on the Internet and install, after which you can use it without any problems.

    Step 4: LiquidCrystal.h Library

    The LiquidCrystal.h library can be downloaded from the official resource -. You can also download from the links below:

    Sketch

    Once you have downloaded the archive, replace the LiquidCrystal folder in the libraries folder of your Arduino installation directory.

    You can see a sample sketch in File -> Examples -> LiquidCrystal -> HelloWorld_SPI(File -> Examples -> LiquidCrystal -> HelloWorld_SPI).

    This concludes our next lesson. We wish you quality projects!

    Liquid Crystal Display (LCD), abbreviated as LCD, is based on liquid crystal technology. When designing electronic devices, we need an inexpensive device to display information and the second equally important factor is the availability of ready-made libraries for Arduino. Of all the available LCD displays on the market, the most commonly used is the LCD 1602A, which can display ASCII character in 2 lines (16 characters in 1 line) each character in the form of a 5x7 pixel matrix. In this article we will look at the basics of connecting a display to Arduino.

    Technical Parameters

    Supply voltage: 5 V
    Display size: 2.6 inches
    Display type: 2 lines x 16 characters
    Backlight color: blue
    Character color: white
    Dimensions: 80mm x 35mm x 11mm

    Display Description

    LCD 1602A is an electronic module based on the HD44780 driver from Hitachi. The LCD1602 has 16 pins and can operate in 4-bit mode (using only 4 data lines) or 8-bit mode (using all 8 data lines), or an I2C interface can be used. In this article I will talk about connecting in 4-bit mode.

    Contact assignment:
    VSS: “-” module power supply
    VDD: “+” module power supply
    VO: Contrast control pin
    RS: Register Select
    RW: Select write or read mode (when connected to ground, set to write mode)
    E: Fall gate
    DB0-DB3: Interface bits
    DB4-DB7: Interface bits
    A: “+” backlight power
    K: "-" backlight power

    On the front of the module there is an LCD display and a group of contacts.

    On the back of the module there are two chips in a “drip” design (ST7066U and ST7065S) and electrical wiring, draw schematic diagram I don’t see the point, I’ll just tell you about resistor R8 (100 Ohm), which serves as a limiting resistor for LED backlight, so you can connect 5V directly to pin A. A little later I’ll write an article in which I’ll tell you how to change LCD backlight display using PWB and transistor.

    Connecting LCD 1602A to Arduino (4-bit mode)

    Required parts:
    Arduino UNO R3 x 1 pc.
    LCD display 1602A (2×16, 5V, Blue) x 1 pc.
    DuPont Wire, 2.54 mm, 20 cm, F-F (Female - Female) x 1 pc.
    Potentiometer 10 kOhm x 1 pc.
    PLS-16 connector x 1 pc.
    Development board MB-102 x 1 pc.
    USB cable 2.0 A-B x 1 pcs.

    Connection:
    To connect we will use a development board; the diagram and table for connecting the LCD1602a to Arduino in 4-bit mode can be seen in the figure below.

    Connecting the display to the breadboard will be done via PLS-16 pins (they need to be soldered to the display). Install the display module in breadboard and connect the power VDD (2nd pin) to 5V (Arduino) and VSS (1st pin) to GND (Arduino), then connect RS (4th pin) to digital pin 8 (Arduino). Ground RW (5th pin) by connecting it to GND (Arduino), then connect pin E to pin 8 (Arduino). For a 4-bit connection, four pins are required (DB4 to DB7). Connect pins DB4 (11th pin), DB5 (12th pin), DB6 (13th pin) and DB7 (14th pin) to Arduino digital pins 4, 5, 6 and 7. 10K potentiometer is used for adjustment display contrast, the connection diagram for the 1602a LCD display is shown below

    The library is already included in the environment IDE development Arduino and there is no need to install it. Copy and paste this example code into the Arduino IDE program window and load it into the controller.

    /* Testing was carried out on Arduino IDE 1.6.11 Test date 09/20/2016. */ #include LiquidCrystal lcd(8, 9, 4, 5, 6, 7); void setup() ( lcd.begin(16, 2); // Initializes LCD 16x2 ) void loop() ( lcd.setCursor(0,0); // Set the cursor to the first line lcd.print("Hello, world" ); // Print the text lcd.setCursor(0,1); // Set the cursor to the second line lcd.print("www.robotchip.ru"); // Print the text )

    Testing was carried out on Arduino IDE 1.6.11

    Test date: 09/20/2016

    #include

    LiquidCrystal lcd (8, 9, 4, 5, 6, 7);

    void setup()

    lcd. begin(16, 2); // Initializes LCD 16x2

    void loop()

    lcd. print ("Hello, world" ) ; // Print text

    lcd. print ("www.robotchip.ru" ) ; // Print text

    Download the program

    A little about the program.
    To facilitate communication between the Arduino and the LCD display, the built-in library in the Arduino IDE is used " LiquidCrystal.h « - which is written for LCD displays using HD44780 (Hitachi) chipset (or compatible chips). This library can handle both 4-bit mode and 8-bit mode LCD connection.

    LCD displayfrequent guest in Arduino projects. But in complex schemes we may have the problem of a lack of Arduino ports due to the need to connect a shield that has very, very many pins. The solution in this situation could be I2C/IIC adapter that connects almost standard for Arduino screen 1602 to Uno, Nano or Mega boards with just 4 pins. In this article we will look at how you can connect an LCD screen with an I2C interface, what libraries you can use, write a short example sketch and look at common errors.

    Liquid Crystal Display LCD 1602 is good choice to output character strings in various projects. It is inexpensive, there are various modifications with different backlight colors, you can easily download ready-made libraries for Arduino sketches. But the main disadvantage of this screen is the fact that the display has 16 digital pins, of which at least 6 are required. Therefore, using this LCD screen without i2c adds serious restrictions for Arduino Uno or Nano boards. If there are not enough contacts, then you will have to buy a board Arduino Mega or save contacts, including by connecting the display via i2c.

    Brief description of LCD 1602 pins

    Let's take a closer look at the LCD1602 pins:

    Each of the pins has its own purpose:

    1. Ground GND;
    2. Power supply 5 V;
    3. Setting monitor contrast;
    4. Command, data;
    5. Writing and reading data;
    6. Enable;

    7-14. Data lines;

    1. Plus backlight;
    2. Minus the backlight.

    Display Specifications:

    • Character display type, it is possible to load symbols;
    • LED backlight;
    • Controller HD44780;
    • Supply voltage 5V;
    • Format 16x2 characters;
    • Operating temperature range from -20C to +70C, storage temperature range from -30C to +80C;
    • Viewing angle 180 degrees.

    Connection diagram of LCD to Arduino board without i2C

    The standard diagram for connecting a monitor directly to an Arduino microcontroller without I2C is as follows.

    Due to the large number of connected contacts, there may not be enough space to connect the necessary elements. Using I2C reduces the number of wires to 4 and occupied pins to 2.

    Where to buy i2c 1602 screens for Arduino

    LCD screen 1602 is quite popular, so you can easily find it both in domestic online stores and on foreign sites. Here are some links to the most available options:

    • A variant of a regular display from a fairly well-known seller Wavgat at a price below 100 rubles.
    • Set of screen and i2c adapter (you need to solder it yourself). Price – below 200 rubles
    • i2c screen shield – LCD 1602 module with control buttons and expansion board.

    Description of the I2C protocol

    Before discussing connecting the display to Arduino via an i2c adapter, let's briefly talk about the i2C protocol itself.

    I2C/IIC(Inter-Integrated Circuit) is a protocol originally created for communication integrated circuits inside electronic device. The development belongs to Philips. The i2c protocol is based on the use of an 8-bit bus, which is needed to communicate blocks in control electronics, and an addressing system, thanks to which you can communicate over the same wires with several devices. We simply transfer data to one or another device, adding the identifier of the desired element to the data packets.

    The most simple circuit I2C can contain one master device (most often an Arduino microcontroller) and several slaves (for example, an LCD display). Each device has an address in the range from 7 to 127. There should not be two devices with the same address in the same circuit.

    The Arduino board supports i2c in hardware. You can use pins A4 and A5 to connect devices using this protocol.

    There are several advantages to I2C operation:

    • Operation requires only 2 lines - SDA (data line) and SCL (sync line).
    • Connecting a large number of leading devices.
    • Reduced development time.
    • Only one microcontroller is required to control the entire set of devices.
    • The possible number of microcircuits connected to one bus is limited only by the maximum capacity.
    • High degree of data safety due to a special surge suppression filter built into the circuits.
    • A simple procedure for diagnosing emerging failures and quickly debugging faults.
    • The bus is already integrated into the Arduino itself, so there is no need to develop an additional bus interface.

    Flaws:

    • There is a capacitive limit on the line - 400 pF.
    • Difficult to program an I2C controller if there are several different devices on the bus.
    • At large quantities devices have difficulty locating a fault if one of them erroneously sets the low level state.

    i2c module for LCD 1602 Arduino

    The fastest and convenient way Using an i2c display in Arduino means purchasing a ready-made screen with built-in protocol support. But there are not very many of these screens and they are not cheap. But the variety standard screens A huge number have already been released. Therefore, the most affordable and popular option today is to purchase and use a separate I2C module - an adapter, which looks like this:

    On one side of the module we see i2c pins - ground, power and 2 for data transfer. On the other adapter we see external power connectors. And, naturally, the board has many legs with which the module is soldered to the standard screen pins.


    i2c outputs are used to connect to the Arduino board. If necessary, we connect external power for backlighting. With the built-in trimmer we can set custom contrast values ​​J

    On the market you can find LCD 1602 modules with already soldered adapters; their use is simplified as much as possible. If you purchased a separate adapter, you will need to first solder it to the module.

    Connecting the LCD screen to Arduino via I2C

    To connect you need the Arduino board itself, a display, development board, connecting wires and potentiometer.

    If you are using a special separate i2c adapter, you must first solder it to the screen module. It's hard to make a mistake there, you can follow this scheme.


    An LCD monitor with i2c support is connected to the board using four wires - two wires for data, two wires for power.

    • The GND pin connects to GND on the board.
    • The VCC pin is at 5V.
    • SCL connects to pin A5.
    • SDA is connected to pin A.

    And that's all! No webs of wires, which are very easy to get tangled in. At the same time, we can simply entrust all the complexity of implementing the i2C protocol to libraries.

    Libraries for working with i2c LCD display

    To interact with Arduino and LCD 1602 via the I2C bus, you will need at least two libraries:

    • The Wire.h library for working with I2C is already available in standard program Arduino IDE.
    • The LiquidCrystal_I2C.h library, which includes a wide variety of commands for controlling the monitor via the I2C bus and allows you to make the sketch simpler and shorter. You need to additionally install the library After connecting the display, you need to additionally install the LiquidCrystal_I2C.h library

    After connecting all the necessary libraries to the sketch, we create an object and can use all its functions. For testing, let's load the following standard example sketch.

    #include #include // Including the library //#include // Connecting an alternative library LiquidCrystal_I2C lcd(0x27,16,2); // Specify the I2C address (the most common value), as well as screen parameters (in the case of LCD 1602 - 2 lines of 16 characters each //LiquidCrystal_PCF8574 lcd(0x27); // Option for the PCF8574 library void setup() ( lcd.init (); // Initialize the display lcd.backlight(); // Connect the backlight lcd.setCursor(0,0); // Set the cursor to the beginning of the first line lcd.print("Hello"); lcd.setCursor(0,1); // Set the cursor to the beginning of the second line lcd.print("ArduinoMaster"); // Type text on the second line ) void loop() ( )

    Description of functions and methods of the LiquidCrystal_I2C library:

    • home() and clear() - the first function allows you to return the cursor to the beginning of the screen, the second does the same, but at the same time deletes everything that was on the monitor before.
    • write(ch) – allows you to print a single character ch to the screen.
    • cursor() and noCursor() – shows/hides the cursor on the screen.
    • blink() and noBlink() – the cursor blinks/does not blink (if its display was enabled before).
    • display() and noDisplay() – allows you to connect/disable the display.
    • scrollDisplayLeft() and scrollDisplayRight() – scrolls the screen one character left/right.
    • autoscroll() and noAutoscroll() – allows you to enable/disable autoscroll mode. In this mode, everyone new symbol is written in the same place, displacing what was previously written on the screen.
    • leftToRight() and rightToLeft() – Setting the direction of the displayed text – left to right or right to left.
    • createChar(ch, bitmap) – creates a character with code ch (0 – 7), using an array of bitmaps to create black and white points.

    Alternative library for working with i2c display

    In some cases, errors may occur when using the specified library with devices equipped with PCF8574 controllers. In this case, the LiquidCrystal_PCF8574.h library can be suggested as an alternative. It extends LiquidCrystal_I2C, so there should be no problems using it.

    Problems connecting i2c lcd display

    If after uploading the sketch you do not see any message on the display, try the following steps.

    First, you can increase or decrease the monitor's contrast. Often characters are simply not visible due to the contrast and backlight mode.

    If this does not help, then check whether the contacts are connected correctly and whether the backlight power is connected. If you used a separate i2c adapter, then check the quality of soldering of the contacts again.

    Another common reason for the lack of text on the screen may be an incorrect i2c address. First try changing the device address in the sketch from 0x27 0x20 or to 0x3F. U different manufacturers different default addresses can be protected. If this does not help, you can run the i2c scanner sketch, which scans all connected devices and determines their address using brute force. An example of an i2c scanner sketch.

    If the screen still does not work, try unsoldering the adapter and connecting the LCD as usual.

    Conclusion

    In this article, we looked at the main issues of using an LCD screen in complex Arduino projects, when we need to save free pins on the board. A simple and inexpensive i2c adapter will allow you to connect a 1602 LCD screen, taking up only 2 analog pins. In many situations this can be very important. Payment for convenience - necessity of use additional module– converter and library. In our opinion, this is not a high price to pay for convenience and we highly recommend using this feature in projects.