Envorimental Monitoring
Loading...
Searching...
No Matches
bh1750.h File Reference

Driver interface for the BH1750 ambient light sensor. More...

#include "stdint.h"
#include "stddef.h"

Go to the source code of this file.

Data Structures

struct  BH1750_HandleTypeDef
 BH1750 driver handle. More...

Macros

#define BH1750_ADDRESS_GND   (0x23 << 1)
#define BH1750_ADDRESS_VCC   (0x5C << 1)
#define BH1750_ADDRESS   BH1750_ADDRESS_GND
#define BH1750_CONTINIOUS_H_RES_MODE   0x10
#define BH1750_CONTINIOUS_H_RES_MODE2   0x11
#define BH1750_CONTINIOUS_L_RES_MODE   0x13
#define BH1750_ONE_TIME_H_RES_MODE   0x20
#define BH1750_ONE_TIME_H_RES_MODE2   0x21
#define BH1750_ONE_TIME_L_RES_MODE   0x23
#define BH1750_PWR_DOWN   0x00
#define BH1750_PWR_ON   0x01
#define BH1750_RST   0x07
#define MEASUREMENT_TIME_H_RES_MODE   120
#define MEASUREMENT_TIME_H_RES_MODE2   120
#define MEASUREMENT_TIME_L_RES_MODE   16
#define BH1750_LUX_COEFF   1.2f
 Lux conversion coefficient: raw / BH1750_LUX_COEFF = lux.

Typedefs

typedef int8_t(* BH1750_I2C_Read_Func) (uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t len)
 Function pointer type for platform I2C read.
typedef int8_t(* BH1750_I2C_Write_Func) (uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t len)
 Function pointer type for platform I2C write.
typedef void(* BH1750_Delay_Func) (uint32_t ms)
 Function pointer type for platform millisecond delay.

Functions

int8_t BH1750_Init (BH1750_HandleTypeDef *dev)
 Powers on the BH1750 and sends the initial measurement mode command.
int8_t BH1750_Read (BH1750_HandleTypeDef *dev)
 Reads the latest illuminance measurement from the BH1750.

Detailed Description

Driver interface for the BH1750 ambient light sensor.

The BH1750 is a 16-bit I2C ambient light sensor with a configurable measurement mode. This driver supports all six modes defined in the datasheet and is platform-independent through injected function pointers.

Typical usage:

hbh1750.i2c_read = my_i2c_read;
hbh1750.i2c_write = my_i2c_write;
hbh1750.delay_ms = my_delay;
float lux = hbh1750.lux;
#define BH1750_ONE_TIME_H_RES_MODE
Definition bh1750.h:66
#define BH1750_ADDRESS_GND
Definition bh1750.h:58
int8_t BH1750_Init(BH1750_HandleTypeDef *dev)
Powers on the BH1750 and sends the initial measurement mode command.
Definition bh1750.c:11
int8_t BH1750_Read(BH1750_HandleTypeDef *dev)
Reads the latest illuminance measurement from the BH1750.
Definition bh1750.c:40
BH1750_HandleTypeDef hbh1750
Definition main.c:146
BH1750 driver handle.
Definition bh1750.h:48
Author
Şule Nur Demirdaş
Date
April 2026

Macro Definition Documentation

◆ BH1750_ADDRESS

#define BH1750_ADDRESS   BH1750_ADDRESS_GND

Default address alias.

◆ BH1750_ADDRESS_GND

#define BH1750_ADDRESS_GND   (0x23 << 1)

I2C address when ADDR pin = GND.

◆ BH1750_ADDRESS_VCC

#define BH1750_ADDRESS_VCC   (0x5C << 1)

I2C address when ADDR pin = VCC.

◆ BH1750_CONTINIOUS_H_RES_MODE

#define BH1750_CONTINIOUS_H_RES_MODE   0x10

Continuous high-res mode (1 lx, ~120 ms).

◆ BH1750_CONTINIOUS_H_RES_MODE2

#define BH1750_CONTINIOUS_H_RES_MODE2   0x11

Continuous high-res mode2 (0.5 lx, ~120 ms).

◆ BH1750_CONTINIOUS_L_RES_MODE

#define BH1750_CONTINIOUS_L_RES_MODE   0x13

Continuous low-res mode (4 lx, ~16 ms).

◆ BH1750_ONE_TIME_H_RES_MODE

#define BH1750_ONE_TIME_H_RES_MODE   0x20

One-time high-res mode (1 lx, ~120 ms).

◆ BH1750_ONE_TIME_H_RES_MODE2

#define BH1750_ONE_TIME_H_RES_MODE2   0x21

One-time high-res mode2 (0.5 lx, ~120 ms).

◆ BH1750_ONE_TIME_L_RES_MODE

#define BH1750_ONE_TIME_L_RES_MODE   0x23

One-time low-res mode (4 lx, ~16 ms).

◆ BH1750_PWR_DOWN

#define BH1750_PWR_DOWN   0x00

Power-down command.

◆ BH1750_PWR_ON

#define BH1750_PWR_ON   0x01

Power-on command.

◆ BH1750_RST

#define BH1750_RST   0x07

Reset data register command.

◆ MEASUREMENT_TIME_H_RES_MODE

#define MEASUREMENT_TIME_H_RES_MODE   120

Typical conversion time for H-res modes.

◆ MEASUREMENT_TIME_H_RES_MODE2

#define MEASUREMENT_TIME_H_RES_MODE2   120

Typical conversion time for H-res2 mode.

◆ MEASUREMENT_TIME_L_RES_MODE

#define MEASUREMENT_TIME_L_RES_MODE   16

Typical conversion time for L-res mode.

Function Documentation

◆ BH1750_Init()

int8_t BH1750_Init ( BH1750_HandleTypeDef * dev)

Powers on the BH1750 and sends the initial measurement mode command.

Parameters
[in,out]devPointer to a BH1750_HandleTypeDef with all fields set.
Returns
0 on success.
-1 if a required function pointer is NULL.
-2 if an I2C write fails.

◆ BH1750_Read()

int8_t BH1750_Read ( BH1750_HandleTypeDef * dev)

Reads the latest illuminance measurement from the BH1750.

For one-time modes the function re-sends the mode command and waits for conversion before reading. For continuous modes it reads immediately. The result is stored in dev->lux.

Parameters
[in,out]devPointer to an initialised BH1750_HandleTypeDef.
Returns
0 on success.
-2 if an I2C write or read fails.