A very cheap, easy to assemble, easy to use Infrared sensor with a long detection distance and has less interference by visible light. The implementations of modulated IR signal immune the sensor to the interferences caused by the normal light of a light bulb or the sun light. This sensor has a screwdriver adjustment to set the appropriate detected distance to make it useful in many applications, and then gives a digital output when it senses something within that range. This sensor does not measure a distance value. It can be used for collision avoidance robot and machine automation. The sensor provides a noncontact detection.
Pin configuration
Brown Wire – 5V to Arduino
Blue Wire – GND to Arduino
Black Wire – Pin 5 to Ardunio
/*E18-D80NK Infrared Distance Ranging Sensor*/ void setup() { Serial.begin(9600); //Start serial communication boud rate at 9600 pinMode(5,INPUT); //Pin 5 as signal input } void loop() { delay(500); if(digitalRead(5)==LOW) { Serial.println("Collision Detected."); } else { Serial.println("No Collision Detected."); } }
The below code acts like a race time sensor for laps. The LCD shield is used and the PIN for time sensor is moved to PIN 0 (instead of PIN 5 above).
First time the collision is detected, the lap timer starts. The next time there is a collision detection, the current timer is moved as ‘Prev’ and a new timer is started
#include <LiquidCrystal.h> #include <Time.h> // initialize the library with the numbers of the interface pins LiquidCrystal lcd(8, 9, 4, 5, 6, 7); time_t elapsedTime=0, prevTime=0; void setup() { Serial.begin(9600); //Start serial communication boud rate at 9600 pinMode(0,INPUT); //Pin 5 as signal input // set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print a message to the LCD. //lcd.setCursor(0,0); //lcd.print("Prev Lap: "); lcd.setCursor(0,1); lcd.print("Curr Lap: "); elapsedTime = -1; setTime(elapsedTime); } void loop() { delay(200); if (elapsedTime != -1) elapsedTime = now(); if(digitalRead(0)==LOW) { if (elapsedTime != -1) { prevTime=elapsedTime; } elapsedTime=0; setTime(elapsedTime); Serial.println("Collision Detected."); } else { Serial.println("No Collision Detected."); } if (elapsedTime != -1) { lcd.setCursor(9,1); lcd.print(second(elapsedTime));lcd.print(" "); lcd.setCursor(9,0); lcd.print(second(prevTime));lcd.print(" "); } }
Output
Sensor gives 0 when it detects any object in front of the sensor otherwise 1. It does show the distance of the object