cancel
Showing results for 
Search instead for 
Did you mean: 

How to implement repeated start for I2C?

NGros
Associate III

Hi all,

i have to readout temperature and humidity of a dhc3021 sensor. Therefore, an Arduino code section is already given:

Wire.beginTransmission(HDC_ADDR); // Readout temp and rh data

Wire.write(0xE0);

Wire.write(0x00);

Wire.endTransmission(false); //repeated start

Wire.beginTransmission(HDC_ADDR);

Wire.requestFrom(HDC_ADDR, 6, true);

if (Wire.available() >= 6) {

const uint8_t temp_msb = Wire.read();

const uint8_t temp_lsb = Wire.read();

const uint8_t crc = Wire.read();

const uint8_t rh_msb = Wire.read();

const uint8_t rh_lsb = Wire.read();

const uint8_t crc2 = Wire.read();

temperature_hdc_raw = (temp_msb << 8) | temp_lsb;

humidity_hdc_raw = (rh_msb << 8) | rh_lsb;

temperature_hdc = -45 +(175 *(temperature_hdc_raw / pow(2, 16)));

humidity_hdc = 100 * (humidity_hdc_raw / pow(2, 16));

}

As you can see, Wire.endTransmission(false); after writing two bytes to the sensor (read request) works as a repeated start condition. I know that HAL_I2C_Mem_Write() would actually do a repeated start, but the thing is, this method requires a memory_address, which in my case is not used, because I am not writing to a register. The sensor kindoff just receives those two bytes 0xE0 and 0x00 and knows what to to. Without repeated start, I can't read the following 6 bytes.

Thank you for any suggestions, highly appreciate.

KR

1 REPLY 1
TDK
Guru

If you want to write 2 bytes, then read 4 bytes, use HAL_I2C_Mem_Read() with 0xE000 as the memory address. It doesn't matter

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