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

Sample code integrated with HDC2010

Hello,

I am using nRF52832, SDK_15.3.0, S132 SoftDevice and Segger for flashing the image.

Is there any sample code integrated with HDC2010 Temperature & Humidity sensor over TWI for reference.

I want to integrate into ble_app_blinky.

Thanks & Regards

Vishnu Beema

  • I apologise for that. I completely forgot to set the SCL and SDA pins to the ones i am using. After setting that the accelerometer is discovered on the correct address but still no HDC2010.

  • Hello,

    By using TWI scanner example I am able to find the address of HDC2010 as 0x40. Attached are the files I implemented and its working fine. I took reference from TI code and there are some other sample exampled code. I modified to Noridc and working fine.

    /**
     * @brief All HDC2010 Temperature & Humidity Sensor related operations are taken care here
     *
     *	Author : Vishnu Beema
     */
    
    #include "sdk_config.h"
    
    #include <stdint.h>
    #include <string.h>
    #include "nordic_common.h"
    #include "nrf.h"
    #include "app_error.h"
    #include "ble.h"
    #include "ble_err.h"
    #include "ble_hci.h"
    #include "ble_srv_common.h"
    #include "ble_advdata.h"
    #include "ble_conn_params.h"
    #include "nrf_sdh.h"
    #include "nrf_sdh_ble.h"
    #include "boards.h"
    #include "app_timer.h"
    #include "app_button.h"
    #include "ble_dtlgs.h"
    #include "nrf_ble_gatt.h"
    #include "nrf_ble_qwr.h"
    #include "nrf_pwr_mgmt.h"
    
    #include "nrf_drv_timer.h"
    #include "nrf_drv_rtc.h"
    #include "nrf_drv_gpiote.h"
    #include "nrf_drv_twi.h"
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    
    #include "hdc2010_operations.h"
    
    #define CONFIGURATION_FIELD_DESCRIPTIONS_REGISTER_ADDRESS   (0x0E)
    #define AUTO_MEASUREMENT_DIABLE                             (0x00)
    
    #define MEASUREMENT_CONFIGURATION_REGISTER_ADDRESS          (0x0F)
    
    #if 1
    // 14-bit resolution
    #define MEASUREMENT_DEFAULT_CONFIGURATION                   (0x00)
    #define MEASUREMENT_TRIGGER                                 (0x01)
    #else
    // 8-bit resolution
    #define MEASUREMENT_DEFAULT_CONFIGURATION                   (0xA0)
    #define MEASUREMENT_TRIGGER                                 (0xA1)
    #endif
    
    #define TEMPERATUR_LSB_WRITE_POINTER_ADDRESS                (0x00)
    
    /* Common addresses definition for temperature sensor. */
    #define HDC2010_ADDR                                        (0x40U)
    
    
    /* TWI instance. */
    const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
    uint8_t u8SlaveAddress = 0;
    
    /* Indicates if operation on TWI has ended. */
    volatile bool m_xfer_done = false;
    
    /* Buffer for samples read from temperature sensor. */
    static uint8_t u8ReadTempHumid[4] = {0};
    
    
    /**
     * @brief Function for handling data from temperature sensor.
     *
     * @param[in] temp          Temperature in Celsius degrees read from sensor.
     */
    void hdc2010_data_handler(void)
    {
        uint32_t u32TempData  = 0;
        uint32_t u32HumidData = 0;
        uint8_t  u8Humidity = 0;
    	int16_t i16Temperature = 0;
    	
        memcpy(&u32TempData,  &u8ReadTempHumid[0], 2);
        memcpy(&u32HumidData, &u8ReadTempHumid[2], 2);
        
        i16Temperature = (int16_t) ( (u32TempData * 1650 / 65536) - 400); // Multiplied by 10
        u8Humidity     = (uint8_t) (u32HumidData * 100 / 65536);
        
        //NRF_LOG_INFO("Temp: %d %d Hum %d", u32TempData, strFlashDataRecord.i16Temperature, strFlashDataRecord.u8Humidity);
        NRF_LOG_INFO("Temp: %d Hum %d", i16Temperature, u8Humidity);
    }
    
    /**
     * @brief TWI events handler.
     */
    static void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
    {
        switch(p_event->type)
        {
            case NRF_DRV_TWI_EVT_DONE:
            {
                if(p_event->xfer_desc.type == NRF_DRV_TWI_XFER_RX)
                {
                    if(u8SlaveAddress == HDC2010_ADDR)
                    {
                        hdc2010_data_handler();
                    }
                }
                
                m_xfer_done = true;
                
                // Reset the Slave address
                u8SlaveAddress = 0;
            }
            break;
            
            default:
                NRF_LOG_INFO("twi_default %d", p_event->type);
                // NRF_DRV_TWI_EVT_ADDRESS_NACK when wrong address is sent
                break;
        }
    }
    
    /**
     * @brief TWI initialization.
     */
    static void twi_init (void)
    {
        ret_code_t err_code;
        
        const nrf_drv_twi_config_t twi_hdc2010_config = {
           .scl                = HDC2010_SENSOR_TWI_SCL_PIN,
           .sda                = HDC2010_SENSOR_TWI_SDA_PIN,
           .frequency          = NRF_DRV_TWI_FREQ_100K,       // Configured to 100k
           .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
           .clear_bus_init     = false
        };
        
        err_code = nrf_drv_twi_init(&m_twi, &twi_hdc2010_config, twi_handler, NULL); // Non-Blocking mode
        //err_code = nrf_drv_twi_init(&m_twi, &twi_hdc2010_config, NULL, NULL); // Blocking mode
        APP_ERROR_CHECK(err_code);
    }
    
    /**
     * @brief Function for reading data from temperature sensor.
     */
    void hdc2010_StartMeasurement_sensor_data(void)
    {
        ret_code_t err_code;
        
        // Trigger 'Measurement_Trigger'
        uint8_t reg[2] = {MEASUREMENT_CONFIGURATION_REGISTER_ADDRESS, MEASUREMENT_TRIGGER};
        
        // Enable TWI
        nrf_drv_twi_enable(&m_twi);
        
        m_xfer_done    = false;
        u8SlaveAddress = HDC2010_ADDR;
        err_code = nrf_drv_twi_tx(&m_twi, HDC2010_ADDR, &reg[0], sizeof(reg), false);
        APP_ERROR_CHECK(err_code);
        while(m_xfer_done == false);
        
        #if 0 // Test code to read all Registers
        // Change the 'Read pointer'
        reg[0] = TEMPERATUR_LSB_WRITE_POINTER_ADDRESS;
        m_xfer_done    = false;
        u8SlaveAddress = HDC2010_ADDR;
        err_code = nrf_drv_twi_tx(&m_twi, HDC2010_ADDR, &reg[0], 1, false);
        APP_ERROR_CHECK(err_code);
        while(m_xfer_done == false);
        
        // Read all 20 bytes
        m_xfer_done    = false;
        u8SlaveAddress = HDC2010_ADDR;
        err_code = nrf_drv_twi_rx(&m_twi, HDC2010_ADDR, &u8ReadTempHumid[0], sizeof(u8ReadTempHumid)); // Make sure buffer size is 20
        APP_ERROR_CHECK(err_code);
        while(m_xfer_done == false);
        
        // Change the 'Read pointer' to "Temperature LSB"
        reg[0] = TEMPERATUR_LSB_WRITE_POINTER_ADDRESS;
        m_xfer_done    = false;
        u8SlaveAddress = HDC2010_ADDR;
        err_code = nrf_drv_twi_tx(&m_twi, HDC2010_ADDR, &reg[0], 1, false);
        APP_ERROR_CHECK(err_code);
        while(m_xfer_done == false);
        
        // Read 4 bytes of "Temperature + Humidity" data. 2 bytes for Temperature and 2 bytes for Humidity.
        m_xfer_done    = false;
        u8SlaveAddress = HDC2010_ADDR;
        memset(&u8ReadTempHumid[0], 0, sizeof(u8ReadTempHumid));
        err_code = nrf_drv_twi_rx(&m_twi, HDC2010_ADDR, &u8ReadTempHumid[0], sizeof(u8ReadTempHumid));
        APP_ERROR_CHECK(err_code);
        while(m_xfer_done == false);
        
        // Disable TWI
        nrf_drv_twi_disable(&m_twi);
    }
    
    void hdc2010_Init(void)
    {
        twi_init();
        
        /*
        // Other than 'Measurement Trigger' all settings are by default configured.
        
        1)  Address 0x04 Interrupt DRDY                 =>  All interrupts are disabled by default.
        2)  Address 0x05 Temperature MAX                =>  Not configured
        3)  Address 0x06 Humidity MAX                   =>  Not configured
        4)  Address 0x07 Interrupt Configuration        =>  All interrupts are disabled by default.
        5)  Address 0x08 Temperature Offset Adjustment  =>  No offset configuration
        6)  Address 0x09 Humidity Offset Adjustment     =>  No offset configuration
        7)  Address 0x0A Temperature Threshold LOW      =>  No Threshold configuration
        8)  Address 0x0B Temperature Threshold HIGH     =>  No Threshold configuration
        9)  Address 0x0C Humidity Threshold LOW         =>  No Threshold configuration
        10) Address 0x0D Humidity Threshold HIGH        =>  No Threshold configuration
        11) Address 0x0E Reset and DRDY/INT Configuration Register  => Auto measurement diabled by default. Initiate measurement via I2C
        12) Address 0x0F Measurement Configuration      => 14-bit Temperature resolution,
                                                           14-bit Humidity resolution,
                                                           Read 'Temperature + Humidity' at a time
        */
        
        ret_code_t err_code;
        uint8_t reg[2] = {0};
        
        // Enable TWI
        nrf_drv_twi_enable(&m_twi);
        
        // Auto measurement diabled by default
        reg[0] = CONFIGURATION_FIELD_DESCRIPTIONS_REGISTER_ADDRESS;
        reg[1] = AUTO_MEASUREMENT_DIABLE;
        
        m_xfer_done    = false;
        u8SlaveAddress = HDC2010_ADDR;
        err_code = nrf_drv_twi_tx(&m_twi, HDC2010_ADDR, &reg[0], sizeof(reg), false); //'false' sent STOP at end of transaction
        APP_ERROR_CHECK(err_code);
        while(m_xfer_done == false)
        {
            NRF_LOG_PROCESS();
        }
        
        /*
          Address 0x0F Measurement Configuration => 14-bit Temperature resolution,
                                                    14-bit Humidity resolution,
                                                    Read 'Temperature + Humidity' at a time
        */
        reg[0] = MEASUREMENT_CONFIGURATION_REGISTER_ADDRESS;
        reg[1] = MEASUREMENT_DEFAULT_CONFIGURATION;
        
        m_xfer_done    = false;
        u8SlaveAddress = HDC2010_ADDR;
        err_code = nrf_drv_twi_tx(&m_twi, HDC2010_ADDR, &reg[0], sizeof(reg), false);
        APP_ERROR_CHECK(err_code);
        while(m_xfer_done == false)
        {
            NRF_LOG_PROCESS();
        }
        
        // Disable TWI
        nrf_drv_twi_disable(&m_twi);
    }
    
    /**
     * @}
     */
    
    hdc2010_operations.h

    Thanks & Regards

    Vishnu Beema

  • Hello Vishnu,

    Thank you very much for your reply. From what i can see the code is pretty much the same so it may be a hardware issue. Again thank you very much for the help.

Related