HTU21D sesnor with Nucleo-L152RE
- December 18, 2018
- 18 replies
- 5305 views
I am newbie to use Nucleo-L152RE board. I have a HTU21D-Temperature & Humidity sensor which used I2C interface to read the temperature data.
So, with using the STM32CubeMX I have generated code for STM32L152RETx chip with UART2 (debug purpose) and I2C1 (PB6/PB7).
Below is my snippet code.
#define HTU21D_ADDR 0x80
#define Temperature_REG 0xE3
I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef huart2;
int main(void)
{
uint8_t t_data[2];
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
MX_USART2_UART_Init();
MX_I2C1_Init();
HAL_I2C_Master_Transmit(&hi2c1, HTU21D_ADDR, &Temperature_REG, 1, 1000);
HAL_I2C_Master_Receive(&hi2c1, HTU21D_ADDR, t_data, 2, 1000);
printf("Temperature read data %x:%x\r\n",t_data[0],t_data[1]);
}
void MX_I2C1_Init()
{
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = I2C_SPEEDCLOCK;
hi2c1.Init.DutyCycle = I2C_CCR_DUTY;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
hi2c1.Init.OwnAddress1 = 0x00;
hi2c1.Init.OwnAddress2 = 0x00;
}
Every time getting the zero value from the sensor. So, can you please tell me what is the problem here?.
Connection of HTU21D sensor and Nucleo-L152RE board are:
SCK --> PB6
SDA --> PB7
VCC --> 3V3
GND --> GND
Attached snap shot of interface between sensor and Nucleo.
