Ceci est une ancienne révision du document !
This month, we will be using a 16×2 LCD display with an i2c interface to do the same thing that we did last month (FCM 111) with the Dallas Semiconductor DS18B20 Temperature sensor. If you remember, we had to use many I/O pins on the RPi. This time, thanks to the i2c display, we will need only 5 pins (+5v, Gnd, Sensor Data, SDA (data) and SCL (clock)) to do the same thing.
Before we get started with our project, a discussion about i2c is in order. I have distilled the following discussion from a wonderful tutorial by 'SFUPTOWNMAKER' at Sparkfun.com which can be found at https://learn.sparkfun.com/tutorials/i2c?_ga=1.242063243.863781319.1463423290
i2c
Inter-Integrated Circuit (i2c) protocol is intended to allow multiple “slave” digital ICs to communicate with one or more master chips. Like Serial Peripheral Interface (SPi), it is intended for only short distance communications within a single device. Like Asynchronous Serial Interfaces (RS-232 or UARTs), it requires only two signal wires to exchange information.
RS232 (Asynchronous Communications)
No clock data is required on the lines; however, both sides must agree on the communications data rate. Requires hardware overhead (UART) at each end.
In addition to the 8 data bits, at least one start and one stop bit are required for each data frame. While it is possible to connect multiple devices on a single serial port, there are issues with multiple devices trying to use the two lines at the same time.
Most UART devices can support only certain set baud rates, the usual maximum is 230400 bits per second.
RX ←——– TX TX ———> RX
SPI
The biggest drawback with SPI is the number of pins required. Connecting a single master to a single slave requires four lines, each additional slave requires one addition chip select (CS) IO pin to the master. If you want to use many sensors/devices connected to the master, the required number of pins can quickly overwhelm the number of available inputs/outputs.
SPI (see diagram below) is good for high data rate full-duplex (simultaneous send/receive of data). Data rates can be upwards of 10 MHz.
i2c
i2c requires only 2 lines like async serial, but those two lines can support up to 1008 slave devices. Unlike SPI, i2c can support a multi-master system – allowing multiple slave devices to communicate to multiple master devices. There is a limitation that the masters can not communicate with each other on the i2c bus, and they must take turns using the bus lines. i2c has a similar overhead to Async in that, for each 8 bits of data, one extra bit is required as an “Ack/Nack” bit. The hardware requirements are more complex than SPI, but less than Async.
Data rates are somewhere between Async and SPI. Most i2c devices can communicate at between 100 KHz to 400 KHz.
In the diagram above, SDA is the data line and SCL is the clock line.
Hopefully I didn’t completely confuse you and you are ready to go ahead with our project.
A very good resource for what we are about to do is at http://www.circuitbasics.com/raspberry-pi-i2c-lcd-set-up-and-programming/
Make sure that i2c is enabled on the RPi. This is set in raspi-config.
Now, in a terminal, use apt-get to install two support libraries. (I wasn’t able to get it to work as a single liner for some reason.):
sudo apt-get install i2c-tools
sudo apt-get install python-smbus
I was able to get Fritzing to come up with the i2c backpack (shown top right).
Hook up the SDA pin of the i2c backpack to PHYSICAL pin 3 on the RPi (this is GPIO2) and the SCL on the backpack to PHYSICAL pin 5 (GPIO3). Pick a free 5v pin on the RPi (either pin 2 or 4) and a free ground (pins 6 or 9) and connect them to the backpack VCC and Gnd. Don’t forget we need the temp sensor connected to GPIO4 as last month (along with the resistor to +5VDC).
Now reboot and once the RPi is up, in a terminal type:
i2cdetect -y 1
This will verify that the i2c interface is working on your Pi, and also tell you what address is used by the LCD display device. Look at the screen dump shown right to see what it should look like.
As you can see, my device is at 3f, but yours might be at a different address. When you create the driver below (either typing directly from the article or from the pastebin page), you will need to enter your device address in line 22.
The first set of code is a library that will work as a driver for the i2c LCD. This should be saved as i2c_lcd_driver.py. The code is on http://pastebin.com/ueu18fNL to save you typing.
Now, we are going to do a short test to make sure everything works. Type in the following code and save it as i2c_test1.py, into the same folder as the driver that we just wrote…
import i2c_lcd_driver from time import *
mylcd = i2c_lcd_driver.lcd()
mylcd.lcd_display_string(“This is a test”,1)
If everything here is correct, you should see “This is a test” at the first character position on the first line of the LCD display. If it’s good, we can move forward. The following code is basically the same code from last month modified to use the i2c display instead of the parallel version.
That pretty much wraps up our discussion of LCDs and i2c. We will be using i2c LCDs and other i2c devices in the future, so you should keep them safe for later on. Next month, we will start working with motors, servos and stepper motors. So, run out, and get a hobby motor to be ready. In a few months, we’ll start working with the Arduino microcontroller and then learn to interface the RPi to control the Arduino.
Until then have fun.
