Using an I2C LCD Display with a Raspberry Pi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is the fourth installment for my tutorials on using LCD displays with the Raspberry Pi the builds of my previous three videos so I recommend you watch them first this tutorial will demonstrate how to control an LCD display using I squared C we'll start with the PCF eight five seven four io expander chip first I use sudo apt-get install Python SM bus to get the Python library for I squared see also install the optional I squared C tools which has some very helpful utilities the modules file located in the SE folder will need to be modified I use sudo nano for editing and append I squared C - dev and I squared C - B cm to 7:08 to the end of the file after saving the modules file it's necessary to reboot the PI for the changes to take effect before getting into LCD displays I want to quickly demonstrate how you control output pins on an I squared C chip first I import SMB bus module then instantiate an SM bus I pass in the bus number which is one on newer PI's but can be 0 on the older revisions I declare the address at the i/o expander chip and X I'll show you how to set and get the address later a byte is comprised of 8 bits a bit can be 0 or 1 and can be thought of it as a numerical representation of an on/off switch there are 256 possible combinations of bits in a byte so I'll loop from 0 to 255 to show them all I use the right byte method to send the byte to the chip and print it to the console in both decimal and binary format okay let's run the program you'll notice now that the LED light patterns match the bite patterns in the console ones are illuminated and zeros are off the first column numbers on the left are the base 10 numbers these are the numbers which we use in our everyday life and to their right are the base 2 or binary equivalents this is the format that computers use to store numbers you may have noticed tilde in front of the byte variable this is the python unary operator which inverts values zeroes to ones and ones to zeros the PCF 8 5 7 4 pins cannot source enough current to drive an LED but they can sink enough current therefore I have the LED anodes connected to 3.3 volts and the cathodes to the PCF 85-74 when the pins are low the LEDs light up when the pins are high the LEDs go off just so there's no confusion a 0 bit sent to an i/o port turns off the i/o pin to 0 volts which is considered false or low and a 1 bit turns on the pin to 3.3 volts which is true or high the cool thing about sending bytes is that you can change eight pins at once with one command the PC f8 574 is an 8-bit chip it has a port of 8 io pins labeled p 0 to p 7 to wire it up we first need to assign a unique address to the chip using the three address pins labeled a 0 to a 2 setting them all low equates hexadecimal address 20 the LCD display control pins are s and enable are connected to p4 and p6 respectively the LCD data lines d4 through d7 are connected to P Zero to p3 by the way my pin selections were chosen to facilitate filming you can use any combination you like as long as you specify the correct parameters in your code VCC is connected along with the SDA and s CL pins which are the serial data and serial clock lines these are responsible for the I squared C communications on the breadboard I have a PC f8 574 I connect the ground pin to the ground rail which is common to the PI L ground the three address pins to set the hex address each expander on the I squared C bus requires a unique address i connect p4 to RS p6 to enable p0 to d4 p1 to d5 p2 to d6 and p3 to d7 since the 3.3 volt SDA and s CL pins line up on both the pie and the expander I'll use a 3 pin jumper to connect them that's all it takes we can now control the LCD display and we are only using 2 GPIO pins on the PI the I squared C detect utility will let us know if everything is connected properly and will also show the hex the last parameter is the bus number again on older pies this would need to be zero instead of one the map shows that there is one I squared C device at hex address 20 now the question is how do we control the LCD display from Python if we import the Adafruit car LCD module and run help on the Ennead method we can see that the constructor takes an optional GPIO parameter by default it assumes the LCD display is connected to the PI's GPIO but we have the option to specify an alternative inside the Adafruit car LCD folder is a module for an MCP i/o expander which is an alternative to the PCF 857 for which I chose because I had them in my parts drawer and they are very easy to work with the Adafruit module specifically supports the mCP to three zero one seven which is a 16-bit expander and the mCP to three zero zero eight which is an 8-bit expander and similar to the PCF eight five seven four unfortunately there's no support provided for the PCF eight five seven four let's open the Adafruit car LCD module and see what's required to implement a GPIO I'll do a search for all occurrences of GPIO looks like we need a set mode method and a BCM property also a setup method and an out property and finally an output method that's only two properties and three methods I created a new python module and imported sm bus there is a class called PCF eight five seven for GPIO I declared the properties DCM and out I've created a skeleton for the three necessary methods output set mode and set up for set mode and setup I'll use the Python path statement because neither method is relevant all pins will be output and numbered by their port numbers which I can handle in the class constructor for output only two lines are required I take the pass parameters and modify a byte representation of the i/o port using a function called change bid then the SM bus write byte method is used to write the modified byte the aisle expander now I'll paste in the dunder score init method which is the class constructor it fires when the class is created a bus is instantiated an address variable is declared the chip is cleared by setting all pins low and invite for your bill is declared to hold the state of the eight i/o pins next I'll paste in the change bit method referenced in the output method this code might be confusing if you haven't worked with bitwise operations which is outside the scope of this video but basically the method just replaces a specified bit in the byte variable with the new value finally I pasted an optional cleanup method to reset the pins on exit that's all it takes we now have a GPIO module for the PCF eight five seven four that's compatible with the Adafruit LCD library back in idle running as root I import the Adafruit car LCD module I import the PCF eight five seven for GPIO module that I just created I instantiate a GPIO and pass in the bus number and the hex address I create variables for the LCD display pin mappings these correspond to the P numbered pins on the PCF eight five seven four I instantiate an Adafruit LCD where I specify our custom GPIO for the PCF eight five seven for clear the display show some text and end up with the cleanup call now let's save and run the program on the breadboard the text is displayed on the LCD the great thing about I squared C is that you can daisy-chain devices we could add numerous devices for only using up to GPIO pins for example suppose we want to add a second LCD display to our PI for starters we just extend the SDA and STL lines to a second PCF eight five seven four chip then we specify a unique hex address I'll set a zero high by connecting it to 3.3 volts and leave a 1 and a 2 connected to ground which is low this equates the hex dress 21 on the breadboard I have a second display and a second PCF 85-74 configured similarly to the first a changed Eazy row too high to ensure a unique address i connect 3.3 volt power i connect SDA and I connect SC l that's all it takes to add additional I squared C devices back in terminal the I squared C detect utility confirms that everything is working it shows that we have I square she devises a text addresses 20 and 21 I'll add a second GPIO but this time I'll specify hex addressed 21 for the second PCF 8 5 7 4 also add a second LCD and pass GPIO to put some text on the second display and then clean it up there you go - LCD display is being controlled from Python using I squared C and only 2 Pi GPIO pins are necessary I was going to end the video here but after filming eyes certain dip additionally found some MC P 2 3 0 0 8 chips in my desk drawer which I figured would make a good addendum the MC P 2 3 0 0 8 has two additional pins and the ordering is different but the wiring is nearly identical the only difference is is that the reset pin must be pulled high by connecting it to 3.3 volts in the Python code I import the Adafruit mCP 2 3 0 XX module instead of the custom PC f8 5 7 4 module instantiate a GPIO using the Adafruit module the parameters are 1 for bus hex 24 address and 8 for the number of i/o pins the rest of the code remains the same this is a great solution because Adafruit already provides the necessary support for the mCP to three zero zero eight on the breadboard we've got text you might have noticed that there was a substantial amount of lag while the text was being displayed this is one downside to that of route MCP module it's not coded very efficiently so writes to the LCD display are relatively slow which could be a drawback depending on the application I looked online and the majority of the I squared see displays that I found use the PCF eight five seven for chips the rest including Adafruit used the mCP chips but it's a good idea before you buy the check which model is provided this tutorial is a viewer request and any additional videos will be audience guided so please comment what you'd like to see you can support this channel by subscribing thanks for watching and please check out my website roto truant info
Info
Channel: rdagger68
Views: 46,183
Rating: undefined out of 5
Keywords: I²C, Liquid-crystal Display (Invention), Raspberry Pi (Computer), Python (Programming Language), PCF8574, MCP23008, HD44780, Adafruit
Id: i5A5AsDRRwQ
Channel Id: undefined
Length: 10min 42sec (642 seconds)
Published: Sun Feb 08 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.