Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。

Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台

Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。

Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。

Micropython ESP频道

micropython esp32 lcd_i2c库 点亮led1602屏幕


micropython的lcd_i2c库来简化与 I2C LCD 的通信点亮led1602屏幕

149eb3ee50f84617a5e913e1c86839b3.png

 工具-管理包

搜索lcd

选择micropython-i2c-lcd安装

5333f5ebd0de4537acaf4606c27cc66e.png


新建 lcd.py 

from lcd_i2c import LCD
from machine import I2C, Pin
 
# 定义 LCD 的 I2C 地址、行数和列数
I2C_ADDR = 0x27
NUM_ROWS = 2
NUM_COLS = 16
 
# 定义 I2C 接口对象,使用指定的引脚和频率初始化
i2c = I2C(1, scl=Pin(3), sda=Pin(2), freq=800_000)
 
# 创建 LCD 对象,并传入所需的参数
lcd = LCD(addr=I2C_ADDR, cols=NUM_COLS, rows=NUM_ROWS, i2c=i2c)
 
# 初始化 LCD 显示屏
lcd.begin()
 
# 在 LCD 显示屏上打印字符串 "Hello World"
lcd.print("Hello World!")


Snipaste_2023-07-01_09-37-52.png


lcd_i2c库其他功能演示

"""I2C LCD showcase"""
 
from lcd_i2c import LCD
from machine import I2C, Pin
from time import sleep
 
# 定义 LCD 的 I2C 地址、行数和列数
I2C_ADDR = 0x27
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
FREQ = 800000   # 如果出现 "Errno 5" 错误,可以尝试降低该值
 
def print_and_wait(text: str, sleep_time: int = 2) -> None:
    """
    打印文本并等待一段时间。
    :param      text:        要打印的文本
    :type       text:        str
    :param      sleep_time:  等待时间(秒)
    :type       sleep_time:  int
    """
    print(text)
    sleep(sleep_time)
 
# 定义 I2C 接口,默认为 'I2C(0)',具体根据设备文档和引脚信息进行调整
i2c = I2C(1, scl=Pin(3), sda=Pin(2), freq=FREQ)
lcd = LCD(addr=I2C_ADDR, cols=I2C_NUM_COLS, rows=I2C_NUM_ROWS, i2c=i2c)
 
# 获取 LCD 的信息和属性
print("LCD is on I2C address {}".format(lcd.addr))
print("LCD has {} columns and {} rows".format(lcd.cols, lcd.rows))
print("LCD is used with a charsize of {}".format(lcd.charsize))
print("Cursor position is {}".format(lcd.cursor_position))
 
# 启动 LCD,因为为了与 Arduino 兼容,初始化时不会自动调用此方法
lcd.begin()
 
# 在 LCD 上打印文本,显示在第一行第一列
lcd.print("Hello World")
print_and_wait("Show 'Hello World' on LCD")
 
# 关闭 LCD 背光
lcd.no_backlight()
print_and_wait("Turn LCD backlight off")
 
# 获取当前背光值
print("Backlight value: {}".format(lcd.get_backlight()))
 
# 打开 LCD 背光
lcd.backlight()
print_and_wait("Turn LCD backlight on")
 
# 获取当前背光值
print("Backlight value: {}".format(lcd.get_backlight()))
 
# 清空 LCD 显示内容
lcd.clear()
print_and_wait("Clear display content")
 
# 打开光标(显示光标)
lcd.cursor()
print_and_wait("Turn cursor on (show)")
 
# 光标闪烁
lcd.blink()
print_and_wait("Blink cursor")
 
# 将光标返回到起始位置
lcd.home()
print_and_wait("Return cursor to home position")
 
# 停止光标闪烁
lcd.no_blink()
print_and_wait("Stop blinking cursor")
 
# 关闭光标(隐藏光标)
lcd.no_cursor()
print_and_wait("Turn cursor off (hide)")
 
# 在屏幕上打印文本
lcd.print("Hello again")
print_and_wait("Show 'Hello again' on LCD")
 
# 关闭显示
lcd.no_display()
print_and_wait("Turn LCD off")
 
# 打开显示
lcd.display()
print_and_wait("Turn LCD on")
 
# 向左滚动显示
for _ in "Hello again":
    lcd.scroll_display_left()
    sleep(0.5)
print_and_wait("Scroll display to the left")
 
# 向右滚动显示
for _ in "Hello again":
    lcd.scroll_display_right()
    sleep(0.5)
print_and_wait("Scroll display to the right")
 
# 设置文本从右到左流动
lcd.clear()
lcd.set_cursor(col=12, row=0)
lcd.right_to_left()
lcd.print("Right to left")
print_and_wait("Set text flow right to left")
 
# 设置文本从左到右流动
lcd.clear()
lcd.set_cursor(col=0, row=0)
lcd.left_to_right()
lcd.print("Left to right")
print_and_wait("Set text flow left to right")
 
# 激活自动滚动
lcd.autoscroll()
print_and_wait("Activate autoscroll")
 
# 禁用自动滚动
lcd.no_autoscroll()
print_and_wait("Disable autoscroll")
 
# 将光标设置到第二行第七列
lcd.clear()
lcd.cursor()
lcd.set_cursor(col=7, row=1)
print_and_wait("Set cursor to row 1, column 7")
lcd.no_cursor()
 
# 设置自定义字符号 0 为 :-)
# 自定义字符号可以设置在位置 0 到 7
lcd.create_char(
    location=0,
    charmap=[0x00, 0x00, 0x11, 0x04, 0x04, 0x11, 0x0E, 0x00]
    # 这是二进制矩阵,可以感受和查看
    # 00000
    # 00000
    # 10001
    # 00100
    # 00100
    # 10001
    # 01110
    # 00000
)
print_and_wait("Create custom char ':-)'")
 
# 显示存储在位置 0 的自定义字符
lcd.print(chr(0))
print_and_wait("Show custom char")



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 ESP56.com All Rights Reserved

执行时间: 0.012541055679321 seconds