Universal Robots Forum

Distance sensor

hello, I have an UR 3 and wanted to connect a distance sensor (the GP2Y0A41SK0F
from sharp) to it. This sensor has an analog output and, like almost every sensor, only outputs the voltage and not the distance. For this reason, I need to write a program or something similar in the robot so that the robot can convert the voltages into a distance. The sensor should be used so that the robot knows how far away it is from an object and then subsequently move in the direction of the object by the specified distance. So I would like to ask what would be the best way to do this so that the robot can convert the voltage into distances. Can someone please help me with this?
Thanks a lot

1 Like

It’s rather straightforward, the documentation on the sensor should give you the voltages for certain distances, if it’s linear it’s relatively easy to determine the formula for converting voltage to distance. You can use excel as well to plot the points they give you and then add a trend line and get the equation this way.

The voltage is a 0-10V signal so at 0 what distance is reported, most sensors have a minimum distance they will read and a max distance, the max distance will be reported near or at 10V.

If it’s linear you are simply solving for y=mx+b where b is the distance at 0V and m is the slope (rise over run). Voltage is your x and the result is the distance.


Hello, thank you very much for the answer. I knew that I have to use a formula to change the voltage in cm. But I don’t know how to change that. I only worked withe the standard functions of the UR and digital Outputs and after I’ve asked the support if it’s possible to let the UR change the voltage into cm, he said that I have to write an extra program into the Robot. But I don’t know how. So I would like to ask where I have to write the program and how it should look like. Hope somebody can help me… Sorry if the question sounds dumb, i am new here.

In the pic there is the curve for the voltage - distance and the formula. So I only need to know how to program it into the robot.

Thank you

  1. fit linear approximation to the curve (you can use any online tool like: Quick Linear Regression Calculator) to get relationship between voltage, and inverse distance. U = ax + b, where x = 1/(L+0.42)
  2. a and b will be approximated (I got a = 0.09, b = 11.6 - eyeballing values from graph, not very precise)
  3. Substitute a and b values for estimated constants.
  4. Covert equation to get L from voltage
  5. With a and b above I got 12.3cm at 1V
1 Like

Very helpful, thank you very much. I made a formula with help of logarithmic regression, which helps to get from volt to cm. But how can I put that formula into the Robot? I am not familiar with the UR Script. I’ve already downloaded the Simulation to see how the Scripts have to look like but I am not able to write one.

So I guess I have to write a Script and uploaded it afterwards on the UR? And how does the Script / Code has to look like?

P.S. The Formula I have is: y=A+Bln(x)
A= 4.1191
B= -1.1272


Hi @zacha.aram

If you would like to get the distance First task you have is to solve with respect to x/distance

Your next task is to take a look at the URScript manual, found in the download section of the support page.

Then you can write a URScript function that will look like this.

def read_distance(voltage):
  local a=4.1
  local b=-1.1
  return pow(2.718, (voltage - a)/b)

I will suggest you add it in the before start sequence of your program as a script placed in a file. You can then use the function in your program as a script node or type it in for expression for assignment of logical statements.


1 Like

hello @Ebbe thank you very much for your help.

so to task number 1, I have x = e^((y- A)/B)
to task number 2, I’ve read two manuals but they are a bit complicated because I am not realy familiar with programing in general, that is why I need a bit help.

After I’ve wrote the Code into the Robot in the before start sequence, it is possible for the Robot to change the Volt input into cm, right?

Many thanks

Hi @zacha.aram

That is similar to my solution in the URScript code.

Yes, if you make a program like this, and attach the sensor to analog_in[0] then the distance will continuously update.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.