Professional Documents
Culture Documents
In 4 bit mode, only LCD pins D4, D5, D6, and D7 are used for data. These
are set in pins_data=[D4, D5, D6, D7] on line 2 below:
from RPLCD import CharLCD
lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23])
lcd.write_string(u'Hello world!')
lcd.cursor_pos = (1, 3)
lcd.write_string(u'Hello world!')
import time
from RPLCD import CharLCD
lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23])
lcd.write_string(u'Hello world!')
time.sleep(2)
lcd.clear()
Blinking Text
Combining lcd.clear() and time.sleep() in a while loop will produce
a blinking text effect:
import time
from RPLCD import CharLCD
lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23])
while True:
lcd.write_string(u"Hello world!")
time.sleep(1)
lcd.clear()
time.sleep(1)
Cursor On/Off
The RPLCD library provides several functions for controlling the cursor. You
can have a block cursor, an underline cursor, or a blinking cursor. Use the
following functions to set the cursor:
Blinking block cursor: lcd.cursor_mode =
CursorMode.blink
o
Line cursor: lcd.cursor_mode = CursorMode.line
o
Cursor off: lcd.cursor_mode = CursorMode.hide
The code below places a blinking cursor after the last character of text.
o
lcd.write_string(u'Hello world!')
lcd.cursor_mode = CursorMode.blink
#lcd.cursor_mode = CursorMode.line
#lcd.cursor_mode = CursorMode.hide
Line Breaks
Text will automatically wrap to the next line if the length of your string is
greater than the column length of your LCD. You can also control where a
text string breaks to the next line by inserting \n\r where you want the
break to occur. The code below will print Hello to the top row, and world!
to the bottom row.
lcd.write_string(u'Hello\n\rworld!')
1
2
3
4
5
6
7
8
9
10
11
from RPLCD import CharLCD
import time
lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23])
while True:
lcd.write_string("Time: %s" %time.strftime("%H:%M:%S"))
lcd.cursor_pos = (1, 0)
lcd.write_string("Date: %s" %time.strftime("%m/%d/%Y"))
import fcntl
import struct
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915,
struct.pack('256s', ifname[:15])
)[20:24])
lcd.write_string("IP Address:")
lcd.cursor_pos = (1, 0)
lcd.write_string(get_ip_address('eth0'))
Custom Characters
Each character on the LCD is an array of 58 of pixels. You can create any
pattern or character you can think of, and display it on the screen as a
custom character. Check out this website for an interactive tool that
creates the bit array used to define custom characters.
First we define the character in lines 4 to 12 of the code below. Then we
use the function lcd.create_char(0-7, NAME) to store the character
in the CGRAM memory of the LCD. Up to 8 (0-7) characters can be stored
at a time. To print the custom character, we
use lcd.write_string(unichr(0)), where the number
in unichr() is the memory location (0-7) defined
in lcd.create_char().
smiley = (
0b00000,
0b01010,
0b01010,
0b00000,
0b10001,
0b10001,
0b01110,
0b00000,
)
lcd.create_char(0, smiley)
lcd.write_string(unichr(0))
omega = (
0b00000,
0b01110,
0b10001,
0b10001,
0b10001,
0b01010,
0b11011,
0b00000,
)
pi = (
0b00000,
0b00000,
0b11111,
0b01010,
0b01010,
0b01010,
0b10011,
0b00000,
)
mu = (
0b00000,
0b10010,
0b10010,
0b10010,
0b10010,
0b11101,
0b10000,
0b10000,
)
drop = (
0b00100,
0b00100,
0b01010,
0b01010,
0b10001,
0b10001,
0b10001,
0b01110,
)
temp = (
0b00100,
0b01010,
0b01010,
0b01110,
0b01110,
0b11111,
0b11111,
0b01110,
)
lcd.create_char(0, omega)
lcd.create_char(1, pi)
lcd.create_char(2, mu)
lcd.create_char(3, drop)
lcd.create_char(4, temp)
lcd.write_string(unichr(0))
lcd.write_string(unichr(1))
lcd.write_string(unichr(2))
lcd.write_string(unichr(3))
lcd.write_string(unichr(4))
Scrolling Text
This program scrolls text from right to left in an infinite loop:
framebuffer = [
'',
'',
]
import time
long_string = 'This string is too long to fit'
while True:
loop_string(long_string, lcd, framebuffer, 1, 16)
1
2
3
4
5
6
7
8
9
10
11
12
import os
import glob
import time
from RPLCD import CharLCD
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
#CELSIUS CALCULATION
def read_temp_c():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = int(temp_string) / 1000.0 # TEMP_STRING IS THE SENSOR OUTPUT,
MAKE SURE IT'S AN INTEGER TO DO THE MATH
temp_c = str(round(temp_c, 1)) # ROUND THE RESULT TO 1 PLACE AFTER THE
DECIMAL, THEN CONVERT IT TO A STRING
return temp_c
#FAHRENHEIT CALCULATION
def read_temp_f():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_f = (int(temp_string) / 1000.0) * 9.0 / 5.0 + 32.0 # TEMP_STRING IS THE
SENSOR OUTPUT, MAKE SURE IT'S AN INTEGER TO DO THE MATH
temp_f = str(round(temp_f, 1)) # ROUND THE RESULT TO 1 PLACE AFTER THE
DECIMAL, THEN CONVERT IT TO A STRING
return temp_f
while True:
lcd.cursor_pos = (0, 0)
lcd.write_string("Temp: " + read_temp_c() + unichr(223) + "C")
lcd.cursor_pos = (1, 0)
lcd.write_string("Temp: " + read_temp_f() + unichr(223) + "F")