
Arduino Quick Start
Example program for StackChan LTR553 Proximity Sensor usage.
#include "M5Unified.h"
#include "LTR5XX.h"
Ltr5xx_Init_Basic_Para device_init_base_para = LTR5XX_BASE_PARA_CONFIG_DEFAULT;
LTR5XX DistanceSensor;
uint16_t read_ps_value;
uint16_t read_als_value;
void setup() {
auto cfg = M5.config();
M5.begin(cfg);
M5.Display.setTextColor(GREEN);
M5.Display.setTextDatum(middle_center);
M5.Display.setFont(&fonts::FreeMonoBold12pt7b);
M5.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 (!DistanceSensor.begin(&device_init_base_para)) {
M5.Display.drawString("Ltr553 Init Fail",
M5.Display.width() / 2,
M5.Display.height() / 2);
while (1) {
delay(10);
}
}
M5.Display.drawString("Ltr553 Init Success", M5.Display.width() / 2,
M5.Display.height() / 2);
// active ps
DistanceSensor.setPsMode(LTR5XX_PS_ACTIVE_MODE);
// active als
DistanceSensor.setAlsMode(LTR5XX_ALS_ACTIVE_MODE);
// not active ps
// DistanceSensor.setPsMode(LTR5XX_PS_STAND_BY_MODE);
// not active als
// DistanceSensor.setAlsMode(LTR5XX_ALS_STAND_BY_MODE);
}
void loop() {
M5.Display.clear();
read_ps_value = DistanceSensor.getPsValue();
read_als_value = DistanceSensor.getAlsValue();
M5.Display.drawString(
"PS:" + String(read_ps_value) + " / " + "ALS:" + String(read_als_value),
M5.Display.width() / 2, M5.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);
}After flashing successfully, you can view the PS (Proximity) and ALS (Ambient Light) readings from the LTR553 proximity sensor on both the serial monitor and the screen.
