please choosego to mobile | Continue to access the PC version
View: 12576|Reply: 5

LCD 2x16 I2C in Python

[Copy link]

3

threads

46

posts

691

credits

Senior member

Rank: 4

credits
691
Published in 2016-3-25 23:11:40 | Show all floors |Read mode
I need your help.
I'd like to display some text on text LCD 2x16 connected by I2C to GPIO. I don't know how to start with it - I'm not programist. Can you give me simple example code please?
Based on https://github.com/duxingkei33/orangepi_PC_gpio_pyH3 I can use buttons but I have no idea how to get LCD working.

10

threads

218

posts

2040

credits

Gold member

Rank: 6Rank: 6

credits
2040
Published in 2016-3-26 09:24:42 | Show all floors
Edited by nopnop2002 at 2016-3-28 22:58

I ported python code of this page to OrangePi-PC.

http://www.raspberrypi-spy.co.uk/2012/07/16x2-lcd-module-control-using-python/

  1. #!/usr/bin/python
  2. #--------------------------------------
  3. #    ___  ___  _ ____
  4. #   / _ \/ _ \(_) __/__  __ __
  5. #  / , _/ ___/ /\ \/ _ \/ // /
  6. # /_/|_/_/  /_/___/ .__/\_, /
  7. #                /_/   /___/
  8. #
  9. #  lcd_16x2.py
  10. #  16x2 LCD Test Script
  11. #
  12. # Author : Matt Hawkins
  13. # Date   : 06/04/2015
  14. #
  15. # http://www.raspberrypi-spy.co.uk/
  16. #
  17. #--------------------------------------

  18. # The wiring for the LCD is as follows:
  19. # 1 : GND
  20. # 2 : 5V
  21. # 3 : Contrast (0-5V)*
  22. # 4 : RS (Register Select)
  23. # 5 : R/W (Read Write)       - GROUND THIS PIN
  24. # 6 : Enable or Strobe
  25. # 7 : Data Bit 0             - NOT USED
  26. # 8 : Data Bit 1             - NOT USED
  27. # 9 : Data Bit 2             - NOT USED
  28. # 10: Data Bit 3             - NOT USED
  29. # 11: Data Bit 4
  30. # 12: Data Bit 5
  31. # 13: Data Bit 6
  32. # 14: Data Bit 7
  33. # 15: LCD Backlight +5V**
  34. # 16: LCD Backlight GND

  35. #import
  36. #import RPi.gpio.as GPIO
  37. from pyA20.gpio import gpio
  38. from pyA20.gpio import port
  39. import time

  40. # Define gpio.to LCD mapping
  41. # for RaspberryPi
  42. #LCD_RS = 7
  43. #LCD_E  = 8
  44. #LCD_D4 = 25
  45. #LCD_D5 = 24
  46. #LCD_D6 = 23
  47. #LCD_D7 = 18
  48. # for OrangePi
  49. LCD_RS = port.PA21
  50. LCD_E  = port.PC3
  51. LCD_D4 = port.PA2
  52. LCD_D5 = port.PC7
  53. LCD_D6 = port.PC4
  54. LCD_D7 = port.PD14

  55. # Define some device constants
  56. LCD_WIDTH = 16    # Maximum characters per line
  57. LCD_CHR = True
  58. LCD_CMD = False

  59. LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
  60. LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line

  61. # Timing constants
  62. E_PULSE = 0.0005
  63. E_DELAY = 0.0005

  64. def main():
  65.   # Main program block
  66. #  gpio.setwarnings(False)
  67.   gpio.init()                      # Use BCM gpio.numbers
  68.   gpio.setcfg(LCD_E, gpio.OUTPUT)  # E
  69.   gpio.setcfg(LCD_RS, gpio.OUTPUT) # RS
  70.   gpio.setcfg(LCD_D4, gpio.OUTPUT) # DB4
  71.   gpio.setcfg(LCD_D5, gpio.OUTPUT) # DB5
  72.   gpio.setcfg(LCD_D6, gpio.OUTPUT) # DB6
  73.   gpio.setcfg(LCD_D7, gpio.OUTPUT) # DB7

  74.   # Initialise display
  75.   lcd_init()

  76.   while True:

  77.     # Send some test
  78.     lcd_string("Rasbperry Pi",LCD_LINE_1)
  79.     lcd_string("16x2 LCD Test",LCD_LINE_2)

  80.     time.sleep(3) # 3 second delay

  81.     # Send some text
  82.     lcd_string("1234567890123456",LCD_LINE_1)
  83.     lcd_string("abcdefghijklmnop",LCD_LINE_2)

  84.     time.sleep(3) # 3 second delay

  85.     # Send some text
  86.     lcd_string("RaspberryPi-spy",LCD_LINE_1)
  87.     lcd_string(".co.uk",LCD_LINE_2)

  88.     time.sleep(3)

  89.     # Send some text
  90.     lcd_string("Follow me on",LCD_LINE_1)
  91.     lcd_string("Twitter @RPiSpy",LCD_LINE_2)

  92.     time.sleep(3)

  93. def lcd_init():
  94.   # Initialise display
  95.   lcd_byte(0x33,LCD_CMD) # 110011 Initialise
  96.   lcd_byte(0x32,LCD_CMD) # 110010 Initialise
  97.   lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
  98.   lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
  99.   lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
  100.   lcd_byte(0x01,LCD_CMD) # 000001 Clear display
  101.   time.sleep(E_DELAY)

  102. def lcd_byte(bits, mode):
  103.   # Send byte to data pins
  104.   # bits = data
  105.   # mode = True  for character
  106.   #        False for command

  107.   gpio.output(LCD_RS, mode) # RS

  108.   # High bits
  109.   gpio.output(LCD_D4, False)
  110.   gpio.output(LCD_D5, False)
  111.   gpio.output(LCD_D6, False)
  112.   gpio.output(LCD_D7, False)
  113.   if bits&0x10==0x10:
  114.     gpio.output(LCD_D4, True)
  115.   if bits&0x20==0x20:
  116.     gpio.output(LCD_D5, True)
  117.   if bits&0x40==0x40:
  118.     gpio.output(LCD_D6, True)
  119.   if bits&0x80==0x80:
  120.     gpio.output(LCD_D7, True)

  121.   # Toggle 'Enable' pin
  122.   lcd_toggle_enable()

  123.   # Low bits
  124.   gpio.output(LCD_D4, False)
  125.   gpio.output(LCD_D5, False)
  126.   gpio.output(LCD_D6, False)
  127.   gpio.output(LCD_D7, False)
  128.   if bits&0x01==0x01:
  129.     gpio.output(LCD_D4, True)
  130.   if bits&0x02==0x02:
  131.     gpio.output(LCD_D5, True)
  132.   if bits&0x04==0x04:
  133.     gpio.output(LCD_D6, True)
  134.   if bits&0x08==0x08:
  135.     gpio.output(LCD_D7, True)

  136.   # Toggle 'Enable' pin
  137.   lcd_toggle_enable()

  138. def lcd_toggle_enable():
  139.   # Toggle enable
  140.   time.sleep(E_DELAY)
  141.   gpio.output(LCD_E, True)
  142.   time.sleep(E_PULSE)
  143.   gpio.output(LCD_E, False)
  144.   time.sleep(E_DELAY)

  145. def lcd_string(message,line):
  146.   # Send string to display

  147.   message = message.ljust(LCD_WIDTH," ")

  148.   lcd_byte(line, LCD_CMD)

  149.   for i in range(LCD_WIDTH):
  150.     lcd_byte(ord(message[i]),LCD_CHR)

  151. if __name__ == '__main__':

  152.   try:
  153.     main()
  154.   except KeyboardInterrupt:
  155.     pass
  156.   finally:
  157.     lcd_byte(0x01, LCD_CMD)
  158.     lcd_string("Goodbye!",LCD_LINE_1)
  159. #    gpio.cleanup()
Copy code
It work fine.


3

threads

46

posts

691

credits

Senior member

Rank: 4

credits
691
 Author| Published in 2016-3-26 15:48:31 from mobile | Show all floors
Edited by mswiniuch at 2016-3-26 16:09

Can you show how to connect on schematic? Or write port.xxx->pin number. I can't understand PCx, PDx. But maybe you can port other code for I2C - it should be easier.
Very simple code in C is here: http://www.itcooky.com/?p=4023

10

threads

218

posts

2040

credits

Gold member

Rank: 6Rank: 6

credits
2040
Published in 2016-3-27 06:03:00 | Show all floors
Edited by nopnop2002 at 2016-3-28 23:01
mswiniuch replied at 2016-3-26 15:48
Can you show how to connect on schematic? Or write port.xxx->pin number. I can't understand PCx, PDx ...

The connection of a wire is same as this.

http://www.raspberrypi-spy.co.uk/2012/07/16x2-lcd-module-control-using-python/

Please refer to this about the name of the pin.

http://linux-sunxi.org/Xunlong_Orange_Pi_2



3

threads

46

posts

691

credits

Senior member

Rank: 4

credits
691
 Author| Published in 2016-4-2 01:26:53 | Show all floors
Thank you nopnop2002 - it works
Belllow some code from me. This is verry simle internet radio control - mpd+mpc
I'm planing to do some menu sterred by three buttons to control not only radio but system.
I'm not programist and don't know pytton verry much so this code isn't so clear ;)

  1. #!/usr/bin/env python

  2. import os
  3. import sys
  4. import subprocess
  5. import time

  6. from time import sleep
  7. from subprocess import *
  8. from pyA20.gpio import gpio
  9. from pyA20.gpio import connector
  10. from pyA20.gpio import port

  11. if not os.getegid() == 0:
  12.   sys.exit('Script must be run as root')

  13. button_pause = connector.gpio1p36  #PIN 36
  14. button_prev  = connector.gpio1p38  #PIN 38
  15. button_next  = connector.gpio1p40  #PIN 40

  16. # Define GPIO to LCD mapping
  17. LCD_RS = port.PA21 #PIN 26
  18. LCD_E  = port.PC3  #PIN 24
  19. LCD_D4 = port.PA2  #PIN 22
  20. LCD_D5 = port.PC7  #PIN 18
  21. LCD_D6 = port.PC4  #PIN 16
  22. LCD_D7 = port.PD14 #PIN 12

  23. # Define some device constants
  24. LCD_WIDTH = 16    # Maximum characters per line
  25. LCD_CHR = True
  26. LCD_CMD = False

  27. LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
  28. LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line

  29. # Timing constants
  30. E_PULSE = 0.0005
  31. E_DELAY = 0.0005

  32. #Init gpio module
  33. gpio.init()

  34. gpio.setcfg(LCD_E,  gpio.OUTPUT) # E
  35. gpio.setcfg(LCD_RS, gpio.OUTPUT) # RS
  36. gpio.setcfg(LCD_D4, gpio.OUTPUT) # DB4
  37. gpio.setcfg(LCD_D5, gpio.OUTPUT) # DB5
  38. gpio.setcfg(LCD_D6, gpio.OUTPUT) # DB6
  39. gpio.setcfg(LCD_D7, gpio.OUTPUT) # DB7

  40. #Set buttons
  41. gpio.setcfg(button_pause, gpio.INPUT)
  42. gpio.setcfg(button_prev,  gpio.INPUT)
  43. gpio.setcfg(button_next,  gpio.INPUT)

  44. #Enable pullup resistor
  45. gpio.pullup(button_pause, gpio.PULLUP)
  46. gpio.pullup(button_prev,  gpio.PULLUP)
  47. gpio.pullup(button_next,  gpio.PULLUP)


  48. def main():

  49.   reset_pressed = 1
  50.   prev_pressed = 1
  51.   next_pressed = 1

  52.   # Initialise display
  53.   lcd_init()

  54.   lcd_string("[  INTERNET    ]",LCD_LINE_1)
  55.   lcd_string("[       RADIO  ]",LCD_LINE_2)

  56.   while True:
  57.     value_out = 0
  58.     pause_pressed = gpio.input(button_pause)
  59.     prev_pressed = gpio.input(button_prev)
  60.     next_pressed = gpio.input(button_next)

  61.     if (pause_pressed == 0):
  62.       value_out = 1
  63.       lcd_string("[ PRESSED      ]",LCD_LINE_1)
  64.       lcd_string("[        PAUSE ]",LCD_LINE_2)
  65.       run_cmd("mpc pause")

  66.     if (prev_pressed == 0):
  67.       value_out = 1
  68.       lcd_string("[ PRESSED      ]",LCD_LINE_1)
  69.       lcd_string("[      PREVIOUS]",LCD_LINE_2)
  70.       run_cmd("mpc prev")

  71.     if (next_pressed == 0):
  72.       value_out = 1
  73.       lcd_string("[ PRESSED      ]",LCD_LINE_1)
  74.       lcd_string("[          NEXT]",LCD_LINE_2)
  75.       run_cmd("mpc next")

  76.     sleep(0.1)


  77. def run_cmd(cmd):  
  78.   p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT)  
  79.   output = p.communicate()[0]  
  80.   return output  

  81. def lcd_init():
  82.   # Initialise display
  83.   lcd_byte(0x33,LCD_CMD) # 110011 Initialise
  84.   lcd_byte(0x32,LCD_CMD) # 110010 Initialise
  85.   lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
  86.   lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
  87.   lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
  88.   lcd_byte(0x01,LCD_CMD) # 000001 Clear display
  89.   time.sleep(E_DELAY)

  90. def lcd_byte(bits, mode):
  91.   # Send byte to data pins
  92.   # bits = data
  93.   # mode = True  for character
  94.   #        False for command

  95.   gpio.output(LCD_RS, mode)

  96.   # High bits
  97.   gpio.output(LCD_D4, False)
  98.   gpio.output(LCD_D5, False)
  99.   gpio.output(LCD_D6, False)
  100.   gpio.output(LCD_D7, False)
  101.   if bits&0x10==0x10:
  102.     gpio.output(LCD_D4, True)
  103.   if bits&0x20==0x20:
  104.     gpio.output(LCD_D5, True)
  105.   if bits&0x40==0x40:
  106.     gpio.output(LCD_D6, True)
  107.   if bits&0x80==0x80:
  108.     gpio.output(LCD_D7, True)

  109.   # Toggle 'Enable' pin
  110.   lcd_toggle_enable()

  111.   # Low bits
  112.   gpio.output(LCD_D4, False)
  113.   gpio.output(LCD_D5, False)
  114.   gpio.output(LCD_D6, False)
  115.   gpio.output(LCD_D7, False)
  116.   if bits&0x01==0x01:
  117.     gpio.output(LCD_D4, True)
  118.   if bits&0x02==0x02:
  119.     gpio.output(LCD_D5, True)
  120.   if bits&0x04==0x04:
  121.     gpio.output(LCD_D6, True)
  122.   if bits&0x08==0x08:
  123.     gpio.output(LCD_D7, True)

  124.   # Toggle 'Enable' pin
  125.   lcd_toggle_enable()

  126. def lcd_toggle_enable():
  127.   # Toggle enable
  128.   time.sleep(E_DELAY)
  129.   gpio.output(LCD_E, True)
  130.   time.sleep(E_PULSE)
  131.   gpio.output(LCD_E, False)
  132.   time.sleep(E_DELAY)

  133. def lcd_string(message,line):
  134.   # Send string to display
  135.   message = message.ljust(LCD_WIDTH," ")
  136.   lcd_byte(line, LCD_CMD)
  137.   for i in range(LCD_WIDTH):
  138.     lcd_byte(ord(message[i]),LCD_CHR)


  139. if __name__ == '__main__':

  140.   try:
  141.     main()
  142.   except KeyboardInterrupt:
  143.     pass
  144.   finally:
  145.     lcd_byte(0x01, LCD_CMD)
  146.     lcd_string("    Goodbye!    ",LCD_LINE_1)
  147.     gpio.cleanup()
Copy code

1

threads

3

posts

29

credits

Novice

Rank: 1

credits
29
Published in 2022-8-13 12:19:40 | Show all floors
nopnop2002Published in 2016-3-27 06:03
Edited by nopnop2002 at 2016-3-28 23:01

Thankyou nanopop2022,

It works well.
You need to log in before you can reply login | Register

Points Rule

Quick reply Top Return list