mswiniuch post at 2016-3-25 23:11:40

LCD 2x16 I2C in Python

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.

nopnop2002 post at 2016-3-26 09:24:42

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/

#!/usr/bin/python
#--------------------------------------
#    _______ ____
#   / _ \/ _ \(_) __/____ __
#/ , _/ ___/ /\ \/ _ \/ // /
# /_/|_/_//_/___/ .__/\_, /
#                /_/   /___/
#
#lcd_16x2.py
#16x2 LCD Test Script
#
# Author : Matt Hawkins
# Date   : 06/04/2015
#
# http://www.raspberrypi-spy.co.uk/
#
#--------------------------------------

# The wiring for the LCD 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: LCD Backlight +5V**
# 16: LCD Backlight GND

#import
#import RPi.gpio.as GPIO
from pyA20.gpio import gpio
from pyA20.gpio import port
import time

# Define gpio.to LCD mapping
# for RaspberryPi
#LCD_RS = 7
#LCD_E= 8
#LCD_D4 = 25
#LCD_D5 = 24
#LCD_D6 = 23
#LCD_D7 = 18
# for OrangePi
LCD_RS = port.PA21
LCD_E= port.PC3
LCD_D4 = port.PA2
LCD_D5 = port.PC7
LCD_D6 = port.PC4
LCD_D7 = port.PD14

# Define some device constants
LCD_WIDTH = 16    # 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

# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005

def main():
# Main program block
#gpio.setwarnings(False)
gpio.init()                      # Use BCM gpio.numbers
gpio.setcfg(LCD_E, gpio.OUTPUT)# E
gpio.setcfg(LCD_RS, gpio.OUTPUT) # RS
gpio.setcfg(LCD_D4, gpio.OUTPUT) # DB4
gpio.setcfg(LCD_D5, gpio.OUTPUT) # DB5
gpio.setcfg(LCD_D6, gpio.OUTPUT) # DB6
gpio.setcfg(LCD_D7, gpio.OUTPUT) # DB7

# Initialise display
lcd_init()

while True:

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

    time.sleep(3) # 3 second delay

    # Send some text
    lcd_string("1234567890123456",LCD_LINE_1)
    lcd_string("abcdefghijklmnop",LCD_LINE_2)

    time.sleep(3) # 3 second delay

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

    time.sleep(3)

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

    time.sleep(3)

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 = Truefor 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):
# Send string to display

message = message.ljust(LCD_WIDTH," ")

lcd_byte(line, LCD_CMD)

for i in range(LCD_WIDTH):
    lcd_byte(ord(message),LCD_CHR)

if __name__ == '__main__':

try:
    main()
except KeyboardInterrupt:
    pass
finally:
    lcd_byte(0x01, LCD_CMD)
    lcd_string("Goodbye!",LCD_LINE_1)
#    gpio.cleanup()It work fine.


mswiniuch post at 2016-3-26 15:48:31

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

nopnop2002 post at 2016-3-27 06:03:00

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



mswiniuch post at 2016-4-2 01:26:53

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 ;)

#!/usr/bin/env python

import os
import sys
import subprocess
import time

from time import sleep
from subprocess import *
from pyA20.gpio import gpio
from pyA20.gpio import connector
from pyA20.gpio import port

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

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

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

# Define some device constants
LCD_WIDTH = 16    # 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

# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005

#Init gpio module
gpio.init()

gpio.setcfg(LCD_E,gpio.OUTPUT) # E
gpio.setcfg(LCD_RS, gpio.OUTPUT) # RS
gpio.setcfg(LCD_D4, gpio.OUTPUT) # DB4
gpio.setcfg(LCD_D5, gpio.OUTPUT) # DB5
gpio.setcfg(LCD_D6, gpio.OUTPUT) # DB6
gpio.setcfg(LCD_D7, gpio.OUTPUT) # DB7

#Set buttons
gpio.setcfg(button_pause, gpio.INPUT)
gpio.setcfg(button_prev,gpio.INPUT)
gpio.setcfg(button_next,gpio.INPUT)

#Enable pullup resistor
gpio.pullup(button_pause, gpio.PULLUP)
gpio.pullup(button_prev,gpio.PULLUP)
gpio.pullup(button_next,gpio.PULLUP)


def main():

reset_pressed = 1
prev_pressed = 1
next_pressed = 1

# Initialise display
lcd_init()

lcd_string("",LCD_LINE_1)
lcd_string("[       RADIO]",LCD_LINE_2)

while True:
    value_out = 0
    pause_pressed = gpio.input(button_pause)
    prev_pressed = gpio.input(button_prev)
    next_pressed = gpio.input(button_next)

    if (pause_pressed == 0):
      value_out = 1
      lcd_string("[ PRESSED      ]",LCD_LINE_1)
      lcd_string("[      PAUSE ]",LCD_LINE_2)
      run_cmd("mpc pause")

    if (prev_pressed == 0):
      value_out = 1
      lcd_string("[ PRESSED      ]",LCD_LINE_1)
      lcd_string("[      PREVIOUS]",LCD_LINE_2)
      run_cmd("mpc prev")

    if (next_pressed == 0):
      value_out = 1
      lcd_string("[ PRESSED      ]",LCD_LINE_1)
      lcd_string("[          NEXT]",LCD_LINE_2)
      run_cmd("mpc next")

    sleep(0.1)


def run_cmd(cmd):
p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT)
output = p.communicate()
return output

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 = Truefor character
#      False for command

gpio.output(LCD_RS, mode)

# 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):
# Send string to display
message = message.ljust(LCD_WIDTH," ")
lcd_byte(line, LCD_CMD)
for i in range(LCD_WIDTH):
    lcd_byte(ord(message),LCD_CHR)


if __name__ == '__main__':

try:
    main()
except KeyboardInterrupt:
    pass
finally:
    lcd_byte(0x01, LCD_CMD)
    lcd_string("    Goodbye!    ",LCD_LINE_1)
    gpio.cleanup()

Ehsanahmadi post at 2022-8-13 12:19:40

nopnop2002Published in 2016-3-27 06:03
Edited by nopnop2002 at 2016-3-28 23:01




Thankyou nanopop2022,

It works well.
page: [1]
View full version: LCD 2x16 I2C in Python