Ambient Light Sensor On STM32
Preface
Ambient Light Sensor (ALS) is a type of photosensitive device, mainly used in mobile phones, laptops, tablets and other display panels. Currently, AMS_OSRAM has launched a new ALS SFH 5721
The SFH5721 Digital Ambient Light Sensor combines an array of photodiodes and wide dynamic
range readout channels to enable ambient light and infrared sensing.
Features:
- Package: clear epoxy
- ESD: 2 kV acc. to ANSI/ESDA/JEDEC JS-001 (HBM)
- Adapted to human eye sensitivity (Vλ)
- Integrated and independent Infrared Detection Channel
- I²C Interface
- supports two I²C slave addresses
- Zero dark count value
- 16 Bit ADC resolution
Block diagram
SFH5721
Nucleo-F401RE
Connection layout
Please note that this board operates on 3.3V, not 5V, and the I2C pins on the sensor board have resistors. If you are using a custom PCB, make sure to add the appropriate resistors.
Register setting Flow
PS:Note that the DEVID may vary, and it is necessary to confirm whether it is 0x00 or 0x01 in the Datasheet.
Sensor Initialization
void Rest_all(){
uint8_t x=1;
x=read_register(DEVID);
if(x==0x00){
enableSFH5721.RESET=1;
write_register(OPSEL,*((uint8_t*)(&enableSFH5721)));
printf("reset ok\n\r");
enableSFH5721.RESET=0;
}
else
{printf("connect fail\n\r");
printf("DEVID=%#x\n\r",x);
for(;;);}
};
This sensor, like other sensors, needs to be initialized first to avoid errors when writing I2C data later on.
Sensor setting
There are several parameters that can be set here to affect the sensitivity of the sensor to light, including AGAIN, DGAIN, and integration time. The AGAIN and integration time mainly affect the sensitivity and maximum light sensitivity, as shown in the figure below. The linear range is mainly taken
Divide the setting program into three parts as follows
setAnalogGan(0x01,0x07);//Analog gain is 4 ,Integration time is 25ms(0x01,0x07)
setDigitalGan(0x00,0x00,1);//Digital gain is 1 ,Wait time 0ms
setlowpassfilter(0x01,ALS);//Digital lowpass filter depth is 3(ALS,0x01)
For AnalogGan
void setAnalogGan(uint8_t Analog_Gan,uint8_t Integration_time)
{
uint8_t feedback=0;
GanSFH5721.AGAIN=Analog_Gan;
GanSFH5721.IT=Integration_time;
write_register(MCONFA,*((uint8_t*)(&GanSFH5721)));
feedback=read_register(MCONFA);
printf("AGAIN=%x \n\r",feedback);
}
DigitalGan
void setAnalogGan(uint8_t Analog_Gan,uint8_t Integration_time)
{
uint8_t feedback=0;
GanSFH5721.AGAIN=Analog_Gan;
GanSFH5721.IT=Integration_time;
write_register(MCONFA,*((uint8_t*)(&GanSFH5721)));
feedback=read_register(MCONFA);
printf("AGAIN=%x \n\r",feedback);
}
Data Getting
After initialization, we can obtain the ALS and IR data. The dark data is used for zero point correction because there may be some deviations due to temperature and complete darkness. We can use the dark data to make corrections.
Below is an example of capturing ALS data (IR and Dark can use this as a template).
uint16_t get_ALS(){
uint8_t x=0;
x=ReadStatus();
if(x==3|| x==7 || x==6 || x==8){
uint8_t buffer[2];
uint16_t DATA=0;
buffer[0]=read_register(DATA3L);
buffer[1]=read_register(DATA3H);
DATA= buffer[1];
DATA = DATA << 8;
DATA += buffer[0];
return DATA;}
else if(x==4)
{printf("data not ready\n\r");
return 0;}
}
ALS and IR Calibration
Therefore, you can refer to examples from other manufacturers in the following table to construct a linear relationship between Lux and IR (there is no standard answer for this part).
This is my method for reference.
Reference for calibration process
- Measure outdoor Lux meter under 50Klux sunlight and calculate the values of Ev and Ee (using the formula above) for SFH5721.
- Repeat step one in different areas to obtain at least 5 data samples.
- Using the 2D formula Z=aY+bX+C, where Z represents the meter value, Y represents Ev, and X represents Ee, refer to the table above.
- After establishing the standard formula table, the actual Lux value can be immediately obtained when obtaining ALS and IR Count values.
- Note that the Dark value is used separately in a dark room or temperature calibration to measure and reset it to zero.
Refer
I highly recommend the h-file coding of DFRobot_TCS3430 here, which can effectively distinguish the areas to be stored temporarily. The I2C read and write operations can refer to the coding method discussed in previous articles.