Created
October 20, 2023 19:34
-
-
Save lucky-rydar/ce5017f9d1831e5b14589fbf536064a3 to your computer and use it in GitHub Desktop.
bh1750 driver for ESP32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "bh1750.h" | |
#include "driver/i2c.h" | |
#include "freertos/FreeRTOS.h" | |
#define AFTER_READ_KOEF 1.2 | |
bool bh1750_op_cmd(uint8_t op) { | |
i2c_cmd_handle_t cmd = i2c_cmd_link_create(); | |
i2c_master_start(cmd); | |
i2c_master_write_byte(cmd, BH1750_ADDRESS << 1 | I2C_MASTER_WRITE, true); | |
i2c_master_write_byte(cmd, op, true); | |
i2c_master_stop(cmd); | |
esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_PERIOD_MS); | |
i2c_cmd_link_delete(cmd); | |
return ret == ESP_OK; | |
} | |
bool bh1750_power_down() { | |
return bh1750_op_cmd(BH1750_POWER_DOWN); | |
} | |
bool bh1750_power_on() { | |
return bh1750_op_cmd(BH1750_POWER_ON); | |
} | |
bool bh1750_reset() { | |
return bh1750_op_cmd(BH1750_RESET); | |
} | |
bool bh1750_set_mode(uint8_t mode) { | |
return bh1750_op_cmd(mode); | |
} | |
uint16_t bh1750_read_light_level() { | |
// read two bytes | |
uint16_t level; | |
i2c_cmd_handle_t cmd = i2c_cmd_link_create(); | |
i2c_master_start(cmd); | |
i2c_master_write_byte(cmd, BH1750_ADDRESS << 1 | I2C_MASTER_READ, true); | |
i2c_master_read(cmd, (uint8_t*)&level, 2, I2C_MASTER_LAST_NACK); | |
i2c_master_stop(cmd); | |
esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_PERIOD_MS); | |
i2c_cmd_link_delete(cmd); | |
if (ret != ESP_OK) { | |
return -1; | |
} else { | |
return level / AFTER_READ_KOEF; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef BH1750_H | |
#define BH1750_H | |
#include <stdint.h> | |
#include <stdbool.h> | |
#define BH1750_ADDRESS 0x23 | |
#define BH1750_POWER_DOWN 0x00 | |
#define BH1750_POWER_ON 0x01 | |
#define BH1750_RESET 0x07 | |
// 120ms 1lx resolution | |
#define BH1750_CONTINUOUS_HIGH_RES_MODE 0x10 | |
// 120ms 0.5lx resolution | |
#define BH1750_CONTINUOUS_HIGH_RES_MODE_2 0x11 | |
// 16ms 4lx resolution | |
#define BH1750_CONTINUOUS_LOW_RES_MODE 0x13 | |
// one time measurements powers down after measurement | |
// 120ms 1lx resolution | |
#define BH1750_ONE_TIME_HIGH_RES_MODE 0x20 | |
// 120ms 0.5lx resolution | |
#define BH1750_ONE_TIME_HIGH_RES_MODE_2 0x21 | |
// 16ms 4lx resolution | |
#define BH1750_ONE_TIME_LOW_RES_MODE 0x23 | |
#define HIGH_RES_DELAY 120 | |
#define LOW_RES_DELAY 16 | |
bool bh1750_power_down(); | |
bool bh1750_power_on(); | |
bool bh1750_reset(); | |
bool bh1750_set_mode(uint8_t mode); | |
uint16_t bh1750_read_light_level(); | |
#endif // BH1750_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment