Output Configuration
While the sensor is in I2C mode, it is possible to write persistent output configurations that take effect the next time the sensor powers on using the provided configuration opmode.
Writing Output Configurations
The sensor has two output pins: Pin0 and Pin1. When using a regular 4-color sensor cable, Pin0 is the white wire while Pin1 is the blue wire. By default these two pins work together to communicate over I2C. During I2C mode, the persistent memory of the sensor can be updated to change the output of the two pins, and these changes will take effect the next power-on. Here is how one would load an output configuration:
Uploading this code and running the opmode once will change the output of the sensor for all future power-ons, until it is put back in I2C using the reset sequence outlined below.
When one pin is configured to be non-i2c while the other remains in I2C, the sensor will revert to I2C mode. Make sure to configure both pins at once, even if you only need the output from one.
Resetting to I2C Mode
Once you write an output configuration, the sensor will no longer communicate data over I2C, but it will listen for attempts to communicate with it over I2C in the first second after a powerup.
To get a Rev Hub to continuously try to communicate with the sensor, initialize and stop the configuration opmode with the sensor unplugged, and you will get a warning which says "failed to communicate with I2C device." At this point, plug in the sensor, and it will reset to I2C mode, with the warning going away and the led turning magenta.
Reset Sequence:
Initialize configuration opmode. (with sensor unplugged)
Stop configuration opmode. (the warning "failed to communicate with I2C device" appears)
Plug in the sensor. (warning goes away, sensor led turns magenta)
Here is a table of possible LED colors and their meaning:
Green
Normal operation
Magenta
Reset to I2C
Blue (in digital mode)
At least one condition true
Blue (after configuration)
Wrote configuration
Red
Bad configuration value, rejected
Orange
Bad configuration, rejected
Digital Thresholds
In this mode, the sensor checks the current reading against a list of thresholds, and if any of them meet, the pin will output true. This example makes Pin0 output true when the distance is below 100mm.
Multiple thresholds for the same pin can be set using repeated calls to setPinDigital:
In the above example, Pin0 will output true if the distance is between 0-100 or 300-400.
Analog Voltage
In this mode, the sensor will output an analog voltage from 0-3.3V corresponding to the distance within a given range. The below example maps 0-3.3 to the full range of the sensor:
I2C Address
The default I2C address of the sensor is 0x52, which is shared with some other optical sensors like the Rev Color Sensor. If you want to connect multiple devices to the same bus, you can change the address of the sensor using the configuration opmode:
Valid I2C addresses have to be between 1 and 127, or 0x1 and 0x7F in hexadecimal.
Last updated