Prerequisites
If not already done so already, install Arduino software and make basic connections, as described in page for MRMS ESP32: Arduino, IMU, eFuse, BT, WiFi, CAN Bus (mrm-esp32). This setup is convenient, but it is not mandatory. You can connect some other microcontroller and power supply.
Task
We will read analog output of the sensor in order to determine reflected light's intensity.
Connections
Starting from the basic connection, let's add a ML-R Light Barrier (mrm-barr). 3.3 V supply can be made using a Dupont cable with 0.1" plugs. We will use ML-R Distribution Pins 3x Passive (mrm-distrib-c) to distribute 3.3 V.
Analog output should be connected to a MCU's pin which supports analog input. We will use ESP32 GPIO pin 36 (blue link in the picture). Output produces a maximum of 3.3 V.
Program
Here is the simplest code.
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.println(analogRead(36));
}
It reads GPIO 36 and prints its value to console. As ESP32 uses 12-bit ADC, the values are in range 0 - 4095 (212).
We could use any other of the 16 analog pins, as long as no other device uses them, but read the paragraph "Limitations".
The transistor measures emitted green light. If ray is interrupted, measured voltage will significantly decrease.
Limitations
Some pins are not available or are limited in usage. Check this list.