This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to use SHT1x sensor?

I just figured it out it is not I2C protocol. I need to write all the command by Bit-Bang mode. How can I send/receive data with manual bit command to SHT1x sensor? Can anybody share any library for SHT1x sensor or Bit-Bang mode?? I am using nrf51822 with SDK11 (S130). THank you!

  • Hi,

    I've had to bit-bang an SHT75 and SHT1x interface on several processors including the nRF5x chips. Unfortunately that code belongs to my employer and I cannot share it. That said, it is a simple matter of controlling a couple of GPIO pins to do the work. The folks at Sensirion do provide example code for you though. Take a look here:

    SHT example code

    Good luck!

    John

  • Hi, Thank you for the answer. But my question is the DATA pin line should be set as output and input, right? is it possible to switch output and input during the command? something like as follows..

    //-----enter code here-----------------------------------------------------------------------------
    char s_write_byte(unsigned char value)
    //----------------------------------------------------------------------------------
    // writes a byte on the Sensibus and checks the acknowledge 
    { 
      unsigned char i,error=0;  
      for (i=0x80;i>0;i/=2)             //shift bit for masking
      { if (i & value) DATA=1;          //masking value with i , write to SENSI-BUS
        else DATA=0;                        
        _nop_();                        //observe setup time
        SCK=1;                          //clk for SENSI-BUS
        _nop_();_nop_();_nop_();        //pulswith approx. 5 us  	
        SCK=0;
        _nop_();                         //observe hold time
      }
      DATA=1;                           //release DATA-line
      _nop_();                          //observe setup time
      SCK=1;                            //clk #9 for ack 
      error=DATA;                       //check ack (DATA will be pulled down by SHT11)
      SCK=0;        
      return error;                     //error=1 in case of no acknowledge
    }
    

    this code to changed to

    //----------------------------------------------------------------------------------
    char s_write_byte(unsigned char value)
    //----------------------------------------------------------------------------------
    // writes a byte on the Sensibus and checks the acknowledge 
    { 
      unsigned char i,error=0;  
      for (i=0x80;i>0;i/=2)             //shift bit for masking
      { if (i & value) nrf_gpio_pin_set(SHT_DATA_pin);          //masking value with i , write to SENSI-BUS
        else nrf_gpio_pin_clear(SHT_DATA_pin);                        
        __NOP();                        //observe setup time
        nrf_gpio_pin_set(SHT_SCK_pin);                          //clk for SENSI-BUS
        __NOP();__NOP();__NOP();        //pulswith approx. 5 us  	
        nrf_gpio_pin_clear(SHT_SCK_pin);
        __NOP();                         //observe hold time
      }
      nrf_gpio_pin_set(SHT_DATA_pin);     
    	
    	//release DATA-line
      __NOP();                          //observe setup time
    	nrf_gpio_cfg_input(SHT_DATA_pin, NRF_GPIO_PIN_NOPULL);
       nrf_gpio_pin_set(SHT_SCK_pin);                            //clk #9 for ack 
    	
    	error = nrf_gpio_pin_read(SHT_DATA_pin);
       nrf_gpio_pin_clear(SHT_SCK_pin);        
       return error;                     //error=1 in case of no acknowledge
    }
    

    am I doing something wrong?? I got error all the time.

  • I don't see anything obvious. Have you tried to watch the clock and data lines with a scope during the transaction? That's usually the most useful debugging technique with these types of things.

    Also, because you are using the nrf_gpio API to control the pins, you likely do not need those __NOP(); calls. There is probably plenty of delay caused by the API calls to meet the timing spec of the SHT part. I'd get those out of there to improve your readability if nothing else.

  • I have nothing wrong with s_write_byte. But whenever try to read_byte, I got error (time out error) ack seem not returned.

  • I just figured out what was wrong. Here is my code for reading SHT1x sensor value with nrf51822.

    https://github.com/kimyd/SHT1X_on_nrf51822

Related