0
votes

Does anyone, who uses STM32 HAL driver, got I2C communication with a Sensirion sensor like SHT25 working and can show me his snippets?

I got communication working using the Code examples from sensirion. (https://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/11_Sample_Codes_Software/Humidity_Sensors/Sensirion_Humidity_Sensors_SHT21_Sample_Code_V1.2.pdf) I get an Acknowledge when i address the sensor, but when i want to read sensor data, i only get "11111111".

2
What have you tried so far? Read the datasheet, there are a list of commands which should be used. Also search for STM32CubeMX and STM32CubeF0/1/2/3/4, plenty of examples.Bence Kaulics

2 Answers

0
votes

Working code for Sensirion SHTC1 on STM32 with HAL drivers:

#define SHTC1_I2C_ADDR  0xE0
#define TEMP_HUM_CMD_MEASURE_T_FIRST    0x7866

//Launch convert
uint8_t cmd[2];
cmd[0] = (uint8_t)(TEMP_HUM_CMD_MEASURE_T_FIRST >>> 8);
cmd[1] = (uint8_t)TEMP_HUM_CMD_MEASURE_T_FIRST;
HAL_I2C_Master_Transmit(&hi2c1, SHTC1_I2C_ADDR, cmd, 2, 100);

//Wait
HAL_Delay(15);

//Read values
uint8_t rawValues[6]; //T MSB, T LSB, T CRC, H MSB, H LSB, H CRC
HAL_I2C_Master_Receive(&hi2c1, SHTC1_I2C_ADDR, rawValues, 6, 100);
uint16_t rawTemp = (uint16_t)((((uint16_t)rawValues[0])<<8) | (uint16_t)rawValues[1]);
uint16_t rawHum = (uint16_t)((((uint16_t)rawValues[3])<<8) | (uint16_t)rawValues[4]);

float hum = (float)((float)100 * (float)rawHum / (float)65536);
float temp =(float)((float)-45 + (float)175 * (float)rawTemp / (float)65536);
0
votes

Use HAL_I2C_Mem_Write() and HAL_I2C_Mem_Read() HAL APIs for writing and reading data through I2C interface from the sensor. What data to write/read and from which memory location to write/read you have to find from sensor data sheet.