embeddedstm32sensorsstm32cubeidenucleo

HTU2X sensor doesn't respond to code at all


Did the following steps just to test if my temperature sensor HTU2X is working or not. Selected my board (Nucleo-64 STM32F411RE) from STM32CubeIDE then enabled I2C1 which in turn enabled pins PB6 (as the SCL) and PB7 (as the SDA) as in the screenshot below, then saved the project. enter image description here

Then added code below:

I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef huart2; 
void I2C1_Scan()
        {
            char msg[32];
            for (uint8_t addr7 = 0; addr7 <= 127; addr7++)
            {
                uint16_t addr8 = addr7 << 1; // what HAL expects
                if (HAL_I2C_IsDeviceReady(&hi2c1, addr8, 2, 1000) == HAL_OK)
                {
                    // print in both 7-bit and 8-bit forms to avoid confusion
                    snprintf(msg, sizeof(msg), "Found: 0x%02X (7b), 0x%02X (8b)\r\n", addr7, addr8);
                    HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY);
                }
            }
            HAL_UART_Transmit(&huart2, (uint8_t*)"No match!\r\n", 12, HAL_MAX_DELAY);
        }
        
       // ...
int main() {
     // ...
        HAL_Delay(100); // give power and peripherals time to settle
        I2C1_Scan();
        while(1) {
        //...

Below is also the wiring: enter image description here

I get "No match!". Any idea where the problem is and how to fix it?


Solution

  • You don't have any pull-up resistors on SDA and SCL lines. Look at the PCB carefully, the 3 jumper pads need to be solder together to use the two on-board pull up resistors.

    enter image description here

    Edit: It is unclear from your post photo where exactly the purple line (SCL) connected to, please make sure it is indeed connect to PB6, which should be the D10 on the CN5 (Arduino Connector) or pin 17 on CN10 (Morpho Connector). pin 18 of CN10 is NC. enter image description here