Version 1.0.2
Added automatically sensitivity adjustment. Fixed bug with sensitivity adjustment error on ESP32.
This commit is contained in:
parent
f77318150c
commit
0f52f39fa7
20
README.md
20
README.md
@ -5,6 +5,11 @@
|
||||
1. ESP8266 RTOS_SDK v3.4
|
||||
2. ESP32 ESP-IDF v5.2
|
||||
|
||||
## Features
|
||||
|
||||
1. Recorded illuminance values from 0.11 to 121241 lux (depends on operation mode and sensitivity).
|
||||
2. Support automatically sensitivity adjustment.
|
||||
|
||||
## Using
|
||||
|
||||
In an existing project, run the following command to install the component:
|
||||
@ -54,20 +59,21 @@ void app_main(void)
|
||||
i2c_master_bus_handle_t i2c_bus_handle;
|
||||
i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle);
|
||||
#endif
|
||||
zh_bh1750_init_config_t zh_bh1750_init_config = ZH_BH1750_INIT_CONFIG_DEFAULT();
|
||||
zh_bh1750_init_config_t bh1750_init_config = ZH_BH1750_INIT_CONFIG_DEFAULT();
|
||||
bh1750_init_config.auto_adjust = true; // Just for an example of how to enable auto adjustment the sensor sensitivity.
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
zh_bh1750_init_config.i2c_port = I2C_PORT;
|
||||
bh1750_init_config.i2c_port = I2C_PORT;
|
||||
#else
|
||||
zh_bh1750_init_config.i2c_handle = i2c_bus_handle;
|
||||
bh1750_init_config.i2c_handle = i2c_bus_handle;
|
||||
#endif
|
||||
zh_bh1750_init(&zh_bh1750_init_config);
|
||||
zh_bh1750_adjust(69); // Just for an example of how to change the sensor sensitivity.
|
||||
zh_bh1750_init(&bh1750_init_config);
|
||||
zh_bh1750_adjust(69); // Just for an example of how to change the sensor sensitivity. Don’t work if auto adjustment is enabled.
|
||||
float lux = 0.0;
|
||||
for (;;)
|
||||
{
|
||||
zh_bh1750_read(&lux);
|
||||
printf("Lux %d\n", (uint32_t)lux);
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
printf("Lux %0.2f\n", lux);
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -10,16 +10,13 @@
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
/**
|
||||
* @brief Default values for zh_bh1750_init_config_t structure for initial initialization of the sensor.
|
||||
*
|
||||
*/
|
||||
#define ZH_BH1750_INIT_CONFIG_DEFAULT() \
|
||||
{ \
|
||||
.i2c_address = I2C_ADDRESS_LOW, \
|
||||
.operation_mode = HIGH_RESOLUTION, \
|
||||
.work_mode = ONE_TIME, \
|
||||
.i2c_port = 0 \
|
||||
.i2c_port = 0, \
|
||||
.auto_adjust = false \
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
@ -27,16 +24,9 @@ extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Structure for initial initialization of the sensor.
|
||||
*
|
||||
* @note Before initialize the sensor recommend initialize zh_bh1750_init_config_t structure with default values.
|
||||
*
|
||||
* @code zh_bh1750_init_config_t config = ZH_BH1750_INIT_CONFIG_DEFAULT() @endcode
|
||||
*/
|
||||
typedef struct
|
||||
typedef struct // Structure for initial initialization of BH1750 sensor.
|
||||
{
|
||||
enum // Sensor address.
|
||||
enum // Sensor I2C address.
|
||||
{
|
||||
I2C_ADDRESS_HIGH = 0x5C, // Data pin is connected to VCC.
|
||||
I2C_ADDRESS_LOW = 0x23 // Data pin is not connected / connected to GND.
|
||||
@ -53,6 +43,7 @@ extern "C"
|
||||
ONE_TIME // One time measurement. Sensor is power down after measurement.
|
||||
} work_mode;
|
||||
bool i2c_port; // I2C port.
|
||||
bool auto_adjust; // Flag of automatic sensitivity adjustment.
|
||||
#ifndef CONFIG_IDF_TARGET_ESP8266
|
||||
i2c_master_bus_handle_t i2c_handle; // Unique I2C bus handle.
|
||||
#endif
|
||||
@ -95,6 +86,8 @@ extern "C"
|
||||
/**
|
||||
* @brief Adjust BH1750 sensor sensitivity.
|
||||
*
|
||||
* @attention Can only be used if the automatic adjustment is not switched on.
|
||||
*
|
||||
* @param[in] value Value of sensitivity.
|
||||
*
|
||||
* @note Range value from 31 to 254. 69 by default. Can be changed continuously during program work.
|
||||
@ -102,7 +95,7 @@ extern "C"
|
||||
* @return
|
||||
* - ESP_OK if adjust was success
|
||||
* - ESP_ERR_INVALID_ARG if parameter error
|
||||
* - ESP_ERR_NOT_FOUND if BH1750 is not initialized
|
||||
* - ESP_ERR_NOT_FOUND if BH1750 is not initialized or auto adjust is enabled
|
||||
* - ESP_FAIL if sending command error or slave has not ACK the transfer
|
||||
* - ESP_ERR_INVALID_STATE if I2C driver not installed or not in master mode
|
||||
* - ESP_ERR_TIMEOUT if operation timeout because the bus is busy
|
||||
|
@ -1 +1 @@
|
||||
1.0.1
|
||||
1.0.2
|
54
zh_bh1750.c
54
zh_bh1750.c
@ -1,7 +1,7 @@
|
||||
#include "zh_bh1750.h"
|
||||
|
||||
#define LOW_RESOLUTION_MEASUREMENT_TIME 24 // L-Resolution mode measurement time max value.
|
||||
#define HIGH_RESOLUTION_MEASUREMENT_TIME 180 // H-Resolution mode measurement time max value.
|
||||
#define LOW_RESOLUTION_MEASUREMENT_TIME 24 // L-Resolution mode measurement time.
|
||||
#define HIGH_RESOLUTION_MEASUREMENT_TIME 180 // H-Resolution mode measurement time.
|
||||
|
||||
#define SENSITIVITY_DEFAULT 69
|
||||
|
||||
@ -25,6 +25,8 @@ static bool _is_initialized = false;
|
||||
static i2c_master_dev_handle_t _bh1750_handle = {0};
|
||||
#endif
|
||||
|
||||
esp_err_t _adjust(const uint8_t value);
|
||||
|
||||
static const char *TAG = "zh_bh1750";
|
||||
|
||||
esp_err_t zh_bh1750_init(const zh_bh1750_init_config_t *config)
|
||||
@ -84,6 +86,7 @@ esp_err_t zh_bh1750_read(float *data)
|
||||
ESP_LOGE(TAG, "BH1750 read fail. BH1750 not initialized.");
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
REPEATE:;
|
||||
esp_err_t esp_err = ESP_OK;
|
||||
uint8_t sensor_data[2] = {0};
|
||||
if (_init_config.work_mode == CONTINUOUSLY)
|
||||
@ -113,7 +116,7 @@ esp_err_t zh_bh1750_read(float *data)
|
||||
ESP_LOGE(TAG, "BH1750 read fail. I2C driver error.");
|
||||
return esp_err;
|
||||
}
|
||||
vTaskDelay(((_sensivity / SENSITIVITY_DEFAULT) * _time) / portTICK_PERIOD_MS);
|
||||
vTaskDelay(_time / portTICK_PERIOD_MS);
|
||||
READ:
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
i2c_cmd_handle = i2c_cmd_link_create();
|
||||
@ -132,19 +135,51 @@ READ:
|
||||
ESP_LOGE(TAG, "BH1750 read fail. I2C driver error.");
|
||||
return esp_err;
|
||||
}
|
||||
uint32_t raw_data = sensor_data[0] << 8 | sensor_data[1];
|
||||
if (raw_data == 65535 || raw_data == 0)
|
||||
{
|
||||
ESP_LOGW(TAG, "BH1750 read error. Sensitivity adjustment required. Current sensivity level %d.", _sensivity);
|
||||
}
|
||||
if (_init_config.auto_adjust == true)
|
||||
{
|
||||
if (raw_data == 65535 && _sensivity > 31)
|
||||
{
|
||||
if (_adjust(_sensivity - 1) == ESP_OK)
|
||||
{
|
||||
goto REPEATE;
|
||||
}
|
||||
}
|
||||
if (raw_data == 0 && _sensivity < 254)
|
||||
{
|
||||
if (_adjust(_sensivity + 1) == ESP_OK)
|
||||
{
|
||||
goto REPEATE;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (_init_config.operation_mode == HIGH_RESOLUTION_2)
|
||||
{
|
||||
*data = (sensor_data[0] << 8 | sensor_data[1]) * (1 / 1.2 * (69.0 / _sensivity) / 2);
|
||||
*data = raw_data * (1 / 1.2 * ((float)SENSITIVITY_DEFAULT / _sensivity) / 2);
|
||||
}
|
||||
else
|
||||
{
|
||||
*data = (sensor_data[0] << 8 | sensor_data[1]) * (1 / 1.2 * (69.0 / _sensivity));
|
||||
*data = raw_data * (1 / 1.2 * ((float)SENSITIVITY_DEFAULT / _sensivity));
|
||||
}
|
||||
ESP_LOGI(TAG, "BH1750 read success.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_bh1750_adjust(const uint8_t value)
|
||||
{
|
||||
if (_init_config.auto_adjust == true)
|
||||
{
|
||||
ESP_LOGE(TAG, "BH1750 adjust fail. Auto adjust is enabled.");
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
return _adjust(value);
|
||||
}
|
||||
|
||||
esp_err_t _adjust(const uint8_t value)
|
||||
{
|
||||
ESP_LOGI(TAG, "BH1750 adjust begin.");
|
||||
if ((value < 31 || value > 254))
|
||||
@ -158,11 +193,12 @@ esp_err_t zh_bh1750_adjust(const uint8_t value)
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
esp_err_t esp_err = ESP_OK;
|
||||
uint8_t sensitivity_data[2] = {value >> 5 | MEASUREMENT_TIME_HIGH_BIT, (value & 0b00011111) | MEASUREMENT_TIME_LOW_BIT};
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create();
|
||||
i2c_master_start(i2c_cmd_handle);
|
||||
i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true);
|
||||
i2c_master_write_byte(i2c_cmd_handle, value >> 5 | MEASUREMENT_TIME_HIGH_BIT, true);
|
||||
i2c_master_write_byte(i2c_cmd_handle, sensitivity_data[0], true);
|
||||
i2c_master_stop(i2c_cmd_handle);
|
||||
esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
||||
i2c_cmd_link_delete(i2c_cmd_handle);
|
||||
@ -174,13 +210,13 @@ esp_err_t zh_bh1750_adjust(const uint8_t value)
|
||||
i2c_cmd_handle = i2c_cmd_link_create();
|
||||
i2c_master_start(i2c_cmd_handle);
|
||||
i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true);
|
||||
i2c_master_write_byte(i2c_cmd_handle, (value & 0b00011111) | MEASUREMENT_TIME_LOW_BIT, true);
|
||||
i2c_master_write_byte(i2c_cmd_handle, sensitivity_data[1], true);
|
||||
i2c_master_stop(i2c_cmd_handle);
|
||||
esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS);
|
||||
i2c_cmd_link_delete(i2c_cmd_handle);
|
||||
#else
|
||||
uint8_t sensitivity_data[2] = {value >> 5 | MEASUREMENT_TIME_HIGH_BIT, (value & 0b00011111) | MEASUREMENT_TIME_LOW_BIT};
|
||||
esp_err = i2c_master_transmit(_bh1750_handle, sensitivity_data, sizeof(sensitivity_data), 1000 / portTICK_PERIOD_MS);
|
||||
esp_err = i2c_master_transmit(_bh1750_handle, &sensitivity_data[0], 1, 1000 / portTICK_PERIOD_MS);
|
||||
esp_err = i2c_master_transmit(_bh1750_handle, &sensitivity_data[1], 1, 1000 / portTICK_PERIOD_MS);
|
||||
#endif
|
||||
if (esp_err != ESP_OK)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user