I'm trying to use the Pi4J java library to write data to an IO Expander via i2C. When I create a new instance of the MCP23017GpioProvider class using the following code:

mcp = new MCP23017GpioProvider(I2CBus.BUS_1, 0x21);

I get the following error: Error writing to /dev/i2c-1 at address 0x21. Got -20001

I have confirmed that I have a rev2 board (hence using bus 1) and that my IO Expander is at address 0x21 by using "i2cdetect -y 1"

Does anyone know what could be causing this error?

P.S. The expander I am using is an MCP23017 16-bit

2 Answers 2


The first thing to check is the permissions you have to access /dev/i2c-1

ls -l /dev/i2c-1

Typically access is allowed to the root and i2c group.

If you are not a member of the i2c group try

sudo adduser your-user-name i2c

You will need to logout and login for the group change to take affect.

  • I added my username to the i2c group and rebooted but still have the same problem :/ May 17, 2014 at 14:37

The same happened to me. adduser did not work. But running as root (sudo) works.

So follow this instructions https://www.raspberrypi.org/forums/viewtopic.php?p=238003

It worked for me.

  • What does adduser have to do with Pi4J, and how do you suggest using sudo with class instantiation in Java?
    – Bex
    Aug 16, 2015 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.