Introduction

IR Proximity Sensor:
IR LED Emitter:
Parts Of List:
Connection
Step 1: Fix the IR Receiver on the breadboard.
Step 2: Fix the IR Emitter 4 on the breadboard.
Connection Of Bread Board To Arduino:
Step 1:
Take the IR Receiver Gnd pin to the Analog pin A0, then Vcc to the Digital pin 02.
Step 2: Take the IR Emitter to connect the Gnd pins commonly then Vcc pins commonly.
Step 3: Connect to the Arduino UNO board.
Step 4: Fix the buzzer in the Arduino board.
Program:
  1. int IRpin = A0; // IR photodiode on analog pin A0
  2. int IRemitter = 2; // IR emitter LED on digital pin 2
  3. int ambientIR; // variable to store the IR coming from the ambient
  4. int obstacleIR; // variable to store the IR coming from the object
  5. int value[10]; // variable to store the IR values
  6. int distance; // variable that will tell if there is an obstacle or not
  7. void setup()
  8. {
  9. Serial.begin(9600); // initializing Serial monitor
  10. pinMode(IRemitter, OUTPUT); // IR emitter LED on digital pin 2
  11. digitalWrite(IRemitter, LOW); // setup IR LED as off
  12. pinMode(11, OUTPUT); // buzzer in digital pin 11
  13. }
  14. void loop()
  15. {
  16. distance = readIR(5); // calling the function that will read the distance and passing the "accuracy" to it
  17. Serial.println(distance); // writing the read value on Serial monitor
  18. // buzzer(); // uncomment to activate the buzzer function
  19. }
  20. int readIR(int times)
  21. {
  22. for (int x = 0; x < times; x++)
  23. {
  24. digitalWrite(IRemitter, LOW); //turning the IR LEDs off to read the IR coming from the ambient
  25. delay(1); // minimum delay necessary to read values
  26. ambientIR = analogRead(IRpin); // storing IR coming from the ambient
  27. digitalWrite(IRemitter, HIGH); //turning the IR LEDs on to read the IR coming from the obstacle
  28. delay(1); // minimum delay necessary to read values
  29. obstacleIR = analogRead(IRpin); // storing IR coming from the obstacle
  30. value[x] = ambientIR - obstacleIR; // calculating changes in IR values and storing it for future average
  31. }
  32. for (int x = 0; x < times; x++) { // calculating the average based on the "accuracy"
  33. distance += value[x];
  34. }
  35. return (distance / times); // return the final value
  36. }
  37. void buzzer()
  38. {
  39. if (distance > 1)
  40. {
  41. if (distance > 100)
  42. {
  43. digitalWrite(11, HIGH);
  44. } else
  45. {
  46. digitalWrite(11, HIGH);
  47. delay(150 - distance); // adjust this value for your convenience
  48. digitalWrite(11, LOW);
  49. delay(150 - distance); // adjust this value for your convenience
  50. }
  51. } else
  52. {
  53. digitalWrite(11, LOW);
  54. }
  55. }
Explanation:
Read more articles on Arduino: