Homepage Wiki Forum Buy

Port Expander PCA9555

From GNUBLIN

severity (level) requirement Gnublin family
Gnublin logo advanced.png Kernel compile, Toolchain All


Contents

If you need additional GPIO-Pins for one of your projects, you can use a Port-Expander to get more GPIO-Pins. In this article we explain how to use the PCA9555 Port-Expander.

The PCA9555 is a Port-Expander with 16 GPIO-Pins, which can be controlled easily over I2C.

MODULE-PORTEXPANDER SCHRAEGBILD 002.jpg


For Beginners

gnbulin-pca9555

If you want to start quickly and use your port expander you can use the program gnublin-pca9555. Here is a example call which sets pin 0 as output with high level:

root@gnublin:~# gnublin-pca9555 -p 0 -o 1

The following command sets the value of the pin low again:

root@gnublin:~# gnublin-pca9555 -p 0 -o 0

If you want to know the state of a OUTPUT channel, you should try:

root@gnublin:~# gnublin-pca9555 -p 0 -r

If you want to set a pin as input, you can do this with the following command:

root@gnublin:~# gnublin-pca9555 -p 3 -i

The pins are numbered as follows:

PCA9555 Pin Value für gnublin-pca9555 -p xx
IO 0_0 0
IO 0_1 1
IO 0_2 2
IO 0_3 3
IO 0_4 4
IO 0_5 5
IO 0_6 6
IO 0_7 7
IO 1_0 8
IO 1_1 9
IO 1_2 10
IO 1_3 11
IO 1_4 12
IO 1_5 13
IO 1_6 14
IO 1_7 15

Other options:

-a     I2C-Address of the module, default is 0x20
-P     Select an whole Port (0,1)
-j     JSON formatted output
-b     minimal output
-h     show helptext

Controlling with gnublin-gpio

At first its necessary to load the driver as shown here if you want to control the Port-Expander with the progragram gnublin-gpio:

root@gnublin:~# modprobe pca953x cnt=<Int> addr1=<I2C-Adresse>
  1. cnt Number of connected Port-Expanders (default=1)
  2. addr1 Address of the first Port-Expander (Hex)
  3. addr2 Address of the second Port-Expander (Hex)
  4. addr3, addr4 , and addr5 like addr1

If the Port-Expander is for example on the Address 0x20 the command looks like this:

root@gnublin:~# modprobe pca953x addr1=0x20

If there are two Port-Expander one one the address 0x20 and the other on 0x24 the command is this:

root@gnublin:~# modprobe pca953x cnt=2 addr1=0x20 addr2=0x24

The address depends how many Address-Jumper are placed.

i2cdetect -y 1 returns the addresses of all connected and recognised I2C-Devices. The PCA9555 Port-Expander is located between 0x20 and 0x27.

GPIO Pin as Output

To set the value of an GPIO Pin use this:

root@gnublin:~# gnublin-gpio -o 1 -p 98

This command set IO0_0 as output with the value HIGH.

To set the Value back on low type:

root@gnublin:~# gnublin-gpio -o 0 -p 98

GPIO Pin as Input

To get the actual value type:

root@gnublin:~# gnublin-gpio -i -p <pin>

The value <pin> is used for the GPIO Pin. In the following section you can find the values for <pin>.

IO Pins of the Port-Expander and the Value of GPIO-sysfs

Here you can see the table of the IO-Pins of the PCA9555 and the corresponding number in the gpio-sysfs. The control works like seen in the example of gpio98.

PCA9555 Pin Wert für /sys/class/gpio/export
IO 0_0 98
IO 0_1 99
IO 0_2 100
IO 0_3 101
IO 0_4 102
IO 0_5 103
IO 0_6 104
IO 0_7 105
IO 1_0 106
IO 1_1 107
IO 1_2 108
IO 1_3 109
IO 1_4 110
IO 1_5 111
IO 1_6 112
IO 1_7 113

For Advanced Users

Preparation

First of all you should solder the needed pins (SCL,SDA,A0,A1,A1,VCC,GND and the used GPIO-Pins) either on the pins of the Gublin-board or on some wires to use it with a breakboard or something like that.

PCA9555.jpg

If you wire it up after soldering, you should know that you must connect SDA and SCL with pullup-resistors (4.7kOhm) to VCC. (I2C specification)

Here you can see how to connect the I2C-BUS:

I2c pca.jpg

Here you can see the pin assignment of the PCA9555 port expander:

PCA9555 pins.jpg

You can configure the I2C-Address of the device, via the pins A0, A1 and A2. Depending on the level of the pins the address changes. (datasheet) If you connect all three pins to GND, the address of the PCA9555 is 0x20 for the reed access and 0x21 for the write access.

Access over GPIO-sysfs

At first the driver module have to be loaded.

root@gnublin:~# modprobe pca953x

Export Pins

To use for example the pin IO 0_0 of the PCA9555 type this command:

root@gnublin:~# echo 98 > /sys/class/gpio/export

This register the Pin IO 0_0 of the PCA9555 in the gpio-sysfs.

Using Pin as Output and changing the value

To configure the Pin as output:

root@gnublin:~# echo out > /sys/class/gpio/gpio98/direction

To set the output to the HIGH-level:

root@gnublin:~# echo 1 > /sys/class/gpio/gpio98/value

To set the level to low again:

root@gnublin:~# echo 0 > /sys/class/gpio/gpio98/value

Using Pin as Input and read the value

To set the Pin as Input:

root@gnublin:~# echo in > /sys/class/gpio/gpio98/direction

Read the value of the Input:

root@gnublin:~# cat /sys/class/gpio/gpio98/value


In this table is shown which IO correspondents with which number of the GPIO-sysfs.

Enable the kernel module

Device Drivers --> GPIO Support --> PCA953x, PCA955x, TCA64xx, and MAX7310 I/O ports 

Compile the kernel and install the modules (for detailed instructions look here). Copy everything onto the SD-card.

Boot the Gnublin-board.

Test with the demo-application

Change to the directory of the demo-application and execute it with the following command:

root@gnublin:~# cd /root/examples/misc/pca9555
root@gnublin:~# gcc -o i2ctest i2ctest.c

The demo-application is easy to understand. With this application you can set all the GPIO-Pins of Port0 to output and set the level to High.

Test with shell

If you want to control the Port-Expander with the shell you can do this like it is described below:

Installing i2c-tools

First of all you need the package i2c-tools, which you can easily install with the command apt-get install i2c-tools (Debian with internet connection) or like it's described in this article

i2c-set syntax

Afterwards you can use the program i2c-set to control the desired IO-Pins of the Port-Expander. The syntax of i2c-set is described below:

root@gnublin:~# i2c-set I2CINTERFACE SLAVEADdRESS REGISTERADDRESS DATA

The values for the synonyms are looking like this:

I2CINTERFACE 		= 1 		(because we use the device i2c-1)
SLAVEADDRESS 		= 0x20		(if A0,A1 and A2 are connected to GND)
REGISTERADDRESS 	= specific, depending on which Pins you want to control.
DATEN			= specific, depending on which Pins you want to control.

The most important registeraddresses of the Port-Expander are 0x02,0x03,0x06 und 0x07.


Register knowledge

The register with the address 0x06 controls the direction (input/output) of all Port-0 pins.

Bit 7 6 5 4 3 2 1 0
Symbol C0.7 C0.6 C0.5 C0.4 C0.3 C0.2 C0.1 C0.0
Default 1 1 1 1 1 1 1 1

The register with the address 0x07 controls the direction (input/output) of all Port-1 pins.

Bit 7 6 5 4 3 2 1 0
Symbol C1.7 C1.6 C1.5 C1.4 C1.3 C1.2 C1.1 C1.0
Default 1 1 1 1 1 1 1 1

If you want to set all the pins of Port-0 as outputs, for example, you just write the value 0x00 at the registeraddress 0x06. Zero means output and one means input.


The register with the address 0x02 controls the level of all port-0 pins.

Bit 7 6 5 4 3 2 1 0
Symbol O0.7 O0.6 O0.5 O0.4 O0.3 O0.2 O0.1 O0.0
Default 1 1 1 1 1 1 1 1

The register with the address 0x03 controls the level of all port-1 pins.

Bit 7 6 5 4 3 2 1 0
Symbol O1.7 O1.6 O1.5 O1.4 O1.3 O1.2 O1.1 O1.0
Default 1 1 1 1 1 1 1 1

If you want to set the level of pin 0 from port-1 to high, for example, you just have to write the value 0x01 at the registeraddress 0x03. Zero means low and one means high.


Example commands

To set all the pins of port-1 as an output we use the following command:

root@gnublin:~# i2cset 1 0x20 0x06 0x00

After this command you can set the level of pin-0 from port-1 to high, with the following command:

root@gnublin:~# i2cset -y 1 0x20 0x02 0x01

If you want to set all pins from port-1 to low again, you can use the following command:

root@gnublin:~# i2cset -y 1 0x20 0x02 0x00
In other languages