Tuning The Distance of an IR Sensor Using Arduino 🧶
This post is going to explore the process of connecting a sensor up to an Arduino circuit, to analyse the data and then come up with a way of calibrating the sensor for a specific task. When using sensors, we need to work out what the sensor values actually mean. In this instance we’re being given numbers (off the sensor) like 570 for a distance of 4cm or 525 at a distance of 26cm. We need to find how to translate these signals (which are just voltage signals coming from the analogue input on the Arduino) into distance values (in cm’s). We do this by wiring up the sensor to our Arduino and sending the analogue signal coming from the sensor to the serial print function in the Arduino IDE.
The circuit was wired up as shown below:

The code used to send the analogue signal to the serial monitor is as follows:
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(100); // delay in between reads for stability
}
Once the equipment has been setup we need to get readings from the sensor, from known distances, to give us an idea of the working conditions of the sensor. The results from our experiment are as follows:
Actual distance | Read distance |
3 cm | 510 |
4 cm | 570 |
5 cm | 525 |
6 cm | 445 |
7 cm | 375 |
8 cm | 330 |
9 cm | 295 |
10 cm | 265 |
11 cm | 245 |
12 cm | 220 |
13 cm | 200 |
14 cm | 185 |
15 cm | 165 |
16 cm | 160 |
17 cm | 150 |
18 cm | 145 |
19 cm | 135 |
20 cm | 130 |
21 cm | 120 |
22 cm | 115 |
23 cm | 105 |
24 cm | 100 |
25 cm | 95 |
26 cm | 90 |
27 cm | 90 |
Once the data was collected it was plotted onto a graph.

Then a line of best fit (power type) was added to our results to determine the best-fit formula for our trend line. We will use this trendline to convert our sensor readings into actual distance values.

The formula that the trendline (produced by excel for us) is but before we can use this number we need to inverse the function (rearrange for
) because the value
represents our distance in cm.
Hint: To save us having to go through all this effort in finding out the best formula we can simply transpose the data plot, add a line of best fit to that and find the formula of that new line. As simple as switching the two columns of data around in Excel before carrying out the same process as described above.
This is the transposed data plotted on a new graph…

The new formula given by Excel is . To check it we can simply plug a value in like 185 and see how it matches up against the results we found earlier. It should come up with a number around 14cm and actually gives a value of 13.46cm when a calculator is used (which is only about 6mm off). When you use the formula that I worked out above you get a value of 13.51cm which is slightly closer to the 14cm that it should be. To improve the accuracy of this even further, different readings could be taken over and over again and the average used in the calculations.
The code was then adapted to convert the analogue signal going into the arduino into the distance value that the object currently was, in cm’s, from the sensor using a new variable called “adjustedVAL”.
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
int adjustedVAL = 2897.6*sensorValue^(-1.029);
// print out the value you read:
Serial.println(adjustedVAL);
delay(100); // delay in between reads for stability
}