.. 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! .. _py_lcd: 3.4 Liquid Crystal Display =============================== LCD1602 is a character type liquid crystal display, which can display 32 (16*2) characters at the same time. As we all know, though LCD and some other displays greatly enrich the man-machine interaction, they share a common weakness. When they are connected to a controller, multiple IOs will be occupied of the controller which has no so many outer ports. Also it restricts other functions of the controller. Therefore, LCD1602 with an I2C bus is developed to solve the problem. * :ref:`cpn_i2c_lcd` * `Inter-Integrated Circuit - Wikipedia `_ |pin_i2c| Here we will use the I2C1 interface to control the LCD1602 and display text. **Required Components** In this project, we need the following components. 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 * - Kepler Kit - 450+ - |link_kepler_kit| You can also buy them separately from the links below. .. list-table:: :widths: 5 20 5 20 :header-rows: 1 * - SN - COMPONENT - QUANTITY - LINK * - 1 - :ref:`cpn_pico_w` - 1 - |link_picow_buy| * - 2 - Micro USB Cable - 1 - * - 3 - :ref:`cpn_breadboard` - 1 - |link_breadboard_buy| * - 4 - :ref:`cpn_wire` - Several - |link_wires_buy| * - 5 - :ref:`cpn_i2c_lcd` - 1 - |link_i2clcd1602_buy| **Schematic** |sch_lcd| **Wiring** |wiring_lcd| **Code** .. note:: * Open the ``3.4_liquid_crystal_display.py`` file under the path of ``kepler-kit-main/micropython`` or copy this code into Thonny, then click "Run Current Script" or simply press F5 to run it. * Don't forget to click on the "MicroPython (Raspberry Pi Pico)" interpreter in the bottom right corner. * For detailed tutorials, please refer to :ref:`open_run_code_py`. * Here you need to use the library called ``lcd1602.py``, please check if it has been uploaded to Pico W, for a detailed tutorial refer to :ref:`add_libraries_py`. .. code-block:: python from machine import I2C, Pin from lcd1602 import LCD import time # Initialize I2C communication; i2c = I2C(1, sda=Pin(6), scl=Pin(7), freq=400000) # Create an LCD object for interfacing with the LCD1602 display lcd = LCD(i2c) # Display the first message on the LCD # Use '\n' to create a new line. string = "SunFounder\n LCD Tutorial" lcd.message(string) # Wait for 2 seconds time.sleep(2) # Clear the display lcd.clear() # Display the second message on the LCD string = "Hello\n World!" lcd.message(string) # Wait for 5 seconds time.sleep(5) # Clear the display before exiting lcd.clear() After the program runs, you will be able to see two lines of text appear on the LCD in turn, and then disappear. .. note:: When the code is running, if the screen is blank, you can turn the potentiometer on the back to increase the contrast. **How it works?** #. Setting up I2C Communication The ``machine`` module is used to set up I2C communication. SDA (Serial Data) and SCL (Serial Clock) pins are defined (pin 20 and 21 respectively), along with the I2C frequency (400kHz). .. code-block:: python from machine import I2C, Pin i2c = I2C(1, sda=Pin(6), scl=Pin(7), freq=400000) #. Initializing the LCD Display The ``LCD`` class from the ``lcd1602`` module is instantiated. This class handles the communication with the LCD display through I2C. An ``LCD`` object is created using the ``i2c`` object. For more usage of the ``lcd1602`` library, please refer to ``lcd1602.py``. .. code-block:: python from lcd1602 import LCD lcd = LCD(i2c) #. Displaying Messages on the LCD The ``message`` method of the ``LCD`` object is used to display text on the screen. The ``\n`` character creates a new line on the LCD. The ``time.sleep()`` function pauses execution for a specified number of seconds. .. code-block:: python string = "SunFounder\n LCD Tutorial" lcd.message(string) time.sleep(2) lcd.clear() #. Clearing the Display The ``clear`` method of the ``LCD`` object is called to clear the text from the display. .. code-block:: python lcd.clear() #. Displaying a Second Message A new message is displayed, followed by a delay and then clearing the screen again. .. code-block:: python string = "Hello\n World!" lcd.message(string) time.sleep(5) lcd.clear()