Help with OPT3101 Sensor

Hello all,

I recently purchased 2 OPT3101 distance sensors and was trying to get them working. I was running the advanced example from Github, and the sensors gave the correct values when I ran the code. However, when I put one of the sensors on my robot, I started experiencing some issues. One of the channels was behaving unreliably, and it started giving negative values.
Here are the values that I was getting:

Distance 0, Distance 1, Distance 2, || amplitude 0, amplitude 1, amplitude 2,  
1067, 947, 159,  || 214, 70, 65, 
1048, 986, -106,  || 208, 73, 64, 
1060, 871, 36,  || 213, 67, 63, 
1032, 1005, -71,  || 208, 71, 63, 
1026, 862, -9,  || 215, 70, 63, 
1040, 1046, -96,  || 207, 71, 61, 
1073, 903, 81,  || 214, 67, 66, 
1034, 970, -44,  || 207, 73, 62, 
1066, 855, 166,  || 209, 69, 65, 
1007, 1083, -190,  || 215, 72, 62, 
1023, 924, 86,  || 221, 66, 65, 
1036, 1054, -184,  || 206, 69, 60, 
1123, 816, -64,  || 189, 67, 63, 
1112, 1011, -130,  || 169, 69, 59, 
1216, 942, -41,  || 168, 65, 62, 
1153, 908, -224,  || 162, 70, 62, 
1221, 983, 135,  || 165, 66, 62, 
1182, 987, -161,  || 160, 70, 62, 
1239, 827, 75,  || 161, 67, 61, 
1208, 1043, -203,  || 159, 68, 59, 
1234, 954, 10,  || 160, 64, 63, 
1174, 986, -238,  || 161, 70, 61, 
1239, 935, -13,  || 164, 64, 62, 
1178, 1000, -141,  || 160, 70, 59, 
1236, 859, -50,  || 159, 66, 62, 
1203, 973, -233,  || 157, 69, 59

And also I get positive values when the amplitude value is greater than 100, but the issue is that I am only getting a range of 10-30 cm reliably otherwise it just gives a value of 20 - 30Cm even when the object is far away.
like:

1693, 617, 344,  || 62, 66, 116, 
1434, 809, 215,  || 59, 71, 116, 
1634, 672, 338,  || 64, 68, 118, 
1414, 774, 269,  || 59, 71, 116, 
1613, 614, 369,  || 65, 68, 119, 
1463, 755, 284,  || 56, 72, 120, 
1594, 629, 366,  || 64, 72, 119, 
1552, 814, 253,  || 59, 73, 118, 
1712, 677, 353,  || 63, 70, 119, 
1453, 721, 310,  || 59, 70, 119, 
1685, 701, 346,  || 62, 70, 120, 
1549, 810, 291,  || 59, 71, 116, 
1618, 519, 373,  || 61, 68, 118, 
1481, 800, 255,  || 60, 71, 117, 
1617, 735, 323,  || 66, 69, 119, 
1453, 754, 282,  || 62, 74, 117, 
1577, 686, 385,  || 64, 74, 119, 
1505, 821, 291,  || 59, 77, 115, 
1705, 638, 369,  || 64, 72, 120, 
1503, 800, 254,  || 59, 74, 118, 
1691, 660, 305,  || 64, 72, 119, 
1519, 836, 278,  || 60, 76, 117, 
1555, 696, 301,  || 65, 72, 121, 
1562, 871, 219,  || 59, 76, 115, 
1664, 727, 378,  || 64, 74, 118, 
1492, 751, 242,  || 60, 74, 117, 
1682, 670, 365,  || 62, 72, 123, 
1479, 879, 252,  || 58, 77, 116, 
1630, 734, 370,  || 64, 72, 118,

The First and the second channels are working fine,

Here is my EEPROM hex dump:

"\xB4\xF0\xB2\xF1\xB4\x65\xD1\x22\xB4\xCE\x00\x00\x0B\x09\x00\x10\x0C\x00\x00\xBF\x29\xE4\x93\x2F\x2A\x20\x49\x7C\x2B\x78\x69\x00\x2C\xA7\x0B\x00\x2D\xBB\xE4\x5E\x2E\xA0\x01\x84\x2F\x8F\xF3\x4C\x30\xAF\x0E\x20\x31\xBA\x03\x5F\x32\x8F\x60\xB0\x33\xDD\xF0\x4B\x34\x0E\xFD\x60\x35\xE2\xCA\x00\x36\x6E\xDC\x00\x37\xED\xE5\x00\x38\x02\xFA\x29\x39\x95\x8D\xF1\x3A\x20\xC2\x4D\x3F\xC8\x08\x00\x41\x10\x80\x8C\x42\x56\x3B\x00\x43\x83\x00\x00\x45\xDD\x85\x8C\x47\xC8\x08\x80\x48\xC8\x08\x00\x49\xC8\x08\x00\x51\xF0\x32\x00\x52\x52\x3D\x00\x53\xED\x33\x00\x54\x6C\x3C\x00\x55\x9D\x34\x00\x5E\x00\x63\x13\x5F\xF8\x16\xBB\x60\x1D\xEA\xD8\x61\xDF\x88\x00\x72\x50\x00\x00\x85\x7A\x26\x00\x86\x82\x26\x00\xB4\xD3\xE4\xE8\xB5\x01\x00\x00\xB8\x1A\x6C\x01\xB9\xA6\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF";

I would really appreciate some help with this issue. Does anyone have any suggestions for how I can resolve this issue?

Thank you in advance for your help!

Please make sure that you’ve configured the sensor correctly and that the sensor’s I2C address is set correctly in the code.

Hi Bidrohini,
Thanks for your replay,
The sensor is configured correctly as per this example sketch from GitHub with address = 0x58;
The sensor is communicating with the MCU properly as I was able to get all the values from the sensor like

sensor.ambient
sensor.i
sensor.q
sensor.amplitude
sensor.distanceMillimeters

And all the values that I am getting other than sensor.distanceMillimeters from the third channel make sense.
And both of my sensors are behaving the same way.
Any idea why?

you may need to configure certain parameters such as integration time and/or gain, and ensure that the sensor is properly positioned and aimed in the direction that you want to measure distance from.

I recommend double checking the datasheet for the Opt3101 to ensure that you have properly configured the sensor for distance measurement, and that you have properly connected the sensor to your MCU.

Hello.

One immediate concern is how low the amplitude readings are. As mentioned on the product page under the FAQs tab we have generally seen that readings with amplitudes below about 200 to 400 start to become too inaccurate to be useful.

Is moving the sensor to the robot platform the only change? Please provide more details about how you are using the sensor (like what targets you are trying to measure with the sensor) and post pictures of your setup.

It looks like you are using something other than the example programs from our OPT3101 library (or are using a modified version), since the examples do not output data in the format shown in your posts. Can you try our examples without modification, or could you post the program you used to gather that data? Also, if you can remove the sensor from your robot to collect a sample output that reflects how you were testing the sensors earlier, that would be useful to have as a point of comparison.

- Patrick

Hello Patrick,
Thank you for taking the time to respond to my query. I really appreciate your help!

In my setup the amplitude values are usually in the lower range, they only increase to a bigger value when the object is closer or a white target is present.

And it is true that I have modified the code to suit my needs, I am using a custom STM32G0-based board thus I have made changes to the library for giving custom I2C pins.
The actual code that I am using is this “OPT3101_arduino”.

And also when I run the default code from Github using Arduino Uno am getting the proper values, I’ll also run the modified code in Arduino Uno and update the results.

I am testing the sensor in an open area inside a room, and I was trying to measure the distance to a brown box and a black box.


This is my crude setup, the green PCB is the STM32 board and the OPT3101 sensor board is plugged into the breadboard.

While I was testing the STM32-based setup (both on the robot and in the table) I noticed that when I place the sensor at some angle the values that I am getting in channel 2 (TX2) are around +200mm to +600mm and in some angle, it gives negative values even when there are no objects nearby. (the other 2 channels give proper values)

But using the Arduino setup I get proper values on all three channels (around +1000mm to +1600mm).

Edit: I tested my code in Arduino Uno also and it is working as expected.

When you test with the Arduino Uno versus your STM32G0-based board, is everything else exactly the same (specificially the sensor mounting, environment, and targets)?

We are not sure why you seem to be getting different behavior, and we are doing some tests to determine if there might be a bug in our library (e.g. one that only affects 32-bit controllers), but in general, it is not unusual to see negative or nonsensical readings when the amplitude is too low. Sometimes this can be mitigated by making sure there is nothing around the sensor that might produce weak reflections that interfere with its readings (although the setup in your picture looks like it should be okay).

Do you see negative distance measurements when the amplitude is higher than about 400 or when a target is positioned where you expect the sensor to be able to detect it? If you only see negative measurements when the amplitude is below 400, that behavior is probably somewhat expected. Our recommendation in that case would be to modify your code to disregard those distance measurement and assume nothing is detected whenever the amplitude is below a certain threshold. Using 400 as a threshold is probably a good starting point, but you might have to adjust that up a little.

- Patrick

Yes, as you can see in the picture the sensor is mounted on the breadboard and connected to the mcu via jumpers, the position of the sensor and the environment is same, just the wire is swaped from the STM32 based board to the arduino uno nearby.

And I did tested the code where it will just ignore any values < 200, but then the issue is that the target object has to be so close to the sensor (around <20cm) or it has to be a white target. then only the amplitude values are >200, and I can also confirm that in all case when amplitude value is greater or near to the 300-400 value it gives a proper reading.

I have also tested it with an ESP32S3 board, and it works with the same setup, so I’m not sure what’s going on with my STM32G030C8T6 board. I guess you can rule out the possibility of the issue being inside the library for 32-bit boards.

distance[0], distance[1], distance[2], || amplitude[0], amplitude[1], amplitude[2],    
511, 878, 629,  || 496, 88, 123, 
492, 988, 531,  || 492, 92, 121, 
517, 960, 657,  || 493, 89, 121, 
490, 991, 492,  || 493, 94, 120, 
510, 879, 604,  || 497, 86, 125, 
486, 984, 561,  || 491, 92, 119, 
516, 874, 671,  || 496, 87, 123, 
493, 997, 547,  || 494, 93, 119, 
507, 913, 679,  || 496, 87, 121, 
486, 1028, 629,  || 492, 92, 120, 
519, 906, 597,  || 497, 89, 124, 
479, 1002, 587,  || 491, 89, 119, 
505, 956, 641,  || 477, 87, 124, 
488, 1060, 582,  || 466, 89, 119, 
520, 930, 679,  || 468, 85, 122, 
494, 1018, 538,  || 466, 89, 120, 
509, 943, 652,  || 471, 87, 123, 
500, 1028, 575,  || 470, 90, 121, 
509, 929, 637,  || 471, 88, 124, 
492, 978, 568,  || 468, 93, 119, 
520, 897, 679,  || 470, 86, 123, 
486, 1041, 553,  || 463, 91, 120, 
513, 898, 653,  || 460, 88, 121, 
486, 1043, 527,  || 453, 88, 119, 
524, 980, 701,  || 457, 85, 123, 
499, 1004, 591,  || 456, 90, 119, 
513, 969, 643,  || 461, 87, 121, 
497, 1018, 538,  || 459, 91, 122, 
528, 906, 662,  || 462, 86, 122, 
488, 1063, 580,  || 456, 90, 119, 
514, 956, 624,  || 462, 87, 121, 
500, 1102, 610,  || 459, 90, 118, 
520, 988, 668,  || 463, 87, 123, 
487, 1028, 605,  || 460, 91, 118, 
521, 945, 675,  || 463, 87, 122, 
502, 999, 581,  || 458, 91, 121, 
528, 971, 633,  || 462, 87, 125, 
499, 1044, 540,  || 460, 88, 120, 
514, 936, 668,  || 461, 85, 124, 
508, 1045, 566,  || 459, 89, 121, 
521, 907, 648,  || 461, 86, 124, 
502, 1054, 567,  || 458, 93, 120, 
524, 935, 634,  || 463, 87, 122, 
486, 1063, 577,  || 438, 81, 117, 
564, 1110, 668,  || 364, 74, 122, 
552, 1079, 617,  || 339, 75, 119, 
606, 1035, 650,  || 350, 76, 124, 
583, 1219, 577,  || 352, 80, 120, 
621, 1023, 655,  || 369, 79, 123, 
618, 1089, 618,  || 360, 80, 119, 
655, 1086, 689,  || 355, 77, 123, 
625, 1082, 526,  || 349, 81, 120, 
654, 1050, 623,  || 351, 77, 122, 
625, 1148, 588,  || 347, 82, 120, 
645, 1010, 660,  || 354, 79, 124, 
622, 1100, 569,  || 346, 81, 119, 
659, 1088, 629,  || 347, 78, 123, 
623, 1122, 582,  || 346, 80, 122, 
653, 1042, 673,  || 348, 79, 123, 
625, 1191, 623,  || 346, 83, 118, 
664, 1086, 695,  || 348, 79, 125, 
609, 1192, 570,  || 345, 84, 119, 
659, 1040, 659,  || 350, 79, 125, 
616, 1134, 572,  || 348, 83, 123, 
644, 1052, 687,  || 350, 77, 123, 
615, 1200, 533,  || 346, 79, 121, 
634, 1032, 678,  || 351, 76, 123, 
626, 1164, 566,  || 346, 81, 121, 
657, 1041, 658,  || 349, 76, 124, 
629, 1122, 569,  || 347, 82, 120, 
635, 1006, 681,  || 350, 78, 124, 
632, 1167, 637,  || 348, 82, 120, 
658, 1078, 699,  || 351, 76, 122, 
611, 1159, 600,  || 346, 81, 122, 
643, 1040, 664,  || 349, 76, 124, 
625, 1185, 543,  || 346, 81, 120, 
663, 1078, 610,  || 351, 78, 122, 
623, 1159, 552,  || 349, 80, 121, 
629, 914, 631,  || 357, 78, 124, 
609, 1132, 524,  || 355, 83, 121, 
638, 1036, 665,  || 359, 78, 124, 
615, 1268, 610,  || 354, 81, 120, 
641, 1024, 645,  || 360, 76, 124, 
607, 1072, 559,  || 355, 82, 122, 
639, 1113, 647,  || 359, 76, 123, 

Ps: am running the board with 3.3v (in all the case).

This might be a long shot, but if your sensor is being powered from different sources whenever you test it with a different microcontroller, then you might try testing if that could be the issue. In other words, instead of powering the OPT3101 from your STM32G030C8T6 board, power it from your Arduino (or some other source you know it to work with) while trying to read it with the STM32G030C8T6. Just make sure there is a common ground connection between the sources and your STM32G030C8T6.

If that is not the issue, then unfortunately the evidence points to some unique issue with your STM32G030C8T6 and its I2C communication. If you absolutely have to use that board, then my only remaining suggestion at this point is to try looking at your system’s I2C signals with an oscilloscope or logic analyzer or to try modifying the program to log I2C accesses and compare results across different platforms. (I expect this to be fairly challenging because of how complex the OPT3101’s initialization and operating procedures are, though.)

- Patrick

I attempted to power it from another source, but unfortunately, I still had no luck. It’s likely related to the controller I’m using and its communication. However, I’ve decided not to pursue this rabbit hole and instead stick with the ESP32 for the time being.

Thank you so much for your help and support. I really appreciate it!

2 Likes