I have been working on a Netduino I2C driver for the Silicon Labs Si7005 Digital I2C Humidity & Temperature Sensor for weather station and building monitoring applications as it looks like a reasonably priced device which is not to complex to interface with.I’m using a SeeedStudio Grove – Temperature&Humidity Sensor (High-Accuracy & Mini) for development.
The first time I try and read anything from the device it fails. Otherwise my driver works as expected.
Bill of materials (prices as at April 2015)
This code just shows the flow, I’ll package into a driver shortly
I strobe the I2C line which seems to help
using (OutputPort i2cPort = new OutputPort(Pins.GPIO_PIN_SDA, true))
{
i2cPort.Write(false);
Thread.Sleep(1000);
}
I then try and read the Device ID (0x50) from register 0X11 but this (and any other read fails)
byte[] writeBuffer = { RegisterIdDeviceId };
byte[] readBuffer = new byte[1];
I2CDevice.I2CTransaction[] action = new I2CDevice.I2CTransaction[]
{
I2CDevice.CreateWriteTransaction(writeBuffer),
I2CDevice.CreateReadTransaction(readBuffer)
};
int length = device.Execute(action, TransactionTimeoutMilliseconds);
Debug.Print("Byte count " + length.ToString());
foreach (byte Byte in readBuffer)
{
Debug.Print(Byte.ToString("X2"));
}
I can read the temperature and humidity by writing to the command register
byte[] writeBuffer = { RegisterIdConiguration, CMD_MEASURE_TEMP };
I2CDevice.I2CTransaction[] action = new I2CDevice.I2CTransaction[]
{
I2CDevice.CreateWriteTransaction(writeBuffer),
};
int length = device.Execute(action, TransactionTimeoutMilliseconds);
Debug.Print("Byte count" + length.ToString());
Then poll for measurement process to finish
conversionInProgress = true
do
{
byte[] writeBuffer = { RegisterIdStatus };
byte[] readBuffer = new byte[1];
I2CDevice.I2CTransaction[] action = new I2CDevice.I2CTransaction[]
{
I2CDevice.CreateWriteTransaction(writeBuffer4),
I2CDevice.CreateReadTransaction(readBuffer4)
};
int length = device.Execute(action, TransactionTimeoutMilliseconds);
Debug.Print("Byte count " + length.ToString());
foreach (byte Byte in readBuffer)
{
Debug.Print(Byte.ToString());
}
if ((readBuffer[RegisterIdStatus] && STATUS_RDY_MASK) != STATUS_RDY_MASK)
{
conversionInProgress = false;
}
} while (conversionInProgress);
Then finally read and convert the value
byte[] writeBuffer = { REG_DATA_H };
byte[] readBuffer = new byte[2];
I2CDevice.I2CTransaction[] action = new I2CDevice.I2CTransaction[]
{
I2CDevice.CreateWriteTransaction(writeBuffer),
I2CDevice.CreateReadTransaction(readBuffer)
};
int length = device.Execute(action, TransactionTimeoutMilliseconds);
Debug.Print("Byte count " + length.ToString());
foreach (byte Byte in readBuffer)
{
Debug.Print(Byte.ToString());
}
int temp = readBuffer[0];
temp = temp << 8;
temp = temp + readBuffer[1];
temp = temp >> 2;
double temperature = (temp / 32.0) - 50.0;
Debug.Print(" Temp " + temperature.ToString("F1"));