IIC (I2C) Communication on Raspberry Pi by using JAVA

MCP23017 with raspberry Pi using I2C Communication

IIC

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 : bit.ly/2koAFzc

Links :
Java Application on Pi Playlist: bit.ly/2eB1O2K

Github Repository: http://bit.ly/2jVML2Y
Schematic: bit.ly/2kow4Nr
Pi4J GPIO Numbering: bit.ly/2ja20av
Download Pi4J Library: 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 : amzn.to/2koHz7x (India)

Buy Raspberry Pi : amzn.to/2jlIOUH (India)

********************************************************************
Subscribe YouTube : goo.gl/FhfdL7

Guys Subscribe to my channel for latest contents into your inbox.

Suvendu Swain
Catch Me On

Suvendu Swain

Blogger/ Educationist at WeArGenius
A person passionate about tech stuffs.
Suvendu Swain
Catch Me On

Related posts