Main Site Documentation

I2C to SHT15


#1

I have a Temp/Humidity sensor breakout board from Sparkfun. The sensor literature says the 2 wires act like IC2 but there is no device addressing so I have been using 0 as the address. When I send it a command I do not get any bytes returned and of course I can get no data back. I have wired Vcc to either the 3.3V out or the 5V in/out, the ground to ground, Data line to Di2, and the Clock line to Di3. I have downloaded the latest GHI SDK and updated the firmare. The display board works fine, but the sensor seems to be dead in the water. Here is my C# class:

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Hardware;
using System.Threading;

namespace USBizi_GraphicsSupport
{
    public class SHT15I2C
    {
 #region constants
        private const int clockRateKHz = 10;
        private byte[] REGISTER_Control = new byte[] {0x01}; //command to configure sensor
        private const byte REGISTER_Temperature = 0x00; //command to request result
        private const byte CONTROL_EnergyModeShutdown = 0x01;
        private const byte CONTROL_DataLengthTwelveBits = 0x60;
        private const byte CONTROL_OneShot = 0x80;
        //ms The maximum time needed by the sensor to convert 12 bits
        private const int conversionTime = 600;
        private const int transactionTimeout = 50; //ms

        //adr  command  r/w
        byte STATUS_REG_W = 0x06;   //000   0011    0
        byte STATUS_REG_R = 0x07;   //000   0011    1
        byte MEASURE_TEMP = 0x03;   //000   0001    1
        byte MEASURE_HUMI = 0x05;   //000   0010    1
        byte RESET = 0x1e;   //000   1111    0

        
        
        //Read Buffer
        byte[] inBuffer = new byte[3];
 #endregion
        I2CDevice.Configuration config = new I2CDevice.Configuration(100, clockRateKHz);

        
        private readonly byte address;
       
        //Init I2C Device & Read/Write Transactions
        I2CDevice tempSensor;
        I2CDevice.I2CWriteTransaction writeTrans;
        I2CDevice.I2CReadTransaction readTrans;

        
        public SHT15I2C()
        {
            Cpu.Pin sck;
            Cpu.Pin data;
            HardwareProvider.HwProvider.GetI2CPins(out sck, out data);

            Cpu.Pin msk;
            Cpu.Pin miso;
            Cpu.Pin mosi;
            HardwareProvider.HwProvider.GetSpiPins(SPI.SPI_module.SPI1,out msk,out miso,out mosi);

            tempSensor = new I2CDevice(config);
            byte[] reset = new byte[1] { RESET };
            writeTrans = tempSensor.CreateWriteTransaction(reset);
            readTrans = tempSensor.CreateReadTransaction(inBuffer);
            I2CDevice.I2CTransaction[] transactions = new I2CDevice.I2CTransaction[] { writeTrans, readTrans };
            int bytesSent = tempSensor.Execute(transactions, transactionTimeout);
        }        
        
       public void Dispose()
        {
            tempSensor.Dispose();
            config = null;
            writeTrans = null;
            readTrans = null;
        }
       public void GetTemperature()
       {
           //byte[] temperatureCommand = { 0, 0, 0, 0, 0, 0,1,1 };  // command used to read temperature 
           byte[] tempCommand = new byte[1] { MEASURE_TEMP };

           I2CDevice.I2CTransaction writeTempCommandTrans = tempSensor.CreateWriteTransaction(tempCommand);
           I2CDevice.I2CTransaction[] tempTransactions = new I2CDevice.I2CTransaction[] { writeTempCommandTrans, readTrans };
           int bytesSent = tempSensor.Execute(tempTransactions, transactionTimeout);
           
           //the conversion time is the maximum time
           //it takes the sensor to convert a physical reading to bits
           Thread.Sleep(transactionTimeout);
     
           byte[] regCommand = new byte[1] { STATUS_REG_W };
           I2CDevice.I2CTransaction writeResultsTrans = tempSensor.CreateWriteTransaction(regCommand);
           I2CDevice.I2CTransaction[] resultsTransactions = new I2CDevice.I2CTransaction[] { writeResultsTrans, readTrans };
           bytesSent = tempSensor.Execute(resultsTransactions, transactionTimeout);

           byte result1 = inBuffer[0];
           byte result2 = inBuffer[1];

           return; 
       }

     }

}

Any suggestions?


#2

Hi JC,

there is an implementation for a SHT11. I don’t know if it used the same protocol , but it seems so.

Maybe you can take a look

http://www.microframework.nl/projects/sensirion-sht11-class/


#3

You are a genius. I don’t know why I couldn’t use the I2C device provided by GHI. I added some additional lines to take the guesswork out:

            Cpu.Pin sck;
            Cpu.Pin data;
            HardwareProvider.HwProvider.GetI2CPins(out sck, out data);
            SHT11_GPIO_IOProvider SHT11_IO = new SHT11_GPIO_IOProvider(data, sck);

Thank you very much - I can finally move forward with this project.


#4

No problem , but I’m not the genius :smiley:

It is Elze Cool’s website , he’s the genius :wink:

But anyhow good luck for your project


#5

Check Http://www.fezzer.com :wink: