This tutorial is all about IIC (I2C) communication on Raspberry Pi by using JAVA and for that, I have used Pi4J library. To demonstrate this in the tutorial I have used MCP23017 Port expander IC with Raspberry Pi. MCp23017 is a 16 Bit input/output Port Expander ICcomes with I2C Interface. To interact with the IC suing IIC (I2C) port here we have used Pi4J and JAVA.
MCP23017 Features:
16-bit input/output port expander with interrupt output
Cascadable for up to 8 devices on one bus
25mA sink/source capability per I/O
Supports 100kHz, 400kHz, and 1.7MHz I2C™Compatible compatible modes
MCP23017 Details : http://bit.ly/2koAFzc
Links :
Java Application on Pi Playlist: http://bit.ly/2eB1O2K
Github Repository: http://bit.ly/2jVML2Y
Schematic: http://bit.ly/2kow4Nr
Pi4J GPIO Numbering: http://bit.ly/2ja20av
Download Pi4J Library: http://bit.ly/2j32blF
Code: http://bit.ly/2jVML2Y
import java.io.IOException; import com.pi4j.io.i2c.I2CBus; import com.pi4j.io.i2c.I2CDevice; import com.pi4j.io.i2c.I2CFactory; import com.pi4j.io.i2c.I2CFactory.UnsupportedBusNumberException; public class Main { public static final int MCP23017_ADDRESS = 0x20; private static final int IODIRA_REGISTER = 0x00; //IODIRA Register. Responsible for input or output private static final int IODIRB_REGISTER = 0x01; //IODIRB Register. Responsible for input or output private static final int GPIOA_REGISTER = 0x12; //GPIOA Register. Write or read value private static final int GPIOB_REGISTER = 0x13; //GPIOB Register. Write or read value //private static final int GPPUA_REGISTER = 0x0C; //PORT A Pull-up value. If set configures the internal pull-ups private static final int GPPUB_REGISTER = 0x0D; ///PORT B Pull-up value. If set configures the internal pull-ups public static void main(String args[]) throws InterruptedException, UnsupportedBusNumberException, IOException { System.out.println("MCP23017 Example"); I2CBus i2c = I2CFactory.getInstance(I2CBus.BUS_1); I2CDevice device = i2c.getDevice(MCP23017_ADDRESS); device.write(IODIRA_REGISTER, (byte) 0x00); device.write(IODIRB_REGISTER, (byte) 0xFF); device.write(GPPUB_REGISTER, (byte) 0xFF); while(true){ System.out.println(device.read(GPIOB_REGISTER)); Thread.sleep(2000); device.write(GPIOA_REGISTER, (byte) 0x00); Thread.sleep(2000); device.write(GPIOA_REGISTER, (byte) 0xFF); } } }
Buy Sensor : http://amzn.to/2koHz7x (India)
Buy Raspberry Pi : http://amzn.to/2jlIOUH (India)
********************************************************************
Subscribe YouTube : https://goo.gl/FhfdL7
Guys Subscribe to my channel for latest contents into your inbox.