如何使用 ARDUINO Mega2560 初始化 APDS-9930 环境光/接近传感器
How to initialize APDS-9930 ambient light / proximity sensor using ARDUINO Mega2560
我买了一个 "APDS-9930" 环境光传感器,它通过 I2C (TWI) 协议进行通信。我打算使用 ARDUINO Mega2560 开发板从中读取环境光级别。当我在网上搜索时,我发现了一个修改后的 ARDUINO 库,它基于 APDS-9960,它声称可以在 ARDUINO UNO 上与 APDS-9930 一起使用。但是,当与 Mega2560 一起使用时,它会给我 "Error initializing" 错误。这里有人知道如何处理这个错误吗?
我已经在很多方面使用了 "Wire.h" 库,以地址 0x14 读取 "CH0 ADC data register",它保存环境电平值(根据数据表)。代码如下:
#define DUMP_REGS
#include <Wire.h>
#include <APDS9930.h>
// Global Variables
APDS9930 apds = APDS9930();
float ambient_light = 0; // can also be an unsigned long
uint16_t ch0 = 0;
uint16_t ch1 = 1;
void setup() {
//analogReference(EXTERNAL);
// Initialize Serial port
Serial.begin(9600);
Serial.println();
Serial.println(F("--------------------------------"));
Serial.println(F("APDS-9930 - Ambient light sensor"));
Serial.println(F("--------------------------------"));
// Initialize APDS-9930 (configure I2C and initial values)
//if ( apds.init() ) {
// Serial.println(F("APDS-9930 initialization complete"));
//} else {
// Serial.println(F("Something went wrong during APDS-9930 init!"));
// }
// Start running the APDS-9930 light sensor (no interrupts)
//if ( apds.enableLightSensor(false) ) {
// Serial.println(F("Light sensor is now running"));
// } else {
// Serial.println(F("Something went wrong during light sensor init!"));
// }
#ifdef DUMP_REGS
/* Register dump */
uint8_t reg;
uint8_t val;
for(reg = 0x00; reg <= 0x19; reg++) {
if( (reg != 0x10) && \
(reg != 0x11) )
{
apds.wireReadDataByte(reg, val);
Serial.print(reg, HEX);
Serial.print(": 0x");
Serial.println(val, HEX);
}
}
apds.wireReadDataByte(0x1E, val);
Serial.print(0x1E, HEX);
Serial.print(": 0x");
Serial.println(val, HEX);
#endif
// Wait for initialization and calibration to finish
delay(500);
}
void loop() {
// Read the light levels (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);
}
// Wait 1 second before next reading
delay(1000);
}
正如上面评论中所讨论的,问题与硬件有关。
在 Arduino Mega 2560 board 上有两个电阻将 SDA 和 SCK(连接器上的引脚 20 和 21)连接到 +5V。
使用这些上拉电阻,无法直接连接工作在 3.3V 的传感器。
解决方案是添加一个电平转换器或移除电路板上的电阻,然后根据需要将它们连接到 5V 或 3.3V,具体取决于您要连接的传感器。
我买了一个 "APDS-9930" 环境光传感器,它通过 I2C (TWI) 协议进行通信。我打算使用 ARDUINO Mega2560 开发板从中读取环境光级别。当我在网上搜索时,我发现了一个修改后的 ARDUINO 库,它基于 APDS-9960,它声称可以在 ARDUINO UNO 上与 APDS-9930 一起使用。但是,当与 Mega2560 一起使用时,它会给我 "Error initializing" 错误。这里有人知道如何处理这个错误吗?
我已经在很多方面使用了 "Wire.h" 库,以地址 0x14 读取 "CH0 ADC data register",它保存环境电平值(根据数据表)。代码如下:
#define DUMP_REGS
#include <Wire.h>
#include <APDS9930.h>
// Global Variables
APDS9930 apds = APDS9930();
float ambient_light = 0; // can also be an unsigned long
uint16_t ch0 = 0;
uint16_t ch1 = 1;
void setup() {
//analogReference(EXTERNAL);
// Initialize Serial port
Serial.begin(9600);
Serial.println();
Serial.println(F("--------------------------------"));
Serial.println(F("APDS-9930 - Ambient light sensor"));
Serial.println(F("--------------------------------"));
// Initialize APDS-9930 (configure I2C and initial values)
//if ( apds.init() ) {
// Serial.println(F("APDS-9930 initialization complete"));
//} else {
// Serial.println(F("Something went wrong during APDS-9930 init!"));
// }
// Start running the APDS-9930 light sensor (no interrupts)
//if ( apds.enableLightSensor(false) ) {
// Serial.println(F("Light sensor is now running"));
// } else {
// Serial.println(F("Something went wrong during light sensor init!"));
// }
#ifdef DUMP_REGS
/* Register dump */
uint8_t reg;
uint8_t val;
for(reg = 0x00; reg <= 0x19; reg++) {
if( (reg != 0x10) && \
(reg != 0x11) )
{
apds.wireReadDataByte(reg, val);
Serial.print(reg, HEX);
Serial.print(": 0x");
Serial.println(val, HEX);
}
}
apds.wireReadDataByte(0x1E, val);
Serial.print(0x1E, HEX);
Serial.print(": 0x");
Serial.println(val, HEX);
#endif
// Wait for initialization and calibration to finish
delay(500);
}
void loop() {
// Read the light levels (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);
}
// Wait 1 second before next reading
delay(1000);
}
正如上面评论中所讨论的,问题与硬件有关。
在 Arduino Mega 2560 board 上有两个电阻将 SDA 和 SCK(连接器上的引脚 20 和 21)连接到 +5V。
使用这些上拉电阻,无法直接连接工作在 3.3V 的传感器。
解决方案是添加一个电平转换器或移除电路板上的电阻,然后根据需要将它们连接到 5V 或 3.3V,具体取决于您要连接的传感器。