ESP8266-Arduino编程实例-APDS-9930环境光和趋近感器驱动

APDS-9930环境光和趋近感器驱动

1、APDS-9930介绍

APDS-9930 在单个 8 引脚封装中提供数字环境光感应 (ALS)、IR LED 和完整的接近检测系统。 接近功能提供 100 毫米的即插即用检测(无前玻璃),因此无需对终端设备或子组件进行工厂校准。 接近检测功能从明亮的阳光到黑暗的房间都能很好地工作。 宽动态范围还允许在手机等深色玻璃后面进行短距离检测。

此外,内部状态机能够在 ALS 和接近测量之间将设备置于低功耗模式,从而提供非常低的平均功耗。 ALS 在非常低的光照条件下或在深色面板后面提供对光强度的明视响应。

APDS-9930具有如下特点:

  • 环境光感应 (ALS)
  • 近似人眼反应
  • 具有上限和下限阈值的可编程中断功能
  • 高达 16 位分辨率
  • 高灵敏度在深色玻璃后面操作
  • 0.01 勒克斯的低勒克斯性能

2、硬件准备

  • ESP8266 NodeMCU开发板一块
  • APDS-9930模块一个
  • 面板板一个
  • 杜邦线若干
  • 数据线一条

硬件接线如下:

在这里插入图片描述

3、软件准备

  • Arduino IDE或VSCode + PlatformIO

在前面的文章中,对如何搭建ESP8266开发环境做了详细的介绍,请参考:

ESP8266 NodeMCU的引脚介绍在前面的文章中做了详细的介绍,请参考:

4、代码实现

本次使用的APDS-9930驱动库如下:

4.1 光线数据采集

1)导入依赖库头文件

1
2
3
4
5
6
7
8
9
#include <Wire.h>
#include <APDS9930.h>

// 创建设备
APDS9930 apds = APDS9930();
float ambient_light = 0; // can also be an unsigned long
uint16\_t ch0 = 0;
uint16\_t ch1 = 1;

2)设备初始化

1
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
void setup() {
// 串口初始化
Serial.begin(9600);
Serial.println();
Serial.println(F("--------------------------------"));
Serial.println(F("APDS-9930 - Ambient light sensor"));
Serial.println(F("--------------------------------"));

// 初始化 APDS-9930
if ( apds.init() ) {
Serial.println(F("APDS-9930 initialization complete"));
} else {
Serial.println(F("Something went wrong during APDS-9930 init!"));
}

// 启动APDS9330(无中断)
if ( apds.enableLightSensor(false) ) {
Serial.println(F("Light sensor is now running"));
} else {
Serial.println(F("Something went wrong during light sensor init!"));
}

// 等待设备初始化和校正完成
delay(500);
}

2)数据采集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void loop() {

// 读取光线数据 (ambient, red, green, blue)
if ( !apds.readAmbientLightLux(ambient_light) ||
!apds.readCh0Light(ch0) ||
!apds.readCh1Light(ch1) ) {
Serial.println(F("Error reading light values"));
} else {
Serial.print(F("Ambient: "));
Serial.print(ambient_light);
Serial.print(F(" Ch0: "));
Serial.print(ch0);
Serial.print(F(" Ch1: "));
Serial.println(ch1);
}

// 等待1s
delay(1000);
}

4.2 趋近数据采集

1)导入依赖库头文件

1
2
3
4
5
6
7
#include <Wire.h>
#include <APDS9930.h>

// 创建设备
APDS9930 apds = APDS9930();
uint16\_t proximity_data = 0;

2)设备初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void setup() {
// 串口初始化
Serial.begin(9600);
Serial.println();
Serial.println(F("---------------------------"));
Serial.println(F("APDS-9930 - ProximitySensor"));
Serial.println(F("---------------------------"));

// 初始化APDS-9930
if ( apds.init() ) {
Serial.println(F("APDS-9930 initialization complete"));
} else {
Serial.println(F("Something went wrong during APDS-9930 init!"));
}


// 启动APDS-9930趋近传感器(无中断)
if ( apds.enableProximitySensor(false) ) {
Serial.println(F("Proximity sensor is now running"));
} else {
Serial.println(F("Something went wrong during sensor init!"));
}
}

3)趋近数据采集

1
2
3
4
5
6
7
8
9
10
11
12
void loop() {

// 读取趋近数据
if ( !apds.readProximity(proximity_data) ) {
Serial.println("Error reading proximity value");
} else {
Serial.print("Proximity: ");
Serial.println(proximity_data);
}
delay(250);
}

文章来源: https://iotsmart.blog.csdn.net/article/details/126152627