0

Analog output example for ML-R Reflectance Sensor B (mrm-ref-b)

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 Reflectance Sensor B (mrm-ref-b). In the picture 3.3 V power supply is made using a ML-R Cable KK254-KK254 10 cm (mrm-kk2.54-2.54-10). An alternative solution is a Dupont cable with 0.1" plugs. If there is only one sensor, You will be able to use Molex KK254 header of the MCU board, like in the right picture. If You have more of them, an adapter will be handy, like ML-R Distribution Pins 3x Passive (mrm-distrib-c). You can experiment with different distances.

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 sensor measures reflected light. It will increase if the surface is more reflective or when the sensor is closer. If the distance is roughly equal, it is possible to distinguish between black and white very well. It is also possible to detect a highly reflective surface, as aluminum foil. Be sure that the sensor is not too close for the last task. If it is, white surface will overwhelm the phototransistor and there will be just a very small difference between white and a foil.

Limitations

Some pins are not available or are limited in usage. Check this list.