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.

Related