cancel
Showing results for 
Search instead for 
Did you mean: 

I2C issues with ATH10, HAL_I2C_Master_Transmit giving error but HAL_I2C_Master_Recieve doesn't

BadEngineer
Associate II

I have a NUCLEO-F103RB and I am trying to communicate with my AHT10 sensor. I understand the basics of I2C but i'm a newbie to stm32. 

For some reason when i do HAL_I2C_MASTER_TRANSMIT it seems to return a failure but HAL_I2C_MASTER_RECIEVE doesn't return any error at all. 

The null_a_char function basically just puts '/0' into the array so i can reuse it next time.

My code is below. 

while (1)
  {
	  null_a_char(buf);
	  buf[0]= 172;		//0xAC
	  ret = HAL_I2C_Master_Transmit(&hi2c1, 112, buf, 8, 10000); //Returns HAL_ERROR

	  null_a_char(buf);		// Just puts /0 in the buff 
	  buf[0]= 172;		//0xAC
	  ret = HAL_I2C_Master_Receive(&hi2c1, 112, buf, 2, 1000);	// Returns HAL_OK

	  null_a_char(buf);
	  ret = HAL_I2C_Master_Receive(&hi2c1, 113, buf, 6, 1000);	// Returns HAL_OK


	  // this returns buff[0]=24, buff[1]=220, buff[2]=105, buff[3]=0, buff[4]=0
	  // buff[5]=0, buff[6]=0
}

From my research, it seems the AHT10 sensor is on address 0x38. I have attached from what I think is the datasheet for the AHT10 as well as a couple pictures of my setup as well as my .ioc file. 

I'm a hobbiest and don't have anyone to turn to, please help. 

BadEngineer_0-1723491126183.png

My AHT10 sensor with the 2 pull up resistors (both connected to positive rail 

BadEngineer_1-1723491168029.png

The 2 wires connecting back to my nucleo board. 

BadEngineer_2-1723491229702.png

Image of my .ioc file

I have attached the AHT10 datasheet (i could be wrong but this is what i found). 

 

 



1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

> ret = HAL_I2C_Master_Transmit(&hi2c1, 112, buf, 8, 10000); //Returns HAL_ERROR

Looks like the device is only expecting 3 bytes for this command (0xAC, DATA0, DATA1), but you're sending 8. It's probably NACKing on the unexpected bytes. Why are you sending 8? Or am I misreading the data sheet?

TDK_0-1723493057140.png

 

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

View solution in original post

4 REPLIES 4
TDK
Guru

> ret = HAL_I2C_Master_Transmit(&hi2c1, 112, buf, 8, 10000); //Returns HAL_ERROR

Looks like the device is only expecting 3 bytes for this command (0xAC, DATA0, DATA1), but you're sending 8. It's probably NACKing on the unexpected bytes. Why are you sending 8? Or am I misreading the data sheet?

TDK_0-1723493057140.png

 

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

It's not very clear, but it looks like the trigger data needs to be 0x33, 0x00.

 

KarlYamashita_1-1723494581545.png

 

 

 

#define AHT10_SLAVE_ADDRESS (0x38 << 1)

#define TRIGGER_MEASUREMENT 0xAC

uint8_t aht10_data[6] = {0};

 

// update for Trigger measurement

aht10_data[0] = TRIGGER_MEASUREMENT;

aht10_data[1] = 0x33;

aht10_data[2] = 0x00;

// send trigger measurement byte plus 2 bytes

HAL_I2C_Master_Transmit(&hi2c1, AHT10_SLAVE_ADDRESS, aht10_data, 3, 100);

 

// Datasheet indicates MCU must wait for the measurement to be completed.

HAL_Delay(100);

 

// read temperature and humidity data, 6 bytes

HAL_I2C_Master_Receive(&hi2c1, AHT10_SLAVE_ADDRESS, aht10_data, 6, 100);

 

 

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.

@Karl Yamashita 

Thank you for the code. 

Yes you are correct i also had to pass it 0x33. 

My follow up question is, in terms of the return data. I am getting the following in my buff[] or for your aht10_data equivalent. 

buff[0] = 24, buff[1]=149, buff[2]=177, buff[3]134, buff[4]=104, buff[5]=181. 

Am i correct to assume that buff[4] and buff[5] and half of buff[3] is tempreture?.
When i plug the numbers in (after converting them into binary) i get the following (420021/2^20)*200-50=30.1C

BadEngineer_0-1723496554142.png

Am i correct in assuming this?

I couldn't find anything that indicates how many bits there are for temperature and humidity, but if is is 20 bits, then your calculation looks correct.

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.