# STM32F103 reading value from lightsensor by I2C

I'm trying to read value from sensor GY-30 BH1750 by I2C but when I check variables in STM Studio, they don't change.

POWER_ON 0x01

RESET 0x07

BH1750_CONTINUOUS_HIGH_RES_MODE 0x10

It's my code which I try to get data :

 buffer[0] = BH1750_POWER_ON;
buffer[1] = RESET;
HAL_I2C_Master_Transmit(&hi2c1, SENSOR_ADDRESS << 1, buffer, 2, 100);
buffer[0] = BH1750_CONTINUOUS_HIGH_RES_MODE;
HAL_I2C_Master_Transmit(&hi2c1, SENSOR_ADDRESS << 1, buffer, 1, 100);
HAL_Delay(180);


What's wrong with code ? Thanks for help.

• OK what's the question – Mitu Raj Dec 5 '17 at 14:02
• Is that everything correct in code and it's problem with connections between stm32 and sensor or it's code problem ? Sorry for that but Im new with stm – kacper1230 Dec 5 '17 at 14:03

Yes this answer is quite late. But...no one else answered this and I'm pretty sure the problem has to do with sending 2 commands simultaneously (and not checking return values for success on function calls doesn't help either). From what I can tell, you cannot send the BH1750_POWER_ON command and the RESET command at the same time (they are not a single command). Also, asynchronous reset is different then the Reset command. The data sheet says the following for RESET (datasheet version 2011.11 - Rev.D page 5):

....... Reset command is for only reset Illuminance data register. ( reset value is '0' ) It is not necessary even power supply sequence .......

Which basically means it doesn't really do much for the user. If you fail a read, you should get an error back. So resetting the data register to '0' won't help to determine if some other problem is occurring. Also, for powering on the device, I have notice a slight delay after sending the command helps the device perform as expected. I've inserted a HAL_Delay(5) after the power on command because of this.

So if the user still wants to use the RESET command, the initialization code would need to be modified as follows:

// Variables
uint16_t nLUX = 0;
uint8_t pBuffer = (uint8_t*)&nLUX;  // So LUX value doesn't have to be recombined

// Power On
pBuffer[0] = BH1750_POWER_ON;
if (HAL_I2C_Master_Transmit(&hi2c1, nAddress, pBuffer, 1, 100) != HAL_OK)
return false;
HAL_Delay(5);

// Reset Illuminance Register
pBuffer[0] = RESET;
if (HAL_I2C_Master_Transmit(&hi2c1, nAddress, pBuffer, 1, 100) != HAL_OK)
return false;

// Set measurement mode
pBuffer[0] = BH1750_CONTINUOUS_HIGH_RES_MODE;
if (HAL_I2C_Master_Transmit(&hi2c1, nAddress, pBuffer, 1, 100) != HAL_OK)
return false;
HAL_Delay(180); // Wait for measurement (or come back after 180 ms instead of idling)

return false;

// nLUX holds your LUX value now...
return true;


Or the version without the RESET

// Variables
uint16_t nLUX = 0;
uint8_t pBuffer = (uint8_t*)&nLUX;  // So LUX value doesn't have to be recombined

// Power On
pBuffer[0] = BH1750_POWER_ON;
if (HAL_I2C_Master_Transmit(&hi2c1, nAddress, pBuffer, 1, 100) != HAL_OK)
return false;
HAL_Delay(5);

// Set measurement mode
pBuffer[0] = BH1750_CONTINUOUS_HIGH_RES_MODE;
if (HAL_I2C_Master_Transmit(&hi2c1, nAddress, pBuffer, 1, 100) != HAL_OK)
return false;
HAL_Delay(180); // Wait for measurement (or come back after 180 ms instead of idling)


const uint8_t nAddress = (SENSOR_ADDRESS << 1);