# Working With Raspberry Pi And 16 X 2 LCD Display

<span class="s"></span>

# Working With Raspberry Pi And 16 X 2 LCD Display


In this tutorial, we will be talking about how to interface the Raspberry Pi and 16 X 2 LCD using GPIO and Python.

At the end of this blog, you will be able to,

*   print your custom message on 16 X 2 LCD
*   print the current time, weather, and weather condition in LCD

To follow this tutorial, you will need the following.

*   Raspberry Pi (We are using Raspberry Pi B+ model)
*   16 X 2 LCD Display
*   Resistors
*   Jumper Wires (Male to male and Female to male)
*   Breadboard

**LCD Pinout**

*   GROUND (GND)
*   VCC +5V (not to be confused with 3.3V)
*   Contrast Adjustment (Vo, usually we use variable resistor)
*   Register Select (RS)RS=0: Command, RS=1: Data
*   Read/Write(RW)RW = 0: WriteRW=1: Read
*   Enable
*   Bit (0) Not required in 4bit operation
*   Bit (1) Not required in 4bit operation
*   Bit (2) Not required in 4bit operation
*   Bit (3) Not required in 4bit operation
*   Bit 4
*   Bit 5
*   Bit 6
*   Bit 7
*   LED Backlight Anode (+)
*   LED Backlight Cathode (-)

**Here is how we wired up our LCD**

![Image for post](https://miro.medium.com/max/60/0*NLvEanUkM5kjcdo7.jpg?q=20)

<noscript><img alt="Image for post" class="fg ev er iv w" src="https://miro.medium.com/max/1300/0*NLvEanUkM5kjcdo7.jpg" width="650" height="650" srcSet="https://miro.medium.com/max/552/0*NLvEanUkM5kjcdo7.jpg 276w, https://miro.medium.com/max/1104/0*NLvEanUkM5kjcdo7.jpg 552w, https://miro.medium.com/max/1280/0*NLvEanUkM5kjcdo7.jpg 640w, https://miro.medium.com/max/1300/0*NLvEanUkM5kjcdo7.jpg 650w" sizes="650px"/></noscript>

![Image for post](https://miro.medium.com/max/48/0*oT5nVtChiESUzDxH.jpg?q=20)

<noscript><img alt="Image for post" class="fg ev er iv w" src="https://miro.medium.com/max/830/0*oT5nVtChiESUzDxH.jpg" width="415" height="516" srcSet="https://miro.medium.com/max/552/0*oT5nVtChiESUzDxH.jpg 276w, https://miro.medium.com/max/830/0*oT5nVtChiESUzDxH.jpg 415w" sizes="415px"/></noscript>

**Note**

In order to control the contrast, you can adjust the voltage presented to Pin 3\. This must be between 0 and 5V. We used a resistor between pin 3 of LCD to GND of Pi.

Your circuit should look something like this.

![Image for post](https://miro.medium.com/max/60/0*deLTWHRLwV299V_l.jpg?q=20)

<noscript><img alt="Image for post" class="fg ev er iv w" src="https://miro.medium.com/max/1812/0*deLTWHRLwV299V_l.jpg" width="906" height="768" srcSet="https://miro.medium.com/max/552/0*deLTWHRLwV299V_l.jpg 276w, https://miro.medium.com/max/1104/0*deLTWHRLwV299V_l.jpg 552w, https://miro.medium.com/max/1280/0*deLTWHRLwV299V_l.jpg 640w, https://miro.medium.com/max/1400/0*deLTWHRLwV299V_l.jpg 700w" sizes="700px"/></noscript>

Rasp<span id="rmm"><span id="rmm">b</span></span>erry Pi b+ GPIO Pinout for reference.

![Image for post](https://miro.medium.com/max/56/0*x2G51tacJwxAKN1C.jpg?q=20)

<noscript><img alt="Image for post" class="fg ev er iv w" src="https://miro.medium.com/max/1432/0*x2G51tacJwxAKN1C.jpg" width="716" height="768" srcSet="https://miro.medium.com/max/552/0*x2G51tacJwxAKN1C.jpg 276w, https://miro.medium.com/max/1104/0*x2G51tacJwxAKN1C.jpg 552w, https://miro.medium.com/max/1280/0*x2G51tacJwxAKN1C.jpg 640w, https://miro.medium.com/max/1400/0*x2G51tacJwxAKN1C.jpg 700w" sizes="700px"/></noscript>

**Python Code**

[Code Goes Here](https://github.com/anishansari/Working-with-raspberry-pi-and-16-X-2-LCD-Display/blob/master/python%20code)

![Image for post](https://miro.medium.com/max/60/0*Bvfox0OcJCA5yEmO.jpg?q=20)

<noscript><img alt="Image for post" class="fg ev er iv w" src="https://miro.medium.com/max/2048/0*Bvfox0OcJCA5yEmO.jpg" width="1024" height="450" srcSet="https://miro.medium.com/max/552/0*Bvfox0OcJCA5yEmO.jpg 276w, https://miro.medium.com/max/1104/0*Bvfox0OcJCA5yEmO.jpg 552w, https://miro.medium.com/max/1280/0*Bvfox0OcJCA5yEmO.jpg 640w, https://miro.medium.com/max/1400/0*Bvfox0OcJCA5yEmO.jpg 700w" sizes="700px"/></noscript>

![Image for post](https://miro.medium.com/max/60/0*uZKcfhI8IOmL-4x8.jpg?q=20)

<noscript><img alt="Image for post" class="fg ev er iv w" src="https://miro.medium.com/max/2048/0*uZKcfhI8IOmL-4x8.jpg" width="1024" height="457" srcSet="https://miro.medium.com/max/552/0*uZKcfhI8IOmL-4x8.jpg 276w, https://miro.medium.com/max/1104/0*uZKcfhI8IOmL-4x8.jpg 552w, https://miro.medium.com/max/1280/0*uZKcfhI8IOmL-4x8.jpg 640w, https://miro.medium.com/max/1400/0*uZKcfhI8IOmL-4x8.jpg 700w" sizes="700px"/></noscript>

**Displaying current date, time and weather info**

In the above tutorial, we saw how to display the custom message on 16 X 2 LCD using raspberry pi and python.Now we shall move on how to display the current weather info on LCD:

*   First, we will need to download the python weather API.Download the pywapi library using the following command on your piwget [_https://launchpad.net/python-weather-api/trunk/0.3.8/+download/pywapi-0.3.8.tar.gz_](https://launchpad.net/python-weather-api/trunk/0.3.8/+download/pywapi-0.3.8.tar.gz)

This will download the tar file of the pywapi library

*   Extract the tar file using
    _tar -zxvf pywapi-0.3.8.tar.gz_

Now, enter the directory using

_cd pywapi-0.3.8_

Now, run the following command

_python setup.py build
python setup.py install_

You are done with installing the pywapi library. Now, download the source code into your Pi, and save it with .py extension (ex — lcd.py).

To run the code, use the following command.

_python lcd.py_

Now, you should be able to see the output as below.

![Image for post](https://miro.medium.com/max/60/0*pj056pVm9pqV5uHi.jpg?q=20)

<noscript><img alt="Image for post" class="fg ev er iv w" src="https://miro.medium.com/max/2048/0*pj056pVm9pqV5uHi.jpg" width="1024" height="466" srcSet="https://miro.medium.com/max/552/0*pj056pVm9pqV5uHi.jpg 276w, https://miro.medium.com/max/1104/0*pj056pVm9pqV5uHi.jpg 552w, https://miro.medium.com/max/1280/0*pj056pVm9pqV5uHi.jpg 640w, https://miro.medium.com/max/1400/0*pj056pVm9pqV5uHi.jpg 700w" sizes="700px"/></noscript>

![Image for post](https://miro.medium.com/max/60/0*ZrsaIWMUf2eY9Jij.jpg?q=20)

<noscript><img alt="Image for post" class="fg ev er iv w" src="https://miro.medium.com/max/2048/0*ZrsaIWMUf2eY9Jij.jpg" width="1024" height="473" srcSet="https://miro.medium.com/max/552/0*ZrsaIWMUf2eY9Jij.jpg 276w, https://miro.medium.com/max/1104/0*ZrsaIWMUf2eY9Jij.jpg 552w, https://miro.medium.com/max/1280/0*ZrsaIWMUf2eY9Jij.jpg 640w, https://miro.medium.com/max/1400/0*ZrsaIWMUf2eY9Jij.jpg 700w" sizes="700px"/></noscript>

![Image for post](https://miro.medium.com/max/60/0*-mY27f5h0ESDo2mu.jpg?q=20)

<noscript><img alt="Image for post" class="fg ev er iv w" src="https://miro.medium.com/max/2048/0*-mY27f5h0ESDo2mu.jpg" width="1024" height="456" srcSet="https://miro.medium.com/max/552/0*-mY27f5h0ESDo2mu.jpg 276w, https://miro.medium.com/max/1104/0*-mY27f5h0ESDo2mu.jpg 552w, https://miro.medium.com/max/1280/0*-mY27f5h0ESDo2mu.jpg 640w, https://miro.medium.com/max/1400/0*-mY27f5h0ESDo2mu.jpg 700w" sizes="700px"/></noscript>

The next step is to find the location code/id of your desired city.

[_https://weather.codes/_](https://weather.codes/)

Open the link and enter your city. You should be able to get unique weather id of your city.

For example, for Bangalore, the weather id is INXX0012, similarly for Kathmandu is NPXX0002.

Now, open your source code and edit the line 8 with your city’s unique weather id.

_#define the weather location id
weather_com_result = pywapi.get_weather_from_weather_com(‘NPXX0003’)_

Now, you should be able to see the weather info of Kathmandu as below.

![Image for post](https://miro.medium.com/max/60/0*27RmNPAuZcg0nA1X.jpg?q=20)

<noscript><img alt="Image for post" class="fg ev er iv w" src="https://miro.medium.com/max/2048/0*27RmNPAuZcg0nA1X.jpg" width="1024" height="463" srcSet="https://miro.medium.com/max/552/0*27RmNPAuZcg0nA1X.jpg 276w, https://miro.medium.com/max/1104/0*27RmNPAuZcg0nA1X.jpg 552w, https://miro.medium.com/max/1280/0*27RmNPAuZcg0nA1X.jpg 640w, https://miro.medium.com/max/1400/0*27RmNPAuZcg0nA1X.jpg 700w" sizes="700px"/></noscript>

![Image for post](https://miro.medium.com/max/60/0*SlonfvsmrlzK52Gl.jpg?q=20)

<noscript><img alt="Image for post" class="fg ev er iv w" src="https://miro.medium.com/max/2048/0*SlonfvsmrlzK52Gl.jpg" width="1024" height="463" srcSet="https://miro.medium.com/max/552/0*SlonfvsmrlzK52Gl.jpg 276w, https://miro.medium.com/max/1104/0*SlonfvsmrlzK52Gl.jpg 552w, https://miro.medium.com/max/1280/0*SlonfvsmrlzK52Gl.jpg 640w, https://miro.medium.com/max/1400/0*SlonfvsmrlzK52Gl.jpg 700w" sizes="700px"/></noscript>

![Image for post](https://miro.medium.com/max/60/0*ygTmNijXDcbehk29.jpg?q=20)

<noscript><img alt="Image for post" class="fg ev er iv w" src="https://miro.medium.com/max/2048/0*ygTmNijXDcbehk29.jpg" width="1024" height="440" srcSet="https://miro.medium.com/max/552/0*ygTmNijXDcbehk29.jpg 276w, https://miro.medium.com/max/1104/0*ygTmNijXDcbehk29.jpg 552w, https://miro.medium.com/max/1280/0*ygTmNijXDcbehk29.jpg 640w, https://miro.medium.com/max/1400/0*ygTmNijXDcbehk29.jpg 700w" sizes="700px"/></noscript>

Get final code from [HERE](https://github.com/anishansari/Working-with-raspberry-pi-and-16-X-2-LCD-Display/blob/master/Final%20Code).

<a href="https://www.buymeacoffee.com/anishansari" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>
