.. note::

    Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

    **Why Join?**

    - **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team.
    - **Learn & Share**: Exchange tips and tutorials to enhance your skills.
    - **Exclusive Previews**: Get early access to new product announcements and sneak peeks.
    - **Special Discounts**: Enjoy exclusive discounts on our newest products.
    - **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions.

    👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today!

.. _1.1.7_py_pi5:

1.1.7 I2C LCD1602
======================

Introduction
------------------

LCD1602 is a character type liquid crystal display, which can display 32
(16*2) characters at the same time.

Required Components
------------------------------

In this project, we need the following components. 

.. image:: ../python_pi5/img/1.1.7_i2c_lcd_list.png

It's definitely convenient to buy a whole kit, here's the link: 

.. list-table::
    :widths: 20 20 20
    :header-rows: 1

    *   - Name	
        - ITEMS IN THIS KIT
        - LINK
    *   - Raphael Kit
        - 337
        - |link_Raphael_kit|

You can also buy them separately from the links below.

.. list-table::
    :widths: 30 20
    :header-rows: 1

    *   - COMPONENT INTRODUCTION
        - PURCHASE LINK

    *   - :ref:`cpn_gpio_extension_board`
        - |link_gpio_board_buy|
    *   - :ref:`cpn_breadboard`
        - |link_breadboard_buy|
    *   - :ref:`cpn_wires`
        - |link_wires_buy|
    *   - :ref:`cpn_i2c_lcd`
        - |link_i2clcd1602_buy|

Schematic Diagram
---------------------

============ ========
T-Board Name physical
SDA1         Pin 3
SCL1         Pin 5
============ ========

.. image:: ../python_pi5/img/1.1.7_i2c_lcd_schematic.png


Experimental Procedures
-----------------------------

**Step 1:** Build the circuit.

.. image:: ../python_pi5/img/1.1.7_i2c_lcd1602_circuit.png


**Step 2**: Setup I2C (see :ref:`i2c_config`. If you have set I2C, skip this step.)

**Step 3:** Change directory.

.. raw:: html

   <run></run>

.. code-block::

    cd ~/raphael-kit/python-pi5

**Step 4:** Run.

.. raw:: html

   <run></run>

.. code-block::

    sudo python3 1.1.7_Lcd1602_zero.py

After the code runs, you can see ``Greetings!, From SunFounder`` displaying on the LCD.

.. note::

    * If you get the error ``FileNotFoundError: [Errno 2] No such file or directory: '/dev/i2c-1'``, you need to refer to :ref:`i2c_config` to enable the I2C.
    * If you get ``ModuleNotFoundError: No module named 'smbus2'`` error, please run ``sudo pip3 install smbus2``.
    * If the error ``OSError: [Errno 121] Remote I/O error`` appears, it means the module is miswired or the module is broken.
    * If the code and wiring are fine, but the LCD still does not display content, you can turn the potentiometer on the back to increase the contrast.



.. warning::

    If there is an error prompt  ``RuntimeError: Cannot determine SOC peripheral base address``, please refer to :ref:`faq_soc` 

**Code** 

.. note::

    You can **Modify/Reset/Copy/Run/Stop** the code below. But before that, you need to go to  source code path like ``raphael-kit/python-pi5``. After modifying the code, you can run it directly to see the effect.


.. raw:: html

    <run></run>

.. code-block:: python

   #!/usr/bin/env python3
   import LCD1602  # Import module for interfacing with LCD1602
   import time     # Import module for timing functions

   def setup():
       # Initialize LCD with I2C address 0x27 and enable backlight
       LCD1602.init(0x27, 1) 
       # Display the message 'Greetings!' at the top-left corner (row 0, column 0)
       LCD1602.write(0, 0, 'Greetings!') 
       # Display the message 'From SunFounder' on the second line (row 1, column 1)
       LCD1602.write(1, 1, 'From SunFounder') 
       time.sleep(2)  # Display messages for 2 seconds

   try:
       setup()  # Run the setup function to initialize the LCD and display messages
       
   except KeyboardInterrupt:
       # Clear the LCD display if a keyboard interruption (e.g., Ctrl+C) occurs
       LCD1602.clear()
       pass  # Proceed with no further action


**Code Explanation**

1. This file is an open source file for controlling I2C LCD1602. It allows us to easily use I2C LCD1602.

   .. code-block:: python

       import LCD1602  # Import module for interfacing with LCD1602

2. The function initializes the I2C system with the designated device symbol. The first parameter is the address of the I2C device, which can be detected through the i2cdetect command (see Appendix for details). The address of I2C LCD1602 is generally 0x27.

   .. code-block:: python

       # Initialize LCD with I2C address 0x27 and enable backlight
       LCD1602.init(0x27, 1) 

3. ``LCD1602.write`` is used to display messages on the LCD. The first two parameters are row and column positions, and the third is the message. Now you can see “Greetings!! From SunFounder” displayed on the LCD.

   .. code-block:: python

       # Display the message 'Greetings!' at the top-left corner (row 0, column 0)
       LCD1602.write(0, 0, 'Greetings!') 
       # Display the message 'From SunFounder' on the second line (row 1, column 1)
       LCD1602.write(1, 1, 'From SunFounder') 

