kr0490
Posts: 13
Joined: Fri Jul 15, 2016 2:21 am

Displaying integers on LCD

Fri Jul 15, 2016 7:51 am

I have a 16x2 LCD I got from sunfounder hooked up through i2c on my Pi3. I can make messages display but can't figure out the correct way to get values (like current temp) to display on the lcd. The working commands I use for text are
LCD1602.write (0,0, 'Message')
and that works but how do I format it to display a changing variable, such as temp for temp=GPIO.input (27)

Major Tom
Posts: 65
Joined: Wed Feb 03, 2016 8:13 am
Location: Strung up in heavens high.

Re: Displaying integers on LCD

Fri Jul 15, 2016 9:26 am

I am guessing you are using Python.

You just need to use string formatting to put your number into a string.

For example,
LCD1602.write (0,0, 'Temp: %s'%temp)

This will convert temp into a string a put it in the place of the %s symbol.

If that doesn't work, try
mystring = 'Temp: %s'%temp
LCD1602.write (0,0, mystring)

That formatting method is a bit old now. Read about newer ways here:
http://stackoverflow.com/questions/1345 ... -be-deprec
My circuit's dead, there's something wrong.

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

Re: Displaying integers on LCD

Fri Jul 15, 2016 10:47 am

kr0490 wrote:I have a 16x2 LCD I got from sunfounder hooked up through i2c on my Pi3. I can make messages display but can't figure out the correct way to get values (like current temp) to display on the lcd. The working commands I use for text are
LCD1602.write (0,0, 'Message')
and that works but how do I format it to display a changing variable, such as temp for temp=GPIO.input (27)
No disagreement with what @Major Tom has said, but to add to it a little..

The simplest demonstration of displaying an integer is something like:

Code: Select all

LCD1602.write (0,0, str(42))
If your variable temp actually contains an integer representing the temperature then you could use:

Code: Select all

LCD1602.write (0,0, str(temp))
(Examples untested as I don't have the display or software you are using.)

BUT...
a changing variable, such as temp for temp=GPIO.input (27)
Temperature is an analogue (continuously varying) quantity. The gpio pins on the RPi header can only detect or be set to DIGITAL (binary) values represented by names such as 0/1, True/False, On/Off.

You cannot simply read the temperature from a gpio pin without the addition of some external analogue-to-digital conversion.

danjperron
Posts: 3408
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Displaying integers on LCD

Fri Jul 15, 2016 11:04 am

Just an add on string format.

In python the simple way to convert a float => integer => string will be the '.format' command.

ex:

Code: Select all

>>> v = 25.51
>>> '{:d}'.format(int(v))
'25'
>>> '{:d}'.format(round(v))
'26'
>>> '{:03d}'.format(round(v))
'026'
>>> '{:+3d}'.format(round(v))
'+26'
>>> '{:+3d}'.format(round(v))
P.S. this is valid with python3 and above.

kr0490
Posts: 13
Joined: Fri Jul 15, 2016 2:21 am

Re: Displaying integers on LCD

Fri Jul 15, 2016 3:00 pm

Yea that GPIO was probably a bad example, I am using an ADC chip to convert the value and using spi. I will give the suggestions a try soon and let you know if they worked. Thanks!

kr0490
Posts: 13
Joined: Fri Jul 15, 2016 2:21 am

Re: Displaying integers on LCD

Sat Jul 16, 2016 4:25 am

That worked, it is now displaying, thanks for the help

Return to “Python”