16x2 LCD Screen

Project 9: 16×2 LCD Screen Test

What you will need:

  • Raspberry Pi
  • Breadboard
  • 16 x 2 LCD Screen
  • 10kΩ Potentiometer
  • 8 Female to Male Jumper Wires
  • 7 Male to Male Jumper Wires

Instructions:

My son and I recently purchased a 16×2 LCD screen and decided it was time to see if we could make it operate. Although not exact, an identically functioning screen similar to what we purchased can be found through Adafruit Industries.

The LCD screen that we found needed to have header pins soldered to it so we could use it with our breadboard. We simply snapped off 16 pins from a 0.1″ header strip, inserted the strip’s pins through the LCD screen’s board, and soldered the pins to the corresponding pads.

If you have never soldered before, this is a great opportunity to learn. There are many tutorials online that show how simple it is. Just be sure to use a minimal amount of heat from your soldering iron in order to avoid damaging the LCD screen’s board.

Once you have soldered the header pins to the LCD screen and connected it to the breadboard, make the connections shown in the breadboard layout above. This is a good time to note that the 10kΩ Potentiometer in the layout can be bypassed but is a convenient contrast control.

Be very careful when connecting all the breadboard layout’s wires as there are a lot of connections to make. Double check your work to avoid damaging your Raspberry Pi.

I should note at this time that our screen was set up to use 5 volts so we used the Raspberry Pi’s 5 volt GPIO power supply pin for the project. Make sure your LCD screen requires 5 volts before you use this pin. If in doubt, use pin 1 from the Raspberry Pi’s GPIO which only supplies 3.3 volts.

The next step necessary to get the LCD screen to work is to download Adafruit Industries’ Open Source Raspberry Pi Python Code Library. It is a growing collection of libraries and example python scripts for controlling a variety of electronics and there is an excellent selection of functions for the 16 x 2 LCD screen we are using.

The easiest way we found to download the library is to use git to clone the library to our Raspberry Pi. First, make sure you have git installed by entering the following commands from a terminal window or the Raspberry Pi’s command line:

pi@raspberrypi ~ $ sudo apt-get update
pi@raspberrypi ~ $ sudo apt-get upgrade
pi@raspberrypi ~ $ sudo apt-get install -y git dialog

My son and I had set up a projects folder on the Raspbian desktop for easy access and that is the location to which we downloaded the Adafruit library. We simply navigated to our directory from the Raspberry Pi command line by typing:

pi@raspberrypi ~ $ cd Desktop/Projects

Next, we cloned the Adafruit Raspberry Pi Python Code Library to our directory by typing:

pi@raspberrypi ~/Desktop/Projects $ git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git

The above command clones the entire library to our directory but for now we are only interested in the Adafruit_CharLCD functions. There is a directory by that name within the library and within that directory is a file named Adafruit_CharLCD.py. We want to copy that file to the same directory where we have the projects on which we have been working.

In our case, we executed a copy command from within our Projects directory which copied the file from the Adafruit directory to our main Projects folder.

pi@raspberrypi ~/Desktop/Projects $ cp Adafruit-Raspberry-Pi-Python-Code/Adafruit_CharLCD/Adafruit_CharLCD.py .

Once the Adafruit_CharLCD.py file is within your projects directory, use your favorite editor to enter the following Python script:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  LCD_screen_test.py
#
# A short Python script to test a 16 x 2 LCD display
# by using the Adafruit Raspberry Pi Python Library
#
# LCD Screen 16x2 Pins
# --------------------
# Pin 1  to GND
# Pin 2  to 5v
# Pin 3  to variable resistor then to GND 'Contrast Adjustment'
# Pin 4  to GPIO 25 for 'Command or Data Mode Switching'
# Pin 5  to GND for 'Read/Write Mode'
# Pin 6  to GPIO 24 for 'Enable Pin'
# Pin 7  to Not Connected
# Pin 8  to Not Connected
# Pin 9  to Not Connected
# Pin 10 to Not Connected
# Pin 11 to GPIO 23 for 'Data 4'
# Pin 12 to GPIO 17 for 'Data 5'
# Pin 13 to GPIO 21 for 'Data 6'
# Pin 14 to GPIO 22 for 'Data 7'
# Pin 15 to 5v for 'Backlight Anode' - Could use variable resistor as a 'Brightness Control'
# Pin 16 to GND for 'LED Backlight Cathode'
#
#  Copyright 2015  Ken Powers
#  

from Adafruit_CharLCD import Adafruit_CharLCD
import RPi.GPIO as GPIO
import time

# Set GPIO pins to Broadcom numbering system
GPIO.setmode(GPIO.BCM)

RUNNING = True
lcd = Adafruit_CharLCD()

lcd.clear()
lcd.message(" Nicholas\n Powers")
print("Press Ctrl + C to Quit")

# Main loop
try:
    while RUNNING:

		# Scroll LCD screen text off to the right
		for x in range (0, 16):
			lcd.scrollDisplayRight()
			time.sleep(.2)

		# Wait 2 seconds
		time.sleep(2)

		# Scroll LCD screen text back into view
		for x in range (0, 16):
			lcd.DisplayLeft()
			time.sleep(.2)

		# Wait 2 seconds
		time.sleep(2)

# If CTRL+C is pressed the main loop is broken
except KeyboardInterrupt:
    RUNNING = False
    print "\Quitting"

# Actions under 'finally' will always be called
# regardless of what stopped the program
finally:
    # Stop and cleanup so the pins
    # are available to be used again
    GPIO.cleanup()

Code Analysis:

As in previous projects, our Python script begins by letting the interpreter know what language we are using. Next, we have a series of comments designed to clarify the intention of our script and to indicate which pins from the LCD screen are attached to our Raspberry Pi’s GPIO port.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  LCD_screen_test.py
#
# A short Python script to test a 16 x 2 LCD display
# by using the Adafruit Raspberry Pi Python Library
#
# LCD Screen 16x2 Pins
# --------------------
# Pin 1  to GND
# Pin 2  to 5v
# Pin 3  to variable resistor then to GND 'Contrast Adjustment'
# Pin 4  to GPIO 25 for 'Command or Data Mode Switching'
# Pin 5  to GND for 'Read/Write Mode'
# Pin 6  to GPIO 24 for 'Enable Pin'
# Pin 7  to Not Connected
# Pin 8  to Not Connected
# Pin 9  to Not Connected
# Pin 10 to Not Connected
# Pin 11 to GPIO 23 for 'Data 4'
# Pin 12 to GPIO 17 for 'Data 5'
# Pin 13 to GPIO 21 for 'Data 6'
# Pin 14 to GPIO 22 for 'Data 7'
# Pin 15 to 5v for 'Backlight Anode' - Could use variable resistor as a 'Brightness Control'
# Pin 16 to GND for 'LED Backlight Cathode'
#
#  Copyright 2015  Ken Powers
#

Next, we import the modules our Python script will require. We also define a constant that is used to determine if our main loop will execute followed by a command to instantiate an LCD via the Adafruit functions.

from Adafruit_CharLCD import Adafruit_CharLCD
import RPi.GPIO as GPIO
import time

# Set GPIO pins to Broadcom numbering system
GPIO.setmode(GPIO.BCM)

RUNNING = True
lcd = Adafruit_CharLCD()

Next, we clear the LCD screen, by using the Adafruit library’s clear() function and display some text on the 16 x 2 LCD screen by using the message() function. In our case, we chose to display my son’s name which helped to keep him interested in the project. The \n in the text is interpreted as a return character which puts my son’s last name on the second line of the LCD screen.

lcd.clear()
lcd.message(" Nicholas\n Powers")
print("Press Ctrl + C to Quit")

Our main loop uses the Adafruit library’s scroll functions to slide our text off the screen to the right and back again until Ctrl + C is pressed. scrollDisplayRight() moves the text to the right and DisplayLeft() moves it left. The functions are inconsistently named so please note that only the first function has the word scroll in front of it.

# Main loop
try:
    while RUNNING:

		# Scroll LCD screen text off to the right
		for x in range (0, 16):
			lcd.scrollDisplayRight()
			time.sleep(.2)

		# Wait 2 seconds
		time.sleep(2)

		# Scroll LCD screen text back into view
		for x in range (0, 16):
			lcd.DisplayLeft()
			time.sleep(.2)

		# Wait 2 seconds
		time.sleep(2)

# If CTRL+C is pressed the main loop is broken
except KeyboardInterrupt:
    RUNNING = False
    print "\Quitting"

# Actions under 'finally' will always be called
# regardless of what stopped the program
finally:
    # Stop and cleanup so the pins
    # are available to be used again
    GPIO.cleanup()

The Python script above performs a very simple task but was a really fun introduction to the 16 x 2 LCD screen and Adafruit’s library of functions. There are several more functions in the library that we haven’t used yet and we may incorporate them into some of our future projects. For now, however, have fun modifying the Python script above.



One thought on “Project 9: 16×2 LCD Screen Test”

Leave a Reply