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

I2C interface with HTU21D using nrf51822

HTU21D.rar hello there i am interfacing HTU21D with nrf51822. i am reading temperature and humidty from HTU21D. humidty register is E5. and temperature register is E3. now my problem is that when i am trying to read only humidity it gives me correct value. when i am trying to read only temperature it gives me wrong value.when i am trying to read both temperature and humidity i am getting both wrong value.

I have uploaded code here. can you tell me what is problem here?? is there any problem in writing i2c driver??

Parents
  • Edit: Added a function that read the temperature.

    Is this question resolved? I have actually implemented this sensor (HTU21D) in one of my projects, so I should be able to help.

    First of all: I would recommend you to use the function nrf_delay_ms() to delay the microcontroller. To use this you must include "nrf_delay.h"

    You are writing to the registers 0xE3 and 0xE5. As stated in the datasheet of HTU21D this is "hold master" mode. When using a hold master mode the argument xfer_pending must be set to 'true': nrf_drv_twi_tx(p_twi_master,slave_addr, &reg_addr,1,true);

    I have rewritten your function for reading the humidity. I cannot test it right now, but I believe it should work.

    void HTU21D_Humidity1(int *r) {
    ret_code_t ret_code;
    uint8_t htu21d_address = 0x40; //7 bit address of HTU21D
    uint8_t trigger_humidiy_no_hold_master = 0xF5; //Address to humidity reading with no hold of twi lines
    uint8_t returned_over_I2C[3] = {0,0,0}; //Array to hold data
    
    ret_code = nrf_drv_twi_tx(p_twi_master, htu21d_address, &trigger_humidiy_no_hold_master, 1, true);//Writing 0xE5 to HTU21D tell the sensor to start humidity readings and hold the master
    nrf_delay_ms(50);//Delay 50 ms, which is the maximum measurement time stated in datasheet
    ret_code = nrf_drv_twi_rx(p_twi_master, htu21d_address, returned_over_I2C, 3, false); //Get raw humidity data
    nrf_delay_ms(5);
    //Calculate humidity
    uint16_t rawHumidity = ((unsigned int) returned_over_I2C[0] << 8) | (unsigned int) returned_over_I2C[1];
    float tempRH = rawHumidity / (float)65536; //2^16 = 65536
    float rh = -6 + (125 * tempRH); //From page 14 in datasheet
    *r = (int)(rh); // this could be change to (int)(rh*10), then one decimal of the humidity data will be kept (but 34.5  = 345 ) 
    }
    

    Function that read the temperature:

    void HTU21D_Temperature1(float *r){
    ret_code_t ret_code;
    uint8_t htu21d_address = 0x40; //7 bit address of HTU21D
    uint8_t trigger_temperature_no_hold_master = 0xE3; //Address to temperature reading with no hold of twi lines
    uint8_t returned_over_I2C[3] = {0,0,0}; //Array to hold data
    
    ret_code = nrf_drv_twi_tx(&p_twi_instance, htu21d_address, &trigger_temperature_no_hold_master, 1, true);//Writing 0xE3 to HTU21D tell the sensor to start temperature readings and hold the master
    nrf_delay_ms(50);//Delay 50 ms, which is the maximum measurement time stated in datasheet
    ret_code = nrf_drv_twi_rx(&p_twi_instance, htu21d_address, returned_over_I2C, 3, false); //Get raw temperature data
    nrf_delay_ms(5);
    uint16_t x = (returned_over_I2C[0]<<8) | returned_over_I2C[1];
    float tempRH = x /(float) 65536; //2^16 = 65536
    float rh = -46.85 + (175.72 * tempRH);
    *r=rh;
    }
    
  • I think for reading temperature with no hold the correct address is 0xF3 instead of 0xE3:

    uint8_t trigger_temperature_no_hold_master = 0xF3; //Address to temperature reading with no hold of twi lines

    instead of:

    uint8_t trigger_temperature_no_hold_master = 0xE3; //Address to temperature reading with no hold of twi lines 
Reply Children
No Data
Related