What's the purpose of this project?
The purpose of this project is to interface the BU27006MUCZ ambient light sensor with an Arduino to measure and monitor light intensity in various environments. The sensor provides accurate lux readings, making it suitable for applications like automatic lighting control, screen brightness adjustments, and environmental monitoring. By reading light intensity data in real-time, users can develop systems that respond to changing light conditions for enhanced energy efficiency and usability in smart environments.
What are we going to learn in this tutorial?
In this tutorial, you’ll learn how to:
- Connect the BU27006 ambient light sensor to an Arduino and establish I2C communication.
- Utilize an existing library for BU27006 compatibility with Arduino, gaining insights into I2C data handling.
- Measure ambient light levels in lux using the sensor, interpreting the data for practical applications.
- Implement sensor-based projects for automated lighting control and environmental monitoring, building skills to create responsive systems based on light intensity.
This hands-on guide provides insights into sensor integration and real-time data monitoring with Arduino.
What do we need to start this project?
As you probably know, we need some hardware and software to do this project. The titles of these hardware and software are provided to you in the table below and you can prepare/download by clicking on each of them and get ready to start.
Required hardware
|
Required software
|
---|---|
Arduino UNO
|
|
First as shown in the image below, we connect the GebraBit BU27006MUCZ module to the Arduino UNO as follows:
Then download and add the GebraBit BU27006MUCZ library to your Arduino IDE.
Required Library
|
|
---|---|
If you don’t know how to add GebraBit libraries to Arduino IDE, refer to the tutorial link below.
BU27006MUCZ library and driver
In addition to the modular design of various sensors and ICs, GebraBit tries to provide variety of structured and hardware-independent libraries in C language for the ease of users in setting up and developing software.
For this purpose, after preparing each GebraBit module, the users can refer to the “tutorial” section of the desired module and download the dedicated library, which contains the “ .h” and “ .c” file (Header and Source) and a sample training program under “GebraBit STM32F303”, “GebraBit ATMEGA32A” or “Arduino” development boards.
All the defined functions and structures in the library are commented in full detail and all the received parameters in the arguments of the functions and their return values, are briefly explained. Since the libraries are hardware independed, the user can easily add the library in any of their favorite compilers and develop it by desired microcontroller and development board.
GebraBit_BU2700MUCZ.h header file
In this file, based on the datasheet of the sensor or IC, all address registers, the values of each register are defined in the form of “Enumeration”. Also, the casing of the BU27006MUCZ sensor and the configurations related to each of the BU27006MUCZ sensor internal blocks are defined in the form of a “STRUCT” with the name GebraBit_BU27006MUCZ.
USER REGISTER MAP
The registry map or sensor commands are defined in this section:
/************************************************
* USER REGISTER MAP *
***********************************************/
#define BU27006MUCZ_SYSTEM_CONTROL 0x40
#define BU27006MUCZ_MODE_CONTROL1 0x41
#define BU27006MUCZ_MODE_CONTROL2 0x42
#define BU27006MUCZ_MODE_CONTROL3 0x43
#define BU27006MUCZ_RED_DATA 0x50 // 2 byte
#define BU27006MUCZ_GREEN_DATA 0x52 // 2 byte
#define BU27006MUCZ_BLUE_DATA 0x54 // 2 byte
#define BU27006MUCZ_IR_DATA 0x56 // 2 byte
#define BU27006MUCZ_FLICKER_DATA 0x58 // 2 byte
#define BU27006MUCZ_FLICKER_COUNTER 0x5A
#define BU27006MUCZ_FIFO_LEVEL 0x5B
#define BU27006MUCZ_FIFO_DATA 0x5C // 2 byte
#define BU27006MUCZ_MANUFACTURER_ID 0x92
#define BU27006MUCZ_ADDRESS 0x38
/*----------------------------------------------*
* USER REGISTER MAP End *
*----------------------------------------------*/
BU27006MUCZ _Ability Enum
This enum is used to activate and deactivate different parts of the sensor:
1. typedef enum Ability
2. {
3. Disable = 0 ,
4. Enable
5. }BU27006MUCZ_Ability;
6.
BU27006MUCZ _Reset_Status Enum
By using this enum, the sensor reset status is determined:
typedef enum
{
FAILED = 0 ,
DONE
}BU27006MUCZ_Reset_Status;
BU27006MUCZ_ RGB_Gain Enum
The values of this enum are used to set the RGB gain of the sensor:
1. typedef enum RGB_Gain
2. {
3. RGB_GAIN_1X = 0,
4. RGB_GAIN_4X = 1,
5. RGB_GAIN_32X = 2,
6. RGB_GAIN_128X = 3,
7. } BU27006MUCZ_RGB_Gain;
8.
BU27006MUCZ_FLC_Gain Enum
The values of this enum are used to set the sensor FLC gain:
1. typedef enum FLC_Gain
2. {
3. FLC_GAIN_1X = 0,
4. FLC_GAIN_2X = 1,
5. FLC_GAIN_4X = 2,
6. FLC_GAIN_8X = 3,
7. FLC_GAIN_16X = 4,
8. FLC_GAIN_32X = 5
9. } BU27006MUCZ_FLC_Gain;
10.
BU27006MUCZ_Interrupt_Channel Enum
To set the source of interruption in the sensor, the values of this enum are used:
1. typedef enum Interrupt_Channel
2. {
3. CLEAR_CHANNEL = 0 ,
4. ALS_CHANNEL
5. }BU27006MUCZ_Interrupt_Channel;
BU27006MUCZ_Interrupt_Mode Enum
Using this enum, the sensor interrupt type is selected:
1. typedef enum Interrupt_Mode
2. {
3. INTERRUPT_DISABLE ,
4. RGB_IR_COMPELETION ,
5. FLICKER_COMPELETION,
6. FIFO_64_DATA_READY
7. } BU27006MUCZ_Interrupt_Mode;
8.
BU27006MUCZ_RGB_Measurement_Mode Enum
To specify the RGB measurement mode of the sensor, the values of this enum are used:
1. typedef enum RGB_Measurement_Mode
2. {
3. _55_mS_MODE = 1,
4. _100_mS_MODE = 2
5. } BU27006MUCZ_RGB_Measurement_Mode;
6.
BU27006MUCZ_FLC_Measurement_Mode Enum
The values of this enum are used to specify the FLC measurement mode of the sensor:
1. typedef enum FLC_Measurement_Mode
2. {
3. _1_KHZ_MODE,
4. _2_KHZ_MODE
5. } BU27006MUCZ_FLC_Measurement_Mode;
6.
BU27006MUCZ_Data_Status Enum
The values of this enum determine whether the read data is updated or not:
1. typedef enum Data_Status
2. {
3. NOT_UPDATED = 0 ,
4. UPDATED
5. }BU27006MUCZ_Data_Status;
6.
BU27006MUCZ_Interrupt_Status Enum
The values of this Enum are used to know whether the interrupt in the sensor is fulfilled or not.
1. typedef enum Interrupt_Status
2. {
3. INTERRUPT_NOT_FULFILLED = 0 ,
4. INTERRUPT_FULFILLED
5. }BU27006MUCZ_Interrupt_Status;
6.
BU27006MUCZ struct
All sensor properties, calibration coefficients and sensor data are defined in this “struct” and All the information and configuration implemented on the sensor are stored in this “structure” and you can see the changes in each part of the sensor in the “Debug Session” environment.
1. typedef struct BU27006MUCZ
2. {
3. uint8_t Register_Cache;
4. uint8_t PART_ID;
5. uint8_t MANUFACTURER_ID;
6. BU27006MUCZ_Reset_Status RESET;
7. BU27006MUCZ_Ability RGB_IR;
8. BU27006MUCZ_Ability FLC;
9. BU27006MUCZ_RGB_Gain RGB_GAIN;
10. BU27006MUCZ_RGB_Measurement_Mode RGB_MEASUREMENT_MODE;
11. BU27006MUCZ_FLC_Gain FLC_GAIN;
12. BU27006MUCZ_FLC_Measurement_Mode FLC_MEASUREMENT_MODE;
13. float ALS_RESOLUTION_TIME;
14. BU27006MUCZ_Data_Status RGB_DATA;
15. BU27006MUCZ_Data_Status FLC_DATA;
16. BU27006MUCZ_Interrupt_Mode INTERRUPT_MODE;
17. BU27006MUCZ_Interrupt_Channel INTERRUPT_CHANNEL;
18. uint8_t FLICKER_COUNTER;
19. uint8_t FIFO_LEVEL;
20. BU27006MUCZ_Interrupt_Status INTERRRUPT_STATUS;
21. uint32_t INTERRUPT_UPPER_THRESHOLD;
22. uint32_t INTERRUPT_LOWER_THRESHOLD;
23. uint8_t REGISTER_DATA[REGISTER_DATA_BUFFER_SIZE];
24. uint16_t RED_DATA;
25. uint16_t GREEN_DATA;
26. uint16_t BLUE_DATA;
27. uint16_t IR_DATA;
28. uint16_t FLICKER_DATA;
29. uint16_t FIFO_DATA[FIFO_DATA_BUFFER_SIZE];
30. float RED_LUX;
31. float GREEN_LUX;
32. float BLUE_LUX;
33. }GebraBit_BU27006MUCZ;
34.
Declaration of functions
At the end of this file, all the functions for reading and writing in BU27006MUCZ registers, sensor configuration and receiving data from the sensor are declared:
1. extern void GB_BU27006MUCZ_Read_Reg_Data(uint8_t regAddr, uint8_t *data) ;
2. extern void GB_BU27006MUCZ_Burst_Read(uint8_t regAddr, uint8_t *data, uint16_t byteQuantity);
3. extern void GB_BU27006MUCZ_Read_Reg_Bits (uint8_t regAddr, uint8_t start_bit, uint8_t len, uint8_t* data);
4. extern void GB_BU27006MUCZ_Write_Command( uint8_t cmd);
5. extern void GB_BU27006MUCZ_Write_Reg_Data(uint8_t regAddr, uint8_t data) ;
6. extern void GB_BU27006MUCZ_Burst_Write(uint8_t regAddr, uint8_t *data, uint16_t byteQuantity) ;
7. extern void GB_BU27006MUCZ_Write_Reg_Bits(uint8_t regAddr, uint8_t start_bit, uint8_t len, uint8_t data);
8. /********************************************************
9. * Declare BU27006MUCZ Configuration Functions *
10. ********************************************************/
11. extern void GB_BU27006MUCZ_Soft_Reset ( GebraBit_BU27006MUCZ * BU27006MUCZ ) ;
12. extern void GB_BU27006MUCZ_RGB_Gain ( GebraBit_BU27006MUCZ * BU27006MUCZ , BU27006MUCZ_RGB_Gain gain ) ;
13. extern void GB_BU27006MUCZ_RGB_Measurement_Mode ( GebraBit_BU27006MUCZ * BU27006MUCZ , BU27006MUCZ_RGB_Measurement_Mode mode ) ;
14. extern void GB_BU27006MUCZ_FLC_Measurement_Mode ( GebraBit_BU27006MUCZ * BU27006MUCZ , BU27006MUCZ_FLC_Measurement_Mode mode ) ;
15. extern void GB_BU27006MUCZ_FLC_Gain ( GebraBit_BU27006MUCZ * BU27006MUCZ , BU27006MUCZ_FLC_Gain gain ) ;
16. extern void GB_BU27006MUCZ_RGB_IR ( GebraBit_BU27006MUCZ * BU27006MUCZ , BU27006MUCZ_Ability rgb ) ;
17. extern void GB_BU27006MUCZ_FLC ( GebraBit_BU27006MUCZ * BU27006MUCZ , BU27006MUCZ_Ability flc );
18. extern void GB_BU27006MUCZ_Interrupt(GebraBit_BU27006MUCZ * BU27006MUCZ , BU27006MUCZ_Interrupt_Mode mode) ;
19. extern void GB_BU27006MUCZ_Check_Data_Updated ( GebraBit_BU27006MUCZ * BU27006MUCZ ) ;
20. extern void GB_BU27006MUCZ_Part_ID ( GebraBit_BU27006MUCZ * BU27006MUCZ ) ;
21. extern void GB_BU27006MUCZ_Manufacture_ID ( GebraBit_BU27006MUCZ * BU27006MUCZ ) ;
22. extern void GB_BU27006MUCZ_Flicker_Counter ( GebraBit_BU27006MUCZ * BU27006MUCZ ) ;
23. extern void GB_BU27006MUCZ_FIFO_Level ( GebraBit_BU27006MUCZ * BU27006MUCZ ) ;
24. extern void GB_BU27006MUCZ_Read_FIFO_Flicker_Data ( GebraBit_BU27006MUCZ * BU27006MUCZ );
25. extern void GB_BU27006MUCZ_initialize( GebraBit_BU27006MUCZ * BU27006MUCZ ) ;
26. extern void GB_BU27006MUCZ_Configuration(GebraBit_BU27006MUCZ * BU27006MUCZ) ;
27. extern void GB_BU27006MUCZ_Read_RGB_IR_FLICKER(GebraBit_BU27006MUCZ * BU27006MUCZ);
28. extern void GB_BU27006MUCZ_Color_Luminosity(GebraBit_BU27006MUCZ * BU27006MUCZ);
29. extern void GB_BU27006MUCZ_Get_Data(GebraBit_BU27006MUCZ * BU27006MUCZ);
30.
GebraBit_BU27006MUCZ.c source file
In this file, which is written in C language, all the functions are commented in full detail, and all the parameters received in the arguments of the functions and their return values are clearly explained so we confine to these explanations and invite users to check this file directly for more information.
Sample program in Arduino
After connecting the module to Arduino and adding the library to the IDE, go to the following path: File > Examples > GebraBit_BU27006MUCZ > RGB
Description of Sample file
Enums and functions required by GebraBit BU27006MUCZ module have been added to the structures. In the next part, a variable named BU27006MUCZ of the GebraBit_BU27006MUCZ structure type (this structure is in the GebraBit_BU27006MUCZ header and is explained in the GebraBit_BU27006MUCZ library description section) is defined for the configuration of the GebraBit BU27006MUCZ module:
GebraBit_BU27006MUCZ BU27006MUCZ;
In the next part of the written code, using the GB_BU27006MUCZ_initialize (&BU27006MUCZ_Module) and GB_BU27006MUCZ_Configuration (&BU27006MUCZ_Module) functions, we set the GebraBit BU27006MUCZ module and finally, in the while part of the program, the data is read from the sensor and the RED, BLUE, GREEN, IR and Flicker values are continuously received:
void setup() {
Wire.begin(); // Initialize the I2C bus
Serial.begin(9600); // Initialize serial communication for debugging
GB_BU27006MUCZ_initialize(&BU27006MUCZ); // Initialize the BU27006MUCZ sensor
GB_BU27006MUCZ_Configuration(&BU27006MUCZ); // Configure the BU27006MUCZ sensor
}
void loop() {
GB_BU27006MUCZ_Get_Data(&BU27006MUCZ); // Read data from the sensor
Serial.print("RED: ");
Serial.print(BU27006MUCZ.RED_LUX);
Serial.println(" lx");
Serial.print("GREEN: ");
Serial.print(BU27006MUCZ.GREEN_LUX);
Serial.println(" lx");
Serial.print("BLUE: ");
Serial.print(BU27006MUCZ.BLUE_LUX);
Serial.println(" lx");
delay(2000); // Delay between readings
}
The Sample file code text:
#include "GebraBit_BU27006MUCZ.h"
GebraBit_BU27006MUCZ BU27006MUCZ;
void setup() {
Wire.begin(); // Initialize the I2C bus
Serial.begin(9600); // Initialize serial communication for debugging
GB_BU27006MUCZ_initialize(&BU27006MUCZ); // Initialize the BU27006MUCZ sensor
GB_BU27006MUCZ_Configuration(&BU27006MUCZ); // Configure the BU27006MUCZ sensor
}
void loop() {
GB_BU27006MUCZ_Get_Data(&BU27006MUCZ); // Read data from the sensor
Serial.print("RED: ");
Serial.print(BU27006MUCZ.RED_LUX);
Serial.println(" lx");
Serial.print("GREEN: ");
Serial.print(BU27006MUCZ.GREEN_LUX);
Serial.println(" lx");
Serial.print("BLUE: ");
Serial.print(BU27006MUCZ.BLUE_LUX);
Serial.println(" lx");
delay(2000); // Delay between readings
}
Connect your arduino to computer and select your Arduino Board
Then Verify and Upload the Sample code
After uploading the code, open the serial monitor and you can see the Sensor values.
In the following, you can download the GebraBit_BU27006MUCZ Library, the schematic of the modules and the “BU27006MUCZ datasheet”.
Program output video
The video of the module operation will be uploaded soon