Raspberry Pi: Temperature and Time on a 16×2 LCD Display

Raspberry Pi: Temperature And Time On A 16×2 LCD DisplayAll hail the mighty NERDBOX!

I’ve continued to tinker with my Raspberry Pi Internet Radio and have recently been focusing on a menu system using the 16×2 display. With a bit of creative coding you can emulate an on-screen menu system quite well with some simple code.

One part I wanted to share this week was my main screen that shows the temperature and the time on a home screen. Both of these elements are updated every few seconds and the delay doesn’t affect button pushes at any time. Consider it like a live desktop monitor as such. Magic!

Here’s a quick video I uploaded today sharing the update, which also includes some of the other features I have been playing with such as custom Radio station names, artist/track scrolling and Tweeting the Pi’s temperature with a button push (more on that in my other blog here).

So, if you’ve read this far, you probably want to know how to get your Pi temperature/time on your LCD screen. Let’s begin…

Step 1: What you need

Pretty obvious this time round:

  • Raspberry Pi setup
  • 16×2 LCD screen (other sizes should work – aim for the HD44780 type)

You need to have the LCD set up and working already, again, lots of information on the web on this. This post is more about looking at the code once you have one set up already.

That’s all really. I wont go into keyboards, text editors etc – comment if you need help here.

Step 2: Context and Familiarisation

Let’s just get our heads around some of the code/functions we will be using first.

A bit of context here on the command we will use to get the Pi’s temperature. Go to terminal and type in the following:

 /opt/vc/bin/vcgencmd measure_temp  

You should get the current temperature displayed in this format:


That’s how we get the temperature for our display (we will use some code later to turn that into a string to use on our LCD).

Now similarly we find out the time, again in Terminal type:


Which should give you something like this:

 Thu Sep 26 21:25:40 BST 2013  

Now when I mention these later, you’ll kind of understand what they are, what they do, and where the information will be coming from.

I personally think if you’re approaching anything with a 16×2 LCD, the best place to start is to try and create an Internet Radio with one first. 

Now that kind of sounds hard, but due to the number of examples, forum conversations and YouTube videos on this subject – it really is as simple as following instructions. Try Meistervisions tutorial as a starting point, and Google yourself mad from there.

Step 3: The Python Code

Let’s jump straight in and take a overall look at my code to kick us off – it may look a bit daunting but if I can work this out, so can you. Grab a coffee, take your time and stick with this. Oh and…my coding is very poor – but it works!

 def menu():  
  timelastchecked = 0  
   if time.time() >= timelastchecked:  
    timelastchecked = time.time()+3  
    mystring = ""  
    mytime = ""  
    mytemp = ""  
    pretemp = "NBX ["  
    posttemp = "] "  
    for i in f.readlines():  
     mytime += i  
     mytime = mytime[11:-13]  
     f=os.popen("/opt/vc/bin/vcgencmd measure_temp")  
     for i in f.readlines():  
      mytemp += i  
      mytemp = mytemp[5:-3]  
      mystring = pretemp + mytemp + posttemp + mytime  
      lcd_byte(LCD_LINE_1, LCD_CMD)  
      lcd_byte(LCD_LINE_2, LCD_CMD)  
      lcd_string("< Off   Menu >",2)  
    if ( GPIO.input(NEXT) == False):  
    if ( GPIO.input(PREV) == False):  

Confused? Great. Let’s break it down…

Lines 1-3: (some initial stuff)

 def menu():  
  timelastchecked = 0  

def menu(): – this is just defining this entire section of code as a module

timelastchecked = 0 – we will use this later on, it helps to not run the code too quick

time.sleep(0.5) – I just use this in case I hol