GebraBit

APDS-9306 sensor project with Arduino

متن سربرگ خود را وارد کنید

APDS9306-project-2048x1463

APDS-9306 sensor project with Arduino

  1. Home
  2. »
  3. Projects
  4. »
  5. APDS-9306 sensor project with Arduino

What's the purpose of this project?

The purpose of this project is to interface the APDS-9306 ambient light sensor with an Arduino to measure and monitor light intensity in various environments. The sensor can detect ambient light levels and provide accurate lux values, making it suitable for applications like automatic lighting control, screen brightness adjustments, and environmental monitoring. By reading light levels in real-time, users can develop systems that react to changing light conditions for energy efficiency and enhanced usability.

What are we going to learn in this tutorial?

In this tutorial, you’ll learn how to:

  • Connect the APDS-9306 sensor to Arduino and establish I2C communication.
  • Modify an existing library to be compatible with Arduino, deepening understanding of I2C data handling.
  • Read ambient light levels in lux using the sensor, interpreting data for real-world applications.
  • Implement sensor-based projects for automated lighting and environmental monitoring, gaining practical skills for creating responsive systems based on light intensity measurements.

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 APDS9306 module to the Arduino UNO as follows:

Then download and add the GebraBit APDS9306 library to your Arduino IDE.

If you don’t know how to add GebraBit libraries to Arduino IDE, refer to the tutorial link below.

APDS9306 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_APDS9306.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 APDS9306 sensor and the configurations related to each of the APDS9306 sensor internal blocks are defined in the form of a “STRUCT” with the name GebraBit_APDS9306.     

USER REGISTER MAP

The registry map or sensor commands are defined in this section:

				
					/************************************************
 *              USER REGISTER MAP               *
 ***********************************************/ 
#define APDS9306_MAIN_CTRL  				0x00
#define APDS9306_ALS_MEAS_RATE  			0x04
#define APDS9306_ALS_GAIN 				    0x05
#define APDS9306_PART_ID 					0x06
#define APDS9306_MAIN_STATUS 				0x07
#define APDS9306_CLEAR_DATA_0 				0x0A
#define APDS9306_CLEAR_DATA_1 				0x0B
#define APDS9306_CLEAR_DATA_2 				0x0C
#define APDS9306_ALS_DATA_0 				0x0D
#define APDS9306_ALS_DATA_1 				0x0E
#define APDS9306_ALS_DATA_2 				0x0F
#define APDS9306_INT_CFG 					0x19
#define APDS9306_INT_PERSISTENCE 			0x1A
#define APDS9306_ALS_THRES_UP_0 			0x21
#define APDS9306_ALS_THRES_UP_1 			0x22
#define APDS9306_ALS_THRES_UP_2 			0x23
#define APDS9306_ALS_THRES_LOW_0 			0x24
#define APDS9306_ALS_THRES_LOW_1 			0x25
#define APDS9306_ALS_THRES_LOW_2 			0x26
#define APDS9306_ALS_THRES_VAR 				0x27	
#define APDS9306_ADDRESS 					0x52	
/*----------------------------------------------*
 *           USER REGISTER MAP End              *
 *----------------------------------------------*/ 
				
			

APDS-9306_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. }APDS9306_Ability;   
6.  

				
			

APDS-9306_ALS_Gain Enum

The values of this enum are used to set the sensor gain:

				
					1. typedef enum ALS_Gain 
2. {
3.   ALS_GAIN_1X  = 0,
4.   ALS_GAIN_3X  = 1,
5.   ALS_GAIN_6X  = 2,
6.   ALS_GAIN_9X  = 3,
7.   ALS_GAIN_18X = 4,
8. } APDS9306_ALS_Gain;
9.  

				
			

APDS-9306_ Interrupt_Channel Enum

To set the interruption source in the sensor, the values of this enum are used:

				
					1. typedef enum Interrupt_Channel 
2. {  
3. 	CLEAR_CHANNEL = 0 ,                      
4. 	ALS_CHANNEL     
5. }APDS9306_Interrupt_Channel;

				
			

APDS-9306_Resolution Enum

The values of this enum are used to select the sensor resolution:

				
					1. typedef enum Resolution
 2. {
 3.   _20_BIT_400_mS,
 4.   _19_BIT_200_mS,
 5.   _18_BIT_100_mS,
 6.   _17_BIT_50_mS ,
 7.   _16_BIT_25_mS ,
 8.   _13_BIT_3P125_mS
 9. } APDS9306_ALS_Resolution;
10.  

				
			

APDS-9306_ Measurement_Rate Enum

The values of this enum are used to define the measurement rate of sensor data values:

				
					1. typedef enum Measurement_Rate
 2. {
 3.   ALS_MEASRATE_25_mS,
 4.   ALS_MEASRATE_50_mS,
 5.   ALS_MEASRATE_100_mS,
 6.   ALS_MEASRATE_200_mS,
 7.   ALS_MEASRATE_500_mS,
 8.   ALS_MEASRATE_1000_mS,
 9.   ALS_MEASRATE_2000_mS,
10. } APDS9306_Measurement_Rate;
11.  

				
			

APDS-9306_Data_Status Enum

The values of this Enum determine whether the read data is new or old values:

				
					typedef enum Data_Status 
{  
	OLD_DATA = 0 ,                      
	NEW_DATA     
}APDS-9306_Data_Status;

				
			

APDS-9306_ Interrupt_Status Enum

The values of this Enum are used to inform about the interruption in the sensor:

				
					1. typedef enum Interrupt_Status 
2. {  
3. 	INTERRUPT_NOT_FULFILLED = 0 ,                      
4. 	INTERRUPT_FULFILLED     
5. }APDS9306_Interrupt_Status;
6.  

				
			

APDS-9306_Interrupt_Mode Enum

Using this enum, the sensor interrupt type is selected:

				
					1. typedef enum Interrupt_Mode 
2. {  
3. 	ALS_THRESHOLD_INTERRUPT = 0,                      
4. 	ALS_VARIATION_INTERRUPT     
5. }APDS9306_Interrupt_Mode;
6.  

				
			

APDS-9306_Power_Status Enum

The occurrence of an error in sensor supply power is determined using the values of this enum.

				
					1. typedef enum Power_Status 
2. {  
3. 	NO_POWER_ISSUE = 0 ,                      
4. 	POWER_ISSUE     
5. }APDS9306_Power_Status;
6.  

				
			

APDS-9306_Interrupt_Persist Enum

Using this enum, it is determined after how many repetitions of a mode, the interrupt should occur:

				
					typedef enum Interrupt_Persist
{
EVERY_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_2_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_3_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_4_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_5_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_6_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_7_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_8_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_9_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_10_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_11_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_12_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_13_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_14_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_15_ALS_VALUE_OUT_OF_THR_RANGE,
CONSECUTIVE_16_ALS_VALUE_OUT_OF_THR_RANGE,
} APDS-9306_Interrupt_Persist;

				
			

APDS-9306_Reset_Status Enum

By using this enum, the sensor reset status is determined:

				
					typedef enum 
{  
	FAILED = 0 ,                      
	DONE     
}APDS-9306_Reset_Status;

				
			

APDS-9306 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 APDS9306
 2. {
 3. 	  uint8_t                         Register_Cache;
 4. 	  uint8_t						  PART_ID;
 5. 	  APDS9306_Reset_Status			  RESET;
 6. 	  APDS9306_Ability                ALS;
 7. 	  APDS9306_ALS_Gain               ALS_GAIN;
 8. 	  float				              ALS_GAIN_VALUE;
 9. 	  APDS9306_Measurement_Rate       MEASUREMENT_RATE;
10. 	  APDS9306_ALS_Resolution         ALS_RESOLUTION;
11. 	  float							  ALS_RESOLUTION_TIME;
12.       APDS9306_Power_Status           POWER_STATUS;
13. 	  APDS9306_Data_Status            DATA;
14. 	  APDS9306_Ability                INTERRUPT;
15.       APDS9306_Interrupt_Channel      INTERRUPT_CHANNEL;
16. 	  APDS9306_Interrupt_Mode         INTERRUPT_MODE;
17.       APDS9306_Interrupt_Persist      INTERRUPT_PERSIST;
18. 	  APDS9306_Interrupt_Status		  INTERRRUPT_STATUS;
19. 	  uint32_t                        INTERRUPT_UPPER_THRESHOLD;
20. 	  uint32_t                        INTERRUPT_LOWER_THRESHOLD;
21. 	  uint8_t 						  REGISTER_DATA[REGISTER_DATA_BUFFER_SIZE];
22. 	  uint32_t               		  CLEAR_DATA;
23. 	  uint32_t               		  ALS_DATA;
24.       float 						  LUMINOSITY;
25. }GebraBit_APDS9306;
26.  

				
			

Declaration of functions

At the end of this file, all the functions for reading and writing in APDS-9306 registers, sensor configuration and receiving data from the sensor are declared:

				
					/********************************************************
 *  Declare Read&Write APDS9306 Register Values Functions *
 ********************************************************/
extern void GB_APDS9306_Read_Reg_Data(uint8_t regAddr,  uint8_t *data)	;
extern void GB_APDS9306_Burst_Read(uint8_t regAddr,  uint8_t *data, uint16_t byteQuantity);
extern void GB_APDS9306_Read_Reg_Bits (uint8_t regAddr, uint8_t start_bit, uint8_t len, uint8_t* data);	
extern void GB_APDS9306_Write_Command( uint8_t cmd);
extern void GB_APDS9306_Write_Reg_Data(uint8_t regAddr,  uint8_t data)	;
extern void GB_APDS9306_Burst_Write(uint8_t regAddr,  uint8_t *data, uint16_t byteQuantity)								;
extern void GB_APDS9306_Write_Reg_Bits(uint8_t regAddr, uint8_t start_bit, uint8_t len, uint8_t data);
/********************************************************
 *       Declare APDS9306 Configuration Functions         *
 ********************************************************/
extern void GB_APDS9306_Soft_Reset ( GebraBit_APDS9306 * APDS9306 )  ;
extern void GB_APDS9306_ALS ( GebraBit_APDS9306 * APDS9306 , APDS9306_Ability als );
extern void GB_APDS9306_ALS_Gain ( GebraBit_APDS9306 * APDS9306 , APDS9306_ALS_Gain gain )  ;
extern void GB_APDS9306_Measurement_Repeat_Rate ( GebraBit_APDS9306 * APDS9306 , APDS9306_Measurement_Rate rate )  ;
extern void GB_APDS9306_ALS_Resolution ( GebraBit_APDS9306 * APDS9306 , APDS9306_ALS_Resolution res ) ;
extern void GB_APDS9306_Read_Part_ID ( GebraBit_APDS9306 * APDS9306  )  ;
extern void GB_APDS9306_Read_STATUS ( GebraBit_APDS9306 * APDS9306 )   ;
extern void GB_APDS9306_Interrupt_Channel ( GebraBit_APDS9306 * APDS9306 , APDS9306_Interrupt_Channel intr );
extern void GB_APDS9306_Interrupt_Mode ( GebraBit_APDS9306 * APDS9306 , APDS9306_Interrupt_Mode mode )  ;
extern void GB_APDS9306_Interrupt( GebraBit_APDS9306 * APDS9306 , APDS9306_Ability intpt )  ;
extern void GB_APDS9306_Interrupt_Persist ( GebraBit_APDS9306 * APDS9306 , APDS9306_Interrupt_Persist persist )  ;
extern void GB_APDS9306_Interrupt_Upper_Threshold ( GebraBit_APDS9306 * APDS9306 , uint32_t upthr ) ;
extern void GB_APDS9306_Interrupt_Lower_Threshold ( GebraBit_APDS9306 * APDS9306 , uint16_t lothr )  ;
extern void GB_APDS9306_initialize( GebraBit_APDS9306 * APDS9306 )  ;
extern void GB_APDS9306_Configuration(GebraBit_APDS9306 * APDS9306)  ;
extern void GB_APDS9306_Get_Raw_Data(GebraBit_APDS9306 * APDS9306);
extern void GB_APDS9306_Luminosity_Reading(GebraBit_APDS9306 * APDS9306);
extern void GB_APDS9306_Get_Data(GebraBit_APDS9306 * APDS9306);
#endif
				
			

GebraBit_APDS-9306.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_APDS9306 > Luminosity

Description of Sample file

Enums and functions required by GebraBit APDS9306 module have been added to the structures. In the next part, a variable named APDS9306 of the GebraBit_APDS9306 structure type (this structure is in the GebraBit_APDS9306 header and is explained in the GebraBit_APDS9306 library description section) is defined for the configuration of the GebraBit APDS9306 module:  

				
					GebraBit_APDS9306 APDS9306;
				
			

In the next part of the written code, using the GB_APDS9306_initialize (&APDS9306) and GB_APDS9306_Configuration (&APDS9306) functions, we set the GebraBit APDS9306 module and finally, in the loop part of the program, the data is read from the sensor and the Luminosity values are continuously received:

				
					void setup() {
    Wire.begin();           // Initialize the I2C bus
    Serial.begin(9600);     // Initialize serial communication for debugging

    GB_APDS9306_initialize(&APDS9306); // Initialize the APDS9306 sensor
    GB_APDS9306_Configuration(&APDS9306); // Configure the APDS9306 sensor
}

void loop() {
    GB_APDS9306_Get_Data(&APDS9306); // Read data from the sensor
    
    Serial.print("luminosity: ");
    Serial.print(APDS9306.LUMINOSITY);
    Serial.println(" lx");
    
    delay(2000); // Delay between readings
}
				
			

The Sample file code text:

				
					#include "GebraBit_APDS9306.h"

GebraBit_APDS9306 APDS9306;

void setup() {
    Wire.begin();           // Initialize the I2C bus
    Serial.begin(9600);     // Initialize serial communication for debugging

    GB_APDS9306_initialize(&APDS9306); // Initialize the APDS9306 sensor
    GB_APDS9306_Configuration(&APDS9306); // Configure the APDS9306 sensor
}

void loop() {
    GB_APDS9306_Get_Data(&APDS9306); // Read data from the sensor
    
    Serial.print("luminosity: ");
    Serial.print(APDS9306.LUMINOSITY);
    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 luminosity values.

In the following, you can download the GebraBit_APDS9306 Library, the schematic of the modules and the “APDS9306 datasheet”.

Program output video

The video of the module operation will be uploaded soon

این مقاله را با دوستانتان به اشتراک بگذارید!

Be the first to write a review

Please help the Gebra team to improve the quality by sending comments and ratings

Your email address will not be published. Required fields are marked *

Shopping cart
Start typing to see posts you are looking for.

Sign in

No account yet?