pdf-icon

Arduino 上手教程

Unit Reflective IR Arduino 使用教程

1.准备工作

2.案例程序

案例说明
Unit Reflective IR 是一款机械式继电器,主要用于检测目标物体的反射红外光,并通过模拟信号和数字信号输出来指示检测状态。本案例将读取 Unit Reflective IR 输出数字信号与模拟信号,并进行显示。注:读取的模拟信号值与数字信号主要用区分障碍物红外反射光状态。并不能转换为精确的测量距离。
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
#include <M5Unified.h>
#include <Arduino.h>
#define IR_ANALOG_PIN 8
#define IR_DIGITAL_PIN 9
void setup()
{
M5.begin();
M5.Display.setFont(&fonts::FreeMonoBold12pt7b);
pinMode(IR_ANALOG_PIN, INPUT);
pinMode(IR_DIGITAL_PIN, INPUT);
}
void loop()
{
int analog = analogRead(IR_ANALOG_PIN);
int digital = digitalRead(IR_DIGITAL_PIN);
M5.Display.clear();
M5.Display.setCursor(20, 40);
M5.Display.printf("analog: %d", analog);
M5.Display.setCursor(20, 80);
M5.Display.printf("digital: %d", digital);
Serial.printf("analog: %d\r\n", analog);
Serial.printf("digital: %d\r\n", digital);
delay(1000);
}

3.编译上传

  • 1.下载模式: 不同设备进行程序烧录前需要下载模式, 不同的主控设备该步骤可能有所不同。详情可参考Arduino IDE上手教程页面底部的设备程序下载教程列表, 查看具体的操作方式。

  • CoreS3长按复位按键(大约2秒)直到内部绿色LED灯亮起,便可松开,此时设备已进入下载模式,等待烧录。

  • 2.选中设备端口, 点击Arduino IDE左上角编译上传按钮, 等待程序完成编译并上传至设备。

4.检测数据读取

读取 Unit Reflective IR 输出数字信号与模拟信号,并进行显示。

On This Page