Shop OBEX P1 Docs P2 Docs Learn Events
Sensirion SHT11 Basic -> Java — Parallax Forums

Sensirion SHT11 Basic -> Java

jmspaggijmspaggi Posts: 629
edited 2008-05-20 15:47 in General Discussion
Hi,

I have now plug the SHT11 in the board, but I got some trouble to make it works. I have put the 4,7K resistor, and wire all the pin.

I tried to translate the BASIC code in the documentation in Java code, but since I don't now BASIC, not sure if it's write.

So here is my code:
        final static int ShtData = CPU.pin14;
        final static int Clock = CPU.pin11;
        
        final static int ShtTemp = 3;
        final static int ShtHumi = 5;
        final static int ShtStatW = 6;
        final static int ShtStatR = 7;
        final static int ShtReset = 30;
        
        final static int Ack = 0;
        final static int NoAck = 1;
        
        final static int DegSym = 186;
                        

        static int ioByte;
        static int ackBit;
        
        public static void SHT_Start ()
        {
            // SHT_START begin
            CPU.setInput(ShtData);
            CPU.writePin(Clock, false);
            CPU.writePin(Clock, true);
            CPU.writePin(ShtData, false);
            CPU.writePin(Clock, false);
            CPU.writePin(Clock, true);
            CPU.setInput(ShtData);
            CPU.writePin(Clock, false);
            // SHT_START end
        }
        
        public static void SHT_Write_Byte ()
        {
            // SH_WRITE_BYTE begin
            CPU.shiftOut(ShtData, Clock, 8, CPU.POST_CLOCK_MSB, ioByte);
            ackBit = CPU.shiftIn(ShtData, Clock, 1, CPU.PRE_CLOCK_LSB);
            System.out.println ("ackBit = " + (ackBit & 1));
            // SH_WRITE_BYTE end
        }
        
        public static void SHT_Wait ()
        {
            // SHT_WAIT begin
            System.out.print ("SHT_Wait ");
            CPU.setInput(ShtData);
            boolean timeOut = true;
            for (int toDelay = 0; toDelay < 250; toDelay++)
            {
                if (!CPU.readPin(ShtData))
                {
                    timeOut = false;
                    break;
                }
            }
            System.out.print  ("Timeout? ");
            System.out.println(timeOut);
            // SHT_WAIT end
        }
        
        public static void SHT_Measure_Temp ()
        {
            SHT_Start ();
            ioByte = ShtTemp;
            SHT_Write_Byte();
            SHT_Wait();
        }
        
        public static void testSHT11 ()
        {
            SHT_Measure_Temp();
        }





I have only implement the first steps, but seems that it's not working because in SHT_Wait, I'm getting a timeout. so before coding everything, I would like to make this piece work. My concern is that I can't figure if the issue is on the board, or on the code. Do you have any clue where my issue can be? What can I do to troubleshot it?

Thanks,

Jean-Marc

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-05-19 16:15
    Here is a SHT11 class and test program.
    Download these into folder ...\lib\stamp\peripheral\sensor\humidity\sht11
    (create that folder if it does not exist).
    It requires the UnsignedIntMath class that goes into folder ...\lib\stamp\math

    regards peter
  • jmspaggijmspaggi Posts: 629
    edited 2008-05-19 17:16
    Thanks Peter.

    Seems that it's working because I'm receiving data, but I'm receiving 25 (celsius(raw)). That's little high. The temperature in the room is currenly 20.8, which is 4.2 celcius from the temperature received from the SHT11. Can this difference come from the internal heater of the sensor? How can I verify if it's on, and switch it off if needed? Can I use the writeByte methode to send the read or write status command, the use the readbyte one? Or should I do something else?

    Also is there a way to calibrate it if the temperature is to far from the real one?

    Thanks,

    Regards,

    Jean-Marc
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-05-19 17:50
    I added 4 functions: heaterOn(), heaterOff(), writeStatus() and readStatus()
    to the sht11 class.
    See if these work. I converted the code from the pbasic code in the sht11 parallax document.

    regards peter
  • jmspaggijmspaggi Posts: 629
    edited 2008-05-19 18:32
    Hi Peter,

    It works very well, thanks.

    I have also identify that my temperature gap come from the regulator on the board which is giving some heat to the sensor.

    However, I will recommand this small modification on the code:

      public void heaterOn() {
        status = readStatus() | 0x04; //%00000100 heater bit = On
        writeStatus();
      }
    
      public void heaterOff() {
        status = readStatus() & 0xF6; //%00000000 heater bit = Off
        writeStatus();
      }
    
    
    




    This allow to not overwrite other settings, but for an unknow reason, this piece of code doesn't work well. Maybe because of the readStatus methode.

    Regards,

    Jean-Marc
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-05-19 18:41
    I wondered if the readStatus() has an error. As it uses the shtStatW command
    instead of the shtStatR command.
    The following adapted readStatus() utilizes the shtStatR command, and according
    to the sht11 datasheet, that command returns a statusbyte followed by a checksum byte.

    · //read status
    · public int readStatus() {
    ··· transmissionStart(); //alert device
    ··· writeByte(shtStatR); //send command
    ··· status = readByte(true); //the status
    ··· readByte(false); //the checksum, ignored
    ··· return status;
    · }

    I like your idea of preserving statusbits. See if this readStatus()
    works better.

    regards peter
  • jmspaggijmspaggi Posts: 629
    edited 2008-05-20 03:09
    Hi Peter,

    Works fine! Thanks.

    I found on error on heaterOff. Should be 0xFB instead of 0XF6.

    Regards,

    Jean-Marc
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-05-20 07:08
    Thanks Jean-Marc.
    Attached is an updated SHT11 class. I made readStatus() and writeStatus() private,
    but added reloadEnable(), reloadDisable(), lowResolution() and highResolution() functions
    that are·set using the statusregister.
    I also added javadocs to all the functions.

    regards peter

    Post Edited (Peter Verkaik) : 5/20/2008 7:22:09 AM GMT
  • jmspaggijmspaggi Posts: 629
    edited 2008-05-20 15:28
    Hi Peter,

    You should update the readStatus() method since it's writing shtStatW instead of shtStatR.

    Everything else is working fine.

    Thanks,

    Jean-Marc
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-05-20 15:47
    Thanks for catching that.
    Attached is the corrected version.

    regards peter
Sign In or Register to comment.