BH1750 Light Intensity Detection Sensor
Uses the original ROHM BH1750FVI chip. Power supply: 3-5V, illuminance range: 0-65535lx. The sensor has a built-in 16-bit AD converter with direct digital output, omitting complex calculations and calibration. It does not distinguish ambient light sources and has spectral characteristics close to visual sensitivity, enabling high-precision measurement of a wide range of brightness at 1 lux. Standard NXP IIC communication protocol; the module includes communication level shifting inside, allowing direct connection to 5V microcontroller IO.
Module Source
Purchase link: https://detail.tmall.com/item.htm?_u=52t4uge534a7&id=598334245306&spm=a1z09.2.0.0.47582e8dxs1caw Materials download link: https://pan.baidu.com/s/13bVmmj0eM22mT8pBusjIyQ?pwd=8889 Extraction code: 8889
Specifications
Operating voltage: 3-5V Operating current: 200uA Detection range: 1~65536 lx Module dimensions: 32.6mm x 15.2mm x 11.6mm Output method: IIC Number of pins: 5 Pin
View Materials
Measurement steps:
- After the module is powered on, it enters power-down mode. It needs to be started by sending a Power On command via IIC.
- After the module is started, send a measurement command via IIC to perform measurement.
- The measurement commands include single-shot measurement and continuous measurement. After the measurement is completed, it enters power-down mode again.
The values corresponding to each command are shown in the table below. The ones we use are: Power On (0x01): Start the module and have it wait for a measurement command. Continuously H-Resolution Mode (0X10): Start measurement with 1 LX resolution. The measurement time is typically 120ms (the manual recommends using this command). One Time H-Resolution Mode (0X20): Start measurement with 1 lx resolution. The measurement time is typically 120ms. After completion, the system automatically sets to "power-down" mode.
Send sequence: Start signal -> Send device address + write -> Wait for module ACK -> Send command -> Wait for module ACK -> Stop signal. Read sequence: Start signal -> Send device address + read -> Wait for module ACK -> Receive high 8 bits of data -> Master sends ACK -> Receive low 8 bits of data -> Master sends NACK -> Stop signal. After reading is complete, combine the high and low bits of the data and divide by 1.2 to obtain the light intensity data.
Porting Process
Pin Selection
Port to Project
Our goal is to port the example to the ESP32-S3 dev board. Complete driver code has been provided for you. Follow the steps below to complete the porting. For detailed instructions on creating folders and new .c and .h files, refer to section 1.4.2 in the [DHT11 Temperature and Humidity Sensor] chapter; we will not repeat it here. The only difference is that we replace the file names bsp_dht11.c and bsp_dht11.h with bsp_bh1750.c and bsp_bh1750.h, and rename the folder to BH1750.
Write Code
In the file bsp_bh1750.c, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-05 LCKFB-lp first version
*/
#include "bsp_bh1750.h"
#include "stdio.h"
unsigned char BUF[8]; //Receive data buffer
void delay_ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
void delay_us(unsigned int us)
{
ets_delay_us(us);
}
void delay_1ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
void delay_1us(unsigned int us)
{
ets_delay_us(us);
}
/******************************************************************
* Function Name: GY30_GPIO_Init
* Function Description: MLX90614 pin initialization
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void GY30_GPIO_Init(void)
{
gpio_config_t lll_config = {
.pin_bit_mask = (1ULL<<BH1750_SCL_PIN)|(1ULL<<BH1750_SDA_PIN), // Configure pins
.mode =GPIO_MODE_OUTPUT, // Output mode
.pull_up_en = GPIO_PULLUP_ENABLE, // Enable pull-up
.pull_down_en = GPIO_PULLDOWN_DISABLE, // Disable pull-down
.intr_type = GPIO_INTR_DISABLE // Disable pin interrupt
};
gpio_config(&lll_config);
}
/******************************************************************
* Function Name: IIC_Start
* Function Description: IIC start sequence
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void IIC_Start(void)
{
SDA_OUT();
SDA(1);
delay_us(5);
SCL(1);
delay_us(5);
SDA(0);
delay_us(5);
SCL(0);
delay_us(5);
}
/******************************************************************
* Function Name: IIC_Stop
* Function Description: IIC stop signal
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void IIC_Stop(void)
{
SDA_OUT();
SCL(0);
SDA(0);
SCL(1);
delay_us(5);
SDA(1);
delay_us(5);
}
/******************************************************************
* Function Name: IIC_Send_Ack
* Function Description: Master sends ACK or NACK signal
* Function Parameters: 0=send ACK 1=send NACK
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void IIC_Send_Ack(unsigned char ack)
{
SDA_OUT();
SCL(0);
SDA(0);
delay_us(5);
if(!ack) SDA(0);
else SDA(1);
SCL(1);
delay_us(5);
SCL(0);
SDA(1);
}
/******************************************************************
* Function Name: I2C_WaitAck
* Function Description: Wait for slave acknowledge
* Function Parameters: None
* Function Return: 0=ACK received 1=timeout, no ACK
* Author: LC
* Notes: None
******************************************************************/
unsigned char I2C_WaitAck(void)
{
char ack = 0;
unsigned char ack_flag = 10;
SCL(0);
SDA(1);
SDA_IN();
delay_us(5);
SCL(1);
delay_us(5);
while( (SDA_GET()==1) && ( ack_flag ) )
{
ack_flag--;
delay_us(5);
}
if( ack_flag <= 0 )
{
IIC_Stop();
return 1;
}
else
{
SCL(0);
SDA_OUT();
}
return ack;
}
/******************************************************************
* Function Name: Send_Byte
* Function Description: Write one byte
* Function Parameters: dat=data to write
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void Send_Byte(uint8_t dat)
{
int i = 0;
SDA_OUT();
SCL(0);//Pull clock low to start data transfer
for( i = 0; i < 8; i++ )
{
SDA( (dat & 0x80) >> 7 );
delay_us(1);
SCL(1);
delay_us(5);
SCL(0);
delay_us(5);
dat<<=1;
}
}
/******************************************************************
* Function Name: Read_Byte
* Function Description: IIC read sequence
* Function Parameters: None
* Function Return: Data read
* Author: LC
* Notes: None
******************************************************************/
unsigned char Read_Byte(void)
{
unsigned char i,receive=0;
SDA_IN();//Set SDA as input
for(i=0;i<8;i++ )
{
SCL(0);
delay_us(5);
SCL(1);
delay_us(5);
receive<<=1;
if( SDA_GET() )
{
receive|=1;
}
delay_us(5);
}
SCL(0);
return receive;
}
/******************************************************************
* Function Name: Single_Write
* Function Description: Write command to BH1750
* Function Parameters: REG_Address=command to write
* Function Return: 0=write success 1=device address error (module not recognized) 2=command error
* Author: LC
* Notes: None
******************************************************************/
char Single_Write_BH1750(uint8_t REG_Address)
{
IIC_Start(); //Start signal
Send_Byte(SlaveAddress); //Send device address + write
if( I2C_WaitAck() != 0 )return 1;
Send_Byte(REG_Address); //Internal register address
if( I2C_WaitAck() != 0 )return 2;
IIC_Stop(); //Send stop signal
return 0;
}
/******************************************************************
* Function Name: Multiple_read_BH1750
* Function Description: Read internal data of BH1750
* Function Parameters: None
* Function Return: Illuminance, unit: lx
* Author: LC
* Notes: None
******************************************************************/
float Multiple_read_BH1750(void)
{
uint16_t dis_data=0;
uint8_t dat_buff[2];
IIC_Start(); //Start signal
Send_Byte(SlaveAddress+1); //Send device address + read
I2C_WaitAck();
dat_buff[0] = Read_Byte(); //Read high 8 bits
IIC_Send_Ack(0); //Respond ACK
dat_buff[1] = Read_Byte(); //Read low 8 bits
IIC_Send_Ack(1); //Respond NACK
IIC_Stop(); //Stop signal
//Combine data, i.e. illuminance data
dis_data=( dat_buff[0] << 8 ) + dat_buff[1];
return ((float)dis_data/1.2);
}
/******************************************************************
* Function Name: GY30_Init
* Function Description: Initialize BH1750
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void GY30_Init(void)
{
GY30_GPIO_Init();
Single_Write_BH1750(0x01);//Power on
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
In the file bsp_bh1750.h, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-05 LCKFB-lp first version
*/
#ifndef _BSP_GY30_H_
#define _BSP_GY30_H_
#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_rom_sys.h"
#include "esp_timer.h"
#include "driver/uart.h"
#include "rom/ets_sys.h"
#include "esp_system.h"
#include "driver/gptimer.h"
#include "esp_log.h"
#include "freertos/queue.h"
#include "driver/spi_master.h"
#include "nvs_flash.h"
#include "esp_adc/adc_cali_scheme.h"
#include "esp_adc/adc_cali.h"
#include "driver/adc.h"
#include "esp_adc_cal.h"
//Port porting
#define BH1750_SCL_PIN 1
#define BH1750_SDA_PIN 2
//Set SDA output mode
#define SDA_OUT() gpio_set_direction(BH1750_SDA_PIN,GPIO_MODE_OUTPUT)
//Set SDA input mode
#define SDA_IN() gpio_set_direction(BH1750_SDA_PIN,GPIO_MODE_INPUT)
//Get SDA pin level change
#define SDA_GET() gpio_get_level(BH1750_SDA_PIN)
//SDA and SCL output
#define SDA(x) gpio_set_level(BH1750_SDA_PIN, (x?1:0))
#define SCL(x) gpio_set_level(BH1750_SCL_PIN, (x?1:0))
#define SlaveAddress 0x46 //Define the slave address of the device on the IIC bus; modify according to the ALT ADDRESS address pin
//When the ALT ADDRESS pin is grounded, the address is 0x46; when connected to power, the address is 0xB8
float Multiple_read_BH1750(void);
char Single_Write_BH1750(uint8_t REG_Address);
void GY30_Init(void);
void delay_us(unsigned int us);
void delay_ms(unsigned int ms);
void delay_1us(unsigned int us);
void delay_1ms(unsigned int ms);
#endif2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Porting Verification
In the main function of your own project, write the following.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-05 LCKFB-lp first version
*/
#include <stdio.h>
#include "bsp_bh1750.h"
void app_main(void)
{
float temp;
GY30_Init();
printf("start\r\n");
while(1)
{
Single_Write_BH1750(0x10); // Continuous high-resolution mode measurement
delay_1ms(180); //Measurement typically takes 120ms
temp = Multiple_read_BH1750(); //Read out data
printf("illuminance = %.2f lx\r\n",temp);
delay_1ms(500);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Power-on effect
Driver code:
File Download
📌 Materials Download Center (click to jump)
📌 In the Materials Download Center -> Module Porting Materials Download, inside the compressed package of this chapter.