I2C sensor interfacing with NetDuino3 WiFi

Hi everyone,
I am trying to interface TH02 (I2C Temperature and Humidity) Sensor with Netduino3 WiFi.

Link to the Grove I2C Temperature and Humidity Sensor

The Arduino code for this sensor to read temperature and humidity works fine!

Working Code Steps for Arduino:-

  1. Set TH02 I2C address as 0x40(64)
  2. Start I2C Transmission
  3. Select configuration register 0x03
  4. Select Normal mode enabled, Temperature register 0x11
  5. Stop I2C transmission
  6. Start I2C Transmission
  7. Select data register 0x00
  8. Stop I2C transmission
  9. Request 3 bytes of temperature data

Similar things is to be done again to read Humidity sensor but only change is (4th line) i.e. Normal mode enabled, Humidity register is 0x01

Now i am trying to do the same thing using NetDuino but i am getting null data. Hey even changed the i2c clock to 100 and 400 KHz but the result is same and have checked the wiring multiple times!

Netduino code:-

I2CDevice th02Sensor = new I2CDevice(new I2CDevice.Configuration(0x40, 50));

I2CDevice.I2CTransaction[] writeConfig = new I2CDevice.I2CTransaction[2];
// Select configuration register 0x03
byte[] configurationRegister1 = new byte[1] { 0x03 };
writeConfig[0] = I2CDevice.CreateWriteTransaction(configurationRegister1);
// Normal mode enabled, Temperature 0x11
byte[] configurationRegister2 = new byte[1] { 0x11 };
writeConfig[1] = I2CDevice.CreateWriteTransaction(configurationRegister2);
int written = th02Sensor.Execute(writeConfig, TransactionTimeout);
Thread.Sleep(1000);

I2CDevice.I2CTransaction[] reading = new I2CDevice.I2CTransaction[2];
// Select data register
byte[] dataRegister = { 0x00 };
reading[0] = I2CDevice.CreateWriteTransaction(dataRegister);
byte[] temperatureData = new byte[3];
reading[1] = I2CDevice.CreateReadTransaction(temperatureData);

// Read the temperature.
int bytesRead = th02Sensor.Execute(reading, TransactionTimeout);
Debug.Print("Bytes of Data Read:- " + bytesRead);
Debug.Print("Temperature bytes data: " + ByteToHex(temperatureData[0]) + ", " + ByteToHex(temperatureData[1]) + ", " + ByteToHex(temperatureData[2]));

The Working Arduino Code is as follows for this TH02 Grove Sensor (Grove - Temperature&Humidity Sensor (High-Accuracy &Mini):-

#include < Wire.h >

//TH02 I2C address is 0x40(64)
#define Addr 0x40

double cTemp = 0.0, humidity = 0.0;

void setup()
{
// Initialise I2C communication as MASTER
Wire.begin();

// Initialise serial communication, set baud rate = 9600
Serial.begin(9600);

// Start I2C Transmission
Wire.beginTransmission(Addr);

// Stop I2C transmission
Wire.endTransmission();
delay(300);
}

void loop()
{
unsigned int data[3];

// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select configuration register
Wire.write(0x03);
// Normal mode enabled, Temperature
Wire.write(0x11);
// Stop I2C transmission
Wire.endTransmission();
delay(500);

// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data register
Wire.write(0x00);
// Stop I2C transmission
Wire.endTransmission();

// Request 3 bytes of data
Wire.requestFrom(Addr, 3);

// Read 3 bytes of data
// Status, temp msb, temp lsb
if(Wire.available() == 3)
{
data[0] = Wire.read();
data[1] = Wire.read();
data[2] = Wire.read();
}

// Convert the data to 14-bits
float temp = ((data[1] * 256.0) + (data[2] & 0xFC)) / 4;
float cTemp = (temp / 32.0) - 50.0;
float fTemp = cTemp * 1.8 + 32 ;

// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select configuration register
Wire.write(0x03);
// Normal mode enabled, Relative Humidity
Wire.write(0x01);
// Stop I2C transmission
Wire.endTransmission();
delay(500);

// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data register
Wire.write(0x00);
// Stop I2C transmission
Wire.endTransmission();

// Request 3 bytes of data
Wire.requestFrom(Addr, 3);

// Read 3 bytes of data
// Status, humidity msb, humidity lsb
if(Wire.available() == 3)
{
data[0] = Wire.read();
data[1] = Wire.read();
data[2] = Wire.read();
}

// Convert the data to 12-bits
float hum = ((((data[1] * 256.0) + (data[2] & 0xF0)) / 16) / 16 ) - 24;
float humidity = (hum * 1.0 - (((hum * 1.0 * hum * 1.0) * (-0.00393)) + (hum * 0.4008) - 4.7844));
humidity = humidity + ((cTemp - 30) * ((humidity * 0.00237) + 0.1937));

Serial.print("Temperature:- ");
Serial.println(cTemp);
Serial.print("Humidity:- ");
Serial.println(humidity);

delay(2000);
}

First thing I would do is to consider changing the clock speed to 10 KHz (assuming that the sensor will accept this).

Do you have a logic analyser to check the data transmission?

Do you have readable form of the schematic? I cannot read the schematic files on the web site.

Have to put pull-up resistors in place? Would also need to check the schematic to see if there are any in place.

Regards,
Mark

The schematic of the TH02 Temperature and Humidity Grove module:

Thanks a lot for the reply!
I just wanted to ask it there any code to check whether an I2C device is connected to the Netduino board!
I mean code that scans the I2C-bus for devices. If a device is found, it is reported to the serial monitor.
Like this one:-
I2C Address Scanner Code for Arduino

There is an I2CScanner sample in the Netduino.Foundation library.

Regards,
Mark

There is an error while using this (using Netduino.Foundation.Communications) library:-

You will need to install the library. It is available as a nuget package.

Regards,
Mark

Hi

I wrote this library and use it on N2 and N3 devices. It’s very lightweight and has minimal dependencies (if you want that approach)

@KiwiBryn
blog.devmobikle.co.nz