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?

Related