I²C display on a Raspberry Pi using python

I²C displays are awesome because they save a lot of pins on your microcontroller. A 20×4 sainsmart display would require 16 total pins, and using a I²C bus reduces it to only 4, with the possibility of sharing with other devices, as specified by the I²C protocol. If you are not familiar with I2C or SPI, the Sparkfun website have a very welcoming introduction [1].

In this tutorial we used a 20×4 sainsmart display with an I²C module included, as seen in figure 1, tp connect to a raspberry pi using the I²C port and a logic level translator module.

figure 1 – backside of a 20×4 LCD display with an I2C module (in black) attached.

Configuring the I2C bus

First we need enable and put the I2C bus to work

`sudo raspi-config`

Select “8 – Advanced Config”-> and enable I2C bus:

2.-raspi-config-port-serie

raspi-config interface showing some of it’s interface configurations.

Open terminal again and install the dependencies:

`sudo apt-get install python-smbus i2c-tools`

There are two I2C ports on the raspberry pi, user and system (better use user)

Running i2cdetect in the terminal will report if
program will probe all the addresses on a bus, and report whether any devices are present.

Connecting the display

Pi-GPIO-header

figure 3 – raspberry pi 2 B pin connections

These displays only works 5V TTL logic, and the raspberry pi is 3V3 logic. So we have to use a logic level converter, and it’s usually available on online electronic shops, as pcb modules. Pay attention and check your connections before turning the power on!

The second I2C port are available at pins 23 (GPIO0 / I2C_SDA) and 24 (GPIO1 / I2_SCL), that you can check on the side figure.

The 3V3 and 5V of the I2C module can be connected to the raspberry pi, but pay attention to the max current rating of the pins.  From the elinux.org wiki page [3]:

The maximum permitted current draw from the 3.3 V pins is 50 mA.

Maximum permitted current draw from the 5 V pin is the USB input current (usually 1 A) minus any current draw from the rest of the board.[18]

  • Model A: 1000 mA – 500 mA -> max current draw: 500 mA
  • Model B: 1000 mA – 700 mA -> max current draw: 300 mA

LCD interface using python

The repository [4] with the code example was extracted from [2], and it’s easy to setup.  The code is very simple and it’s a good example on how to use python to interface with hardware. The hierarchy of the files are lcd.py –> i2c_lcddriver.py –> lcd_lib.py.

It is pretty straight forward to use, and can be adapted to other uses. Figure 4 shows my test results.

i2c display in raspberry pi test

figure 4 – display test

Using the RPI GPIOs

The gpio program is a good way to start messing around the raspberry pi’s gpios. Some the commands are:

gpio -v
gpio readall

Other possibilities can be found at [5].

References:

[1] https://learn.sparkfun.com/tutorials/raspberry-pi-spi-and-i2c-tutorial
[2] http://www.recantha.co.uk/blog/?p=4849
[3] http://elinux.org/RPi_Low-level_peripherals
[4] https://github.com/espelhos-sonoros/i2c-display
[5] http://elinux.org/RPi_GPIO_Code_Samples#RPi.GPIO

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s