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

HTU21D no response over I2C

Hi,

I am using the example code supplied by the blog for reading humidity from the HTU21D sensor using TWI. My humidity and temperature readings are constant and never change. They appear as:

Humidity: -6.0 Temperature: -46.85

Based on the code, these values would indicate that no signal output is being received over I2C.

I have tried to run the TWI_scanner example but no device was found on the I2C bus.

No pull-up resistors used here (didn't work with pull-up resistors either), my wiring: Vin - Vdd 3.3 - Vdd GND - GND SCL - P0.27 SDA - P0.26

I am confident soldering and wiring is not the issue, does anyone have any advice on using this issue?

Thank you!

Update: Here is an image of my circuit.

The red jumper is Vdd (~3.3V) and it is attached to the Vin pin on the HTU21D.

The 3V3 pin of the HTU21D was left with no connection (not necessary I believe).

The black jumper is ground, attached to ground on the HTU21D.

The yellow jumper is SDA, running from the SDA pin on the HTU21D to pin 27 on the nRF52.

The white jumper is SCL, running from the SCL pin on the HTU21D to pin 26 on the nRF52.

Both the SDA and SCL lines have a 4.7 kOhm resistor in parallel, connected to Vdd via the red jumper branch.

Update 2: When debugging in Keil, a realistic value prints for Temperature and Humidity now!

Humidity: 30.1 Temperature: 25.0

However, when I am not in debugging mode in Keil, only those garbage values from above print. It was only after I stepped through the functions in debug mode that I got realistic values. What does this mean?

Here is the premise of my code for reading temperature:

Start temperature reading (no hold master) by writing 0xF3 to HTU21D I2C address 0x40:

ret_code_t start_temperature_no_hold_master() {
      ret_code_t ret_code;
      uint8_t command_address = TEMPERATURE_NO_HOLD_MASTER_ADDRESS; 
      ret_code = nrf_drv_twi_tx(&m_twi_master, HTU21D_ADDRESS, &command_address, 1, false);
      return ret_code;
}

Fetch temperature:

 ret_code_t fetch_temperature_no_hold_master(int *temperature){
    ret_code_t ret_code;
    uint8_t returned_over_I2C[3]; //Array to hold returned data
    ret_code = nrf_drv_twi_rx(&m_twi_master, HTU21D_ADDRESS, returned_over_I2C, 3); 
    uint16_t rawTemperature = ((unsigned int) returned_over_I2C[0] << 8) | (unsigned int)  returned_over_I2C[1];
    float tempTemperature =  (float)rawTemperature / (float)65536; //2^16 = 65536
    float rh = (float)-46.85 + ((float)175.72 *(float)tempTemperature); 
    *temperature = (int)(rh*10); 
    return ret_code;
}

My main function:

int main(void)
{
ret_code_t err_code;  

int temperature;//Integer used to hold temperature
	
/* Initializing TWI master  */
err_code = twi_master_init();
APP_ERROR_CHECK(err_code);

    if (true)
    {        
    int i = 1;
    if (i < 5)
       {

          start_temperature_no_hold_master(); //Start temperature reading
              fetch_temperature_no_hold_master(&temperature);
              SEGGER_RTT_printf(0, "Temperature: ");
              print_as_float(temperature,1); 
           }
     }
}

Humidity functions are the same as temperature and are not shown.

Any help is greatly appreciated!

Update 3:

My code is not delaying properly when using nrf_delay_ms(), and this may be causing my sensor to be unable to respond to my TWI commands properly.

I have include my nrf_delay.h file for inspection.

  • Hi Hung, I have added long delays in my code, for example nrf_delay_ms(10000) in between reading and writing to the register and printing to the screen in Segger RTT Viewer. When I program the nRF52 and view its output in RTT Viewer, the temperature is printed very rapidly as if the delays are not happening. I wanted to print a value every 10 seconds, but instead the program prints every 1 second or faster. If I could get the code to execute slower then I believe it would work. Do you know how I can make the delays execute? Thank you

  • Hi,

    You may want to check if the nrf_delay_ms() delay actually works. Try to test with the blinky example. Simply toggle an LED and check if it blink the correct interval.

  • For my blinky example in SDK12.2, LD5 blinks every 1 second when using nrf_delay_ms(1000). However, using nrf_delay_ms(5000) makes LD5 blink very rapidly. Does nrf_delay_ms() have a maximum delay limit?

    Also, when I was stepping through my code for the sensor, I noticed that the nrf_delay_ms() commands will go through the same number of NOP lines no matter the time value specified. Do you think this is the problem? If so, is there something wrong with my nrf_delay.h file? I have included my nrf_delay.h file above if you'd be willing to take a look.

  • Please try to test with our blinky example in \examples\peripheral\blinky\pca10040
    Please try to change the nrf_delay_ms() to delay for 5000ms to test. You should see the LED change every 5 seconds.

    The maximum delay time for that function should be up to 71 minute as it use uint32_t for the delay time in us.

  • That is the blinky example I used, which I described above. Using nrf_delay_ms(5000) makes LD5 blink very rapidly. It's good to know there's no maximum delay, that must mean something is going wrong in nrf_delay.h...

Related