Vishruth_kumar
Posts: 51
Joined: Thu Feb 04, 2016 6:07 pm
Location: Anand, Gujarat, India
Contact: Website Twitter YouTube

16x2 LCD Display

Tue Jul 05, 2016 6:46 pm

Currently i want to display the reading of LDR on 16x2 lcd screen . I am using th HD44780 library which doesnt have a function to read integer values and write it on the LCD .
usually in main() , if you want to write "Raspberry pi " in LCD you write

from HD44780 import HD44780
lcd=HDD44780()
lcd.message("Raspberry pi")


But my code in my code for LDR the instantaneous value gets stored in a variable and i wish to pass that in the function . PLEASE Help me out.

The code for LCD (HD44780) is:

Code: Select all

  
import RPi.GPIO as GPIO  
from time import sleep  
class HD44780:  
def __init__(self, pin_rs=7, pin_e=8, pins_db=[25, 24, 23, 18]):  
  
        self.pin_rs=pin_rs  
        self.pin_e=pin_e  
        self.pins_db=pins_db  
  
        GPIO.setmode(GPIO.BCM)  
        GPIO.setup(self.pin_e, GPIO.OUT)  
        GPIO.setup(self.pin_rs, GPIO.OUT)  
        for pin in self.pins_db:  
            GPIO.setup(pin, GPIO.OUT)  
  
        self.clear()  
  
    def clear(self):  
        """ Blank / Reset LCD """  
  
        self.cmd(0x33) # $33 8-bit mode  
        self.cmd(0x32) # $32 8-bit mode  
        self.cmd(0x28) # $28 8-bit mode  
        self.cmd(0x0C) # $0C 8-bit mode  
        self.cmd(0x06) # $06 8-bit mode  
        self.cmd(0x01) # $01 8-bit mode  
  
    def cmd(self, bits, char_mode=False):  
        """ Send command to LCD """  
  
        sleep(0.001)  
        bits=bin(bits)[2:].zfill(8)  
  
        GPIO.output(self.pin_rs, char_mode)  
  
        for pin in self.pins_db:  
            GPIO.output(pin, False)  
  
        for i in range(4):  
            if bits[i] == "1":  
                GPIO.output(self.pins_db[::-1][i], True)  
  
        GPIO.output(self.pin_e, True)  
        GPIO.output(self.pin_e, False)  
  
        for pin in self.pins_db:  
            GPIO.output(pin, False)  
  
        for i in range(4,8):  
            if bits[i] == "1":  
                GPIO.output(self.pins_db[::-1][i-4], True)  
  
  
        GPIO.output(self.pin_e, True)  
        GPIO.output(self.pin_e, False)  
  
    def message(self, text):  
        """ Send string to LCD. Newline wraps to second line"""  
  
        for char in text:  
            if char == '\n':  
                self.cmd(0xC0) # next line  
            else:  
                self.cmd(ord(char),True)
    
Last edited by Vishruth_kumar on Wed Jul 06, 2016 7:29 am, edited 1 time in total.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: 16x2 LCD Display

Tue Jul 05, 2016 8:00 pm

Your python script is close to meaningless without indentation.

Please edit your posting and use the [ Code ] button at the top of the message edit screen to wrap your script in [ CODE ] markers.

Asking the question once is normally considered sufficient on these forums.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: 16x2 LCD Display

Tue Jul 05, 2016 8:30 pm

str() is probably what you are looking for.

Vishruth_kumar
Posts: 51
Joined: Thu Feb 04, 2016 6:07 pm
Location: Anand, Gujarat, India
Contact: Website Twitter YouTube

Re: 16x2 LCD Display

Wed Jul 06, 2016 7:22 am

The class HD44780() which i have used is from this blog:
http://www.rpiblog.com/2012/11/interfac ... ry-pi.html

I have pasted the python script with indentation below;

Being a beginner to python i dint know much about coding . The LDR values ranges from 1 to 2000 , approximately after every 0.5 secs and i want to pass that values to a function which will display that instantaneous value to LCD .

Sorry for posting it two times . Can you please write the function for me.

Vishruth_kumar
Posts: 51
Joined: Thu Feb 04, 2016 6:07 pm
Location: Anand, Gujarat, India
Contact: Website Twitter YouTube

Re: 16x2 LCD Display

Wed Jul 06, 2016 7:30 am

I have edited my previous reply in code form please check!!..
Help Me Out!!!

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: 16x2 LCD Display

Wed Jul 06, 2016 7:35 am

Vishruth_kumar wrote:Can you please write the function for me.
Fortunately, the str() function has already been written and is part of the built-in functions of the python interpreter.

Documentation here:
https://docs.python.org/3/library/functions.html

Example:

Code: Select all

pi@rpi2b ~ $ python3
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> ft = str(42)
>>> ft
'42'
>>>

Vishruth_kumar
Posts: 51
Joined: Thu Feb 04, 2016 6:07 pm
Location: Anand, Gujarat, India
Contact: Website Twitter YouTube

Re: 16x2 LCD Display

Wed Jul 06, 2016 11:24 am

Now , I am using adafruit library which is quite similiar to my previous library . As guided by you , I have used str() function , and the program is free from errors .
The output window i.e the python shell shows correct reading of the LDR .
But the LCD shows reading as 4 4 4 4 .... upto 16X2 characters.

I am attaching the complete code ; Please help me fix this bug!!

Code: Select all

from time import sleep


class Adafruit_CharLCD(object):

    # commands
    LCD_CLEARDISPLAY        = 0x01
    LCD_RETURNHOME          = 0x02
    LCD_ENTRYMODESET        = 0x04
    LCD_DISPLAYCONTROL      = 0x08
    LCD_CURSORSHIFT         = 0x10
    LCD_FUNCTIONSET         = 0x20
    LCD_SETCGRAMADDR        = 0x40
    LCD_SETDDRAMADDR        = 0x80

    # flags for display entry mode
    LCD_ENTRYRIGHT          = 0x00
    LCD_ENTRYLEFT           = 0x02
    LCD_ENTRYSHIFTINCREMENT = 0x01
    LCD_ENTRYSHIFTDECREMENT = 0x00

    # flags for display on/off control
    LCD_DISPLAYON           = 0x04
    LCD_DISPLAYOFF          = 0x00
    LCD_CURSORON            = 0x02
    LCD_CURSOROFF           = 0x00
    LCD_BLINKON             = 0x01
    LCD_BLINKOFF            = 0x00

    # flags for display/cursor shift
    LCD_DISPLAYMOVE         = 0x08
    LCD_CURSORMOVE          = 0x00

    # flags for display/cursor shift
    LCD_DISPLAYMOVE         = 0x08
    LCD_CURSORMOVE          = 0x00
    LCD_MOVERIGHT           = 0x04
    LCD_MOVELEFT            = 0x00

    # flags for function set
    LCD_8BITMODE            = 0x10
    LCD_4BITMODE            = 0x00
    LCD_2LINE               = 0x08
    LCD_1LINE               = 0x00
    LCD_5x10DOTS            = 0x04
    LCD_5x8DOTS             = 0x00

    def __init__(self, pin_rs=7, pin_e=8, pins_db=[25, 24, 23, 18], GPIO=None):
        # Emulate the old behavior of using RPi.GPIO if we haven't been given
        # an explicit GPIO interface to use
        if not GPIO:
            import RPi.GPIO as GPIO
            GPIO.setwarnings(False)
        self.GPIO = GPIO
        self.pin_rs = pin_rs
        self.pin_e = pin_e
        self.pins_db = pins_db

        self.GPIO.setmode(GPIO.BCM)
        self.GPIO.setup(self.pin_e, GPIO.OUT)
        self.GPIO.setup(self.pin_rs, GPIO.OUT)

        for pin in self.pins_db:
            self.GPIO.setup(pin, GPIO.OUT)

        self.write4bits(0x33)  # initialization
        self.write4bits(0x32)  # initialization
        self.write4bits(0x28)  # 2 line 5x7 matrix
        self.write4bits(0x0C)  # turn cursor off 0x0E to enable cursor
        self.write4bits(0x06)  # shift cursor right

        self.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF

        self.displayfunction = self.LCD_4BITMODE | self.LCD_1LINE | self.LCD_5x8DOTS
        self.displayfunction |= self.LCD_2LINE

        # Initialize to default text direction (for romance languages)
        self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT
        self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)  # set the entry mode

        self.clear()

    def begin(self, cols, lines):
        if (lines > 1):
            self.numlines = lines
            self.displayfunction |= self.LCD_2LINE

    def home(self):
        self.write4bits(self.LCD_RETURNHOME)  # set cursor position to zero
        self.delayMicroseconds(3000)  # this command takes a long time!

    def clear(self):
        self.write4bits(self.LCD_CLEARDISPLAY)  # command to clear display
        self.delayMicroseconds(3000)  # 3000 microsecond sleep, clearing the display takes a long time

    def setCursor(self, col, row):
        self.row_offsets = [0x00, 0x40, 0x14, 0x54]
        if row > self.numlines:
            row = self.numlines - 1  # we count rows starting w/0
        self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row]))

    def noDisplay(self):
        """ Turn the display off (quickly) """
        self.displaycontrol &= ~self.LCD_DISPLAYON
        self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)

    def display(self):
        """ Turn the display on (quickly) """
        self.displaycontrol |= self.LCD_DISPLAYON
        self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)

    def noCursor(self):
        """ Turns the underline cursor off """
        self.displaycontrol &= ~self.LCD_CURSORON
        self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)

    def cursor(self):
        """ Turns the underline cursor on """
        self.displaycontrol |= self.LCD_CURSORON
        self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)

    def noBlink(self):
        """ Turn the blinking cursor off """
        self.displaycontrol &= ~self.LCD_BLINKON
        self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)

    def blink(self):
        """ Turn the blinking cursor on """
        self.displaycontrol |= self.LCD_BLINKON
        self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)

    def DisplayLeft(self):
        """ These commands scroll the display without changing the RAM """
        self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT)

    def scrollDisplayRight(self):
        """ These commands scroll the display without changing the RAM """
        self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVERIGHT)

    def leftToRight(self):
        """ This is for text that flows Left to Right """
        self.displaymode |= self.LCD_ENTRYLEFT
        self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)

    def rightToLeft(self):
        """ This is for text that flows Right to Left """
        self.displaymode &= ~self.LCD_ENTRYLEFT
        self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)

    def autoscroll(self):
        """ This will 'right justify' text from the cursor """
        self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT
        self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)

    def noAutoscroll(self):
        """ This will 'left justify' text from the cursor """
        self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT
        self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)

    def write4bits(self, bits, char_mode=False):
        """ Send command to LCD """
        self.delayMicroseconds(1000)  # 1000 microsecond sleep
        bits = bin(bits)[2:].zfill(8)
        self.GPIO.output(self.pin_rs, char_mode)
        for pin in self.pins_db:
            self.GPIO.output(pin, False)
        for i in range(4):
            if bits[i] == "1":
                self.GPIO.output(self.pins_db[::-1][i], True)
        self.pulseEnable()
        for pin in self.pins_db:
            self.GPIO.output(pin, False)
        for i in range(4, 8):
            if bits[i] == "1":
                self.GPIO.output(self.pins_db[::-1][i-4], True)
        self.pulseEnable()

    def delayMicroseconds(self, microseconds):
        seconds = microseconds / float(1000000)  # divide microseconds by 1 million for seconds
        sleep(seconds)

    def pulseEnable(self):
        self.GPIO.output(self.pin_e, False)
        self.delayMicroseconds(1)       # 1 microsecond pause - enable pulse must be > 450ns
        self.GPIO.output(self.pin_e, True)
        self.delayMicroseconds(1)       # 1 microsecond pause - enable pulse must be > 450ns
        self.GPIO.output(self.pin_e, False)
        self.delayMicroseconds(1)       # commands need > 37us to settle

    def message(self, text):
        """ Send string to LCD. Newline wraps to second line"""
        for char in text:
            if char == '\n':
                self.write4bits(0xC0)  # next line
            else:
                self.write4bits(ord(char), True)

if __name__ == '__main__':
    import RPi.GPIO as GPIO
    import time
    lcd = Adafruit_CharLCD()
    lcd.clear()

    GPIO.setmode(GPIO.BCM)
    pin_to_circuit = 4
    def rc_time (pin_to_circuit):
     count = 0
  
     #Output on the pin for 
     GPIO.setup(pin_to_circuit, GPIO.OUT)
     GPIO.output(pin_to_circuit, GPIO.LOW)
     time.sleep(0.5)

     # Change the pin back to input
     GPIO.setup(pin_to_circuit, GPIO.IN)
  
     #Count until the pin goes high
     while (GPIO.input(pin_to_circuit) == GPIO.LOW):
        count += 1

     return count

 #Catch when script is interrupted, cleanup correctly
try:
    # Main loop
    while True:
        
        ft=str(pin_to_circuit)
        print rc_time(pin_to_circuit)
        lcd.message(ft)
except KeyboardInterrupt:
    pass
finally:
    GPIO.cleanup()

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: 16x2 LCD Display

Wed Jul 06, 2016 9:08 pm

Vishruth_kumar wrote:the LCD shows reading as 4 4 4 4 .... upto 16X2 characters.
That seems to be what you are asking it to do!

This is the loop in your script that writes a message to the lcd:

Code: Select all

    while True:
       
        ft=str(pin_to_circuit)
        print rc_time(pin_to_circuit)
        lcd.message(ft)
Earlier in the main code you define pin_to_circuit:

Code: Select all

    pin_to_circuit = 4
Nothing in the while True: loop changes the value of pin_to_circuit, so it repeatedly displays its value of 4.

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: 16x2 LCD Display

Thu Jul 07, 2016 4:42 am

I think what you were meaning to write is

Code: Select all

while True:
       
        ft=rc_time(pin_to_circuit)
        print ft
        lcd.message(ft)

Vishruth_kumar
Posts: 51
Joined: Thu Feb 04, 2016 6:07 pm
Location: Anand, Gujarat, India
Contact: Website Twitter YouTube

Re: 16x2 LCD Display

Thu Jul 07, 2016 3:05 pm

actually pin_to_circuit is the data pin from which ldr gives reading to GPIO #4 to raspberry pi
That is why i defined pin_to_circuit as 4

The while loop you sent has error; Please Help Me!! Thanks for the previous response

I am uploading the error

Code: Select all

Traceback (most recent call last):
  File "/home/pi/ldr.py", line 228, in <module>
    lcd.message(ft)
  File "/home/pi/ldr.py", line 190, in message
    for char in text:
TypeError: 'int' object is not iterable

Vishruth_kumar
Posts: 51
Joined: Thu Feb 04, 2016 6:07 pm
Location: Anand, Gujarat, India
Contact: Website Twitter YouTube

Re: 16x2 LCD Display

Thu Jul 07, 2016 3:34 pm

Hey its working , The LCD is showing the reading in continuous manner i.e. 258 344 ...... and so on.I have used str() function. I want it to refresh the lcd after every iteration . Can you please tell me where should i place the function lcd.clear() ? ..
the code of main() is: Please help me!!..

Code: Select all

if __name__ == '__main__':
    import RPi.GPIO as GPIO
    import time
    lcd = Adafruit_CharLCD()
    lcd.clear()

    GPIO.setmode(GPIO.BCM)
    pin_to_circuit = 4
    def rc_time (pin_to_circuit):
     count = 0
  
     #Output on the pin for 
     GPIO.setup(pin_to_circuit, GPIO.OUT)
     GPIO.output(pin_to_circuit, GPIO.LOW)
     time.sleep(0.5)

     # Change the pin back to input
     GPIO.setup(pin_to_circuit, GPIO.IN)
  
     #Count until the pin goes high
     while (GPIO.input(pin_to_circuit) == GPIO.LOW):
        count += 1

     return count

 #Catch when script is interrupted, cleanup correctly
try:
    # Main loop
    while True:
        
        print rc_time(pin_to_circuit)
        lcd.message(str(rc_time(pin_to_circuit)))
        
       
except KeyboardInterrupt:
    pass
finally:
    GPIO.cleanup()

Vishruth_kumar
Posts: 51
Joined: Thu Feb 04, 2016 6:07 pm
Location: Anand, Gujarat, India
Contact: Website Twitter YouTube

Re: 16x2 LCD Display

Thu Jul 07, 2016 6:25 pm

The problem is solved , Thank you!!
I am uploading the correct code:

Code: Select all

from Adafruit_CharLCD import Adafruit_CharLCD
import RPi.GPIO as GPIO
import time
lcd = Adafruit_CharLCD()
lcd.clear()

GPIO.setmode(GPIO.BCM)
pin_to_circuit = 4
def rc_time (pin_to_circuit):
     count = 0
  
     #Output on the pin for 
     GPIO.setup(pin_to_circuit, GPIO.OUT)
     GPIO.output(pin_to_circuit, GPIO.LOW)
     time.sleep(0.5)

     # Change the pin back to input
     GPIO.setup(pin_to_circuit, GPIO.IN)
  
     #Count until the pin goes high
     while (GPIO.input(pin_to_circuit) == GPIO.LOW):
        count += 1

     return count

 #Catch when script is interrupted, cleanup correctly
try:
    # Main loop
    while True:
        
        print rc_time(pin_to_circuit)
        lcd.clear()
        lcd.message('LDR Reading:')
        lcd.message(str(rc_time(pin_to_circuit)))
        
       
except KeyboardInterrupt:
    pass
finally:
    GPIO.cleanup()

Return to “Python”