Before installing, we should have an infrared LED receiver ready and not receive light from the side, which is more directional than the sensor. I made it with tape, but you can use a straw or anything that blocks the light. Cut a small piece of electronic tape and wrap around the infrared LED receiver to form a tube. Trim the edges with scissors until about 1 cm long. Take a look at the picture and see how I made it with tape. Write the code above on the Arduino program and upload it to the Arduino. If you want to \"hear\" the distance, I also added a buzzer to this project. Simple proximity sensor using infrared/description: Measure the distance to the obstacle using infrared light emitted by an infrared LED and read the value using an infrared photoelectric diode. Accuracy is not perfect, but works well for small projects. Author: Ricardo Uvina/date: 01/10/2012/version: 1. 0int IRpin = 0; /The IR photodiodes on the analog pin A0int itrap = 2; /IR transmitter LED on digital pin 2int ambientIR; /Variable to store IR from ambientint obstacleIR; /Variable to store IR from object value [10]; /Variables that store IR values; /Variable, which will determine whether there is an obstacle or no setting (){Serial. begin(9600); /Initialize the serial monitor pinMode ( Remitter, output); /IR transmitter LED on digital pin 2 (IRemitter,LOW); /Set the ir led to turn off pinMode (11,OUTPUT); /Buzzer in digital pin 11}void loop(){ Distance = readIR (5); /Call the function that reads the distance and passes \"accuracy\" to it. println(distance); /Write read value/buzzer on serial monitor (); /Uncomment to activate buzzer function}int readIR(int times){for(int x=0; x100){ /If the obstacle is too close to digital writing (11,HIGH); }else{ The beep will be faster when the obstacle is close to the number (11,HIGH); delay(150-distance); /Please adjust this value for your convenience (11,LOW); delay(150-distance); Adjust this value for convenience}else{ If there is no obstacle, digital writing (11,LOW); }} Of course, you can edit it to suit your own project. For example, you can have the robot change direction or speed depending on the distance read from the infrared sensor. Connect the resistor from the 5 v pin to the anode pin of the ir led receiver. All anode pins of the IR led transmitter are connected to the digital pin 2. The wire goes from analog pin 0 to the anode pin of the infrared LED receiver. Don\'t forget to connect all the cathode pins of the LEDs to the ground pins. The buzzer is optional, but it will connect to the digital pins 11 and ground if you use it. Infrared is not visible to the naked eye, but you can see it through a digital camera, which helps to see if the LED works. Look at these photos.