Using An I2C Enabled LCD Screen With The Raspberry Pi
In previous posts
I’ve covered using 16×2 and 20×4 LCD screens with the Raspberry Pi
using Python. These are relatively easy to use but require a number of
connections to be made to the Pi’s GPIO header. This setup can be
simplified with the use of an I2C enabled LCD screen.
These can either be purchased as a complete unit or you can attach an I2C backpack to a standard screen.
Here is an I2C backpack :
and here is a backpack soldered to the back of a standard 16×2 LCD screen :
This is a great way to add an I2C enabled LCD screen to your Pi with only four wires!
This requires a high level voltage (5V) and a low level voltage (3.3V) which the device uses as a reference. The HV pins can be connected to the screen and two of the LV pins to the Pi’s I2C interface.
While experimenting I found that it worked fine without the level
shifting but I couldn’t be certain this wasn’t going to damage the Pi at
some point. So it’s probably best to play it safe!
CODE:These can either be purchased as a complete unit or you can attach an I2C backpack to a standard screen.
Here is an I2C backpack :
and here is a backpack soldered to the back of a standard 16×2 LCD screen :
This is a great way to add an I2C enabled LCD screen to your Pi with only four wires!
Step 1 – Connect Screen to the Pi
The I2c module can be powered with either 5V or 3.3V but the screen works best if it provided with 5V. However the Pi’s GPIO pins aren’t 5V tolerant so the I2C signals need to be level shifted. To do this I used an I2C level shifter.This requires a high level voltage (5V) and a low level voltage (3.3V) which the device uses as a reference. The HV pins can be connected to the screen and two of the LV pins to the Pi’s I2C interface.
Level Shifter | Pi | I2C Backpack |
---|---|---|
LV | 3.3V (pin 1) | – |
LV1 | SDA (pin 3) | – |
LV2 | SCL (pin 5) | – |
GND | GND (pin 6) | GND |
HV | 5V (pin 2) | VCC |
HV1 | SDA | |
HV2 | SCL |
Step 2 – Download the Example Script
The example script will allow you to send text to the screen via I2C. It is very similar to my scripts for the normal 16×2 screen. To download the script directly to your Pi you can use :wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/lcd_i2c.py
Step 3 – Enable the I2C Interface
In order to use I2C devices you must enable the interface on your Raspberry Pi. This can be done by following my “Enabling The I2C Interface On The Raspberry Pi” tutorial. By default the I2C backpack will show up on address 0x27.Step 4 – Run Script
Running the script can be done using :sudo python lcd_i2c.pyHopefully you will see something like this :
Troubleshooting
If you are having problems then :- Double check your wiring
- Start from a fresh Raspbian image
- Make sure you set up I2C as mentioned in “Enabling The I2C Interface On The Raspberry Pi” tutorial
- Check the screen is detected when you follow the “Testing hardware” section in the above tutorial
- Ensure SDA and SCL are correctly wired to Pins 3 and 5 on the Pi via the level shifter
- Adjust the contrast ratio using the trimmer on the i2c backpack
https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/lcd_i2c.py
import smbus import time # Define some device parameters I2C_ADDR = 0x27 # I2C device address LCD_WIDTH = 16 # Maximum characters per line # Define some device constants LCD_CHR = 1 # Mode - Sending data LCD_CMD = 0 # Mode - Sending command LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line LCD_BACKLIGHT = 0x08 # On #LCD_BACKLIGHT = 0x00 # Off ENABLE = 0b00000100 # Enable bit # Timing constants E_PULSE = 0.0005 E_DELAY = 0.0005 #Open I2C interface #bus = smbus.SMBus(0) # Rev 1 Pi uses 0 bus = smbus.SMBus(1) # Rev 2 Pi uses 1 def lcd_init(): # Initialise display lcd_byte(0x33,LCD_CMD) # 110011 Initialise lcd_byte(0x32,LCD_CMD) # 110010 Initialise lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size lcd_byte(0x01,LCD_CMD) # 000001 Clear display time.sleep(E_DELAY) def lcd_byte(bits, mode): # Send byte to data pins # bits = the data # mode = 1 for data # 0 for command bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT bits_low = mode | ((bits<<4 0xf0="" bits="" bits_high="" bits_low="" block="" bus.write_byte="" def="" display="" enable="" for="" high="" i="" in="" initialise="" ispy="" lcd="" lcd_backlight="" lcd_byte="" lcd_cmd="" lcd_init="" lcd_string="" lcd_toggle_enable="" line="" low="" main="" message="" more="" ord="" program="" range="" send="" some="" string="" test="" text="" time.sleep="" to="" toggle="" true:="" while=""> RPiSpy",LCD_LINE_1) lcd_string("> I2C LCD",LCD_LINE_2) time.sleep(3) if __name__ == '__main__': try: main() except KeyboardInterrupt: pass finally: lcd_byte(0x01, LCD_CMD) 4>REFF: http://www.raspberrypi-spy.co.uk/2015/05/using-an-i2c-enabled-lcd-screen-with-the-raspberry-pi/
Tidak ada komentar:
Posting Komentar