chiragatha
Posts: 5
Joined: Fri May 19, 2017 8:19 pm

display custom characters In 20*4 LCD screen

Tue Jul 04, 2017 7:51 am

Hello,
I am using 20*4 LCD Screen , I want to display custom made characters . I tried doing it using RPLCD library but it displayed some weird stuff
The code which I m using to display simple text is given in the link below
Can any one help me with adding a function with my code which can be used to display characters

http://www.raspberrypi-spy.co.uk/2012/0 ... ng-python/

Any help is appreciated
Thank you

DirkS
Posts: 10840
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: display custom characters In 20*4 LCD screen

Tue Jul 04, 2017 8:41 am

chiragatha wrote:I tried doing it using RPLCD library but it displayed some weird stuff
What is the RPLCD library (post link).
Post your code (in [ code] tags.
Define 'weird stuff' (upload an image to image hosting site and post a link here).

chiragatha
Posts: 5
Joined: Fri May 19, 2017 8:19 pm

Re: display custom characters In 20*4 LCD screen

Tue Jul 04, 2017 9:11 am

RPLCD library
https://pypi.python.org/pypi/RPLCD/0.3.0

The code which I m using to display simple texts

Code: Select all

import RPi.GPIO as GPIO
import time
 
# Define GPIO to LCD 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 = 20    # Maximum characters per line
LCD_CHR = True
LCD_CMD = False
 
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line
 
# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005
 
def main():
  # Main program block
 
  GPIO.setmode(GPIO.BCM)       # Use BCM GPIO numbers
  GPIO.setup(LCD_E, GPIO.OUT)  # E
  GPIO.setup(LCD_RS, GPIO.OUT) # RS
  GPIO.setup(LCD_D4, GPIO.OUT) # DB4
  GPIO.setup(LCD_D5, GPIO.OUT) # DB5
  GPIO.setup(LCD_D6, GPIO.OUT) # DB6
  GPIO.setup(LCD_D7, GPIO.OUT) # DB7
  GPIO.setup(LED_ON, GPIO.OUT) # Backlight enable
 
  # Initialise display
  lcd_init()
 
  # Toggle backlight on-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 test
    lcd_string("--------------------",LCD_LINE_1,2)
    lcd_string("Rasbperry Pi",LCD_LINE_2,2)
    lcd_string("Model B",LCD_LINE_3,2)
    lcd_string("--------------------",LCD_LINE_4,2)
 
    time.sleep(3) # 3 second delay
 
    lcd_string("Raspberrypi-spy",LCD_LINE_1,3)
    lcd_string(".co.uk",LCD_LINE_2,3)
    lcd_string("",LCD_LINE_3,2)
    lcd_string("20x4 LCD Module Test",LCD_LINE_4,2)
 
    time.sleep(3) # 20 second delay
 
    # Blank display
    lcd_byte(0x01, LCD_CMD)
 
    time.sleep(3) # 3 second delay
 
def lcd_init():
  # Initialise display
  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 display
  time.sleep(E_DELAY)
 
def lcd_byte(bits, mode):
  # Send byte to data pins
  # bits = data
  # mode = True  for character
  #        False for command
 
  GPIO.output(LCD_RS, mode) # RS
 
  # High bits
  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():
  # Toggle enable
  time.sleep(E_DELAY)
  GPIO.output(LCD_E, True)
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)
  time.sleep(E_DELAY)
 
def lcd_string(message,line,style):
  # Send string to display
  # style=1 Left justified
  # style=2 Centred
  # style=3 Right justified
 
  if style==1:
    message = message.ljust(LCD_WIDTH," ")
  elif style==2:
    message = message.center(LCD_WIDTH," ")
  elif style==3:
    message = message.rjust(LCD_WIDTH," ")
 
  lcd_byte(line, LCD_CMD)
 
  for i in range(LCD_WIDTH):
    lcd_byte(ord(message[i]),LCD_CHR)
 
def lcd_backlight(flag):
  # Toggle backlight on-off-on
  GPIO.output(LED_ON, flag)
 
if __name__ == '__main__':
 
  try:
    main()
  except KeyboardInterrupt:
    pass
  finally:
    lcd_byte(0x01, LCD_CMD)
    lcd_string("Goodbye!",LCD_LINE_1,2)
    GPIO.cleanup()
code I used for displaying characters

Code: Select all

from RPLCD import CharLCD, cleared, cursor
lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23])

smiley = (
    0b00000,
    0b01010,
    0b01010,
    0b00000,
    0b10001,
    0b10001,
    0b01110,
    0b00000,
)
lcd.create_char(0, smiley)
lcd.write_string(unichr(0))

Is there any function i can add to my code to display a smiley

DirkS
Posts: 10840
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: display custom characters In 20*4 LCD screen

Tue Jul 04, 2017 9:38 am

Check the pin numbering in your code.
Looks to me that the second script is set up to use different pins than the first script.
Example: first script RS is on GPIO7 (phyical pin 26).
In the second script it's on GPIO37 which doesn't exist (or at least not available on the header)
That's assuming RPLCD uses BCM numbering. If it's using BOARD numbering (physical pin numbers) it still isn't right because physical pin 37 is GPIO26.

See https://pinout.xyz/

pcmanbob
Posts: 13460
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: display custom characters In 20*4 LCD screen

Tue Jul 04, 2017 9:49 am

Hi.

I use the RPLCD library myself and find it simple and easy to use even if you want to define your own characters.

but it does use BOARD numbering and not BCM numbering
This is the default numbering.

Code: Select all

 pi   display
15    5 RS
16    6 E
21    11 D4
22    12 D5
23    13 D6
24    14 D7
display pins 1,5,16 to Ground
display pin 2 5V
display pin 3 contrast
display pin 15 Backlight
so to use your pin numbering from the first code you would need to set the pins in RPLCD library like this

Code: Select all

lcd - gpio - board
RS  - 7    -  26
E   - 8    -  24
D4  - 25   -  22
D5  - 24   -  18
D6  - 23   -  16
D7  - 18   -  12
LEDon - 15 -  10
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
dbrgn
Posts: 19
Joined: Sat May 11, 2013 2:49 pm
Location: Switzerland

Re: display custom characters In 20*4 LCD screen

Mon Jul 31, 2017 2:33 pm

It looks like you're mixing two LCD libraries. That probably causes problems.

Can you try to display the text with RPLCD as well, instead of using the `lcd_string` function?

Return to “Beginners”