> For the complete documentation index, see [llms.txt](https://docs.petoi.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.petoi.com/~/changes/MnaoHQ6wOvffal3XIPZR/extensible-modules/ir-distance-sensor.md).

# IR Distance Sensor

## Function introduction

This module integrates two IR distance sensors,  it measures distance through reflected light waves, It is used to detect the presence of an object within a specific range. The sensor consists of an IR LED and a photosensor (phototransistor) pair. The light emitted by the IR LED gets reflected by any object placed in front of the sensor and this reflection is detected by the photosensor(phototransistor). Any white (or lighter) colored surface reflects more than a black (or darker) colored surface. It is suitable to be used to measure complex objects, such as tracing an object.

<figure><img src="/files/g7rLO37BidPzI7O2Tmkd" alt=""><figcaption></figcaption></figure>

## Hardware setup

* NyBoard

Connecting to the NyBoard with wire as shown in the following picture:

<figure><img src="/files/6Bdo5UwZlLonUPWp5VRO" alt=""><figcaption></figcaption></figure>

* BiBoard

<figure><img src="/files/mFHHXhoOPb6YdGHVT75y" alt=""><figcaption></figcaption></figure>

For specific use, the end connected to the sensor can be fixed on the robot's head (included in Bittle's mouth, or attached to the top of Nybble's head), of course, you can also use your creativity according to your needs.

## Software setup

Install the latest version of the **Adafruit NeoPixel** library using the [Arduino IDE](https://www.arduino.cc/en/software).

<figure><img src="/files/pNsWQdGzCYL1sAo8QRut" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/yMwSdTzwp3bDFI8busLp" alt=""><figcaption></figcaption></figure>

The code using this sensor has been integrated into the [**OpenCat**](https://github.com/PetoiCamp/OpenCat) (NyBoard)/ [**OpenCatEsp32**](https://github.com/PetoiCamp/OpenCatEsp32) (BiBoard) project. Uncomment the line **`#define DOUBLE_INFRARED_DISTANCE`**  in the **OpenCat.ino  / OpenCatEsp32.ino**, as shown in the figure below, and then use the Arduino IDE to upload the sketch to the robot main board. The sketch can reproduce the example function of integrating the robot action.

### NyBoard

#### Prepare the Arduino UNO development environment

With **NyBoard V1\_\***, you can simply choose **Arduino Uno**.&#x20;

<figure><img src="/files/4JBNM8f9Sdm40i7VCH7O" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/4H7lthHNX7OnS2l943U9" alt=""><figcaption></figcaption></figure>

### BiBoard

#### [Prepare the ESP32 development environment](https://docs.petoi.com/arduino-ide/upload-sketch-for-biboard#2.1-prepare-the-esp32-development-environment)

<figure><img src="/files/BV1FPGVaiuH4CPKElFdI" alt=""><figcaption></figcaption></figure>

If you want to test the function of this sensor alone or want to learn more about its principles. You can use the Arduino IDE to upload the demo sketch(**testDoubleInfraredDistance.ino**), as shown below:

<figure><img src="/files/GEaCkQUhBn7ZgmVoSYjq" alt=""><figcaption></figcaption></figure>

This demo sketch implements real-time printing of the analog values (rL, rR) and data that have been processed (dL,dR) of the two analog pins (A2 and A3) in the [serial monitor](https://docs.petoi.com/arduino-ide/serial-monitor). You can also use the serial plotter to view the two analog pins (A2 and A3) more intuitively. The waveform graph is generated by the analog value of the pin output along the time axis.

<figure><img src="/files/WdQ6rEe7nmOJ7noyEel4" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/PuPI9Kqp6K1VfQkqC3IT" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/4yoj4EMUoaNf0vXZAB6e" alt=""><figcaption></figcaption></figure>

### The demo code

The demo code is in the OpenCat code repository on GitHub (specific path: OpenCat/ModuleTests/testDoubleInfraredDistance). You can visit our GitHub code repository <https://github.com/PetoiCamp/OpenCat> to download the complete code, as shown in the following picture:

<figure><img src="/files/kIyOeImwR9EVmKBvjfD8" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.petoi.com/~/changes/MnaoHQ6wOvffal3XIPZR/extensible-modules/ir-distance-sensor.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
