cancel
Showing results for 
Search instead for 
Did you mean: 

Temperature sensor not working

TechyAry
Associate III

To get my first sensor project done and after selecting my board, I enabled I2C1 which triggered pins PB6 and PB7 as shown below: 

Capture.PNG

Then used code below (in main.c) with the purpose that the LED is on when the temperature is higher than 40 degrees and off otherwise. 
 

#include "main.h"

extern I2C_HandleTypeDef hi2c1;   // I2C handle from CubeMX
#define HTU21D_ADDR (0x40 << 1)  // 7-bit address shifted for HAL
#define TRIGGER_TEMP_MEASURE_HOLD  0xE3

I2C_HandleTypeDef hi2c1;

UART_HandleTypeDef huart2;

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_I2C1_Init(void);

float HTU21D_ReadTemperature(void)
{
	uint8_t cmd = TRIGGER_TEMP_MEASURE_HOLD;
	uint8_t data[2];

	// Send temperature measurement command
	HAL_I2C_Master_Transmit(&hi2c1, HTU21D_ADDR, &cmd, 1, HAL_MAX_DELAY);

	// Receive 2 bytes (temperature)
	HAL_I2C_Master_Receive(&hi2c1, HTU21D_ADDR, data, 2, HAL_MAX_DELAY);

	// Combine and mask status bits
	uint16_t rawTemp = (data[0] << 8) | data[1];
	rawTemp &= 0xFFFC;

	// Convert to Celsius 
	float temp = -46.85 + 175.72 * ((float)rawTemp / 65536.0);

	return temp;
}

int main(void)
{

	HAL_Init();
	SystemClock_Config();
	MX_GPIO_Init();
	MX_USART2_UART_Init();
	MX_I2C1_Init();
	while (1)
	{
		if ( HTU21D_ReadTemperature() > 40.0f)
			HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);  // LED ON
		else
			HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // LED OFF

		HAL_Delay(1000);  // check every 1s
	}
}

The wiring is also this way:
PB6 -> Sensor's SCL (there's also a 220 Ohm resister between SCL and MCU 3.3V)
PB7 -> Sensor's SDL (there's also another 220 Ohm resister between SDL and MCU 3.3V)
sensor's GND -> MCU GND
sensor's VCC -> MCU 3.3V

The issue is that the LED is on while the room's temperature is obviously less than 40 degrees!

Any idea where the problem is, please? 

10 REPLIES 10
AAArdvark
Associate II

220 ohm is a very small resistor value to act as an I2C-pullup, that could be the issue. However check the return values of HAL_I2C_Transmit and Receive to see if there are any errors and step through the sensor read-function with a debugger to see if the problem is the I2C-communication, data-conversion or something else

AScha.3
Super User

1. your pullups 220r are way to low value, try 4k7  .

2. whats the value, the sensor is giving ?

If you feel a post has answered your question, please click "Accept as Solution".

Are two resistors with the values 1KΩ or 2KΩ fine for that?

Declared globals int res_trans, res_recev; then used: 

res_trans = HAL_I2C_Master_Transmit(...);

res_recev = HAL_I2C_Master_Receive(...);

 and ran through the debugger. Both res_trans, res_recev are 1. So there's an error, yeah?

Do you mean the HTU21D_ReadTemperature by the read-function?

 

My sensor's mark is HUT2X

1- Wdym by 4k7?

2- Do you mean the return values of HAL_I2C_Master_Transmit/HAL_I2C_Master_Receive functions? 

1.  4k7 = 4.7 kohm ( 2 k maybe also ok, but still little bit low value; depends on sensor, what is allowed)

2. just the value, the sensor is telling;  receive -> data = ?

to see, whats the value from sensor (valid, or just error, nothing useful received...everything is possible.)

 

If you feel a post has answered your question, please click "Accept as Solution".

1- Am going to purchase 3 KOhm resisters. Hopefully they're OK for that sensor. 

2- Still don't get what you mean, sorry. 

The transmit and receive are the only functions that communicate with the sensor, the rest is merely computations. 

Not sure how to find the problem. 

 

3 k ok, you dont have it ?

(I usually have some big resistors , through hole, SMD 1206, 0805, 0603 , something to choose from affordable assortments;  see at Ali...like:  https://de.aliexpress.com/item/1005006844678887.html

or similar .)

2. just look and tell, what is the response from the sensor;    What its value ? 

To see, is your sensor giving credible data , or some 0xFF , = nothing read really.

If you feel a post has answered your question, please click "Accept as Solution".

Yes, if the return value is 1 there's an error; you can step inside the I2C-functions in the debugger and will probably see a timeout due to the low pullup value. It's good practice to always check return values of functions (and print them in case of errors) because it makes troubleshooting much easier. For the recommended pullup value look into the datasheet of the temperature sensor, there will probably be a schematic-recommendation; normally 4,7k is ok for I2C. Also an easy way to test the connection between an STM32 and an I2C-device is the function HAL_I2C_IsDeviceReady

I used two 3K resisters, yet the same result! 

2- Tell? What is tell? What response do you mean? Again, if you mean the receive and transmit functions, I told you about their return values, if you mean something else, what is it? I can't understand you.