树莓派4B直插LCD 1602a实践

LCD1602a显示效果

找出来吃灰多年的 LCD 1602a 液晶屏,记得之前用树莓派 3B 驱动过,那时没做记录全都忘记了,今天用树莓派 4B 直插 LCD 1602a 看看效果。
网上的资料很多都是使用 I2C 方式驱动,需要 I2C 模块,好处是接线与配置都简单,而我穷的没有。。。

1
2
LCD 1602a 拥有单行显示 16 个字符,总共两行显示空间(所以叫 1602),
在一些显示规格要求不高的场景应用还是很多的。

1. 准备清单

  1. 树莓派 4B
  2. LCD 1602a
  3. 面包板
  4. 杜邦线几根

2. 人家的接线方式

**LCD 针脚** **说明** **接线** **树莓派针脚**
01 VSS (GND) Breadboard GND(面包板接地)
02 VDD (+5v) Breadboard +5v(面包板5V)
03 VO (Contrast,显示对比度调整) Middle Pin Trimmer(接电位器中间线)
04 RS GPIO7 26
05 RW Breadboard GND
06 E GPIO8 24
07 D0
08 D1
09 D2
10 D3
11 D4 GPIO25 22
12 D5 GPIO24 18
13 D6 GPIO23 16
14 D7 GPIO18 12
15 A (+5V) Breadboard +5v
16 K (GND) Breadboard GND

参考的接线图

参考的LCD接线图

3. 我的接线方式

既然提到了电位器,我依然没有,但是电位器不就是可调电阻吗,资料上说接5V然后调整,那我直接供3.3V行不行呢?

这是我的接线图

**LCD 针脚** **说明** **接线** **树莓派针脚**
01 VSS (GND) Breadboard GND(面包板接地)
02 VDD (+5v) Breadboard +3.3V(面包板3.3V)
03 VO (Contrast) Breadboard GND(面包板接地,与3.3V)
04 RS GPIO7 26
05 RW Breadboard GND
06 E GPIO8 24
07 D0
08 D1
09 D2
10 D3
11 D4 GPIO25 22
12 D5 GPIO24 18
13 D6 GPIO23 16
14 D7 GPIO18 12
15 A (+5V) Breadboard +5v
16 K (GND) Breadboard GND
1
2
本来想用Fritzing画一下我的接线方式,怎知Fritzing元件库里竟然没有树莓派4B。。
那就只好来个凌乱一点的手机照了

我的接线实物图

1
2
3
123就对应的VSS VDD VO,接到了3.3v与GND,其他都一样,
是为了控制显示对比度,调太高的话看不清,太低的话看不清,
那我这里直接给个固定值

4. Python 编程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#  MBTechWorks.com 2016
# Control an LCD 1602 display from Raspberry Pi with Python programming

#!/usr/bin/python

# Pinout of the LCD:
# 1 : GND
# 2 : 5V power
# 3 : Display contrast - Connect to middle pin potentiometer
# 4 : RS (Register Select)
# 5 : R/W (Read Write) - Ground this pin (important)
# 6 : Enable or Strobe
# 7 : Data Bit 0 - data pin 0, 1, 2, 3 are not used
# 8 : Data Bit 1 -
# 9 : Data Bit 2 -
# 10: Data Bit 3 -
# 11: Data Bit 4
# 12: Data Bit 5
# 13: Data Bit 6
# 14: Data Bit 7
# 15: LCD Backlight +5V
# 16: LCD Backlight GND

import RPi.GPIO as GPIO
import time

# GPIO to LCD mapping
LCD_RS = 7 # Pi pin 26
LCD_E = 8 # Pi pin 24
LCD_D4 = 25 # Pi pin 22
LCD_D5 = 24 # Pi pin 18
LCD_D6 = 23 # Pi pin 16
LCD_D7 = 18 # Pi pin 12

# Device constants
LCD_CHR = True # Character mode
LCD_CMD = False # Command mode
LCD_CHARS = 16 # Characters per line (16 max)
LCD_LINE_1 = 0x80 # LCD memory location for 1st line
LCD_LINE_2 = 0xC0 # LCD memory location 2nd line

# Define main program code
def main():

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers
GPIO.setup(LCD_E, GPIO.OUT) # Set GPIO's to output mode
GPIO.setup(LCD_RS, GPIO.OUT)
GPIO.setup(LCD_D4, GPIO.OUT)
GPIO.setup(LCD_D5, GPIO.OUT)
GPIO.setup(LCD_D6, GPIO.OUT)
GPIO.setup(LCD_D7, GPIO.OUT)

# Initialize display
lcd_init()

# Loop - send text and sleep 3 seconds between texts
# Change text to anything you wish, but must be 16 characters or less

while True:
lcd_text("Hello World!",LCD_LINE_1)
lcd_text("",LCD_LINE_2)

lcd_text("Rasbperry Pi",LCD_LINE_1)
lcd_text("16x2 LCD Display",LCD_LINE_2)

time.sleep(2) # 3 second delay

lcd_text("ABCDEFGHIJKLMNOP",LCD_LINE_1)
lcd_text("1234567890123456",LCD_LINE_2)

time.sleep(2) # 3 second delay

lcd_text("I love my",LCD_LINE_1)
lcd_text("Raspberry Pi!",LCD_LINE_2)

time.sleep(2)

lcd_text("lison.cc",LCD_LINE_1)
lcd_text("For more R Pi",LCD_LINE_2)

time.sleep(2)

# End of main program code


# Initialize and clear display
def lcd_init():
lcd_write(0x33,LCD_CMD) # Initialize
lcd_write(0x32,LCD_CMD) # Set to 4-bit mode
lcd_write(0x06,LCD_CMD) # Cursor move direction
lcd_write(0x0C,LCD_CMD) # Turn cursor off
lcd_write(0x28,LCD_CMD) # 2 line display
lcd_write(0x01,LCD_CMD) # Clear display
time.sleep(0.0005) # Delay to allow commands to process

def lcd_write(bits, mode):
# High bits
GPIO.output(LCD_RS, mode) # RS

GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)
if bits&0x10==0x10:
GPIO.output(LCD_D4, True)
if bits&0x20==0x20:
GPIO.output(LCD_D5, True)
if bits&0x40==0x40:
GPIO.output(LCD_D6, True)
if bits&0x80==0x80:
GPIO.output(LCD_D7, True)

# Toggle 'Enable' pin
lcd_toggle_enable()

# Low bits
GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)
if bits&0x01==0x01:
GPIO.output(LCD_D4, True)
if bits&0x02==0x02:
GPIO.output(LCD_D5, True)
if bits&0x04==0x04:
GPIO.output(LCD_D6, True)
if bits&0x08==0x08:
GPIO.output(LCD_D7, True)

# Toggle 'Enable' pin
lcd_toggle_enable()

def lcd_toggle_enable():
time.sleep(0.0005)
GPIO.output(LCD_E, True)
time.sleep(0.0005)
GPIO.output(LCD_E, False)
time.sleep(0.0005)

def lcd_text(message,line):
# Send text to display
message = message.ljust(LCD_CHARS," ")

lcd_write(line, LCD_CMD)

for i in range(LCD_CHARS):
lcd_write(ord(message[i]),LCD_CHR)


#Begin program
try:
main()

except KeyboardInterrupt:
pass

finally:
lcd_write(0x01, LCD_CMD)
lcd_text("So long!",LCD_LINE_1)
lcd_text("lison.cc",LCD_LINE_2)
GPIO.cleanup()
# MBTechWorks.com 2016
# Control an LCD 1602 display from Raspberry Pi with Python programming

#!/usr/bin/python

# Pinout of the LCD:
# 1 : GND
# 2 : 5V power
# 3 : Display contrast - Connect to middle pin potentiometer
# 4 : RS (Register Select)
# 5 : R/W (Read Write) - Ground this pin (important)
# 6 : Enable or Strobe
# 7 : Data Bit 0 - data pin 0, 1, 2, 3 are not used
# 8 : Data Bit 1 -
# 9 : Data Bit 2 -
# 10: Data Bit 3 -
# 11: Data Bit 4
# 12: Data Bit 5
# 13: Data Bit 6
# 14: Data Bit 7
# 15: LCD Backlight +5V
# 16: LCD Backlight GND

import RPi.GPIO as GPIO
import time

# GPIO to LCD mapping
LCD_RS = 7 # Pi pin 26
LCD_E = 8 # Pi pin 24
LCD_D4 = 25 # Pi pin 22
LCD_D5 = 24 # Pi pin 18
LCD_D6 = 23 # Pi pin 16
LCD_D7 = 18 # Pi pin 12

# Device constants
LCD_CHR = True # Character mode
LCD_CMD = False # Command mode
LCD_CHARS = 16 # Characters per line (16 max)
LCD_LINE_1 = 0x80 # LCD memory location for 1st line
LCD_LINE_2 = 0xC0 # LCD memory location 2nd line

# Define main program code
def main():

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers
GPIO.setup(LCD_E, GPIO.OUT) # Set GPIO's to output mode
GPIO.setup(LCD_RS, GPIO.OUT)
GPIO.setup(LCD_D4, GPIO.OUT)
GPIO.setup(LCD_D5, GPIO.OUT)
GPIO.setup(LCD_D6, GPIO.OUT)
GPIO.setup(LCD_D7, GPIO.OUT)

# Initialize display
lcd_init()

# Loop - send text and sleep 3 seconds between texts
# Change text to anything you wish, but must be 16 characters or less

while True:
lcd_text("Hello World!",LCD_LINE_1)
lcd_text("",LCD_LINE_2)

lcd_text("Rasbperry Pi",LCD_LINE_1)
lcd_text("16x2 LCD Display",LCD_LINE_2)

time.sleep(3) # 3 second delay

lcd_text("ABCDEFGHIJKLMNOP",LCD_LINE_1)
lcd_text("1234567890123456",LCD_LINE_2)

time.sleep(3) # 3 second delay

lcd_text("I love my",LCD_LINE_1)
lcd_text("Raspberry Pi!",LCD_LINE_2)

time.sleep(3)

lcd_text("lison.cc",LCD_LINE_1)
lcd_text("For more R Pi",LCD_LINE_2)

time.sleep(3)

# End of main program code


# Initialize and clear display
def lcd_init():
lcd_write(0x33,LCD_CMD) # Initialize
lcd_write(0x32,LCD_CMD) # Set to 4-bit mode
lcd_write(0x06,LCD_CMD) # Cursor move direction
lcd_write(0x0C,LCD_CMD) # Turn cursor off
lcd_write(0x28,LCD_CMD) # 2 line display
lcd_write(0x01,LCD_CMD) # Clear display
time.sleep(0.0005) # Delay to allow commands to process

def lcd_write(bits, mode):
# High bits
GPIO.output(LCD_RS, mode) # RS

GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)
if bits&0x10==0x10:
GPIO.output(LCD_D4, True)
if bits&0x20==0x20:
GPIO.output(LCD_D5, True)
if bits&0x40==0x40:
GPIO.output(LCD_D6, True)
if bits&0x80==0x80:
GPIO.output(LCD_D7, True)

# Toggle 'Enable' pin
lcd_toggle_enable()

# Low bits
GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)
if bits&0x01==0x01:
GPIO.output(LCD_D4, True)
if bits&0x02==0x02:
GPIO.output(LCD_D5, True)
if bits&0x04==0x04:
GPIO.output(LCD_D6, True)
if bits&0x08==0x08:
GPIO.output(LCD_D7, True)

# Toggle 'Enable' pin
lcd_toggle_enable()

def lcd_toggle_enable():
time.sleep(0.0005)
GPIO.output(LCD_E, True)
time.sleep(0.0005)
GPIO.output(LCD_E, False)
time.sleep(0.0005)

def lcd_text(message,line):
# Send text to display
message = message.ljust(LCD_CHARS," ")

lcd_write(line, LCD_CMD)

for i in range(LCD_CHARS):
lcd_write(ord(message[i]),LCD_CHR)


#Begin program
try:
main()

except KeyboardInterrupt:
pass

finally:
lcd_write(0x01, LCD_CMD)
lcd_text("So long!",LCD_LINE_1)
lcd_text("lison.cc",LCD_LINE_2)
GPIO.cleanup()

5. 另外

1
如何查看自己的树莓派pin与gpio对应关系,终端输入pinout,打印如下图 

树莓派打印pinout

参考资料