在我的文章之后 控制16×2个带Python的LCD模块和a Raspberry Pi I decided to make a few enhancements. These included :
- 添加一个10Kohm可变电阻器来调整对比度
- 添加5Kohm可变电阻器以调节背光亮度
- 添加晶体管以允许打开和关闭背光
- 允许左对齐,居中对齐和右对齐
更新后的面包板如下所示:
对比度调整
现在,通过10Kohm微调电位器的中间引脚为引脚3提供0V至5V的电压,以便可以调整显示对比度。
背光亮度和切换
15/16引脚通过NPN传输器(BC547,BC548或同等产品)与560ohm和2Kohm的微调电位器串联,可通过额外的GPIO连接激活。 液晶屏背光的处理方式与我之前切换标准LED的方式完全相同 使用GPIO输出引脚控制LED 文章。使用固定电阻可确保永远无法将电阻调节到560欧姆以下,如果将微调电位计设置为零欧姆,则可以保护背光。晶体管的基极通过27Kohm电阻连接到另一个GPIO引脚。
文字对齐
功能“lcd_string”已修改为接受第二个参数。此参数取值1、2或3,并确定文本在屏幕上的显示方式。
蟒蛇
这是更新的代码:
#!/usr/bin/python #-------------------------------------- # ___ ___ _ ____ # / _ \/ _ \(_) __/__ __ __ # / , _/ ___/ /\ \/ _ \/ // / # /_/|_/_/ /_/___/ .__/\_, / # /_/ /___/ # # lcd_16x2.py # 16x2 液晶屏 Test Script with # backlight control and text justification # # Author : 马特 Hawkins # Date : 06/04/2015 # # //www.ytguangda.com/ # #-------------------------------------- # The wiring for the 液晶屏 is as follows: # 1 : GND # 2 : 5V # 3 : Contrast (0-5V)* # 4 : RS (Register Select) # 5 : R/W (Read Write) - GROUND THIS PIN # 6 : Enable or Strobe # 7 : Data Bit 0 - NOT USED # 8 : Data Bit 1 - NOT USED # 9 : Data Bit 2 - NOT USED # 10: Data Bit 3 - NOT USED # 11: Data Bit 4 # 12: Data Bit 5 # 13: Data Bit 6 # 14: Data Bit 7 # 15: 液晶屏 Backlight +5V** # 16: 液晶屏 Backlight GND #import import RPi.GPIO as 通用输入输出 import time # Define 通用输入输出 to 液晶屏 mapping LCD_RS = 7 LCD_E = 8 LCD_D4 = 25 LCD_D5 = 24 LCD_D6 = 23 LCD_D7 = 18 LED_ON = 15 # Define some device constants LCD_WIDTH = 16 # Maximum characters per line LCD_CHR = True LCD_CMD = False LCD_LINE_1 = 0x80 # 液晶屏 RAM address for the 1st line LCD_LINE_2 = 0xC0 # 液晶屏 RAM address for the 2nd line # Timing constants E_PULSE = 0.0005 E_DELAY = 0.0005 def main(): # Main program block 通用输入输出.setwarnings(False) 通用输入输出.setmode(GPIO.BCM) # Use BCM 通用输入输出 numbers 通用输入输出.setup(LCD_E, 通用输入输出.OUT) # E 通用输入输出.setup(LCD_RS, 通用输入输出.OUT) # RS 通用输入输出.setup(LCD_D4, 通用输入输出.OUT) # DB4 通用输入输出.setup(LCD_D5, 通用输入输出.OUT) # DB5 通用输入输出.setup(LCD_D6, 通用输入输出.OUT) # DB6 通用输入输出.setup(LCD_D7, 通用输入输出.OUT) # DB7 通用输入输出.setup(LED_ON, 通用输入输出.OUT) # Backlight enable # Initialise 显示 lcd_init() # Toggle backlight 上-off-on lcd_backlight(True) time.sleep(0.5) lcd_backlight(False) time.sleep(0.5) lcd_backlight(True) time.sleep(0.5) while True: # Send some centred text lcd_string("Rasbperry Pi",LCD_LINE_1,2) lcd_string("16x2 液晶屏 Test",LCD_LINE_2,2) time.sleep(3) # 3 second delay # Send some left justified text lcd_string("1234567890123456",LCD_LINE_1,1) lcd_string("abcdefghijklmnop",LCD_LINE_2,1) time.sleep(3) # 3 second delay # Send some right justified text lcd_string("Raspberrypi-spy",LCD_LINE_1,3) lcd_string(".co.uk",LCD_LINE_2,3) time.sleep(3) # 20 second delay # Send some centred text lcd_string("Follow me 上",LCD_LINE_1,2) lcd_string("Twitter @RPiSpy",LCD_LINE_2,2) time.sleep(3) def lcd_init(): # Initialise 显示 lcd_byte(0x33,LCD_CMD) # 110011 Initialise lcd_byte(0x32,LCD_CMD) # 110010 Initialise lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size lcd_byte(0x01,LCD_CMD) # 000001 Clear 显示 time.sleep(E_DELAY) def lcd_byte(bits, mode): # Send byte to data pins # bits = data # mode = True for character # False for command 通用输入输出.output(LCD_RS, mode) # RS # 你好gh bits 通用输入输出.output(LCD_D4, False) 通用输入输出.output(LCD_D5, False) 通用输入输出.output(LCD_D6, False) 通用输入输出.output(LCD_D7, False) if bits&0x10==0x10: 通用输入输出.output(LCD_D4, True) if bits&0x20==0x20: 通用输入输出.output(LCD_D5, True) if bits&0x40==0x40: 通用输入输出.output(LCD_D6, True) if bits&0x80==0x80: 通用输入输出.output(LCD_D7, True) # Toggle 'Enable' pin lcd_toggle_enable() # Low bits 通用输入输出.output(LCD_D4, False) 通用输入输出.output(LCD_D5, False) 通用输入输出.output(LCD_D6, False) 通用输入输出.output(LCD_D7, False) if bits&0x01==0x01: 通用输入输出.output(LCD_D4, True) if bits&0x02==0x02: 通用输入输出.output(LCD_D5, True) if bits&0x04==0x04: 通用输入输出.output(LCD_D6, True) if bits&0x08==0x08: 通用输入输出.output(LCD_D7, True) # Toggle 'Enable' pin lcd_toggle_enable() def lcd_toggle_enable(): # Toggle enable time.sleep(E_DELAY) 通用输入输出.output(LCD_E, True) time.sleep(E_PULSE) 通用输入输出.output(LCD_E, False) time.sleep(E_DELAY) def lcd_string(message,line,style): # Send string to 显示 # style=1 Left justified # style=2 Centred # style=3 Right justified if style==1: 信息 = 信息.ljust(LCD_WIDTH," ") elif style==2: 信息 = 信息.center(LCD_WIDTH," ") elif style==3: 信息 = 信息.rjust(LCD_WIDTH," ") lcd_byte(line, 液晶屏_CMD) for i in range(LCD_WIDTH): lcd_byte(ord(message[i]),LCD_CHR) def lcd_backlight(flag): # Toggle backlight 上-off-on 通用输入输出.output(LED_ON, flag) if __name__ == '__main__': try: main() except KeyboardInterrupt: pass finally: lcd_byte(0x01, 液晶屏_CMD) lcd_string("Goodbye!",LCD_LINE_1,2) 通用输入输出.cleanup()
可以使用以下命令下载此脚本 这个连结 或使用以下命令直接连接到您的Pi:
wget //bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/lcd_16x2_backlight.py
请记住要更新脚本顶部的常量,以匹配您在Pi上使用的GPIO信号。一如既往,这些GPIO引用是Broadcom信号名称,如我 通用输入输出头文章.
看看我的另一个 液晶屏相关帖子 其中包括20的详细信息×本文使用的4版屏幕。
12条留言
再次只有我。一世 ’我们必须再次进入maplins以订购正确的新零件。
1. 10Kohm可变电阻
2. 5Kohm可变电阻
3. NPN转接器
多谢,伙计
你好
很棒的文章,只是一个问题。您使用了哪个NPN传输器?
在Maplin中查看时,有很多具有不同特征的选择,例如2N5551,BC635等
干杯
嘿马克,他们是BC547
干杯
首先:感谢出色的教程!多亏了这一点,我有了一个不错的新时钟,上面有日期和所有内容。我相信我可以以某种方式实现自己的目标,但是您的文章/代码解决了许多使我的代码极其复杂的谜团(我没有’没注意到HD44780字符恰好是ascii值之类的东西…).
我试图了解所有发生的情况,但lcd_init()仍然让我有些困惑。我不’不知道我是否只是完全阅读了HD44780文档。我可以遵循此处描述的初始化:
http://www.protostack.com/blog/2010/03/character-lcd-displays-part-1/
您能否详细说明一次通话的功能?
I’我目前正在尝试读取RSS供稿并将其发送到LCD。我想使用内置的shift函数,但是如果我在供稿中使用一行,而在时间上使用另一行,则必须在软件本身中操纵rss行,而不能仅发送一个char字符,对吗?
该程序运行,但左– Right – Center isn’t working.
和我’我花了2个小时试图弄清楚是什么’s wrong.
3是对齐中心,而1是& 2 are aligning Left
将一些打印语句添加到lcd_string函数。确认“message” and “LCD_WIDTH”变量是正确的,并且IF语句正在调用正确的分支。
I’m使用2N3904。我必须下降到1K才能看到ON / OFF状态。
27K几乎看不见。
pingback: 20×4使用Python控制LCD模块|树莓派间谍
好项目!感谢分享。
很好的教程,谢谢!一个问题:在这里,显示器的亮度由可变电阻调节。
I’m寻找一种根据周围光线的功能自动“自动”调节亮度的解决方案。因此,一个光敏电阻应为我提供周围光线的一个值。但是,如何通过树莓派控制亮度?
这是控制LED亮度的示例:
//gpiozero.readthedocs.io/en/stable/recipes.html#led-with-variable-brightness
液晶屏引脚16仍将接地,但LCD引脚15将通过电阻(最小100ohm)连接至GPIO17(或您要使用的任何其他GPIO)。