If you need a small display on your Pi, then you're right here! With little effort and a handfull of parts it's possible to use a 16x2 I2C display on the Raspberry.
First of all you have to update you Pi, therefor enter the command
sudo apt-get update
in the terminal. To be able to use the I2C connection you have to enable it. The first step is to enter the command
Now you'll see the setup utility, in which you have to choose the menu 8 Advanced Options.
In the next window you have to select A7 I2C. In all following windows you can use Yes or OK to enable the I2C bus. With Finish you'll go back to the terminal.
The second step requires two new entries in the module file. Therefor call it by entering
sudo nano /etc/modules
in the terminal. If the file has opened, expand it by adding the following two lines:
With CTRL+X, Yes and Return the file's going to be saved and closed.
No the bus is enabled on the Pi, but to be able to use it with Python you need python-smbus and the i2c-tools. Both can be downloaded with the following terminal command:
sudo apt-get install -y python-smbus i2c-tools
Afterwards the Raspberry needs a restart, so the I2C bus can be activated. You can do this via the GUI or via the terminal with
The few required parts are:
|1||Raspberry Pi||B 2|
|1||Logic Level Shifter||3V3 <-> 5V|
The logic-level-shifter is needed because the Raspberry works with the logical level of 3.3V, but the display (at least mine) works with 5V. Such shifters are available for under one Euro on eBay.
The wiring has to be done as shown in the picture below, which follows the rules of the given table:
|Raspberry Pi||Level Shifter||I2C Display|
|GND (Pin 6)||GND||GND|
|3V3 (Pin 1)||LV|
|SDA (Pin 3)||LV1|
|SCL (Pin 5)||LV2|
|5V (Pin 2)||HV||Vcc|
The finished setup should look like this:
The display itself is a common HD44780-display, which got an I2C backpack. Those "backpacks" are available as single part or already soldered on a display. Just look on eBay, they are also really cheap and work also with a 20x4 display.
The code's on, how could it be different, GitHub. You can load it directly on your Pi from there.
Both Python files, 16x2_I2C_Display.py and lcd.py, must be copied into the same directory, because the first one calls the second one.
This program shows the IP address on the display for 15 seconds after it has booted. It imports the functions of the lcd.py file.
In the 'main' function you can see how it does what it does. First of all the display needs to be initialized, then you can send a command to print some text on it. You can also enable/disale its background LED.
lcd.initialize() lcd.printString("TEXT", lcd.LINE_1) lcd.noBacklight() lcd.Backlight()
Here happens the important stuff - the communication with the display. This works very similar to the Arduino (look here). At this you can send the commands with Python as easy as with C. In the sendByte()-function you can see that I always send BACKLIGHT along with the commands. This has to be done, because the display will turn off the background light if you do it otherwise.
Via smbus (here as i2c declared) the address is sent, followed by the data. In the sendByte() funcion those data will be sent as a sequence of blocks, containing: type, data, backlight. The delay is necesarry, without those blocks won't be delivered properly.
You can now use the lcd.py for every other project. Just copy it into your project directory and call it in your python file by writing import lcd at the beginning of the file. Note that it has to be in the same directory as the 'calling' file is in.
You can also use a 20x4 display, just edit line 15 in the lcd.py file (16 -> 20) and use Line_3 and Line_4 in the printString command.
I want to print the IP address for 15 seconds after the Pi has booted. Therefor you have to run the script after that.
First of all I copied both files into the directory /home/pi/Autostart/ and renamed the 16x2_I2C_Display.py to showIP.py.
Then open the terminal and enter the following command:
sudo nano /etc/rc.local
Here you have to set the path and the file which has to be started after the Pi has booted. In my case it looks like this:
/bin/sleep 15 && cd /home/pi/Autostart/ && sudo python showIP.py & exit 0
sleep 15 gives the number of seconds that should be delayed until the script will be started. It is needed, because the Raspberry gets its ID not right after the start, but a couple of seconds after it. To be sure, I gave 15 seconds. & at the end of the line means, that the script is executed in the background. The exit 0 command terminates the rc.local script. Make sure that the exit 0 command is placed at the bottom of the rc.local file, otherwise all following content won't be executed at the system start.
With CTRL+O the file will be saved, and with CTRL+X you close it. After another reboot you should see a similar picture to mine below.
Questions and comments: deloarts.wordpress.com