Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

32
Adafruit 16x2 Character LCD + Keypad for Raspberry Pi Created by lady ada Last updated on 2016-11-02 05:32:42 PM UTC

Transcript of Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

Page 1: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

Adafruit 16x2 Character LCD + Keypad for Raspberry PiCreated by lady ada

Last updated on 2016-11-02 05:32:42 PM UTC

Page 2: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

236677777777

927

29293030313132

Guide Contents

Guide ContentsOverviewParts List

1) Resistors2) Potentiometer3) Pushbuttons4) i2c Port Expander Chip5) Male Header Pins6) Printed Circuit Board7) Raspberry Pi Plate Header8) BumperAdditional

AssemblyModel B+ Protection

UsageSetting up your Pi for I2CUsing the example Python codeTesting the LibraryAdjusting ContrastUsing the library codeDownload

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 2 of 33

Page 3: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

Overview

This new Adafruit plate makes it easy to use a 16x2 Character LCD. We really like therange of LCDs we stock in the shop, such as our classic blue &white (http://adafru.it/181) as well as the fancy RGB negative (http://adafru.it/399) and RGBpositive (http://adafru.it/398). Unfortunately, these LCDs do require quite a few digital pins, 6to control the LCD and then perhaps another 3 to control the RGB backlight for a total of 9pins. That's nearly all the GPIO available on a Raspberry Pi!

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 3 of 33

Page 4: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

With this in mind, we wanted to make it easier for people to get these LCD into theirprojects so we devised a Pi plate that lets you control a 16x2 Character LCD, up to 3backlight pins AND 5 keypad pins using only the two I2C pins on the Pi! The best partis you don't really lose those two pins either, since you can stick i2c-based sensors, RTCs,etc and have them share the I2C bus. This is a super slick way to add a display without allthe wiring hassle.

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 4 of 33

Page 5: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

WARNING: The new Raspberry Pi model B+ with 4 USB ports overlaps closely withbacklight resistor leads on the char LCD plate. You will need to cover the USB port andback of resistors with electrical tape to protect them from touching. See more details andpictures on the assembly page of this guide.This plate is perfect for when you want to build a stand-alone project with its own userinterface. The 4 directional buttons plus select button allows basic control without having toattach a bulky computer.

The kit is designed for any Raspberry Pi - version 1 and 2.

If you want plug in a Cobbler or Gertboard at the same time, check out our StackingHeader, you can fit an IDC cable over it if the Plate is assembled with thispart. (http://adafru.it/1112)

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 5 of 33

Page 6: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

Parts ListCheck to make sure your kit comes with the following parts.Sometimes we make mistakesso double check everything and email [email protected] if you need replacements!

We recently adjusted the kit so the buttons are on the RIGHT side instead of the left.The parts list is otherwise the same, its just a little more stable than before

1) Resistors

There is a total of 3 resistors in this kit. For resistors labeled RED and BLUE on the PCB,they are 1/4W 5% 220 ohm resistors (Red, Red, Brown, Gold). For the resistor labeledGREEN on the PCB, it is a 1/4W 5% 330 ohm resistor (Orange Orange Brown Gold).

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 6 of 33

Page 7: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

2) Potentiometer

There is one 10k trim potentiometer. This part will go in the spot labeled Contrast

3) Pushbuttons

There are a total of 5 x 6mm tactile switch pushbuttons. These will be used in the UP,DOWN, LEFT, RIGHT and SELECT locations on the PCB.

4) i2c Port Expander Chip

There is one of these MCP23017 i2c (16 input/output) port expander chips in the kit. This ishow we are able to only use 2 R-Pi pins to run the entire LCD and buttons. Click here formore info on this chip. (http://adafru.it/732)

5) Male Header Pins

There is one strip of 36 male header pins in the kit. These will be used to attach the LCD tothe PCB.

6) Printed Circuit Board

There will be one PCB in the kit as shown above.

7) Raspberry Pi Plate Header

There will be one extra-tall 26 pin female header for plugging into the Pi

8) Bumper

There will be one rubber bumper as shown above

Additional

You'll also need an LCD to place into the shield.

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 7 of 33

Page 8: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

Your LCD may have 16 pins (Monochrome) or 18 pins (RGB) and may have 2 rows ofconnectors or one. This is normal and does not affect the display

You can also use 16x2 LCDs that are the same size that do not have an RGBbacklight, or have no backlight at all.

We carry Negative type (http://adafru.it/399) or Positive type (http://adafru.it/398) LCDs aswell as Blue and White LCDs (http://adafru.it/181)

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 8 of 33

Page 9: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

Assembly

Check the kit against the parts listto verify you have all the partsnecessary

We recently adjusted the kit sothe buttons are on the rightside instead of the left. Theparts list is otherwise thesame, its just a little morestable than before

Put the printed circuit board into avise or board holder, heat up yoursoldering iron and make sureyou're ready to go!

We'll start with the first resistorGREEN - which has orange,orange, brown, gold bands on it.This resistor acts as the backlightcontrol resistor for the greenbacklight pin.

Bend the resistor into a 'staple'and slide it into the slot markedGREEN on the PCB. Resistorsdo not have a direction so youcan put it in 'either way' and it'llwork find.

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 9 of 33

Page 10: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

Bend the 'legs' of the resistor outso it sits flat against the PCB andflip it over.

This way the resistor won't fall outwhile soldering.

With your soldering iron heatedup and ready, solder in bothleads of the resistor. To do this,heat up the round ring pad andthe wire lead at the same time for2 or 3 seconds, then dip the endof the solder into the heated jointto melt it in.

Then remove the solder and thesoldering iron.

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 10 of 33

Page 11: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

Once the soldering is complete,we can clean up by clipping theleads of the resistor. This keepsthem from shorting to somethingelse. Use diagonal or flushcutters to clip the wires rightabove where the solder jointends.

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 11 of 33

Page 12: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

Since you did so great with thefirst resistor, we'll place all of therest now at the same time.

The two 220 ohmresistors RED and BLUE -named because they are thebacklight series resistors for theRGB backlights on the LCDs.These resistors are colored RedRed Brown Gold.

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 12 of 33

Page 13: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

Solder the resistors just like youdid with the first one.

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 13 of 33

Page 14: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

Clip all the leads.

Next up we will place the buttons.These buttons are useful to senda signal to the Pi (say if you havea basic menu system). We have a4-way 'direction pad' for

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 14 of 33

Page 15: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

up/down/left/right input and abutton to the right calledSELECT. These 5 buttons shouldbe able to make 'talking' back toyour project easy. These areconnected to the I2C portexpander chip so they require noextra pins on the Pi, our librarydoes the work of reading whetherthey are pressed.

All the buttons are the same, andthey should snap nicely intoplace. Press down onto eachbutton until it snaps in and sits flatagainst the PCB.

We recently adjusted the kit sothe buttons are on the RIGHTside instead of the left. Thebuttons snap in the same butthey're on the right

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 15 of 33

Page 16: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

Flip over the PCB and check thatall the legs for the buttons aresticking out.

Solder each leg, taking care notto accidentally 'short' two buttonlegs together. The ones for thedirectional pads are very close!

Next, place the 10Kpotentiometer (the orange-facedthing with three legs) into the spotabove the RESET button. It willonly fit one way. This is thecontrast potentiometer which willadjust how dark the charactersappear. All displays are slightlydifferent so you'll adjust this oncethe display is soldered in.

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 16 of 33

Page 17: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

The kit may come with twopotentiometers - a big blue onefor breadboarding the LCD and asmaller orange one for the shieldkit. You can throw away orrecycle the blue one, use only theorange one here!

We recently adjusted the kit tohave the potentiometer in thecenter rather than the right, goesin the same way, just in themiddle!

Flip over the PCB and solder inthe three legs of thepotentiometer

We're nearly done! Now we willplace the I2C port expander chip.Double check that it has theMCP23017-E/SP marking on it.This is a 16-pin expander chip,that uses the i2c bus. That meansyou can send it commands usingthe i2c pins on an Pi and control16 more digital pins! 5 of thosepins go to the buttons, 6 go to theLCD control and 3 are used forthe backlight (the remaining 2 are

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 17 of 33

Page 18: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

unused).

Unlike buttons or resistors, chipsdo have a direction and the mustbe put in the right way! First, usea flat table to carefully bend thelegs of the chip so they areparallel. Then slip it into thesilkscreened outline so that thenotch at the end of the chip is onthe right. Click the image to theleft to make absolutely sureyou've got it in the right way.Once you are sure, press thechip into place

We recently adjusted the kit sothe chip is more to the left, its thesame alignment, just shifted over

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 18 of 33

Page 19: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

Flip over the PCB and solder inthe 28 pins of the port expander.

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 19 of 33

Page 20: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

Next we will attach the header,there is a standard 'extra tall'header included in the kit.However, if you want to attach acobbler or GertBoard, etc to theplate, you may want to opt forone of our stacking headers,they're extra long so you can plugin an IDC cable on top!

Before we solder in the header,remove the bumper from thebacking and attach it on theunderneath so its right over theEthernet jack when the PCB isplugged in.

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 20 of 33

Page 21: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

On kits where the buttons are onthe left, put it right below thecontrast pot, as shown. This willkeep the pot from touching theEthernet jack.

On kits where the buttons are onthe right, put it right next to thebuttons (it shouldnt overlap anybutton legs) sitting flat

Place the 2x13 header onto yourPi, and slide the plate on top, itshould fit perfectly.

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 21 of 33

Page 22: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

Now we will solder all 26 pins forthe plate - this will send powerand data between the two boardsand also provide a mechanicalstability. You may need to holdthe plate down with tape to get itsitting flat against the Pi.

Solder in one pin, we suggest thetop right, then check if the plate issitting flat. If not, heat up the jointwith one hand while stabilizingwith the other until its right.

Then solder the other 25 pins

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 22 of 33

Page 23: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

Finally, we place the LCD. Break off a piece of 18 or 16 pinheader from the stick in the kit.

If you have an RGB display,you'll need 18, for a blue&whiteyou'll need 16

Place the header into theremaining slot with the long endsof the pins sticking UP. If youhave only 16 pins, leave the tworightmost pads empty

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 23 of 33

Page 24: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

Slide the LCD over the header sothat it is perfectly centered overthe four holes and the silkscreen.

Your LCD may have two rowsof connectors or one row. If itsa two-row-connector LCD wedo not use the bottom row, justcontinue using the LCD as itwill fit just fine!

The LCD should 'snap' in againstthe buttons

Solder all the pins!

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 24 of 33

Page 25: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

Flip over the assembly, carefully.Then solder in the 16 or 18 pinsof header

WARNING: If you're using the new Raspberry Pi model B+ with 4 USB ports you will needto take some extra steps to ensure the backlight resistor leads do not touch the USB portsand short out. See the steps below for more details.

Model B+ Protection

With the new Raspberry Pi model B+ and its extra USB ports there's a slight problem withthe layout of the character LCD shield. As you can see in the photo below, the three

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 25 of 33

Page 26: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

through-hole backlight resistors have their leads right above one of the metal USB ports.Unfortunately these leads are quite close and can potentially short against the metal USBport.

There is current limiting in the resistor circuit so it shouldn't damage the Pi if there is a short,but to be sure it is advised to cover the USB port and resistor leads in a few layers ofelectrical tape. Be sure to cut the resistor leads as short as possible with flush cutters andtry to ensure there are no sharp edges from the cut leads or solder. See the photo belowwith arrows pointing to the two locations you should place electrical tape.

Note that if you are using a Raspberry Pi model A or B (i.e. Pi with only 1 or 2 USBports) you can skip this step and move on. You only need to add this tape if you'reusing a Raspberry Pi model B+, the Pi with 4 USB ports.

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 26 of 33

Page 27: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

That's it! You're done soldering,now you just have to run the codein the Usage section next.

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 27 of 33

Page 28: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

UsageThe backlights will not turn on until you have the code running so don't be alarmed ifnothing lights up when you plug in the plate! If you have a 'positive' style LCD, you may seesome squares on the first line, that's normal

Setting up your Pi for I2CFor a more basic introduction to setting up I2C on your Pi, take a diversion to this Adafruittutorial: http://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c (http://adafru.it/aTI)

It is required to install kernel I2C support before continuing!

You'll also want to run:

sudo apt-get install python-smbussudo apt-get install i2c-tools

i2c-tools isn't strictly required, but it's a useful package since you can use it to scan for anyI2C or SMBus devices connected to your board. If you know something is connected, butyou don't know it's 7-bit I2C address, this library has a great little tool to help you find it:

sudo i2cdetect -y 0 (if you are using a version 1 Raspberry Pi)sudo i2cdetect -y 1 (if you are using a version 2 Raspberry Pi)

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 28 of 33

Page 29: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

This will search /dev/i2c-0 or /dev/i2c-1 for all address, and if an Adafruit LCD Plate isconnected, it should show up at 0x20

Once both of these packages have been installed, you have everything you need to getstarted accessing I2C and SMBus devices in Python.

Using the example Python codeThe LCD Pi Plate Python code for Pi is available on Github at:https://github.com/adafruit/Adafruit_Python_CharLCD (http://adafru.it/dJo)

Note that this is an updated library which can be installed independently of other Pythoncharacter LCD code. If you're looking for the older library, it can still be found at its home onGitHub (http://adafru.it/dK9). For new projects try using the newer library described here.

The easiest way to get the code onto your Pi is to hook up an Ethernet cable, and clone itdirectly using 'git', which is installed by default on most distros. Simply run the followingcommands from an appropriate location (ex. "/home/pi"):

sudo apt-get updatesudo apt-get install build-essential python-dev python-smbus python-pip gitsudo pip install RPi.GPIOgit clone https://github.com/adafruit/Adafruit_Python_CharLCD.gitcd Adafruit_Python_CharLCDsudo python setup.py install

Testing the LibraryOnce the code has be downloaded to an appropriate folder, and you have your LCD Plateproperly connected, you can start with the basic demo display, which is run by simplyexecuting the char_lcd_plate.py script in the examples folder:

cd examplessudo python char_lcd_plate.py

The example will turn the backlight on to different RGB colors (don't worry if you're using amonochrome backlight LCD, it is normal to see the backlight turn on and off) and then askyou to press buttons. As you press different buttons the button name should be printed andthe backlight color changed. Try pressing different buttons to see what happens on thedisplay.

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 29 of 33

Page 30: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

Press Ctrl-C to quit the program.

Adjusting ContrastThe plate uses a character LCD with an external contrast potentiometer. The first time youuse it, adjust the potentiometer in the bottom right until you see the text clearly. If you don'tupload code to the Pi, some boxes may appear instead, or you may see nothing at all.

Using the library codeLook at the contents of the char_lcd_plate.py file to see the basic usage of the characterLCD plate class. The basic usage is to import the library and create an instance of theAdafruit_CharLCDPlate class. The char LCD plate class is smart enough to know how totalk to the character LCD plate without any configuration or parameters.

Once you have an instance of the Adafruit_CharLCDPlate class there are a few functionsyou can call to interact with the display:

message(text)Print the provided string message to the display. The text string can includelinebreak characters ('\n') and will move to the next line when found in the string.

clear()Clear the display and reset the position of message printing to the first columnand first line.

set_color(red, green, blue)Set the color of the red, green, and blue backlight LEDs. Each color valueshould be a 1 for on or 0 for off. For example to set a red backlight callset_color(1,0,0) or to set a purple color call set_color(1,0,1).

You can find a more detailed write-up of the library in this character LCDguide (http://adafru.it/dKa) (see the Usage and Raspberry Pi Char LCD Plate pages).

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 30 of 33

Page 31: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

DownloadAdafruit's Pi Python codebase (http://adafru.it/aOg)EagleCAD PCB files on GitHub (http://adafru.it/rSA)Fritzing object in the Adafruit Fritzing Library (http://adafru.it/aP3)

Check the Usage page for how to install the example python code directly from your Piusing git.

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 31 of 33

Page 32: Adafruit 16x2 Character LCD + Keypad for Raspberry Pi

© Adafruit Industries https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi

Page 32 of 33