escondax
Posts: 29
Joined: Tue Dec 27, 2016 7:38 am

Pi4J - Mcp23017 java interface errors

Mon Nov 06, 2017 12:40 pm

Hello everyone,

I am trying to use mcp23017 integrated circuit for my project and i started to write java code in netbeans.

But unfrotunately ı get an error and i couldn't solve it.

Here what i did ;

First of all, I created new java project in my desktop computer(not in raspberry pi) and added all necessary library for rpi gpio and i2c as like below.

Image

Then , I started to write MCp23017 main code and called it my main class.
Here is the code;

Code: Select all

import com.pi4j.gpio.extension.mcp.MCP23017GpioProvider;
import com.pi4j.gpio.extension.mcp.MCP23017Pin;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CFactory.UnsupportedBusNumberException;
import com.pi4j.wiringpi.I2C;
import com.pi4j.wiringpi.Gpio;
import java.io.IOException;

import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author burak.dogancay
 */
public class Pi4jI2Control {
    
   
    public static int ldrval; 
    public static int incnumber;
    public static int incnumberl;
    public static void I2csetup() throws UnsupportedBusNumberException, IOException{
        
         final GpioController gpio = GpioFactory.getInstance();
          final MCP23017GpioProvider provider = new MCP23017GpioProvider(I2CBus.BUS_7, 0x27);
      
        
        GpioPinDigitalInput myinputs= gpio.provisionDigitalInputPin(provider, MCP23017Pin.GPIO_A0, "MyInput-A0", PinPullResistance.PULL_UP);
       GpioPinDigitalOutput myoutput[] = {
        gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_A1, "MyOutput-A1", PinState.LOW),
        gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B1, "MyOutput-B1", PinState.LOW)
                };
       
       while(true){
        try {
            ldrval = gpio.getState(myinputs).getValue();
            if (ldrval==1){
                gpio.setState(true, myoutput);
                System.out.println("The ldr value is correctly read : %d"+ldrval);
            }
            else{
                gpio.setState(false, myoutput);
                System.out.println("The ldr value is correctly read : %d"+ldrval);
            }
            if((incnumber>=15)&&(incnumberl==incnumber)){
                break;
            }
            incnumber+=1;
            incnumberl=incnumber;
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(Pi4jI2Control.class.getName()).log(Level.SEVERE, null, ex);
        }
   
    }
        
       gpio.shutdown();
    }
}
I created the circuit and checked I2C adresses as like below (i2cdetect -y 1):
Image

Then I built my application succesfully
Image

After that, I ccopied compiled file to the Raspberry pi with filezilla(in this case ı didn't install pi4j additionally in raspberrypi, ı assumed this compiled pi4j file libs enough to run .jar file).

Here is how i run the program(compiled .jar file) :
Image

After this process, I run my java file inside raspberry pi, but unfortunately ı got this error which is related to pi4j(below picture).(Also ı checked this program without of pi4j implementation and everything works except pi4j (I2C bus, gpio ...etc) )

Image

Kernel version : Linux raspberrypi 4.9.59+ #1047 Sun Oct 29 11:47:10 GMT 2017 armv6l GNU/Linux
Used pi4j version : pi4j-1.2-SNAPSHOT


What do you think, whatis wrong.I checked adresses that ı use for mc23017 and set as 0x27 and I2CBus.BUS_7

escondax
Posts: 29
Joined: Tue Dec 27, 2016 7:38 am

Re: Pi4J - Mcp23017 java interface errors

Mon Nov 06, 2017 1:31 pm

I solved this problem changing

this line of code :

Code: Select all

final MCP23017GpioProvider provider = new MCP23017GpioProvider(I2CBus.BUS_7, 0x27);
with this one :

Code: Select all

final MCP23017GpioProvider provider = new MCP23017GpioProvider(I2CBus.BUS_1, 0x27);
In this case I2CBus.Bus always should be as I2CBus.BUS_1 because we are using 1 as checking I2C (i2cdetect -y 1)

fischp
Posts: 1
Joined: Mon Dec 11, 2017 4:32 pm

Re: Pi4J - Mcp23017 java interface errors

Mon Dec 11, 2017 5:09 pm

Hi, this is my first post so please be gentle, especially if I'm being a bone-head...

I'm trying to figure out the parameters to the MCP23017GpioProvider(I2CBus.BUS_1, 0x21) declaration in my code. My 23017 is mapped to address 0x00 by connecting the three chip address lines to ground. I understand that we're supposed to use BUS_1 from reading many posts, but I'm unclear about the 0x21 address in the above example. The large majority of examples I've found show either x20 or x21 as the address, but some posts show the chip's physical address as x00 as in my case. Why wouldn't the correct declaration for my chip configuration be: MCP23017GpioProvider(I2CBus.BUS_1, 0x00)?

I've honestly looked for documentation but couldn't find anything that defined which address to use. I Sthere a good document which desribes how to figure out the correct address to use?

Thanks in advance!

P

User avatar
DougieLawson
Posts: 30441
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Pi4J - Mcp23017 java interface errors

Mon Dec 11, 2017 9:01 pm

If you connect the A0, A1 & A2 lines to ground the address is 0x20.

Code: Select all

A0	A1	A2	Address
0	0	0	0x20
1	0	0	0x21
0	1	0	0x22
1	1	0	0x23
0	0	1	0x24
1	0	1	0x25
0	1	1	0x26
1	1	1	0x27
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

Return to “Java”

Who is online

Users browsing this forum: No registered users and 3 guests