Connecting LCD to Arduino board. Connecting and working with the display WH1602 Lcd 1602 dimensions

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 display options, 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 is popular 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 the Arduino Uno:

Sample 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 the Arduino IDE and does not need to be downloaded and installed additionally. 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 all! 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 on 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 an LCD 1202 connection to Arduino without I2C, it is very easy to get tangled in the wires, which may cause the display to not work correctly. 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. Another common reason why the display may not work is incorrect setting of the I2C address. 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 affordable 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!

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 );

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 this does not suit us. 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 is as follows.


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 it 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

When assembling my metal detector, I ended up with an LCD display 1602, built on the HD44780 controller. I decided not to miss the opportunity and connect it to my Chinese analogue of Arduino UNO.

This is the 1602 display we will connect to Arduino today.

The numbers “1602” indicate that the display consists of 2 lines, 16 characters each. This is a fairly common screen, using which people construct watches, testers and other gadgets. The display comes with green and blue backlighting.

I soldered a comb of contacts to the display so that I could easily connect the wires.

We will connect the 1602 display to Arduino via a 4-bit parallel interface. There is also an option for an 8-bit interface, but it uses more wires, and we won’t see any gains in this.

In addition to the display and Arduino, we will need wires and a 10 kOhm variable resistor. The resistor will fit any brand, as long as it has the required value.

Power is supplied to the display via 1st (VSS) And 2nd (VDD) conclusions. To conclusions 15 (A) And 16 (K)- power is supplied to the display backlight. Since the same +5V voltage is used for power and lighting, we will power them from the Arduino pins "5V" And "GND". The main thing is not to confuse the polarity, otherwise you can burn the display electronics.

3rd conclusion (V0) We connect a variable resistor to the leg, we will use it to control the contrast of the display. The resistor can not be used, but the output "V0" connect to GND. In this case, the contrast will be maximum and there will be no possibility of its smooth adjustment.

5th conclusion (RW) used to read from or write to the display. Since we will only write to the display, we will connect this pin to ground (GND).

Conclusions: 4th (RS), 6th (E), 11th (D4), 12th (D5), 13th (D6), 14th (D7) connect to Arduino digital pins. It is not necessary to use the same pins as mine, you can connect to any digital ones, the main thing is then to set them correctly in the sketch.

My connected Arduino, all I have to do is connect it to the computer via USB and upload the sketch.

In the sign we will use a sketch from the standard set.

In Arduino IDE we select "File" -"Samples" -"Liquid Crystal" - "Hello World".

Let's look at the sketch code.

In line "Liquid Crystal lcd", in brackets, are the digital pins that are used on the Arduino. Pins are displayed in the following sequence: RS, E, DB4, DB5, DB6, DB7. If you used other digital pins when connecting the display, enter them in the correct sequence in brackets.

In line "lcd.print("hello, world!");" a greeting is displayed on the display, by default this is the inscription "Hello, world!", you can change it to any of your own, we write in Latin.

We upload the sketch to Arduino and here is the result. Instead of “hello, world!” I entered my website. The line below, the timer counts down the time.

Character display LCD1602 with blue backlight - liquid crystal display (Liquid Crystal Display) whose screen is capable of simultaneously displaying up to 32 characters (16 columns, 02 rows). Connection to is carried out via a synchronous 8-bit parallel interface. Examples of working with character displays are described in the section.

Characteristics

  • Type of output information: symbolic.
  • Language in display ROM: Latin, Japanese.
  • Ability to upload your own symbols: yes.
  • Output information format: 16×02 characters;
  • Display type: LCD.
  • Display technology: STN.
  • Viewing angle: 180°.
  • Backlight type: LED.
  • Backlight color: blue.
  • Character color: white.
  • Controller: HD44780.
  • Interface: synchronous, 8-bit, parallel.
  • Supply voltage 5 V.
  • Operating temperature: -20 ... +70 °C.
  • Storage temperature -30 ... +80 °C.
  • Dimensions: 80x36 mm.

Connection

Connecting the LCD1602 display via a parallel interface is a little more complicated than via the I2C bus, and an additional element is also required to adjust the contrast.

The display data bus consists of 8 lines (D0-D7), but if you connect only the higher 4 lines (D4-D7), as shown in the figure, this does not reduce the speed of the display. For convenience, we connected pins D4-D7 of the display to pins of the same name D4-D7 . You can connect pins D0-D7, E and RS of the display to any pins by specifying them in the sketch when declaring the library object.

№: Display: Arduino: Purpose:
16 K(LED-) GND Cathode (minus) of LED backlight.
15 A(LED+) 5V Anode (plus) of LED backlight.
14...7 D7...D0(DB7...DB0) Any Data Bus consisting of 8 lines. In the above diagram, only the senior 4 lines are used, because this does not affect the speed of the display.
6 E Any Enable signal.
5 RW GND Selecting the direction (Read / Write) of data transfer: “1” - reading from the display / “0” - writing to the display. The pin is connected to GND because The data is only written to the display.
4 R.S. Any Register Selection of the information recipient: “1” - data register / “0” - instruction register.
3 V0(VEE)
Display contrast setting: 0 ... +5 V DC.
2 VDD(VCC) 5V Display logic power: +5 VDC.
1 VSS(GND) GND Common power supply (ground).

Nutrition

The 5 VDC display logic supply voltage is supplied to the VDD (VCC) and VSS (GND) pins of the display.

The 5 VDC backlight supply voltage is supplied to the A (Anode) and K (Cathode) terminals of the display.

A potential for setting the contrast of 0 ... +5 V DC is applied to pin V0 of the display.

More about the display

The character display is based on an STN (Super Twisted Nematic) LCD display controlled by an HD44780 controller and has a synchronous parallel 8-bit interface. The display is equipped with a blue LED backlight and is capable of simultaneously displaying up to 32 characters (16 columns, 02 rows), which is where the name of the display comes from: LCD1602. The HD44780 controller has a ROM that stores numbers, Latin characters and some Japanese characters for displaying them on the display. Missing characters, incl. and Cyrillic characters can be loaded into the controller’s RAM memory to display inscriptions in Russian or non-standard characters (for example, “emoticons”).

If you connect to the display pins, you can convert its synchronous 8-bit parallel interface to an I2C bus (turning the display from an LCD1602 into