I2C does not seem to work, no way to debug?

I’m trying to use an IO Expander with this setup:

And my code:

`// Relay Control Unit
// IO Expander Class for SX1509
class IoExpander
i2cPort = null;
i2cAddress = null;

constructor(port, address)
    if(port == I2C_12)
        // Configure I2C bus on pins 1 & 2
        i2cPort = hardware.i2c12;
    else if(port == I2C_89)
        // Configure I2C bus on pins 8 & 9
        i2cPort = hardware.i2c89;
        server.log("Invalid I2C port specified.");

    i2cAddress = address << 1;

// Read a byte
function read(register)
    local data = i2cPort.read(i2cAddress, format("%c", register), 1);
    if(data == null)
        server.log("I2C Read Failure");
        return -1;

    return data[0];

// Write a byte
function write(register, data)
    i2cPort.write(i2cAddress, format("%c%c", register, data));


// Port mapping
// io4 - 1 - k1 off
// io5 - 2 - k1 on
// io6 - 3 - k2 off
// io7 - 4 - k2 on
// io0 - 5 - k3 off
// io2 - 6 - k3 on

// address = 0x20

// RGB LED Class
class Relay extends IoExpander {
// IO Pin assignments

a = [4,5,6,7,0,2];
onPort = [5, 7, 2];
offPort = [4, 6, 0];

constructor()    {
    base.constructor(I2C_89, 0x20);
    //c = 3;
    // read(0x00); //should be 0xff
    write(0x00, 0x00); //RegData - set all pins to low
    read(0x00); //should be 0x00
    write(0x01, 0x00); //Direction - set all as output


function relayOn(relay) {
    server.log("ON "+relay);
    write(0x00, onPort[relay]);
    write(0x00, 0x00);

function relayOff(relay) {
    write(0x00, offPort[relay]);
    write(0x00, 0x00);


// Construct Interface
relay <- Relay();

function test() {

// Register with the server
imp.configure(“Relay Control”, [], []);


It doesn’t seem to be working, and when I tried the read command, I got an error. Looking at the I2C lines using a logic analyzer I see both pins constantly high and nothing seems to happen. As I have no way to see what the internal I2C code is doing, I don’t know how to proceed with my debugging. Any thoughts?


p.s. This syntax doesn’t seem to work

c = relay.read(0x00);

it gives me an error like c is undefined or something… it works fine like this…


So I can see a couple of big issues:

  • You’re configuring I2C for pins 8 & 9, but connecting the device to pins 1 & 2. This would explain why you’re seeing no transitions on the lines. Change the I2C_89 to I2C_12 and try again?

  • You’re using 0x20 as the address for the IO expander, which is the 7 bit address. You need to shift this left one. Try 0x40 instead.

The reason c=relay.read(0x00) isn’t working is likely because c has not been declared. Try local c=relay.read(0x00)… though fix the two things above first!

Thanks. I saw sck/data and figured i was using 8/9; doh!

also, the address gets bitshifted in the IoExapnder code, so that should be fine.

I’m also (obviously) new to squirell; why don’t I have to say ‘local’ when I created the arrays in the objects…

a = [4,5,6,7,0,2];

is it because that’s a global scoped variable?

is it because that’s a global scoped variable?

Yes it is. Though honestly, you’re quite right that that’s a little bit arbitrary and confusing.


Ok, so I made some progress. I wasn’t getting anything happening on Pin1 (scl) and after trying a dozen other things, I tried ejecting and reinserting the imp; that fixed it. sigh

So I’m trying to write to a register on the expander, but it doesn’t seem to be doing anything. I read from register 0x00 value 0x00, write to it 0xff, read from it again, and it is still 0x00. I suppose this is more pertaining to the ioexpander, but it is related to one of the ones used in some rgb led example. Any idea what could be causing me grief? Here’s the logic analyzer output : http://i.imgur.com/wPe5P.png

(picture was truncated, check out the link)

In the datasheet it says reading from 0x00 will ALWAYS return the value on the pins, not what you wrote there:

“Write: Data to be output to the output-configured IOs
Read: Data seen at the IOs, independent of the direction configured.”

I presume you’ve not configured the IOs as outputs yet - they default to inputs - so this may be expected behavior. Try writing 0x00 to register 0x01, THEN write 0xff to 0x00 and read it back?

yes, again, i feel dumb. i figured that out, but didn’t post as i was hoping no one would bother following up.

i’m basing the design on the jane(?) design, and i can verify that i’m getting 3.3V to the mosfet, but the mosfet isn’t turning on. havn’t had a chance to play with it to figure out why. stuck there now…

yup, another dumb mistake; working now. win.

We’ve all been there!