I2C write return value

I have implemented a simple i2c reader for the HIH-6130, everything seems to work but a couple of observations…

  1. despite example code (github etc) the address is (0x27 <<1) not 0x27.

2.i2c.write((0x27 <<1), "") returns a value of -4, which isnt documented.

anyway everything works ok as far as I can tell heres the code…

`
// Temperature and Humidity Monitor using Honeywell HIH6130 sensor
//hardware.configure(I2C1_89);

local i2c = hardware.i2c89;
i2c.configure(CLOCK_SPEED_100_KHZ);

local out_temp = OutputPort(“Temperature”);
local out_humid = OutputPort(“Humidity”);
address <- (0x27 << 1);

function main()
{
server.log(“main()…”)

// Start conversion
local e = i2c.write(address, "");
server.log(format("Result: %d", e ));

// wait for 36 ms and then read the result
imp.wakeup( 0.036, getsample )

}

function getsample()
{
server.log(“getsample()…”)

// Read out temperature and humidity, zero length subaddress means the write
// subaddr phase will be totally skipped and we'll just do the read
local th = i2c.read(address, "", 4);
if ( th == null )
{
    server.log("I2C.read failed");
    return;
}

server.log(format("%02x:%02x:%02x:%02x", th[0], th[1], th[2], th[3]) );
// note data will be marked as stale because each read kicks off another
// cycle as we always do a write-before-read on i2c
// hence, we need to mask top bits of humidity byte

// Form temp and humidity
local temperature = ((((th[2]         << 6 ) | (th[3] >> 2)) * 165) / 16383.0) - 40;
local humidity    = ((((th[0] & 0x3F) << 8 ) | (th[1]     ))        / 163.83 );

// log and print
server.log(format("temperature %.2fC humidity %.2f%%",temperature, humidity));
server.show(format("%.2fC %.2f%%", temperature, humidity));

//Send data to server
out_temp.set(temperature);
out_humid.set(humidity);

// wait for next reading
imp.wakeup(10, main)

}

imp.configure(“Temp/Humid”, [],[out_temp, out_humid]);
// Get a sample, then sleep
main();

`

Hmm, where did you see the address was 0x27? The only example I can find is this one: http://devwiki.electricimp.com/doku.php?id=nora …which has the address as 0x4e (which is 0x27<<1).

mmm cant find the example, (since I shamelessly borrowed the code from somewhere on git hub), I will search further, anyway glad to know 4e is ok, but then why the -4 return value from the initial i2c_write?
ps thanx for reply hugo, you must spend a lot of time on these forums

I’ve been suing the HIH-6130 for months works great hears my code lifted from old dev wiki
`
function getsample() {

// Start conversion
i2c.write(0x4e, "");

// Wait at least 36ms
imp.sleep(0.05);

// Read out temperature and humidity, zero length subaddress means the write
// subaddr phase will be totally skipped and we'll just do the read
local th = i2c.read(0x4e, "", 4);

// note data will be marked as stale because each read kicks off another
// cycle as we always do a write-before-read on i2c
// hence, we need to mask top bits of humidity byte

// Form temp and humidity
local temperature = ((((th[2]         << 6 ) | (th[3] >> 2)) * 165) / 16383.0) - 40;
local humidity    = ((((th[0] & 0x3F) << 8 ) | (th[1]     ))        / 163.83 );

local tempOut=format("%.1f",temperature);
local humOut=format("%.1f",humidity);
// Post to cloud
local _httpOut=“vm|200.53;200.54|”+tempOut+";"+humOut+"|"+getDateTime()+"|Temp;RH|C;%|"+_rssi+"|"+hardware.voltage();
_wsOut.set(_httpOut);
}`

controlCloud - it looks like it was your code I used, so thanks,
I changed your code a bit to not use a sleep,
so do you get a -4 from the initial i2c_write ?

I don’t know as it just worked first time on 3 different imps are you using the Sparkfun breakout?

Yeh everything is working but the return code is strange, here -4

// Start conversion local e = i2c.write(address, ""); server.log(format("Result: %d", e ));

Yeh using the breakout with some fancy soldering, a la nora,

Just checking to see if the above code example (in principle) is still working. I’ve been using a different device and need to find one that operates at 3.3vdc. My problem manifested itself when I was trying to use i2c12 even though i2c89 worked just fine. Is there any evidence to support the above working with i2c12?

Fundamentally, even though the AM2315 works fine on i2c89, I need to find another device, since it may fail to work in the future after EI upgrades!

Thanks!

I suspect error -4 is a bug in our driver because you are sending no data. Looking at the AM2315 datasheet, the requirement for the device wakeup waveform is not standard I2C though - if the device wanted a delay between address and I2C stop it should be doing clock stretching… hence we don’t support it.

Not sure why you think the AM2315 may not work after our upgrades? Using an i2c level shifter would make the voltage levels compatible with the imp’s 3.3v I/Os.

The above code (slightly tweaked for my needs) works fine. I have also ordered an i2c compatible level shifter to retry the AM2315, due to its convenient packaging (albeit with incredibly light gauge wires).

As always, this forum is an incredible resource and I appreciate all the feedback!

FWIW…I ordered an “i2c compatible” level shifter from Adafruit and still have no luck with the AM2315 on ports 1& 2. I’ve tested the HIH-6130 from SparkFun and it works very well on both 1&2 as well as 8&9. I’m simply going to switch to it as my standard device when I need accurate humidity (as well as temperature). In addition, the wiring got a bit sloppy when I introduced the level shifter and the HIH-6130 is a lower cost solution (compared to the AM2315 and level shifter).

But…thanks for the feedback and suggestions!