Connecting a 1602 display to an Arduino mega. Connecting the LCD to the Arduino board. The read operation is implemented similarly

For some time, this display lay idle.


And now there is a desire to attach it to one of the projects. You can, of course, try to find a library with ready-made functions, but in this case the picture of how the display works will be incomplete, and we are not happy with that. Once you understand the operating principle of an LCD display, it will not be difficult to write your own library for the desired display if it is missing or is not satisfactory in some way.

So, let's begin.
The first thing to do is to find the pinout, that is, which contact is responsible for what, the second is to find the name of the controller that controls the display, to do this, download the datasheet for this LCD and open it on the first page.


Contacts are counted from left to right, the first one is marked with a red arrow. The supply voltage is 5 volts, the control controller S6A0069 or similar, for example, ks0066U.

Why were we looking for the name of the control controller? The fact is that in the datasheet on the display there are time delays (timing diagram), the command system is described, but there is no banal initialization, and without it there is nowhere.
Next, open the second page and see a table that says which contact is responsible for what.


DB7…DB0– data/address bus.

R/W- determines what we will do, read (R/W=1) or write (R/W=0)

R/S– determines whether we will send a command (RS=0) or data (RS=1)

E– strobe input, by changing the signal at this input we allow the display to read/write data.

LED±– backlight control.

I must say that on the display I received, the backlight will not just turn on; to do this, you need to solder in a resistor, marked on the board as R7. But for now we don’t need it.

Download the datasheet for the control controller and find the initialization instructions. Pictures can be enlarged by clicking on them.



It turns out that there are two such instructions, for 8-bit and 4-bit modes. What kind of modes are these? These modes determine how many wires data will be transmitted: four or eight. Let's look at the transmission 4 wires, in this case, the display will work slower, but we will save 4 pins of the microcontroller, and the implementation of the eight-bit mode is not much different.

The information connection diagram is as follows.


Contrast can be adjusted by connecting a potentiometer between the power pins.

I would like to draw your attention to the fact that during initialization R/S And R/W are always equal to zero, that is, we will send teams.

During initialization you can configure:

  • N - number of displayed lines
  • C - turn the cursor on or off
  • B - make the cursor blink
  • I/D - increase or decrease the address counter value
  • SH - move display window
Let's look at the last two points in more detail.
The picture below shows at what address we need to write data so that it is displayed in a certain position, for example, if we want to display a symbol on first position of second line, then we must write to address 0x40.


After this, the counter value will automatically change, either increase or decrease, and along with it the position of the cursor will change.

By the way, the memory we write to is called DDRAM, everything that we write into this memory will be displayed on the display, there is still CGROM, which stores the character generator table.


This table cannot be changed, but ready-made symbols can be taken from it. Another type of memory is CGRAM, it is also a character generator table, but we draw the characters in this table ourselves.


Now a few words about the movement of the screen, the fact is that usually on the display we do not see all of the DDRAM, but only a certain part, as shown in the picture below.


We can also write in the invisible part, but what we write will not be visible until we move the screen window to this place.

We're done with theory, let's move on to practice.
The picture of communication with an LCD display in 4-bit mode looks like this.


Data is sent in bytes, but since we have a 4-bit mode, in order to send a byte you need to make 2 sendings, with the most significant bit first. In the picture, the first parcel is designated D7 (high tetrad), the second D3 (low tetrad). Before the next sending, we must check the busy flag and if it is not set, we can send again; if it is set, we wait until the controller that controls the LCD finishes its business.

Having a general picture of the sending, let's figure out how to implement the sending operation.


To send you need to use an 8-bit bus:
  • R/W set to 0
  • issue command code/data to the bus
  • delay 2us
  • lower strobe E

The read operation is implemented similarly:

  • make sure that the control controller is free
  • R/W set to 1
  • raise strobe E (at this moment the LCD will output data to the bus)
  • delay 2us
  • we read what the LCD gave
  • lower strobe E
Where did the 2us delay come from?

Above the timings there is a table that says what the delays shown on the graph are equal to, and so the duration of the strobe pulse - tw should be equal to 230nS or 450nS depending on the supply voltage, we took it a little with a margin. Why did we only take this delay into account? Because the value of the remaining delays is very small.

To send over a 4-bit bus:

  • make sure that the control controller is free
  • set RS to 0 (command) or 1 (data), depending on what we will send
  • R/W set to 0
  • raise strobe E (set to 1)
  • we issue the highest notebook to the bus
  • delay 2us
  • lower strobe E
  • delay 1us
  • raise strobe E (set to 1)
  • we issue the low tetrad to the bus
  • delay 2us
  • lower strobe E

To read on a 4-bit bus:

  • make sure that the control controller is free
  • data port for input with pull-up
  • set RS to 0 (command) or 1 (data), depending on what we will read
  • R/W set to 1
  • raise strobe E (set to 1)
  • delay 2us
  • read the senior notebook
  • lower strobe E
  • delay 1us
  • raise strobe E (set to 1)
  • delay 2us
  • we read the lower notebook
  • lower strobe E

Raising the strobe and outputting the command/data to the bus can be swapped. Now it will not be difficult to initialize the display. To simplify initialization, we will replace reading the busy flag with a delay, and we will consider working with the flag later.
It should be noted that during initialization in 4-bit mode, 4-bit instructions are used, and after initialization, an 8-bit instruction system is used, so for initialization we implement a separate function for sending commands void Write_Init_Command(uint8_t data).
//Initialization code for Atmega16 #define F_CPU 8000000UL #define LCD_PORT PORTA #define LCD_DDR DDRA #define LCD_PIN PINA #define DATA_BUS 0XF0 #define RS 0 #define RW 1 #define E 2 #include #include void Write_Init_Command(uint8_t data) ( //legs through which commands/data are transmitted to the LCD_DDR output |= DATA_BUS; //we will send the command LCD_PORT &= ~(1<A cheerfully blinking cursor indicates that the initialization was successful. IN

  • 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 in a standard way for the I2C bus: the SDA pin of the module is connected to analog port A4, the SCL pin is connected 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 a LiquidCrystal type variable 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 change it up 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 in the upper left corner, at 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 the 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). 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 p. 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 instruction code for carriage return to position (0, 0) of the LCD display is transmitted, which can also be found out by studying the technical description of the 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).

Sometimes we are faced with the problem of outputting various information from Arduino to the outside world. Often, using a serial port is impossible, inconvenient and unprofitable.

A character display is one of the simplest and cheapest means of displaying information because it has its own microcontroller that stores the encoded characters. This system simplifies the use of these displays, but at the same time limits their use to displaying only text information, unlike graphic displays.

In the example, we will look at the Winstar wh1602l1 display, one of the most common displays on the hd44780 controller. In addition, you can connect LCD 2004 and other similar ones.
The first two digits indicate the number of characters per line, and the second the number of lines, so the selected display has 2 lines of 16 characters.
This connection method involves occupying at least 6 ports of the Arduino microcontroller. If necessary, you can connect the 1602 text display via the I2C interface (2 ports).

Of the additional elements, we need a variable resistor to control the contrast. Otherwise, everything is connected according to the diagram, according to the datasheet and the selected Arduino outputs in the program.

Pins 15 and 16 on the display are responsible for the backlight; it can be turned off or the brightness can be automatically adjusted when connecting a photoresistor to the Arduino as a brightness sensor.

In our example, we will read data from the serial port and display it on the display:

#include // Connect the library for working with character displays LiquidCrystal lcd(13, 11, 5, 4, 3, 2); // (RS, E, D4, D5, D6, D7) connect the display outputs according to the sequence, R/W – GND, since we will write data to the display and not read void setup() ( lcd.begin(16, 2); // Initialize LCD 1602 // lcd.begin(20, 4); // Initialize LCD 2004 Serial.begin(9600); // Start the serial port ) void loop() ( if (Serial.available()) // If data comes from the port, then... ( delay(100); lcd.clear(); // Completely clear the screen while (Serial.available() > 0) // If data comes from the port greater than 0, then ... ( lcd.write(Serial.read()); // Read values ​​from the serial port and display them on the display ) ) )

You can complicate the code and output the DS1307 real time clock on Arduino to your LCD1602.

Now let's take a closer look at all the functions in the library LiquidCrystal:

The first and most important thing is that using this library you cannot display Russian letters, even if the display has these characters in memory. This problem can be solved either by other libraries, or by writing values ​​using hexadecimal code.

lcd.print();- the simplest and most frequently used, used to display information.

lcd. clear(); - used to clean the display.

lcd.setCursor(x, y); - places the cursor at a specific place.

X – change of position in the line

Y – line change

For example, lcd.setCursor(0, 0); this is the top left cell.

lcd.home(); - places the cursor at position 0, 0

lcd.home(); = lcd.setCursor(0, 0);

lcd. scrollDisplayLeft(); - shift left

lcd. scrollDisplayRight(); - shift right

Lcd.createChar(Name, array); - creating your own sign.

For example, the degree sign looks like this:

Celc = (B00111, B00101, B00111, B00000, B00000, B00000, B00000, B00000 );

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.

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

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

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

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

There are a large number of different liquid crystal display (LCD) indicators from different manufacturers on the market. Almost all of them are similar in parameters, interface signals, and control commands. At the moment, the most common LCD indicators on the 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, the built-in HD44780 controller or its full analogues are used. 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 for the HD44780 controller in PDF format can be viewed at this link -. Maybe someone will like the documentation of one of the analogues of this controller - SPLC780D. Link in PDF format - .

Character LCD indicators from Winstar.

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

Connecting an LCD indicator to a 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. A low signal level means that a command has been generated on the data bus, a high level means that a command has been generated on the data 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. In one exchange cycle, a byte of information is transmitted.
  • 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, LCD indicators are rather 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 Arduino UNO R3 board. 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 a different number of 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 the 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.

The function has different call forms for different formats and data types.

print(char d) If a char argument outputs the character code

char d= 83;
disp.print(d); // prints the character S
disp.print('S'); // prints the character S

print(int d) If the argument is an integer type, then prints a string with the decimal representation of the number

int d= 83;
disp.print(d); // prints the line “83”
disp.print(83); // prints the line “83”

print(float) Real types are output as ASCII characters, two decimal places

float d= 7.65432;
disp.print(d); // prints the string “7.65”
disp.print(7.65432); // prints a string “7.65”

print(* str) If the argument is a pointer to a string, then the text string is printed.

char letters= (65, 66, 67);
disp.print(“Letters”); // prints the string “Letters”
disp.print(letters); // outputs a string of 3 characters with codes 65, 66, 67

print(int d, DEC) Prints an ASCII string - the decimal representation of a number

int d= 83;
disp.print(d, DEC); // output the string “83”

print(int d, HEX) Outputs an ASCII string - hexadecimal representation of a number

int d= 83;
disp.print(d, HEX); // output the line “53”

print(int d, OCT) Prints an ASCII string - the octal representation of a number

int d= 83;
disp.print(d, OCT); // output the line “123”

print(int d, BIN) Outputs an ASCII string - the binary representation of a number

int d= 83;
disp.print(d, BIN); // output the string “01010011”

print(float d, N) For real numbers, the N parameter specifies the number of digits after the decimal point.

disp.print(7.65432, 0); // prints the string “7”
disp.print(7.65432, 2); // prints the string “7.65”
disp.print(7.65432, 4); // outputs the string “7.6543”

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 the specific indicator model.

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("ughhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
disp.print("UFHTSCHSHSHSHYYYYYYYYYYYYYYYYYYYYYYY");
}

void loop()
{ }

Liquid crystal display (LCD) mod. 1602 (datasheet) is an excellent choice for your projects.

The first thing that pleases us is the low price. The second is the availability of ready-made libraries for Arduino. Third, there are several modifications, which also come with different backlights (blue, green). In this article, we will look at the basics of connecting this display to Arduino and give an example of a small project to display the light level on a display using a photoresistor.

Contacts and connection diagram of LCD 1602 to Arduino

The pins on this display are numbered from 1 to 16. They are located on the back of the board. How exactly they connect to Arduino is shown in the table below.

Table 1. Connecting LCD 1620 pins to Arduino

Connecting the 1602 to the Arduino If the 1602 display is powered by the Arduino via a 5-volt USB cable and the corresponding pin, a 2 kOhm value can be used for the display contrast pin (3rd connector - Contrast). For the Back LED+ contact, you can use a 100 Ohm resistor. You can also use a variable resistor - potentiometer to manually adjust the contrast.

Based on Table 1 and the diagram below, connect your LCD display to the Arduino. To connect you will need a set of conductors. It is advisable to use different colored wires to avoid confusion.

Table 2. Preferred conductor colors

Connection diagram for LCD display 1602 to Arduino:


Basic example program for working LCD 1602 with Arduino

The example uses Arduino pins 0, 1, 2, 3, 4, and 5 to connect the corresponding pins 4, 6, 11, 12, 13, and 14 from the 1602 display (see Table 1). After that, in the Arduino code, we initialize lcd() as follows:

LiquidCrystal lcd(0, 1, 2, 3, 4, 5);

This piece of code explains to the Arduino exactly how the LCD display is connected.

The entire source file of the weather station project, which uses the LCD 1602 display, can be downloaded from this link.

LCD 1602A, Arduino and light sensor (photoresistor)

In the example, we will look at connecting a display modification - 1602A and a photoresistor. As a result of this project, we will be able to display numerical values ​​proportional to the light intensity on the display.


This example will be a good start for beginners to understand Arduino. It is worth noting that the 1602 display has various modifications. Accordingly, the location of the contacts on them may differ slightly.

Necessary materials

  • 1 Arduino UNO;
  • 1 breadboard (63 rails);
  • 1 light sensor (photoresistor);
  • 1 potentiometer 50 kOhm;
  • 1 LCD display 1602A;
  • 1 resistor 10 kOhm;
  • 1 rail of connectors (16 pins);
  • 1 USB cable.

LCD Display 1602A

Displays are usually sold without soldered connectors. That is, you will have to hold the soldering iron in your hands. You will need 16 pins. Solder on the side of the short legs, leave the long legs for further connection to the board or other peripheral devices.

After desoldering, you can install the display on the breadboard. Preferably, on the lowest track, so that you still have the opportunity to connect the display through additional connectors to the board.

Connecting 1602A Display to Arduino

The first thing you need to do is power the display. Connect the two cables from +5 volts and ground to the corresponding plus/minus rows on the breadboard.

Connect: the 5 volt (5V) pin from the Arduino to one of the breadboard tracks.

Connect: Arduino's Ground (GND) pin to another track (breadboard).

After this, we connect the power of the screen and its backlight to the tracks on the breadboard, on which we get 5 volts and minus.

Connect: the GND (minus) track on the breadboard to pin 1 on the LCD screen (labeled VSS).

Connect: the 5 volt (plus) track on the breadboard to pin 2 on the LCD screen (labeled VDD).

Connect: the 5 volt (plus) track on the breadboard to pin 15 on the LCD screen (labeled A).

Connect: the GND (minus) track on the breadboard to pin 16 on the LCD screen (labeled K).

We connect our Arduino to a personal computer via a USB cable and voila! The screen should turn on.

The next step is to connect a potentiometer to adjust the display contrast. Most guides use a 10k ohm potentiometer, but 50k ohms will work too. Due to the larger range of resistance values ​​at the output of the potentiometer, more precise adjustment becomes more difficult, but for us in this case this is not critical. Mount the potentiometer on the breadboard and connect its three pins.

Connect: the first pin on the potentiometer to the negative on the breadboard.

Connect: the middle pin of the potentiometer to pin 3 on the display (labeled V0).

Connect: the third pin on the potentiometer to the positive on the breadboard.

After power is supplied to the board via the USB cable, the first row of the display should be filled with rectangles. If you don't see them, turn the potentiometer slightly from left to right to adjust the contrast. Later, when we display numerical values ​​on the screen, you will be able to adjust the contrast more precisely. If your display looks something like this, you're doing it right:

Let's continue. Now we need to communicate between the Arduino and the 1602A LCD to display the characters.

To do this, connect 4 pins of the display (RS) to 7 pins of the Arduino (yellow connector). Display pin 5 (RW) – to the row of ground pins on the breadboard (black cable).

Display pin 6 (E) – to Arduino pin 8 (PWM).

Display pin 11 (D4) – to Arduino pin 9 (PWM).

Display pin 12 (D5) – to Arduino pin 10 (PWM).

Display pin 13 (D6) – to Arduino pin 11 (PWM).

Display pin 14 (D7) – to Arduino pin 12 (PWM).

Program for Arduino IDE - displaying text on the 1602A display

The piece of code presented below just needs to be copied and pasted into the Arduino IDE and loaded onto the board:

#include <LiquidCrystal.h>

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

lcd.begin(16, 2);

lcd.setCursor(0,1);

lcd.write("LIGHT: ");

After loading the program onto the board, the following message will appear on the second line of the display:

A kind of "hello world!" launched on LCD 1602A. I congratulate you.

We connect the photoresistor and upload the entire program to Arduino

Now let's connect the photoresistor. Connect three wires to the free rails on the breadboard (let's call them 1, 2, 3). Leave some space in the rails for the light sensor itself and the resistor.

We connect the GND rail from the breadboard to rail 1. A0 (analog input) from Arduino - to rail 2. 5 volts from the breadboard - to rail 3.

Next we connect our sensor and resistor to the prepared rails. Which legs go to the ground and which ones go to the power supply does not matter for our light sensor and resistor (unlike, for example, an LED, which has a cathode and an anode). So there is no confusion here.

We connect the light sensor to rail 1 and rail 2. The resistor is connected to rail 2 and rail 3.

Now let's go back to our program and add a few lines to the currently empty body of the loop() function:

int sensorValue = analogRead(A0);

lcd.setCursor(7,1);

lcd.print(sensorValue);

After uploading the final version of our program to the Arduino, the display will display the current light level values.



What else to read