2019-02-27 10:10:09 +01:00
Character-Based LCD Display
===========================
2018-08-22 22:05:28 +02:00
2018-11-14 22:12:27 +01:00
.. seo ::
2024-05-07 07:16:56 +02:00
:description: Instructions for setting up character-based HD44780 LCDs.
2018-11-14 22:12:27 +01:00
:image: lcd.jpg
2024-05-07 07:16:56 +02:00
The `` lcd_pcf8574 `` and `` lcd_gpio `` display components allow you to use HD44780-compatible, character-based LCDs
2024-05-07 07:40:12 +02:00
with ESPHome. This component is only for LCDs that display individual characters on a screen
2024-05-07 07:16:56 +02:00
(usually 8-40 columns and 2-4 rows), and not for LCDs that can control each pixel individually.
2022-11-18 09:19:55 +01:00
.. figure :: images/lcd-hello_world.jpg
:align: center
:width: 60.0%
2022-11-17 14:12:46 +01:00
.. note ::
2024-05-07 07:40:12 +02:00
Multiple versions of the display exist, supporting different character sets:
2022-11-17 14:53:26 +01:00
- HD44780UA00 English-Japanese which includes katakana characters, some Greek letters and mathematical symbols
2022-11-17 14:12:46 +01:00
- HD44780UA02 English-European which includes Greek, Cyrillic and Western European characters (with some diacritics)
2022-11-17 14:53:26 +01:00
- HD44780UBxx custom, manufacturer-specific character sets
2024-05-07 07:40:12 +02:00
2022-11-17 15:24:44 +01:00
It is also possible to add eight user-defined characters.
2019-02-27 10:10:09 +01:00
2022-11-17 14:27:40 +01:00
.. _lcd-pcf8574:
2019-02-27 10:10:09 +01:00
2022-11-17 14:22:48 +01:00
lcd_pcf8574 Component
---------------------
2022-11-17 14:12:46 +01:00
2024-05-07 07:40:12 +02:00
`` lcd_pcf8574 `` is for LCD displays with a PCF8574 GPIO expander module connected to all the data pins. This has the
2022-11-17 14:53:26 +01:00
benefit that you only need to connect two data wires to the ESP instead of the six or ten as with the :ref: `lcd-gpio` .
The communication happens via :ref: `I²C Bus <i2c>` , you need to have an `` i2c: `` section in your configuration.
2019-02-27 10:10:09 +01:00
.. figure :: images/lcd-pcf8574.jpg
:align: center
:width: 75.0%
2022-11-18 09:19:55 +01:00
LCD Display with a PCF8574 board attached on the back
2019-02-27 10:10:09 +01:00
.. code-block :: yaml
# Example configuration entry
i2c:
sda: D0
scl: D1
display:
- platform: lcd_pcf8574
2022-11-17 14:53:26 +01:00
dimensions: 20x4
address: 0x27
2019-02-27 10:10:09 +01:00
lambda: |-
it.print("Hello World!");
Configuration variables:
***** ***** ***** ***** *** *
2019-11-12 15:37:46 +01:00
- **dimensions** (**Required** , string): The dimensions of the display with `` COLUMNSxROWS `` . If you're not
2019-02-27 10:10:09 +01:00
sure, power the display up and just count them.
- **address** (*Optional* , int): The :ref: `I²C <i2c>` address of the PCF8574 chip, defaults to `` 0x3F `` .
- **lambda** (*Optional* , :ref: `lambda <config-lambda>` ): The lambda to use for rendering the content on the display.
See :ref: `display-lcd_lambda` for more information.
- **update_interval** (*Optional* , :ref: `config-time` ): The interval to re-draw the screen. Defaults to `` 1s `` .
- **id** (*Optional* , :ref: `config-id` ): Manually specify the ID used for code generation.
2022-11-17 15:24:44 +01:00
.. note ::
2024-05-07 07:40:12 +02:00
If you're not seeing anything on the display, try turning the contrast potentiometer around on the
2022-11-17 15:24:44 +01:00
PCF8574 board.
2019-02-27 10:10:09 +01:00
.. _lcd-gpio:
2022-11-17 14:22:48 +01:00
lcd_gpio Component
------------------
2019-02-27 10:10:09 +01:00
2022-11-18 09:19:55 +01:00
The `` lcd_gpio `` version of this component addresses the screen directly and does not employ a GPIO expander module.
2024-05-07 07:40:12 +02:00
Each of the data pins of the LCD needs a dedicated GPIO pin on the ESP. Connecting the screen this way offers
2022-11-18 09:19:55 +01:00
faster refresh, especially in conjunction with an :ref: `LCD Menu <lcd_menu>` .
2018-08-22 22:05:28 +02:00
2022-11-17 15:24:44 +01:00
.. figure :: images/lcd_gpio.svg
2018-08-22 22:05:28 +02:00
:align: center
:width: 75.0%
2022-11-17 15:24:44 +01:00
LCD Display GPIO pinout
2018-08-22 22:05:28 +02:00
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-08-22 22:05:28 +02:00
# Example configuration entry
display:
- platform: lcd_gpio
2022-11-17 14:53:26 +01:00
dimensions: 20x4
2018-08-22 22:05:28 +02:00
data_pins:
2024-05-07 07:40:12 +02:00
- GPIOXX
- GPIOXX
- GPIOXX
- GPIOXX
enable_pin: GPIOXX
rs_pin: GPIOXX
2018-08-22 22:05:28 +02:00
lambda: |-
it.print("Hello World!");
Configuration variables:
2019-02-27 10:10:09 +01:00
***** ***** ***** ***** *** *
2018-08-22 22:05:28 +02:00
2019-11-12 15:37:46 +01:00
- **dimensions** (**Required** , string): The dimensions of the display with `` COLUMNSxROWS `` . If you're not
2022-11-18 09:19:55 +01:00
sure, power the display on, turn contrast high up and just count them.
- **data_pins** (**Required** , list of :ref: `pins <config-pin_schema>` ): A list of the data pins you
2024-05-07 07:40:12 +02:00
have hooked up to the LCD. The list can either be 4 items long (operating in 4-bit mode with
either the first 4 data pins connected or the last 4 data pins connected), or 8 items long (when you have
2022-11-18 09:27:15 +01:00
connected all 8 data pins).
2022-11-18 09:19:55 +01:00
- **enable_pin** (**Required** , :ref: `pin <config-pin_schema>` ): The pin you have `` E `` (`` 06 `` ) hooked up to.
- **rs_pin** (**Required** , :ref: `pin <config-pin_schema>` ): The pin you have `` RS `` (`` 04 `` ) hooked up to.
- **rw_pin** (*Optional* , :ref: `pin <config-pin_schema>` ): Optionally set the pin you have `` R/W `` (`` 05 `` ) hooked up to. You can also just permanently connect that pin to `` GND `` .
2018-08-22 22:05:28 +02:00
- **lambda** (*Optional* , :ref: `lambda <config-lambda>` ): The lambda to use for rendering the content on the display.
See :ref: `display-lcd_lambda` for more information.
- **update_interval** (*Optional* , :ref: `config-time` ): The interval to re-draw the screen. Defaults to `` 1s `` .
- **id** (*Optional* , :ref: `config-id` ): Manually specify the ID used for code generation.
2022-11-17 15:24:44 +01:00
.. note ::
2024-05-07 07:40:12 +02:00
If you're not seeing anything on the display, make sure you apply `` 3.3V `` to the `` VEE `` (`` 03 `` ) contrast control
2022-11-17 15:24:44 +01:00
pin of the board. You can use a potentiometer to make it adjustable.
2018-08-22 22:05:28 +02:00
.. _display-lcd_lambda:
Rendering Lambda
----------------
2019-02-07 18:07:16 +01:00
The LCD displays has a similar API to the fully fledged :ref: `display-engine` , but it's only a subset as LCD displays
2018-08-22 22:05:28 +02:00
don't have a concept of individual pixels. In the lambda you're passed a variable called `` it ``
2019-06-03 19:34:17 +02:00
as with all other displays. In this case however, `` it `` is an instance of either `` GPIOLCDDisplay `` or `` PCF8574LCDDisplay `` .
2018-08-22 22:05:28 +02:00
The most basic operation with LCD Displays is writing static text to the screen as in the configuration example
at the top of this page.
Each of the three methods (`` print `` , `` printf `` and `` strftime `` ) all optionally take a column and row arguments at the
beginning which can be used to print the text at a specific position. These arguments are set to `` 0 `` (column) and `` 0 `` (row)
by default which means the character at the top left.
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-08-22 22:05:28 +02:00
display:
- platform: lcd_gpio # or lcd_pcf8574
# ...
lambda: |-
// Print 0 at the top left
it.print("0");
// Print 1 at the second row and second column.
it.print(1, 1, "1");
// Let's write a sensor value (let's assume it's 42.1)
2018-10-20 14:53:27 +02:00
it.printf("%.1f", id(my_sensor).state);
2022-11-18 09:19:55 +01:00
// Result: "42.1" (the dot will appear on the segment showing "2")
2018-08-22 22:05:28 +02:00
// Print a right-padded sensor value with 0 digits after the decimal
2018-10-20 14:53:27 +02:00
it.printf("Sensor value: %8.0f", id(my_sensor).state);
2018-08-22 22:05:28 +02:00
// Result: "Sensor value: 42"
// Print the current time
2018-12-02 13:12:27 +01:00
it.strftime("It is %H:%M on %d.%m.%Y", id(my_time).now());
2018-08-22 22:05:28 +02:00
// Result for 10:06 on august 21st 2018 -> "It is 10:06 on 21.08.2018"
2018-12-02 13:12:27 +01:00
# (Optional) For displaying time:
time:
2022-11-18 09:19:55 +01:00
- platform: homeassistant
2018-12-02 13:12:27 +01:00
id: my_time
2018-08-22 22:05:28 +02:00
Please see :ref: `display-printf` for a quick introduction into the `` printf `` formatting rules and
:ref: `display-strftime` for an introduction into the `` strftime `` time formatting.
2022-11-17 14:53:26 +01:00
User Defined Characters
-----------------------
The LCD display has the possibility to define up to eight user defined characters occupying the characters
`` 0 `` to `` 7 `` and mirrored at `` 8 `` to `` 15 `` (i.e. `` \x08 `` can be used instead of the `` \0 `` that can
be problematic in strings). Each character has eight lines of five bits, with the first line on the top
and the most significant bit on the left, meaning that `` 0b10000 `` followed by six zeros and a `` 0b00001 ``
2024-05-07 07:40:12 +02:00
defines a dot at the upper left and lower right of the character.
2022-11-17 14:53:26 +01:00
.. code-block :: yaml
display:
- platform: lcd_pcf8574
id: mydisplay
# ...
user_characters:
- position: 0
data:
- 0b00000
- 0b01010
- 0b00000
- 0b00100
- 0b00100
- 0b10001
- 0b01110
- 0b00000
- position: 7
data:
- 0b00000
- 0b01010
- 0b00000
- 0b00100
- 0b00100
- 0b00000
- 0b01110
- 0b10001
lambda: |-
it.print("Hello, world \x08 \x07!");
2022-11-17 15:24:44 +01:00
Try this `custom character generator <https://omerk.github.io/lcdchargen/> `__ to design your own sybmols.
2022-11-17 14:53:26 +01:00
2019-06-03 19:34:17 +02:00
Backlight Control
-----------------
With the `` lcd_pcf8574 `` the backlight can be turned on by `` it.backlight() `` and off by `` it.no_backlight() `` in the
2020-05-10 21:27:59 +02:00
display lambda definition. The jumper on the PCF8574 board needs to be closed for the backlight control to work.
Keep in mind that the display lambda runs for every `` update_interval `` , so if the backlight is turned on/off there,
2019-06-03 19:34:17 +02:00
it cannot be overridden from other parts.
2022-11-18 09:19:55 +01:00
With the `` lcd_gpio `` , the backlight is lit by applying `` Vcc `` to the `` BLA `` (`` 15 `` ) pin and connect `` BLK `` (`` 16 `` )
2024-05-07 07:40:12 +02:00
pin to `` GND `` . The backlight can draw more power than the microcontroller output pins can supply, so it is advisable
2022-11-17 15:24:44 +01:00
to use a transistor as a switch to control the power for the backlight pins.
2022-11-17 14:53:26 +01:00
Below an example for a typical use-case where the backlight is turned on when a motion sensor activates and
turns off `` 90 `` seconds after the last activation of the sensor.
2019-06-03 19:34:17 +02:00
.. code-block :: yaml
display:
- platform: lcd_pcf8574
id: mydisplay
# ...
binary_sensor:
- platform: gpio
# ...
on_press:
then:
- binary_sensor.template.publish:
id: backlight
state: ON
- binary_sensor.template.publish:
id: backlight
state: OFF
- platform: template
id: backlight
filters:
- delayed_off: 90s
on_press:
then:
- lambda: |-
id(mydisplay).backlight();
on_release:
then:
- lambda: |-
id(mydisplay).no_backlight();
2022-03-24 07:38:01 +01:00
2018-08-22 22:05:28 +02:00
See Also
--------
- :doc: `index`
2019-02-27 10:10:09 +01:00
- :doc: `/components/switch/gpio`
- :doc: `/components/binary_sensor/gpio`
2022-11-17 14:17:34 +01:00
- :ref: `LCD Menu <lcd_menu>`
2023-04-12 11:37:13 +02:00
- :ref: `Add pages to LCD display <lambda_magic_pages>`
2019-02-27 10:10:09 +01:00
- :doc: `/components/pcf8574`
2022-11-17 14:12:46 +01:00
- `HD44780U (LCD-II) datasheet <https://www.sparkfun.com/datasheets/LCD/HD44780.pdf> `__
- `Charset cheatsheet <https://user-images.githubusercontent.com/1550668/173113487-9c98e866-8ee4-4a3c-a83f-61fe62057c5f.png> `__
2022-11-17 14:53:26 +01:00
- `Custom Character Generator <https://omerk.github.io/lcdchargen/> `__
2018-08-24 22:44:01 +02:00
- `Arduino LiquidCrystal Library <https://www.arduino.cc/en/Reference/LiquidCrystal> `__
2022-11-17 14:12:46 +01:00
- :apiref: `lcd_base/lcd_display.h`
2019-02-07 13:54:45 +01:00
- :ghedit: `Edit`