Setup
We will test the IC using a Raspberry Pi. Set the I2C address of the PCA9535 to 0x20 by pulling down all three address pins. And connect I2C wires, SDA and SCL, to Raspberry Pi I2C-1 bus. After enabling I2C bus on RPi, we can install i2c utility as follow.
$ sudo apt install i2c-tools
Then, we can scan the bus using the following command. We should see the address 0x20.
$ i2cdetect -y 1
Once we can see the PAC9535 on the bus, we can read its interrnal registers e.g. we can read port 1 input register at address 0x01 as follow.
$ i2cget -y 1 0x20 0x01
We will also present an example to use a utility lib for I2C bus to control PCA9535 which is connected to RPi. For that install prerequisites as follows.
cd ~ sudo apt -y update sudo apt -y install build-essential cmake git # wxWidgets sudo apt -y install libgtk-3-dev checkinstall sudo apt install libwxgtk3.0-gtk3-dev # opencv sudo apt -y install git pkg-config libavcodec-dev libavformat-dev libswscale-dev sudo apt -y install libopencv-dev #jsoncpp sudo apt -y install libjsoncpp-dev # ceUtil git clone --depth 1 https://github.com/yan9a/ceutil.git cd ceutil chmod +x ceUtil.sh ./ceUtil.sh c
C++ Example
An example to use PCA9535 with Raspberry Pi can be found at the following link.
https://github.com/yan9a/rpi/blob/master/bus/pca9535_i2c/pca9535_i2c.cpp
It simply read first two register values which holds PCA9535 line input values. We can compile and run it as follows.
$ g++ pca9535_i2c.cpp -o pca9535_i2c $ ./pca9535_i2c
PCA9535 has 8 internal registers as follows.
- Address 0 = Port 0 input
- Address 1 = Port 1 input
- Address 2 = Port 0 output
- Address 3 = Port 1 output
- Address 4 = Port 0 polarity
- Address 5 = Port 1 polarity
- Address 6 = Port 0 configuration
- Address 7 = Port 1 configuration
https://github.com/yan9a/rpi/blob/master/bus/pca9535_cei2c/src/pca9535_cei2c.cpp
I use cmake to build it. Use https://github.com/yan9a/rpi/blob/master/bus/pca9535_cei2c/pca9535_cei2c.sh to build and run it.
$ ./pca9535_cei2c.sh
Using Device Tree Overlay
We can use PCA9535 IO lines as normal GPIO lines by enabling Raspberry Pi device tree overlay support for it [2]. Edit /boot/config.txt and add the following lines.
dtoverlay=pca953x,pca9535,addr=0x20
After rebooting, if you scan the i2c bus 1, you should see UU instead of 0x20 at the corresponding address. And you should see new gpio device when listing dev directory.
i2cdetect -y 1 ls -l /dev/gpio*
We can use old way of controlling gpio using sysfs. But, in this example, we will use newer approach of using libgpiod library for it [3]. Install the library as follow.
sudo apt install gpiod libgpiod-dev
Check gpiochip information using the following commands.
gpiodetect gpioinfo
If PCA9535 appears as gpio chip 2, its input channel 12 can be read as
gpioget 2 13
Command to set line number 15 is
gpioset 2 15=1
A simple C example to control its GPIO lines using libgpiod is at the following link.
https://github.com/yan9a/rpi/blob/master/bus/gpiodeg/gpiod_eg.cpp
References
[1] ceutil. C++ utilities lib. 2022.url: https://github.com/yan9a/ceutil.
[2] Raspberry Pi. Firmware boot overlays.
url: https://github.com/raspberrypi/firmware/blob/master/boot/overlays/README.
[3] Jeff Tranter. GPIO Programming: Exploring the libgpiod Library. 2021 Jan 20
url: https://www.ics.com/blog/gpio-programming-exploring-libgpiod-library.
No comments:
Post a Comment
Comments are moderated and don't be surprised if your comment does not appear promptly.