Shop OBEX P1 Docs P2 Docs Learn Events
Problems while converting SHT11 sample code to Java — Parallax Forums

Problems while converting SHT11 sample code to Java

Ahmed BAhmed B Posts: 5
edited 2008-11-08 08:08 in General Discussion
Greetings,
·
I recently bought a SHT11 Precision Temperature and Relative Humidity sensor, advertised to work with the Javelin stamp. Sample code was provided for me, but unfortunately it was in the subset of PBASIC for the BS2 stamp. I was able to convert most of the code to Java, but I ran into problems when I came to the functions for measuring temperature and relative humidity. Below is the function from the example code that came with the sensor, it can also be found at: http://www.parallax.com/detail.asp?product_id=28018
·
SHT_Measure_Humidity:
· GOSUB SHT_Start
· ioByte = ShtHumi
· GOSUB SHT_Write_Byte
· GOSUB SHT_Wait
· ackBit = Ack
· GOSUB SHT_Read_Byte
· soRH.HighByte = ioByte
· ackBit = NoAck
· GOSUB SHT_Read_Byte
· soRH.LowByte = ioByte
·
· rhLin = (soRH ** 26542)
· rhLin = rhLin - ((soRH ** 3468) * (soRH ** 3468) + 50 / 100)
· rhLin = rhLin - 40
· rhTrue = ((tC / 10 - 25) * (soRH ** 524 + 1) + (rhLin * 10)) + 5 / 10
RETURN
·
The main problem for me at this point is that in the original code, soRH was a “Word” and you could use “HighByte” to change the MSB to be equal to ioByte in order for SHT_Read_Byte to be able to use. However, I know of no way in the Javelin subset of Java to accomplish an equivalent operation.
·
I was also concerned if the method I used to create pulses would damage the pins, below is an example of the original code and my java equivalent:
·
(This function wants you to create a “start” sequence by having “the data line being pulled low during the middle of one pulse and released in the middle of the next&#8221[noparse];)[/noparse]
·
SHT_Start:
· INPUT ShtData
· LOW Clock
· HIGH Clock
· LOW ShtData
· LOW Clock
· HIGH Clock
· INPUT ShtData
· LOW Clock
RETURN
·
· public void SHT_Start() {
··· 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);
· }
·
Does anyone have any suggestions? I have been trying to figure out how to convert this code off and on for the past few weeks, and although many resources like Jon Williams’ article and Sensirion’s data sheet have been very useful in helping me understand how to communicate with the sensor, I keep having trouble with Measure_Humidity function.
·
Thanks

Comments

  • Paul RingPaul Ring Posts: 16
    edited 2005-12-19 22:11
    Hi!
    I saw you had some questions about using the SHT11 sensor together with Javelin. I don't know if you solved the problem or not, but either way I'm attaching my solution - maybe that will help you in some way. But if you solved the communication problems, I have a question: When you're reading the temperature (converted to C) - does the SHT11 sensor produce stable values? When I'm reading the temperature, for every reading I get a different value (I can assure you the temperature is not fluctuating where the sensor is located), one reading can produce a value of 25.2C and another one 26.5C - but most of the time 25.2. Are you experiencing the same problems?
    I'm also using a floating point processor (uM-FPU) which is doing the actual temperature conversions.
    Please let me know if you know anything about this.

    Regards
    /Paul
  • Paul RingPaul Ring Posts: 16
    edited 2005-12-21 21:41
    Puhh...I had to dig into this to find out what the problem was, and I finally managed to solve the readings problem. Unfortunately the BS2 example code posted on the sensor page is not working on the Javelin and if it’s working (and that by pure luck!) it produces unstable readings. Given the sensor has a temperature accuracy of +/- 0.5 °C @ 25 °C it seemed strange the readings were oscillating between 2 values were the differences in temperature were > 1.5 degrees. I mean...if you get 23.2 °C one time and 25.6 °C another time you start wondering.

    Looking at the data sheet for the SHT11 found here www.parallax.com/dl/docs/prod/datast/shtx.pdf I realized the problem was the timing diagrams for the clock and data lines– the number of bits sent/received did not matched the specifications! The question one should ask is WHY Parallax is posting BS2 code only and no Javelin code?
    I mean since they’re advertising the SHT11 sensor is working with the Javelin too they should post a working example too!
    To make a long history short – I attach a working version of the code. If you want to use it you might also need a floating point co-processor.

    /Paul
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-12-22 15:22
    Hi Paul,

    Thanks for your· test class.
    I took the liberty to split it up into a sht11 class and test program.
    I have replaced the floating point math by integer fraction math
    as it is done on the basic stamp. This·is good for temperature and
    humidity values displayed as xx.x values.

    http://groups.yahoo.com/group/JavelinCode/files/Javelin%20Stamp%20IDE/lib/stamp/peripheral/sensor/humidity/sht11/

    The package requires the UnsignedIntMath class here
    http://groups.yahoo.com/group/JavelinCode/files/Javelin%20Stamp%20IDE/lib/stamp/math/

    I have no sht11 sensor, so if you would like to test it, please do.
    Let us us know the results.

    regards peter
  • Paul RingPaul Ring Posts: 16
    edited 2005-12-22 18:19
    Hello Peter,

    Very nice job! Everything is working just fine! Please see the output below.
    Downloading the program to Javelin takes less time - compared to downloading my code - and the memory usage is much less too! Your code consumes ~5000 bytes, while mine consumed around ~9000 bytes. Nice memory usage optimization!

    Temp (celsius) = 23.1
    Rel. Humidity (%) = 30.5
    Compensated Rel. Humidity (%) = 30.3

    It's working great!
    You really made this code look like state of the art code. Thinking of the code Parallax releases officially, the code you posted has a much higher quality! I hope the people at Parallax appreciates the nice job you do.

    Regards
    /Paul

    Post Edited (Paul Edfeldt) : 12/22/2005 6:25:42 PM GMT
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-12-22 18:54
    Thanks Paul, but you did the tougher part, to get the communication right.
    The big advantage is that there now is a class, and the multiply unsigned integer
    with unsigned fraction (method umulf() in class UnsignedIntMath) really is
    invaluable. With that you can replace most floating point operations.
    Just see how the ** operator on the basic stamp is used to get
    high accurate and high resolution results, just by reorganizing formula's.

    regards peter
  • kapilkapil Posts: 1
    edited 2006-02-07 19:09
    HI everyone,

    My self Kapil from India. I am doing my B. Tech Project in field of sensor network. Basically, we are building a sensor node for microclimate monitoring. We came across the SHT71. Though they have written that it is not I2C compatible but we found it is I2C compatible. Please tell me one thing what is the address of this sensor. First 3 bits are fixed 000 but what about rest 4 bits???
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-02-07 20:54
    check this out:
    http://www.phanderson.com/picaxe/sht71.html

    The protocol is NOT i2c compatible!
    datasheet:
    http://www.driesen-kern.de/downloads/dbsht1x7xenglishdk0704.pdf

    regards peter
    ·
  • BobValBobVal Posts: 48
    edited 2008-11-08 02:58
    Peter:



    I tried the SHT11.java code using a SHT7x (same as the SHT1x according the whitesheet) and all I get as output is:



    Temp (celsius) = -40.0
    Rel. Humidity (%) = -4.0
    Compensated Rel. Humidity (%) = -4.-6

    The SHT7x pin outs are described at the bottom.·

    I am confused as to where to begin to locate the problem.

    Kept trying to upload the Specs but Attachment Manager kept crashing here is a link to it, if you can't get there let me know and I will upload it to another site I have and paste a link to it.· http://www.a-wit.com/web_c-stamp/forums/index.php?act=attach&type=post&id=156

    Reading the specs it appears to be exactly what you programmed, yet I never get anything good.

    Bob
  • BobValBobVal Posts: 48
    edited 2008-11-08 05:36
    Well it turns out the the SHT7X needs the two resistors on the clock needs pull down and on data needs the pull up.

    Thank goodness Peters code says something about these (I guess the SHT1X has them).

    Peter you also show a 330 ?? on the data? not sure what this is?
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-11-08 08:08
    Hi Bob,
    The 330 ohm protects both javelin and SHT datapin in case both pins are outputs
    but with opposit levels. The value is not that critical but should stay below
    (0.8Volt/5Volt)*4.7k = 0.75k = 750 ohm to guarantee a low output of 0.8V max.

    regards peter
Sign In or Register to comment.