Raspberry Pi - PCA9536 Input/Output Expander Java Tutorial
by Dcube Tech Ventures in Circuits > Electronics
1672 Views, 7 Favorites, 0 Comments
Raspberry Pi - PCA9536 Input/Output Expander Java Tutorial
The PCA9536 is an 8-pin CMOS device that provides 4 bits of General Purpose parallel Input/Output (GPIO) expansion for I2C-bus/SMBus applications. It consists of a 4-bit Configuration register to serve the purpose of input or output selection, 4-bit Input Port register, 4-bit Output Port register and a 4-bit Polarity Inversion register active HIGH or active LOW operation. Here is its demonstration with the raspberry pi using java code.
What You Need..!!
1. Raspberry Pi
2. PCA9536
LINK : https://dcubestore.com/product/pca9536-digital-4-channel-input-output-i2c-mini-module/
3. I²C Cable
LINK: https://dcubestore.com/product/i2c-cable/
4. I²C Shield for Raspberry Pi
LINK : https://dcubestore.com/product/i2c-shield-for-raspberry-pi-3-pi2-with-outward-facing-i2c-port-terminates-over-hdmi-port/
5. Ethernet Cable
Connections:
Take an I2C shield for raspberry pi and gently push it over the gpio pins of raspberry pi.
Then connect the one end of I2C cable to PCA9536 sensor and the other end to the I2C shield.
Also connect the Ethernet cable to the pi or you can use a WiFi module.
Connections are shown in the picture above.
Code:
The java code for PCA9536 can be downloaded from our github repository- Dcube Store
Here is the link for the same :
https://github.com/DcubeTechVentures/PCA9536/blob/master/Java/PCA9536.java
We have used pi4j library for java code, the steps to install pi4j on raspberry pi is described here:
You can also copy the code from here, it is given as follows:
// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// PCA9536
// This code is designed to work with the PCA9536_I2CIO I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Digital...
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;
import java.io.IOException;
public class PCA9536
{
public static void main(String args[]) throws Exception
{
// Create I2C bus
I2CBus Bus = I2CFactory.getInstance(I2CBus.BUS_1);
// Get I2C device, PCA9536 I2C address is 0x41(65)
I2CDevice device = Bus.getDevice(0x41);
// Select configuration register
// All pins configured as inputs
device.write(0x03, (byte)0xFF);
// Output to screen
System.out.printf("All Pins State are HIGH %n");
Thread.sleep(500);
// Read 1 byte of data
byte[] data = new byte[1];
data[0] = (byte)device.read(0x00);
// Convert the data to 4-bits
int data1 = (data[0] & 0x0F);
for(int i=0; i<4; i++)
{
if((data1 & ((int)Math.pow(2, i))) == 0)
{
System.out.printf("I/O Pin %d State is LOW %n", i);
}
else
{
System.out.printf("I/O Pin %d State is HIGH %n", i);
Thread.sleep(500);
}
}
}
}
Applications:
PCA9536 can be employed as an I/O expander. It provides a simple solution when additional input/output is required. Usually it is employed in systems which require expansion for ACPI power switches, sensors, push buttons, LEDs, fans, etc.