# Drive a 16x2 LCD with the Raspberry Pi

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/001/726/medium800/raspberry_pi_IMG_5694.jpg?1396775762)

Adding a LCD to any project immediately kicks it up a notch. This tutorial explains how to connect an inexpensive HDD44780 compatible LCD to the Raspberry Pi using 6 GPIO pins. While there are other ways to connect using I2C or the UART, this is the most direct method that gets right down to the bare metal.   
  
This technique:

- allows for&nbsp; **inexpensive LCDs** to be used
- does not require any **i2c drivers**
- won't steal the only **serial port** on the Pi.

The example Python code sends date,&nbsp;time, and the Pi's IP address&nbsp;to the display. If you are running a Pi in headless mode, being able to determine the IP address at a glance is really handy.

![](https://cdn-learn.adafruit.com/assets/assets/000/001/756/medium800/raspberry_pi_timeip.jpg?1396776081)

# To Follow This Tutorial You Will Need
- [Standard LCD 16x2 + extras](http://adafruit.com/products/181 "Link: http://adafruit.com/products/181")
- [Pi T-Cobbler Plus,](https://www.adafruit.com/product/2028)&nbsp;[Pi Cobbler Plus for Model B+ / Pi 2](https://www.adafruit.com/products/2029)&nbsp;or the original [Pi Cobbler](https://www.adafruit.com/product/914)
- [(2) Half size breadboards](https://www.adafruit.com/products/64)&nbsp;
- [Hook-up Wire](https://www.adafruit.com/index.php?main_page=adasearch&q=hook-up+wire+spool "Link: https://www.adafruit.com/index.php?main\_page=adasearch&q=hook-up+wire+spool")
- [A Raspberry Pi](https://www.adafruit.com/?q=raspberry%20pi) (compatible with all 26pin and 40pin Pi releases to date)

Info: 

# Drive a 16x2 LCD with the Raspberry Pi

## Wiring the Cobbler to the LCD

# The LCD
![](https://cdn-learn.adafruit.com/assets/assets/000/001/737/medium800/raspberry_pi_IMG_5592.jpg?1396775896)

Whenever you come across a LCD that looks like it has 16 connectors it is most likely using a HD44780 controller. These devices provide the&nbsp;same pinouts making them relatively easy to work with. The LCD uses a parallel interface meaning that we will need many pins from our raspberry pi to control it. In this tutorial we will use 4 data pins (4-bit mode)&nbsp;and two control pins.  
  
The **data pins** are straightforward. They are sending data to the display (toggled high/low). We will only be using write mode and not reading any data.  
  
The **register select** pin has two uses. When pulled low it&nbsp;can&nbsp;send commands to the LCD (like position to move to, or clear the screen). This is referred to as writing to the instruction or command&nbsp;register. When toggled the other way (1) the register select pin goes into a data mode and will be used to send data to the screen.  
  
The **read/write** pin will be pulled low (write only)&nbsp;as we only want to write to the LCD based on this setup.  
  
The **enable** pin will be toggled to write data to the registers.

# LCD Pinout
1. Ground
2. VCC - **5v not 3.3v**
3. Contrast adjustment (VO) from potentiometer  
4. Register Select (RS). RS=0: Command, RS=1: Data
5. Read/Write (R/W). R/W=0: Write, R/W=1: Read ( **we won't use this pin** )  
6. Clock (Enable). Falling edge triggered
7. Bit 0 **(Not used in 4-bit operation)**
8. Bit 1 **(Not used in 4-bit operation)**
9. Bit 2 **(Not used in 4-bit operation)**
10. Bit 3 **(Not used in 4-bit operation)**
11. Bit 4
12. Bit 5
13. Bit 6
14. Bit 7
15. Backlight LED Anode (+)
16. Backlight LED Cathode (-)

Before wiring, check that your LCD has an LED backlight, not an EL backlight. LED backlights use 10-40mA of power, EL backlights use 200+ma! EL backlights are often cheap to get but are not usable, make sure you don't use one or you will overload the Pi. Some cheap LCDs that have LED backlights do not include a resistor on the LCD module for the backlight, if you're not sure, connect a 1Kohm resistor between pin 15 and 5V instead of connecting directly. **All Adafruit LCDs have LED backlights with built in resistors so you do not need an extra resistor!**  

# 5v LCD vs 3.3v Pi
The raspberry Pi GPIOs are designed for&nbsp;3.3v, but our LCD is a 5v device. **It's fine to use a 5v display, but only if we are sending data out of the Pi**. We are not going to use the 3.3v power rail on the Cobbler, and we will tie the **RW** (read/write) pin of the display to GND as we do not want the display sending a 5v signal into the Pi.   
  
Don't cross the streams!

# Wiring Diagram
First, connect the Cobbler power pins to the breadboard power rail. +5.0V from the Cobbler goes to the red striped rail (red wire) and GND from the cobbler goes to the blue striped rail (black wire)  
  
In order to send data to the LCD we are going to wire&nbsp; it up as follows

- Pin #1 of the LCD goes to ground
- Pin #2 of the LCD goes to +5V
- Pin #3 (Vo) connects to the middle of the potentiometer
- Pin #4 (RS) connects to the Cobbler #22
- Pin #5 (RW) goes to ground
- Pin #6 (EN) connects to Cobbler #17
- Skip LCD Pins #7, #8, #9 and #10
- Pin #11 (D4) connects to cobbler #25
- Pin #12 (D5) connects to Cobbler #24
- Pin #13 (D6) connects to Cobber #23
- Pin #14 (D7) connects to Cobber #18
- Pin #15 (LED +) goes to +5V (red wire)
- Pin #16 (LED -) goes to ground (black wire)

Then connect up the potentiometer, the left pin connects to ground (black wire) and the right pin connects to +5V (red wire)

# Here's a sketch of the T-Cobbler Plus version:
![](https://cdn-learn.adafruit.com/assets/assets/000/069/298/medium800/learn_raspberry_pi_6E70A5FA-A7F4-4502-93C0-71A1A677B71F.jpeg?1547326747)

# Sketch for a 26 pin Cobbler Raspberry Pi (v1, v2)
![](https://cdn-learn.adafruit.com/assets/assets/000/069/300/medium800/learn_raspberry_pi_9257941F-B62C-456E-AD33-8EC5BBC04124.jpeg?1547326832)

# Preparing the LCD
  
Before you start, make sure you have a strip of 0.1" male header and a 10K potentiometer. All Adafruit Character LCDs come with these parts so you should be good to go.   
  
![](https://cdn-learn.adafruit.com/assets/assets/000/001/744/medium800/raspberry_pi_parts.jpg?1396775953)

Most LCDs have a strip of 16 pins on the top, if the header is a little longer, just break it off until its the right length

![](https://cdn-learn.adafruit.com/assets/assets/000/001/745/medium800/raspberry_pi_headersize.jpg?1396775958)

Next you'll need to solder the header to the LCD. **You must do this, it is not OK to just try to "press fit" the LCD!**

![](https://cdn-learn.adafruit.com/assets/assets/000/001/746/medium800/raspberry_pi_lcdbb.jpg?1396775965)

Start by connecting the **5V** and **GND** wires from the cobbler to the breadboard. Then connect pins #1, #2 and #15, #16 to the breadboard power rails as shown. The backlight should come on. If it doesn't, check the wiring!

![](https://cdn-learn.adafruit.com/assets/assets/000/001/752/medium800/raspberry_pi_lcdpower.jpg?1396776028)

![](https://cdn-learn.adafruit.com/assets/assets/000/001/750/medium800/raspberry_pi_charbacklite.jpg?1396776003)

Next, wire up the contrast potentiometer as shown above, with the middle pin connecting to LCD pin #3 and the other two pins going to 5V and ground.  
  
Twist the potentiometer until you see the first line of the LCD fill with boxes. If you don't see the boxes, check your wiring!

![](https://cdn-learn.adafruit.com/assets/assets/000/001/751/medium800/raspberry_pi_charboxes.jpg?1396776014)

Finish the wiring for the **RS, RW, EN, D4, D5, D6,** and **D7** pins as shown in the diagram up top.

That's it! Now you're ready to run the Python script to draw text on the screen!

# Drive a 16x2 LCD with the Raspberry Pi

## Necessary Packages

# Update Your Pi to the Latest Raspbian
Your Pi will need to be running the latest version of Raspbian. This tutorial was written using Raspbian Stretch (Nov. 2018). Checkout our guide for [Preparing an SD Card for your Raspberry Pi](https://learn.adafruit.com/adafruit-raspberry-pi-lesson-1-preparing-and-sd-card-for-your-raspberry-pi) if you have not done so already. After the installation is complete be sure and run the following commands to make sure your installation packages are up to date.&nbsp;

```auto
sudo apt-get update -y
sudo apt-get upgrade -y
```

# Install pip3
pip3 is already installed with a full Raspbian installation, but the Raspbian Lite does not include pip3 so it needs to be installed as shown below.

```auto
sudo apt-get install python3-pip
```

# Install adafruit-blinka
```auto
sudo pip3 install adafruit-blinka
```

# Install adafruit-circuitpython-charlcd
```auto
sudo pip3 install adafruit-circuitpython-charlcd
```

# Drive a 16x2 LCD with the Raspberry Pi

## Python Script

![](https://cdn-learn.adafruit.com/assets/assets/000/001/754/medium800/raspberry_pi_timeip.jpg?1396776056)

The following&nbsp; code can be downloaded to your raspberry pi and run to get the date, time and IP address of your machine on the LCD display.

# The Code
https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Drive_a_16x2_LCD_with_the_Raspberry_Pi/Drive_a_16x2_LCD_with_the_Raspberry_Pi.py

## Download the Code
Let's put this file right in your home directory for simplicity. The wget command makes things easy.

```auto
wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Drive_a_16x2_LCD_with_the_Raspberry_Pi/Drive_a_16x2_LCD_with_the_Raspberry_Pi.py
```

# Running the Code
The following command will start the program and you should see the LCD display come to life with date, time and IP address.

```auto
sudo python3 ./Drive_a_16x2_LCD_with_the_Raspberry_Pi.py
```

# Drive a 16x2 LCD with the Raspberry Pi

## Display Time & IP on Every Boot

It's all fine and dandy to have a script which we can manually run, but wouldn't it be nice to have the time and ip address pop up on the display when the Raspberry Pi boots up? This is done using an init script which runs our Python code every time your Raspberry Pi boots up.&nbsp;

## Download the service file
The following command will allow you to download the lcd.service file directly to your Pi.

```auto
wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Drive_a_16x2_LCD_with_the_Raspberry_Pi/lcd.service
```

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Drive_a_16x2_LCD_with_the_Raspberry_Pi/lcd.service

## Place the lcd.service file
The lcd.service file needs to be copied into the correct location and the systemctl command can be used to start | stop | enable the service. It is a good idea to test this before enabling as there might be a minor path difference on your system.&nbsp;

```auto
sudo cp lcd.service /etc/systemd/system
```

## Test the lcd.service
```auto
sudo systemctl daemon-reload
sudo systemctl start lcd.service
ps auxww | grep -i 16x2
```

![](https://cdn-learn.adafruit.com/assets/assets/000/069/574/medium800/learn_raspberry_pi_ps-rpi-screenshot.png?1547774442)

The following commands read the updates to the service file, start the lcd.service and confirm that the process is running. If the script shows up in the 'ps' command output you have done everything correctly and can now enable the service and reboot. The service should activate upon bootup automatically.

## Enable lcd.service
```auto
sudo systemctl enable lcd.service
```

Now on each boot the LCD will automatically show the date/time/ip address on startup. This means you will know when the Pi is reachable and what the ip address is without having to plug a monitor in.

# Drive a 16x2 LCD with the Raspberry Pi

## Time Zone

Last, but not least: My Pi came configured with UT (Universal Time). I prefer to see time based on my time zone, which is Mountain. Here is how to configure time on the Pi for any location. This is a one time configuration setting that will survive between reboots.

We can use raspi-config to easy set our timezone. Choose the following:

- Localisation Options
- Change Timezone
- Continent / Country
- Time zone

## Run raspi-config
```auto
sudo raspi-config
```

![](https://cdn-learn.adafruit.com/assets/assets/000/069/578/medium800/learn_raspberry_pi_2019-01-17-182646_1920x1080_scrot.png?1547775015)

![](https://cdn-learn.adafruit.com/assets/assets/000/069/575/medium800/learn_raspberry_pi_2019-01-17-182436_1920x1080_scrot.png?1547774979)

![](https://cdn-learn.adafruit.com/assets/assets/000/069/576/medium800/learn_raspberry_pi_2019-01-17-182451_1920x1080_scrot.png?1547774985)

![](https://cdn-learn.adafruit.com/assets/assets/000/069/577/medium800/learn_raspberry_pi_2019-01-17-182505_1920x1080_scrot.png?1547774995)


## Related Guides

- [Adafruit IO Basics: Analog Input](https://learn.adafruit.com/adafruit-io-basics-analog-input.md)
- [Using LoraWAN and The Things Network with Feather](https://learn.adafruit.com/the-things-network-for-feather.md)
- [micro:bit Lesson 4. Sensing Light](https://learn.adafruit.com/micro-bit-lesson-4-sensing-light-and-temperature.md)
- [Using QMK on RP2040 Microcontrollers](https://learn.adafruit.com/using-qmk-on-rp2040-microcontrollers.md)
- [OpenClaw on Raspberry Pi](https://learn.adafruit.com/openclaw-on-raspberry-pi.md)
- [PyPortal IoT Plant Monitor with Google Cloud IoT Core and CircuitPython](https://learn.adafruit.com/pyportal-iot-plant-monitor-with-google-cloud-iot-core-and-circuitpython.md)
- [micro:bit Lesson 2. Controlling LEDs on Breadboard](https://learn.adafruit.com/micro-bit-lesson-2-controlling-leds-on-breadboard.md)
- [BLE Volume Knob with CircuitPython](https://learn.adafruit.com/bluetooth-le-hid-volume-knob-with-circuitpython.md)
- [Scrambled Number Security Keypad](https://learn.adafruit.com/scrambled-number-security-keypad.md)
- [Ladyada's Toolkit](https://learn.adafruit.com/ladyadas-toolkit.md)
- [Control an LED With Your Voice using Watson and Raspberry Pi](https://learn.adafruit.com/tjbot-control-an-led-with-your-voice-watson-on-raspberry-pi.md)
- [Working with Multiple Same Address I2C Devices](https://learn.adafruit.com/working-with-multiple-i2c-devices.md)
- [CircuitPython Libraries on Linux and the 96Boards DragonBoard 410c](https://learn.adafruit.com/circuitpython-libraries-on-linux-and-the-96boards-dragonboard-410c.md)
- [Programming with Scratch 2 or 3 on Raspberry Pi](https://learn.adafruit.com/programming-with-scratch-on-raspberry-pi.md)
- [Trellis Feather DSP-G1 Synthesizer](https://learn.adafruit.com/feather-trellis-dsp-g1-synthesizer.md)
