pdf-icon

Arduino 上手教程

CoreS3 LTR553 距离接近传感器

M5CoreS3内置LTR553距离接近传感器, 参考下方API & 案例即可实现接近状态读取。

案例程序

begin()

功能说明:

  • 初始化接近传感器

原型:

bool begin(Ltr5xx_Init_Basic_Para *init_base_para);
bool begin(Ltr5xx_Init_Interrupt_Para *init_interrupt_para);

参数:

参数 类型 描述
init_base_para Ltr5xx_Init_Basic_Para * 基本监测初始化
init_interrupt_para Ltr5xx_Init_Interrupt_Para * 中断监测初始化

返回值:

返回值 描述
true 初始化成功
false 初始化失败

setPsMode()

功能说明:

  • 初始化接近传感器

原型:

bool setPsMode(uint8_t mode);

参数:

参数 类型 描述
mode uint8_t 1: 启用接近传感器监测
0:传感器待机

返回值:

返回值 描述
true 初始化成功
false 初始化失败

setAlsMode()

功能说明:

  • 初始化接近传感器

原型:

bool setAlsMode(uint8_t mode);

参数:

参数 类型 描述
mode uint8_t 1: 启用环境光传感器监测
0:传感器待机

返回值:

返回值 描述
true 初始化成功
false 初始化失败

getPsValue()

功能说明:

  • 初始化接近传感器

原型:

uint16_t getPsValue(void);

参数:

  • None

返回值:

返回值 描述
uint16_t 监测距离模拟值

getAlsValue()

功能说明:

  • 初始化接近传感器

原型:

uint16_t getAlsValue(void);

参数:

  • None

返回值:

返回值 描述
uint16_t 监测环境光lux

案例程序:

cpp
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include "M5CoreS3.h"
Ltr5xx_Init_Basic_Para device_init_base_para = LTR5XX_BASE_PARA_CONFIG_DEFAULT;
uint16_t read_ps_value;
uint16_t read_als_value;
void setup() {
auto cfg = M5.config();
CoreS3.begin(cfg);
CoreS3.Display.setTextColor(GREEN);
CoreS3.Display.setTextDatum(middle_center);
CoreS3.Display.setFont(&fonts::Orbitron_Light_24);
CoreS3.Display.setTextSize(1);
device_init_base_para.ps_led_pulse_freq = LTR5XX_LED_PULSE_FREQ_40KHZ;
device_init_base_para.ps_measurement_rate = LTR5XX_PS_MEASUREMENT_RATE_50MS;
device_init_base_para.als_gain = LTR5XX_ALS_GAIN_48X;
if (!CoreS3.Ltr553.begin(&device_init_base_para)) {
CoreS3.Display.drawString("Ltr553 Init Fail",
CoreS3.Display.width() / 2,
CoreS3.Display.height() / 2);
while (1) {
delay(10);
}
}
CoreS3.Display.drawString("Ltr553 Init Success", CoreS3.Display.width() / 2,
CoreS3.Display.height() / 2);
// active ps
CoreS3.Ltr553.setPsMode(LTR5XX_PS_ACTIVE_MODE);
// active als
CoreS3.Ltr553.setAlsMode(LTR5XX_ALS_ACTIVE_MODE);
// not active ps
// CoreS3.Ltr553.setPsMode(LTR5XX_PS_STAND_BY_MODE);
// not active als
// CoreS3.Ltr553.setAlsMode(LTR5XX_ALS_STAND_BY_MODE);
}
void loop() {
CoreS3.Display.clear();
read_ps_value = CoreS3.Ltr553.getPsValue();
read_als_value = CoreS3.Ltr553.getAlsValue();
CoreS3.Display.drawString(
"PS:" + String(read_ps_value) + " / " + "ALS:" + String(read_als_value),
CoreS3.Display.width() / 2, CoreS3.Display.height() / 2);
Serial.printf("ps value = %d\r\n", read_ps_value);
Serial.printf("als value = %d\r\n", read_als_value);
delay(100);
}
On This Page