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?

  • Seriously?  From over a year ago?  I don't remember much detail of what was done then.

    The code I was developing is somewhat complex and not an easy to understand example.  It included data logging to SPI flash with a timestamp in addition to a custom BLE service.  It's well beyond the scope of a simple HTS221 interface and too much to use as a HTS221 example.  Plus it's proprietary work I can't entirely disclose.

    I attempted to cut out code snippets relating to just using the HTS221 above.  You should have enough information from that  to effectively interface to the HTS221 and use it in your application.

    Regards,  Max

        

  • Another thought for you.  The nRF52840 DK and the HTS221 are supported in Atmosphere IoT and should provide an easy path to generate some working code.  You can either register for Atmosphere IoT or use the version from DigiKey.

    I haven't tried porting one of their projects back into SES, but they claim it's possible to export to external tools.

  • I had set this up in Atmosphere IoT for a nRF52840 DK.  You can import the project and compile if interested.

    https://github.com/OldMax44/HTS221-Example

    I downloaded the compiled source from this and it didn't look easy to work with or move into another environment like SES.  It does provide a working nRF52 BLE interface to the HTS221 sensor and the ability to create a phone app to display readings.

  • I set up a couple peripheral examples at;

    https://github.com/OldMax44/HTS221-Example/tree/master/Nordic

    They are based on the SDK twi_sensor example.  I wanted to provide direct access to the registers of the HTS221.  Plus I felt the linear interpolation calculations for temperature and humidity should be run in floating point.  The examples are deliberately verbose to give feedback on what's happening.  The log info can always be commented out or deleted.

    I built and tested using SDK 17.0.0 with SES on the nRF52840 DK shown in the picture.

Related