pdf-icon

Arduino Guide

Unit Reflective IR Arduino Tutorial

1. Preparation

2. Example Program

Example Explanation
The Unit Reflective IR is a mechanical relay used to detect the reflection of infrared light from an object and indicate the detection status through analog and digital signal outputs. In this example, we will read the digital and analog signals output by the Unit Reflective IR and display them. Note: The analog signal value and digital signal read from the sensor are mainly used to distinguish the infrared reflection status of obstacles, but they do not convert to precise distance measurements.
#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. Compile and Upload

    1. Download Mode: Different devices may require entering download mode before programming. The steps for this may vary depending on the main control device. For more details, refer to the Arduino IDE Getting Started Guide , which includes specific download tutorial lists for each device.
  • For CoreS3, press and hold the reset button (for about 2 seconds) until the internal green LED lights up, and then release it. This indicates the device is in download mode and ready for programming.

    1. Select the device port and click the compile and upload button in the top left corner of Arduino IDE, then wait for the program to compile and upload to the device.

4. Data Detection

Read the digital and analog signals output by the Unit Reflective IR and display them.

On This Page