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

Problem with communication with HTS221 sensor

Hello Nordic community,

I have a nrf52-DK with SDK15.2.0 and I'm trying to communicate with ST HTS221 sensor. I have the original  STEVAL-MKI141V2 evaluation board.

I use the twi_sensor example, provided in the SDK. I added the hts221 driver from the SDK and used the default functions for initialization:


#include <stdio.h>
#include "boards.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "nrf_drv_twi.h"
#include "nrf_delay.h"


#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

#include "hts221.h"

#include "nrf_twi_mngr.h"

/* TWI instance ID. */
#define TWI_INSTANCE_ID             0

#define MAX_PENDING_TRANSACTIONS    33




void sen_init();
void read_th();

NRF_TWI_MNGR_DEF(m_nrf_twi_mngr, MAX_PENDING_TRANSACTIONS, TWI_INSTANCE_ID);

NRF_TWI_SENSOR_DEF(m_nrf_twi_sensor, &m_nrf_twi_mngr, HTS221_MIN_QUEUE_SIZE);

HTS221_INSTANCE_DEF(m_hts221, &m_nrf_twi_sensor, HTS221_BASE_ADDRESS);

////static hts221_data_t m_sample;

//void print_identity(ret_code_t r, void *p_register_data)
//{
//    NRF_LOG_INFO("Identity: %d", *((uint8_t *)p_register_data));
//}



void sen_init ()
{
	uint32_t err;
	
    nrf_drv_twi_config_t const config = {
       .scl                = 27,
       .sda                = 26,
       .frequency          = NRF_DRV_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_LOWEST,
       .clear_bus_init     = false
    };


    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    NRF_LOG_INFO("TWI sensor example started.");
    NRF_LOG_FLUSH();

    err = nrf_twi_mngr_init(&m_nrf_twi_mngr, &config);
    APP_ERROR_CHECK(err);

    err = nrf_twi_sensor_init(&m_nrf_twi_sensor);
    APP_ERROR_CHECK(err);

    err = hts221_init(&m_hts221);
    APP_ERROR_CHECK(err);
		
		err = hts221_avg_cfg (&m_hts221, HTS221_TEMP_SAMPLES_8, HTS221_HUMIDITY_SAMPLES_16);
		APP_ERROR_CHECK(err);
		
		
	//	err = hts221_data_rate_cfg (&m_hts221, HTS221_ODR_ONESHOT);
	//	APP_ERROR_CHECK(err);
		
		
		uint8_t reg;
		
		err = hts221_who_am_i_read (&m_hts221, NULL, &reg);
		APP_ERROR_CHECK(err);
		
		NRF_LOG_INFO("WHO AM I: %d ", reg);
    NRF_LOG_FLUSH();
}


void read_th()
{
		uint32_t err;
		int16_t temperature = 0;
		int16_t humidity = 0;
		int16_t rawtemp;
		int16_t rawhum;

		
			err = hts221_temp_read(&m_hts221, NULL  ,&rawtemp);
			APP_ERROR_CHECK(err);
	NRF_LOG_INFO("RAW TEMP : %d\n", rawtemp);
			
			temperature = hts221_temp_process(&m_hts221,  rawtemp) /8;

		
			err = hts221_hum_read(&m_hts221, NULL , &rawhum);
			APP_ERROR_CHECK(err);
	NRF_LOG_INFO("RAW HUMIDITY : %d\n", rawhum);
	
			humidity = hts221_hum_process(&m_hts221,  rawhum) /2;

	
			NRF_LOG_INFO("Temp: %d  || Humidity: %d\n", temperature, humidity);
}


/**
 * @brief Function for main application entry.
 */
int main(void)
{	
		
		sen_init();
		


    NRF_LOG_INFO("Here.");
    NRF_LOG_FLUSH();
    //APP_ERROR_CHECK(err);

		while(1)
		{
					
			nrf_delay_ms(500);
			
		//	read_th();
			
			//NRF_LOG_FLUSH();
			}
 }

/** @} */

When I try to read the whoami register, I get value 0. When I try to read the temperature and humidity data, always get the same value.

I tried to communicate with ST LIS2DH12 accelerometer the same way with the same example and everything is normal. Is it possible the problem to be in the hts221 libraries?

  • It could be. What I did was to write a new library, based on the arduino one for this sensor. Thank you for the support!

  • I'm also interested in the HTS221 sensor.  I used the above code as a starting point to interface to the STEVAL-MKI141V2 board and used the driver included in SDK 15.2.  I wired the HTS221 to a nRF52840 DK (see picture below).  I used SES to add the code to a BLE peripheral template.

    ilkobg didn't reply as to ever getting this working, but after some trial and error, it worked for me.  I used a callback to report the humidity and temperature readings.  There's really no documentation on how to use this driver nor an example.  Apparently the HTS221 is used in the Thingy, but I'm not familiar with that environment and didn't find any useful code example.   A code snippet included below and a screenshot of the SES debug terminal displaying results.

     

    //
    // Set up the TWI instance for the HTS221 sensor 
    //
    
    NRF_TWI_MNGR_DEF(m_nrf_twi_mngr, MAX_PENDING_TRANSACTIONS, TWI_INSTANCE_ID);
    NRF_TWI_SENSOR_DEF(m_nrf_twi_sensor, &m_nrf_twi_mngr, HTS221_MIN_QUEUE_SIZE);
    HTS221_INSTANCE_DEF(m_hts221,&m_nrf_twi_sensor,HTS221_BASE_ADDRESS);
    
    // TWI (with transaction manager) initialization.
    static void twi_config(void)
    {
        uint32_t err_code;
    
        nrf_drv_twi_config_t const config = {
           .scl                = ARDUINO_SCL_PIN,
           .sda                = ARDUINO_SDA_PIN,
           .frequency          = NRF_DRV_TWI_FREQ_100K,
           .interrupt_priority = APP_IRQ_PRIORITY_LOWEST,
           .clear_bus_init     = false
        };
    
        NRF_LOG_INFO("TWI HTS221 sensor starting.");
        NRF_LOG_FLUSH();
    
        err_code = nrf_twi_mngr_init(&m_nrf_twi_mngr, &config);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_twi_sensor_init(&m_nrf_twi_sensor);
        APP_ERROR_CHECK(err_code);
    
        err_code = hts221_init(&m_hts221);
        APP_ERROR_CHECK(err_code);
    		
        err_code = hts221_avg_cfg (&m_hts221, HTS221_TEMP_SAMPLES_8, HTS221_HUMIDITY_SAMPLES_16);
        APP_ERROR_CHECK(err_code);
    		
    }
    
    void m_temp_callback (uint32_t twi_result_t, int16_t * p_rawtemp)
    {
          
          NRF_LOG_INFO("RAW TEMP : %d", rawtemp);
          temperature = hts221_temp_process(&m_hts221,  rawtemp)/8;  
          
    }
    
    void m_hum_callback(uint32_t twi_result_h, int16_t * p_rawhum)
    {
         int tempF;
    
         NRF_LOG_INFO("RAW HUMIDITY : %d\n", rawhum);
         humidity = hts221_hum_process(&m_hts221,  rawhum)/2;
         //
         // 2nd task competed
         // Temperature and Humidity both processed
         // Display results
         tempF = temperature*1.8+32;
         NRF_LOG_INFO("Temp: %d Degrees C,  %d Degrees F", temperature, tempF);
         NRF_LOG_INFO("Humidity: %d%%\n", humidity)
    }
    
    void read_th()
    {
          uint32_t err;
          // Submit temperature read command to TWI manager
          // m_temp_callback called when data is returned
          err = hts221_temp_read(&m_hts221, &m_temp_callback , &rawtemp);
          APP_ERROR_CHECK(err);
          // Submit humidity read command to TWI manager
          // m_hum_callback called when data is returned
          err = hts221_hum_read(&m_hts221, &m_hum_callback , &rawhum);
          APP_ERROR_CHECK(err);
    }
    
    /**@brief Function for application main entry.
     */
    int main(void)
    {
        bool erase_bonds;
        uint8_t reg;
    
        // Initialize.
        log_init();
        timers_init();
        buttons_leds_init(&erase_bonds);
        power_management_init();
        ble_stack_init();
        peer_manager_init(erase_bonds);
        if (erase_bonds == true)
        {
            NRF_LOG_INFO("Bonds erased!");
        }
        gap_params_init();
        gatt_init();
        advertising_init();
        services_init();
        conn_params_init();
    
        twi_config();
    
        nrf_delay_ms(500);
    
        hts221_who_am_i_read (&m_hts221, NULL, &reg);
        
        NRF_LOG_INFO("WHO AM I: %02X ", reg);
    
        // Start execution.
        NRF_LOG_INFO("Bluetooth started.");
        application_timers_start();
        advertising_start();
    
        // Trial temperature and humidity reading
        read_th();
    
        // Enter main loop.
        for (;;)
        {
            idle_state_handle();
        }
    }
    
    

  • Hi, I've been recently trying to establish a communication between the HTS221 sensor that I have in a STEVAL-IDI003V2 board. For now I've used the code ilkobg has posted. After linking correctly the hts221 library and adding some lines of code to the skd_config.h file, the code compiles and I get the exact same results that ilkobg explains.
    Right now I'm quite stuck with this problem and I don't know what else can I do in order to establish a sucessful communication, as I think the hardware is fine.


    maxx44, could you share your whole code if you don't mind? Or do you know any possible reason behind my problem?

    Thank you in advance.

  • Hi,

    Sorry for the delayed response.  I've moved on to another project and had to set the one above aside, but I was able to establish readings from the HTS221 as mentioned above.  Beyond the snippet I showed, what I have is specific to the PCA10056 board shown and development with SES and a SDK ble_peripheral template to which I've added code.  I doubt this would all do you much good unless you're in the same environment.  Plus it's a lot to wallow through.

    What I did find is that the driver in the SDK requires you to also configure the TWI manager and enable this in your sdk_config.h file.  I found this complicated things more than I needed, but I went along with it rather than have to dive in and modify the driver or come up with my own.  Look carefully through the configuration settings at the beginning of the code I showed above and see if it helps get the driver set up.

    Regards,  Max

Related