• Arduino display connection. Connecting a text screen to Arduino

    This instruction shows how to connect to Arduino and use 16x2 and 20x4 LCD screens.

    These screens have a built-in backlight based on a low-power LED and operate from +5 V. To connect these LCD screens you will need 6 pins. You can use any pins on your Arduino!

    The instructions are written based on LCD screens from Adafruit - blue&white 16x2, RGB 16x2 LCD, and blue&white 20x4, RGB 20x4. If you are using an LCD screen from another manufacturer, there is no 100% guarantee that it will work (although it works 99% of the time).

    Character and graphic LCDs - what's the difference?

    There are a huge number of different LCD screens. In this article we will look at character LCDs. Similar screens- a great option for displaying text. You can also customize the display of icons, but the size of these icons should not exceed 7 pixels (very small!).

    The photo below shows an example of how a 16-character LCD monitor works with two lines:

    If you look closely, you will see small rectangles in which the symbols are displayed. Each rectangle is a separate grid of pixels. For comparison, a graphical LCD screen is shown below:

    A graphical liquid crystal display has one large grid of pixels (in in this example- 128x64). You can display text on it, but it is better to display images. Graphic LCDs are usually larger, have more pins for connection, and are somewhat more difficult to use than text LCDs.

    In this article we will only cover text/character screens!

    Different models of LCD screens

    After we have limited the type of screens under consideration, let's look at what they are.


    Even though they are only used to display text, there are different models and form factors: in the upper left corner there is a 20x4 LCD screen with white text on a blue background, in the upper right - 16x4 with black text on a green background, in the lower left - 16x2 with white text on a blue background and 16x1 with black text on a gray background.

    The good news is that all of these screens are interchangeable. If you have configured one of them, you can replace it with another model. The Arduino sketch will have to change a little, but the connection is the same!


    In this part we use LCD screens with one rail and 16 pins for connection (see photo above). There is also an LCD with 2 rails of 8 contacts for connection (in the figure below).


    Connecting the second model to a solderless circuit board is more difficult.

    Connecting a character LCD screen to Arduino

    Installing contact rails


    In addition to the LCD screen, you will need additional wiring. Firstly, a 10 kOhm potentiometer. Using the potentiometer we will adjust the contrast of the display. Each LCD screen has different contrast settings, so adjustments are a must. Additionally, you will need a 0.1" contact rail.


    If the contact rail is too long, extra contacts It’s fashionable to just cut it off!

    You need to solder the pins to the LCD display.

    When soldering, be extremely careful not to damage your Breadboard! You can first “grab” the first and 16 contacts, and then solder the rest.


    Power and lighting

    Connecting power and lighting

    Explanations


    We're starting to get to some interesting things! Install your LCD on the breadboard.


    We power the breadboard from our Arduino. Connect +5V to the red rail and Gnd to the blue rail.


    After this, we will connect the backlight of our LCD screen. Connect pin 16 to gnd and pin 15 to +5V. Most LCD screens have resistors for the backlight.

    If there are no resistors on your module, you will have to add one between 5V and pin 15. To calculate the resistor values, check the maximum current for powering the backlight and the approximate value of the voltage drop from the datasheet. Subtract the voltage drop value from 5V, then divide by the maximum amperage and round to the nearest higher standard resistor value. For example, if the voltage drop is 3.5V and the current is 16mA, the resistor value would be: (5 - 3.5)/0.016 = 93.75 ohms, or 100 ohms after rounding to the standard value. If you can't find the datasheet, use a 220 ohm resistor. True, in this case the backlight can be quite pale.


    Connect your Arduino to power. The backlight should light up.

    By the way, some cheap LCD screens do not have backlighting!

    Circuit for adjusting contrast

    Circuit for adjusting contrast

    Explanations


    Install the potentiometer. In the photo it is to the right of pin 1.


    Connect one side of the potentiometer to +5V and the other to Gnd. Connect the middle contact of the potentiometer to pin 3 on the LCD.


    Now we connect the logic of our screen - this is a separate circuit from the backlight! Pin 1 goes to Gnd and pin 2 goes to +5V.


    Turn on your Arduino. If on LCD Monitor There is a backlight, it should light up. Rotate the potentiometer knob to see the first rectangle of pixels on the first line.

    If everything worked, congratulations. This means that logic, lighting and contrast work! If it does not work, do not proceed to the next steps in the instructions until you find out what the error is!

    Final connection

    D0 to D7, RS, EN, and RW. D0-D7 are the pins that store the values ​​sent to the display. The RS pin tells the controller whether we will display data (e.g. ASCII character) or is a control byte (for example, changing the cursor position). The EN pin is short for "enable" and with this pin we tell the LCD when the data is ready to be read. The RW pin is used to set the direction - we want to display (usually) or read (less commonly used) data from the display.

    Not all of these pins need to be connected to the Arduino. For example, there is no need to use RW if we are only displaying data on the screen, so it is enough to “pull it up” to the Gnd pin. In addition, it is possible to communicate with the LCD screen using 4 pins instead of 8. It is likely that logical question, in what cases are 8 contacts used? This most likely affects the data transfer speed. That is, by using 8 contacts instead of 4, you can increase the speed of information exchange by 2 times. IN in this case,speed is not important so we use 4 pins to connect LCD to Arduino.

    So, we need 6 pins: RS, EN, D7, D6, D5, and D4.

    To work with the LCD screen, we will use the LiquidCrystal library, which greatly simplifies the process of setting up pins. One of the advantages of this library is that you can use any pins on the Arduino to connect the LCD pins. So by the end of this guide you will be able to easily replace the contacts if it is critical for your project.

    Final display connection

    Explanations


    As mentioned above, we won't be using the RW pin, so we'll pull it to ground. This is pin 5.


    Then we connect RS - this is pin #4. We use the brown wire to connect it to digital pin #7 on the Arduino.


    Use a white wire to connect the EN pin - pin #6 to the digital pin digital #8 on the Arduino.


    It's time for the data contacts. DB7 is pin #14 on the LCD. It is connected with an orange wire to pin #12 on the Arduino.


    There are three data pins left, DB6 (pin #13 yellow), DB5 (pin #12 green) and DB4 (pin #11 blue). They connect to pins #11, 10 and 9 on the Arduino respectively.


    As a result of Connection, you will get something similar to the photo on the left.

    We use a character LCD

    It's time to upload the sketch to Arduino to control the LCD screen. The LiquidCrystal library is installed in the Arduino IDE by default. So we just need to download one of the examples and adjust it a little in accordance with the pins that we used to connect.

    Open the sketch File→Examples→LiquidCrystal→HelloWorld.

    We update information about pins. We are looking for the following line:

    LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

    And change it to:

    Now you can compile and upload the sketch to the Arduino.


    If necessary, adjust the contrast


    Naturally, you can use the LCD display with any size. For example, the photo below shows the operation of a 20x4 LCD.


    Or black text on a green background:


    One of the advantages of screens with black text on a green background is the ability to turn off the backlight.


    Let's use how many lines

    Let's understand how the LCD screen handles long messages and multiple lines. For example, if you change the following line:

    lcd.print("hello, world!");

    To the next one:

    lcd.print("hello, world! this is a long long message");

    LCD display 16x2 will truncate everything after the 16th character:


    But the 20x4 LCD will carry the undisplayed characters from the first line to the third (the second line will continue to the fourth). It’s not very convenient, but at this stage you’ll have to put up with it. So when displaying long strings, count the characters so you don't exceed the allowed length.


    LCD with RGB backlight

    These screens work just like regular screens, but there are three LEDs (red, green, blue) for backlighting, so you can use different backlight colors.

    After connecting the LCD and checking it according to the instructions above, connect the LEDs to the PWM analog pins of your Arduino to fine-tune the colors. If you are using an Arduino Uno, you should have three free PWM pins left. connect the red LED (pin 16 on the LCD) to Digital 3, the green LED (pin 17) to Digital 5, and the blue LED (pin 18 on the LCD) to digital 6. The LCD module already has resistors, so you don’t need to connect additional ones necessary.


    Now upload the below sketch to your Arduino.

    // include the libraries in the sketch:

    #include

    #include

    #define REDLITE 3

    #define GREENLITE 5

    #define BLUELITE 6

    // declare the number of contacts we use

    // for data transfer

    LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

    // brightness can be changed in the range 0 -> 255

    int brightness = 255;

    // set the number of columns and rows on the LCD:

    lcd.begin(16, 2);

    // display the message on the LCD.

    lcd.print("RGB 16x2 Display ");

    lcd.setCursor(0,1);

    lcd.print(" Multicolor LCD ");

    pinMode(REDLITE, OUTPUT);

    pinMode(GREENLITE, OUTPUT);

    pinMode(BLUELITE, OUTPUT);

    brightness = 100;

    for (int i = 0; i< 255; i++) {

    setBacklight(i, 0, 255-i);

    for (int i = 0; i< 255; i++) {

    setBacklight(255-i, i, 0);

    for (int i = 0; i< 255; i++) {

    setBacklight(0, 255-i, i);

    void setBacklight(uint8_t r, uint8_t g, uint8_t b) (

    // set up the red LED - it's brighter than the others!

    r = map(r, 0, 255, 0, 100);

    g = map(g, 0, 255, 0, 150);

    r = map(r, 0, 255, 0, brightness);

    g = map(g, 0, 255, 0, brightness);

    b = map(b, 0, 255, 0, brightness);

    // common anode, so invert!

    r = map(r, 0, 255, 255, 0);

    g = map(g, 0, 255, 255, 0);

    b = map(b, 0, 255, 255, 0);

    Serial.print("R = "); Serial.print(r, DEC);

    Serial.print(" G = "); Serial.print(g, DEC);

    Serial.print(" B = "); Serial.println(b, DEC);

    analogWrite(REDLITE, r);

    analogWrite(GREENLITE, g);

    analogWrite(BLUELITE, b);

    The result of this sketch is shown in the video below.

    createChar command

    You'll probably want to use special characters. For example, if you are designing a project using a temperature sensor (thermocouple), the symbol (°) will be useful.

    This can be achieved using the createChar command. Alternatively, you might find a great website that will do all the dirty work of creating new symbols for you!

    Leave your comments, questions and share personal experience below. New ideas and projects are often born in discussions!

    Today we will try to focus on output to a text display. The most popular chip is the HD44780 (or its compatible KS0066). Let's list their pros and cons:

    Pros:

    1. Low price.
    2. Easy to program, the code will be the same for any model.
    3. Variety of models - the most common: 8x1, 16x2, 20x4. You can also find quite exotic 40x4 models, i.e. four lines of 40 characters each.
    4. Ability to connect multiple displays to one Arduino.
    5. Ability to set your own symbols.

    Cons:

    1. Not all displays support Russian characters. For more details, please refer to the description of the specific display.
    2. Connecting without using the I2C bus requires the use of 10-16 wires, which is very bad. with I2C - 4 wires.

    Based on the above, I will only consider connecting the display via I2C.

    Let's try it.

    What do we need?

    1. Arduino (I took the Nano model)
    2. Display on the HD44780 chip with or without an I2C module (then you will need a separate LC1602 IIC board) - in our case 16x2 without an I2C module
    3. 10Kohm resistor (If necessary manual control backlight).
    4. Potentiometer (If you need manual control of the backlight).
    5. Breadboard development board.
    6. LiquidCrystal_I2C library. http://www.ansealk.ru/files/LiquidCrystal_V1.2.1.zip

    Small digression No. 1: How to distinguish a display with an I2C module?

    It's actually quite simple. If, turning the display over, we see a long block of connectors (usually 16 pieces), then there is no I2C module on the display:

    And this is what the display looks like with the I2C module already installed:

    SCL, SDA, VCC, GND pins are used to connect Arduino. The two contacts on the left - in the picture they are closed with a jumper - are needed for the backlight to work.

    If the module is not connected, you will have to do it yourself. The main thing you should pay attention to is to connect the contacts in in the right order. Typically the first and 16 pins are marked. Sometimes it happens that 15-16 contacts, through which the backlight is controlled, can be located in front of the first (in this case they will be numbered). On the module itself, the first pin can also be designated not by a number, but by a square around the pin itself.

    Schemes:

    Let's put together the following diagram:

    I would like to draw your attention to the following points:

    1. If you come across a display with an I2C module already soldered, then the wires marked in gray will not be needed. Otherwise, nothing changes.
    2. If we do not want to change the brightness of the display, then the diagram will be simplified:

    As you noticed, two pins on the I2C module labeled LED are responsible for backlighting the display. If we don’t want to use brightness control, we can simply close them.

    Now let's look at the code.

    Almost everything here should be familiar to us. In line 5 we indicate the device address. In lines 16 and 17 - the number of characters in the line and the number of lines. In lines 20-22 - We create an object for working with the display and describe the parameter for working with it.

    Small digression No. 2: How to find out the address of an I2C device?

    For the most part, the address can be found in the datasheet for the chip on which the I2C device is built. If this is not possible, here is a link to an archive with a sketch and diagrams - http://www.ansealk.ru/files/Arduino_lcd_i2c.zip which determines the addresses of all devices connected via the I2C bus. You just need to connect the device to the Arduino, upload the sketch, open the console and see the address.

    Here we see a function that, in fact, will be responsible for output to the display. The output principle is something like this:

    We set the position of the beginning of the output using the setCursor() function

    Printing a string using the print() function

    After that next function print() will start output from the next position after which the previous input ended. I would also like to draw your attention to the fact that, unlike output to the console, the println() function is not used here to complete the output and line feed.

    Thus, the inscription “Test LCD1602” will appear on the screen in the first line, and the second line will indicate the display resolution and a counter showing how many cycles our sketch has completed.

    But, if we need to display many variable values ​​on the screen, this method is not entirely convenient. The fact is that the display output procedure is very energy-intensive and slow, and we make output in this function as many as 7 times. It will be much easier to generate the string in advance and then output it entirely. The sprintf() formatted input function will help us with this.

    Small digression #3: Formatted input function sprintf().

    In the C language there are several very convenient functions for outputting strings - they are called formatted output functions - printf (from the words print and format). In our specific case We are interested in the sprintf function, which does not print anything to the screen, but generates a string for subsequent output. It looks something like this:

    sprintf (str , "String %d for output ", i );

    The function generates a string (marked in blue) using a template (yellow) into which the values ​​of the variables (in green) are substituted. The resulting result will be written to a string variable (in red).

    There can be several templates and variables. In this case, variables are written separated by commas. The main thing is to ensure that the number of patterns in a line corresponds to the number of variables. Variables for templates are taken sequentially, i.e. the value of the first variable is substituted into the first template, the value of the second variable into the second, etc.

    What are templates? Any pattern begins with the "%" character and ends with one of ten (in the case of Arduino - seven) type characters. Between them there may be quite a lot of information about how to output the value, or there may be nothing at all.

    Let's look at what can be in the template. In general, the template looks like this:

    %[flag ][width ][.precision ]type

    Square brackets indicate that the element enclosed within them may be missing. A vertical bar indicates that one of the specified values ​​​​should be selected in this field (in our case, one of the letters H, I, or L).

    Let's first deal with the required element of the template - the type. It specifies what type of variable will be output and can take one of the following values:

    Symbol Meaning
    c One character
    s Character string
    d,i Signed decimal integer
    o Integer octal
    u Unsigned decimal integer
    x, X Integer hexadecimal
    p Pointer (in hexadecimal)
    f Fractional number in fixed format
    e,E Fractional number in scientific format
    g, G Fractional number in scientific or fixed format

    Those types that are not applicable when working with Arduino are marked in gray. Thus, to output a string you need to specify "%s", and to output an integer - "%d".

    Next, let's look at the width field. The number in it indicates the minimum width of the field in which the template will be displayed. If the size of the value in the variable is smaller, the field will be padded with spaces; if it is larger, the entry will go beyond the field. Thus, the template "%6d" for the number 385 will output 385 (note the three spaces before the number).

    The precision specifier always begins with a dot and the following number specifies different actions depending on the type of the value. For types "d,o,u,x" it will indicate the minimum number of characters that should appear during processing. For type "f" - the number of decimal places. For type "s" - the maximum number of string characters that will be output. For example, "%6.1f" for the number 34.2345 will output "34.1" (note that the dot is also considered a sign and there will be two spaces before the number). Or the pattern "%.3s" from the string "precision" will output only the first three characters - "precision".

    The flag allows you to change the display of the displayed value:

    You can read more about printf function templates on the Internet. Here I gave brief overview most frequently used features.

    So our output function, rewritten to use formatted output, would look like this:

    Note that in lines 33 and 37 we generate the entire line for output, and in lines 34 and 38 we print them.

    Finally, our favorite setup and loop functions.

    In line 47 we set the display resolution, in line 48 we turn on the backlight (the brightness of which can be adjusted with a potentiometer). In line 49 we set the cycle counter to zero. We will increase it by one in the 37th line of the output (remember the count++ construction?). Finally, on line 56 we call the display function discussed earlier. All.

    What can be changed or improved?

    For example, you can do automatic control illumination depending on the illumination, using a photoresistor or light sensor from the weather station discussed in several articles earlier. For example, in strong lighting conditions, increase the brightness of the backlight, and at night, reduce it. Or screw on a motion sensor and turn on the backlight when an object appears in front of the display, or... In general, I think you already understand that, if desired, by replacing one or more components and writing a piece of code, you can quite seriously improve the usability of the display. We can also use our own developed symbols for display.

    I do not consider all these questions here, since they are beyond the scope of a review for beginners.

    And that's all I have for today.

    Arduino. Connecting the LCD display

    26 ratings, Average rating: 5 out of 5

    In this lesson we will talk about character-synthesizing liquid crystal indicators, connecting them to the Arduino board and controlling the indicators using the LiquidCrystal and LiquidCrystalRus libraries.

    LED seven-segment indicators, although they are the cheapest indication option for electronic devices, but their use is limited by two significant drawbacks.

    • It is practically difficult to connect more than 8 bits to a microcontroller LED indicators. Requires a large number of pins, significant indicator currents, complex keys, low frequency regeneration, etc.
    • It is impossible to display symbolic information on seven-segment indicators.

    For withdrawal text information or numbers larger than 4 digits, it is much more practical to use liquid crystal sign-synthesizing indicators (displays). Their advantages include:

    • convenient interface for connecting to microcontrollers;
    • low power consumption;
    • low supply voltage;
    • durability.

    There are a wide variety of liquid crystal displays (LCD) on the market. different manufacturers. Almost all of them are similar in parameters, interface signals, and control commands. IN at the moment the most common LCD indicators on Russian market are devices manufactured by Winstar, Taiwan. I will refer to the indicators of this company. But the information is also quite applicable to character LCD displays from other manufacturers.

    General information.

    Character-synthesizing or symbolic indicators display information in the form of characters of a certain capacity. One familiarity displays one character. The number of familiar places determines the digit capacity of the indicator. Information on indicators can be displayed on several lines, so for indicators of this type the number of characters per line and the number of lines are always indicated.

    Information is displayed on a liquid crystal matrix with LED backlight. The backlight comes in a variety of colors, which greatly enlivens monochrome text information.

    To control the liquid crystal matrix and organize the indicator interface, use the built-in HD44780 controller or its complete analogues. This controller determines the indicator interface signals and control commands.

    The HD44780 has become the de facto standard for liquid crystal display (LCD) displays. Technical documentation via HD44780 controller PDF format You can view it at this link -. Maybe someone will like the documentation of one of the analogues of this controller - SPLC780D. Link in PDF format - .

    Character LCD Winstar indicators.

    I know of the following options for LCD indicators from this company.

    Indicator type Display format, characters x lines Dimensions, mm Dimensions of visible area, mm Link to documentation, PDF format
    WH0802A1 8 x 2 58 x 32 38 x 16
    WH1202A 12 x 2 55.7 x 32 46 x 14.5
    WH1601A 16 x 1 80 x 36 66 x 16
    WH1601B 16 x 1 85 x 28 66 x 16
    WH1601L 16 x 1 122 x 33 99 x 13
    WH1602A 16 x 2 84 x 44 66 x 16
    WH1602B 16 x 2 80 x 36 66 x 16
    WH1602C 16 x 2 80 x 36 66 x 16
    WH1602D 16 x 2 85 x 30 66 x 16
    WH1602J 16 x 2 80 x 36 66 x 16
    WH1602L1 16 x 2 122 x 44 99 x 24
    WH1602M 16 x 2 85 x 32.6 66 x 16
    WH1602O 16 x 2 85 x 25.2 66 x 16
    WH1602P 16 x 2 85 x 25.2 66 x 16
    WH1602S 16 x 2 59 x 29.3 52 x 15
    WH1602T 16 x 2 65.4 x 28.2 54.8 x 19
    WH1602W 16 x 2 80 x 36 66 x 16
    WH1602V2 16 x 2 66.7 x 23.3 61 x 15.9
    WH1604A 16 x 4 87 x 60 62 x 26
    WH1604B 16 x 4 70.6 x 60 60 x 32.6
    WH2002A 20 x 2 116 x 37 85 x 18.6
    WH2002D 20 x 2 89 x 21.5 75 x 15
    WH2002L 20 x 2 180 x 40 149 x 23
    WH2002M 20 x 2 146 x 43 123 x 23
    WH2004A 20 x 4 98 x 60 77 x 25.2
    WH2004B 20 x 4 98 x 60 77 x 25.2
    WH2004D 20 x 4 77 x 47 60 x 22
    WH2004G 20 x 4 87 x 58 74.4 x 24.8
    WH2004H 20 x 4 87 x 58 74.4 x 24.8
    WH2004L 20 x 4 146 x 62.5 123.5 x 43
    WH2402A 24 x 2 118 x 36 94.5 x 16
    WH4002A 40 x 2 182 x 33.5 154.4 x 16.5
    WH4004A 40 x 4 190 x 54 147 x 29.5

    LCD connection indicator to the microcontroller.

    Connection diagrams, timing diagrams, signal parameters, control commands, and symbol codes are described in detail in the documentation for the HD44780 controller. I will provide only the most necessary data on connecting indicators to microcontrollers.

    Typically, LCD indicators have 16 pins.

    Pin number Signal I - input O - output Signal purpose
    1 Vss - Ground (common)
    2 Vdd - Power supply +5V
    3 Vo - Display contrast control. Input for connecting the middle output of the voltage divider + 5 V. You can use a trimming resistor with a resistance of 10-20 kOhm.
    4 R.S. I Register selection: 0 – command register; 1 – data register. Low level signal means that a command has been generated on the data bus, high level– data on the bus.
    5 R/W I Data transfer direction:

    0 – record;

    1 – reading.

    In many applications the read function is not used, so the signal is often connected to ground.

    6 E I Bus operation strobe (on negative edge).
    7 DB0 I/O Low-order bits of eight-bit mode. Not used with a four-bit interface.
    8 DB1 I/O
    9 DB2 I/O
    10 DB3 I/O
    11 DB4 I/O The most significant bits of the eight-bit mode or the data bits of the four-bit interface.
    12 DB5 I/O
    13 DB6 I/O
    14 DB7 I/O
    15 A - Backlight power anode (+).
    16 K - Backlight power cathode (-). The current must be limited.

    The pin number (first column) is for the most common option. It’s better to check by downloading the documentation for your indicator type from the table in the previous section.

    Character LCD displays support two connection options to the microcontroller:

    • Using an 8-bit data bus. All bus signals DB0-DB7 are connected. A byte of information is transmitted in one exchange cycle.
    • Using a 4-bit data bus. Only the 4 most significant bits DB4-DB7 are connected. Information is transmitted four bits per bus clock cycle.

    The first option provides data transfer to the display at a higher speed. The second one requires 4 fewer pins to connect the indicator. Undoubtedly, it is more important to reduce the number of connection pins than to increase the exchange speed. Moreover, the LCD indicators are quite slow devices with a regeneration cycle time of 10-20 ms.

    Connecting a character LCD display to the Arduino board.

    I will connect the WH2004A indicator (4 lines of 20 characters each) in four-bit exchange mode to the board Arduino UNO R3. You can view the documentation for the WH2004 LCD display at this link.

    The diagram looks like this.

    Resistors R2 and R3 determine the contrast of the indicator. You can connect a trim resistor and set the required image clarity. I often use WH2004 indicators, and in my circuits I choose these resistor values.

    I connected the indicator backlight LEDs to a 5 V power source through resistor R1 (30 Ohm). With this I set the current to about 25 mA. Dim, but glowing. You can see well in the dark. Although WH2004 indicators allow backlight current up to 580 mA.

    Library for controlling LCD indicators in the Arduino LiquidCrystal system.

    There is a standard library for controlling LCD indicators based on the HD44780 controller. I will describe her methods in detail.

    LiquidCrystal(…)

    Class constructor. May have different number arguments.

    • LiquidCristal(rs, en, d4, d5, d6, d7) – four-bit interface, the RW signal is not used (connected to ground).
    • LiquidCristal(rs,rw, en, d4, d5, d6, d7) – four-bit interface, RW signal is used.
    • LiquidCristal(rs, en, d0, d1, d2, d3, d4, d5, d6, d7) – eight-bit interface, the RW signal is not used (connected to ground).
    • LiquidCristal(rs, rw, en, d0, d1, d2, d3, d4, d5, d6, d7) – eight-bit interface, RW signal is used.

    Arguments:

    • rs – RS signal pin number;
    • rw – RW signal output number;
    • en – signal output number E;
    • d0, d1, d2, d3, d4, d5, d6, d7 – data bus pin numbers.

    LiquidCrystal disp(6, 7, 2, 3, 4, 5);

    void begin(cols, rows)

    Initializes the indicator interface. Sets the indicator dimension. The method must be called first, before other class functions are used.

    Arguments:

    • cols – number of characters in the line;
    • rows – number of rows.

    disp.begin(20, 4); // we use a display - 4 lines of 20 characters

    void clear()

    Clearing the screen, placing the cursor in the upper left corner.

    disp.clear(); // reset display

    void home()

    Place the cursor in the upper left corner.

    disp.home(); // to the beginning of the screen

    void setCursor(col, row)

    Places the cursor at the specified position.

    • col – X coordinate, numbering from 0;
    • row – Y coordinate, numbering from 0.

    setCursor(0,1); // cursor to the beginning of the second line

    byte write(data)

    Displays a symbol on the screen. Returns the number of bytes transferred.

    The following sketch displays data from serial port. Data can be transmitted by the Arduino IDE port monitor.

    // Serial port data output on LCD indicator
    #include


    char data;

    void setup()
    {
    Serial.begin(9600); // initialize the serial port
    disp.begin(20, 4); //
    }

    void loop()
    {
    if (Serial.available()) ( // if there is data
    data= Serial.read(); // read the symbol
    if((data != 0xd) && (data != 0xa)) ( // line feed
    disp.write(data); // display the symbol on the screen
    }
    }
    }

    I have a large indicator - 4 lines of 20 characters. It contains two HD44780 controllers. Therefore, sequentially transmitted characters fill first the first line, then the third, then the second and fourth. Those. through the line. This property must be taken into account for certain types of indicators. The documentation for each LCD indicator indicates the character addressing sequence.

    byte print(data)

    Displays text on the screen. Returns the number of bytes transferred.

    Arguments:

    data – data to be displayed on the screen. May have char types, byte, int, long, string.

    There may be a second, optional argument.

    byte print(data, BASE)

    • BASE – specifies the number system:
    • BIN – binary;
    • DEC – decimal;
    • OCT – octal:
    • HEX – hexadecimal.

    An example of a program that prints a text string on the display.

    // displaying a text line on the LCD indicator
    #include

    LiquidCrystal disp(6, 7, 2, 3, 4, 5); // create an object

    void setup()
    {
    disp.begin(20, 4); // initialize the display 4 lines of 20 characters
    disp.print("Test string");
    }

    void loop()
    { }

    void cursor()

    Enables cursor display mode. The position where the next character will be output is underlined.

    disp.cursor(); // allow cursor display

    void noCursor()

    Disables display of the cursor.

    disp.noCursor(); // disable cursor display

    void blink()

    Enables blinking cursor mode. Used in conjunction with the cursor() function. The result depends on specific model indicator.

    disp.blink(); // allow blinking cursor

    void noBlink()

    Disables the blinking cursor mode.

    disp.noBlink(); // disable blinking cursor

    void display()

    Turns on the screen after it has been turned off by noDisplay(). The screen will display the information that was there before the shutdown.

    display.display(); // turn on the display

    void noDisplay()

    Turns off the screen. The information is stored in memory and appears when the display is turned on.

    disp.noDisplay(); // turn off the display

    void scrollDisplayLeft()

    Scrolls the display contents one character to the left.

    disp. scrollDisplayLeft(); // move everything to the left

    void scrollDisplayRight()

    Scrolls the display contents one character to the right.

    disp. scrollDisplayRight(); // move everything to the right

    void autoscroll()

    Enable automatic text scrolling mode. As each character is displayed, all text on the screen will shift by one character. The leftToRight() and rightToLeft() functions determine which direction the information is shifted.

    disp. autoscroll())(; // enable autoscroll

    void noAutoscroll()

    Turn off automatic text scrolling.

    disp. noAutoscroll())(; // disable autoscrolling

    void leftToRight()

    Sets the test output mode from left to right. New symbols will appear to the right of the previous ones.

    leftToRight(); // left-to-right mode

    void rightToLeft()

    Sets the test output mode from right to left. New symbols will appear to the left of the previous ones.

    rightToLeft(); // right-to-left mode

    void createChar(num, data)

    Method for creating a custom symbol. The controller allows the creation of up to 8 characters (0...7) of 5x8 pixels. The symbol image is specified by an 8-byte array. The least significant 5 bits of each byte determine the state of the pixels in the row.

    To output a custom character, you can use the write() function with the character number.

    // creating a custom symbol
    #include

    LiquidCrystal disp(6, 7, 2, 3, 4, 5); // create an object

    byte smile = (
    B00000000,
    B00010001,
    B00000000,
    B00000000,
    B00010001,
    B00001110,
    B00000000,
    B00000000
    };

    void setup()
    {
    disp.createChar(0, smile); // create a symbol
    disp.begin(20, 4); // initialize the display 4 lines of 20 characters
    disp.print("Smile ");
    disp.write(byte(0)); // print the symbol
    }

    void loop()
    { }

    Here is an example of a program that displays the Russian alphabet.

    // Russian alphabet output
    #include

    LiquidCrystalRus disp(6, 7, 2, 3, 4, 5); // create an object

    void setup()
    {
    disp.begin(20, 4); // initialize the display 4 lines of 20 characters
    disp.print("abvgdeezhziyklmnoprst");
    disp.print("ABVGDEYOZHIYKLMNOPRST");
    disp.print("ufhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh,");

    Category: . You can bookmark it.

    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 Arduino digital ports; 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 operation is the same - the serial port monitor displays the distance to the object 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.

    • The FC-113 module is based on the PCF8574T chip, which is an 8-bit shift register - an input-output “expander” for the I2C serial bus. In the figure, the microcircuit is designated DD1.
    • R1 is a trim resistor for adjusting the contrast of the LCD display.
    • Jumper J1 is used to turn on the display backlight.
    • Pins 1…16 are used to connect the module to the LCD display pins.
    • Contact pads A1...A3 are needed to change the I2C address of the device. By soldering the appropriate jumpers, you can change the device address. The table shows the correspondence of addresses and jumpers: “0” corresponds to an open circuit, “1” to an installed jumper. By default, all 3 jumpers are open and the device address 0x27.

    2 Connection diagram for LCD display to Arduino via I2C protocol

    The module is connected to Arduino as standard for the I2C bus: the SDA pin of the module is connected to analog port A4, SCL pin - to analog port A5 of Arduino. The module is powered by +5 V from Arduino. The module itself is connected by pins 1…16 to the corresponding pins 1…16 on the LCD display.


    3 Library for work via I2C protocol

    Now we need a library to work with LCD via the I2C interface. You can use, for example, this one (link in the line "Download Sample code and library").

    Downloaded archive LiquidCrystal_I2Cv1-1.rar unzip to a folder \libraries\, which is located in the Arduino IDE directory.

    The library supports a set of standard functions for LCD screens:

    FunctionPurpose
    LiquidCrystal() creates type variable LiquidCrystal and accepts display connection parameters (pin numbers);
    begin() initializing the LCD display, setting parameters (number of lines and characters);
    clear() clearing the screen and returning the cursor to the starting position;
    home() return the cursor to the starting position;
    setCursor() setting the cursor to a given position;
    write() displays the symbol on the LCD screen;
    print() displays text on the LCD screen;
    cursor() shows the cursor, i.e. underlining under the place of the next character;
    noCursor() hides the cursor;
    blink() cursor blinking;
    noBlink() Cancel flashing;
    noDisplay() turning off the display while saving all displayed information;
    display() turning on the display while saving all displayed information;
    scrollDisplayLeft() scroll the display contents 1 position to the left;
    scrollDisplayRight() scroll the display contents 1 position to the right;
    autoscroll() enable autoscroll;
    noAutoscroll() disable autoscroll;
    leftToRight() sets the text direction from left to right;
    rightToLeft() text direction from right to left;
    createChar() creates a custom character for the LCD screen.

    4 Sketch for text output to LCD screen via I2C bus

    Let's open the sample: File Samples LiquidCrystal_I2C CustomChars and we'll rework it a little. We will display a message at the end of which there will be a blinking symbol. The comments to the code comment on all the nuances of the sketch.

    #include // include the Wire library #include // connect the LCD library #define printByte(args) write(args); // uint8_t heart = (0x0,0xa,0x1f,0x1f,0xe,0x4,0x0); // bit mask of the “heart” symbol LiquidCrystal_I2C lcd(0x27, 16, 2); // Set address 0x27 for 16x2 LCD display void setup() ( lcd.init(); // initializing the LCD display lcd.backlight(); // turn on the display backlight lcd.createChar(3, heart); // create a “heart” symbol in memory cell 3 lcd.home(); // place the cursor to the left top corner, to position (0,0) lcd.!"); // print a line of text lcd.setCursor(0, 1); // move the cursor to line 2, character 1 lcd.print(" i "); // print message on line 2 lcd.printByte(3); // print the “heart” symbol located in the 3rd cell lcd.print(" Arduino "); } void loop() (// flashing the last character lcd.setCursor(13, 1); // move the cursor to line 2, character 1 lcd.print("\t"); delay(500); lcd.setCursor(13, 1); // move the cursor to line 2, character 1 lcd.print(" "); delay(500); }

    By the way, the characters written by the command lcd.createChar();, remain in the display memory even after turning off the power, because written to display ROM 1602.

    5 Create your own symbols for LCD display

    Let's take a closer look at the issue of creating your own symbols for LCD screens. Each character on the screen consists of 35 dots: 5 wide and 7 high (+1 reserve line for underlining). In line 6 of the above sketch we define an array of 7 numbers: (0x0, 0xa, 0x1f, 0x1f, 0xe, 0x4, 0x0). Let's convert hexadecimal numbers to binary: {00000, 01010, 11111, 11111, 01110, 00100, 00000} . These numbers are nothing more than bit masks for each of the 7 lines of the symbol, where "0" denotes a light point and "1" a dark point. For example, a heart symbol specified as a bitmask will appear on the screen as shown in the figure.

    6 LCD screen control via I2C bus

    Let's upload the sketch to Arduino. The inscription we specified with a blinking cursor at the end will appear on the screen.


    7 What's behind I2C bus

    As a bonus, let's look at the timing diagram for displaying the Latin characters "A", "B" and "C" on the LCD display. These characters are stored in the display ROM and are displayed on the screen simply by transmitting their addresses to the display. The diagram is taken from the RS, RW, E, D4, D5, D6 and D7 pins of the display, i.e. already after the FC-113 “I2C parallel bus” converter. We can say that we are diving a little deeper into the hardware.


    Timing diagram of the output of Latin characters “A”, “B” and “C” on the LCD display 1602

    The diagram shows that the characters that are in the display ROM (see page 11 of the datasheet, link below) are transmitted in two nibbles, the first of which determines the table column number, and the second - the row number. In this case, the data is “latched” at the edge of the signal on the line E(Enable), and the line R.S.(Register select) is in a logical one state, which means data is being transferred. A low state on the RS line means instructions are being sent, which is what we see before each character is transmitted. In this case, the carriage return instruction code is transmitted to position (0, 0) of the LCD display, which can also be found out by studying technical description display.

    And one more example. This timing diagram shows the output of the Heart symbol on the LCD display.


    Again, the first two impulses Enable comply with instructions Home()(0000 0010 2) - return the carriage to position (0; 0), and the second two - output to the LCD display stored in memory cell 3 10 (0000 0011 2) the “Heart” symbol (instruction lcd.createChar(3, heart); sketch).