In this sensor tutorial, you are going to learn some basics about Infrared Sensors, IR sensors. You will learn how to use the IR sensors and send values to the Serial Monitor. There are a couple of variants for the IR sensor. There are IR sensors with three or four pins. The version of the four pins allows you to digital values as well as analog values. IR sensors with three pins usually are only digital.
What you will need to know?
You should have basic knowledge about breadboards and digital / analog input. You can read about this at How to use a Breadboard and Arduino Tutorial: 4.0 Introduction into digital / analog pins.
For this tutorial we will use several commands. One of the commands is to read analog values. You can read about this command at Arduino Tutorial: 4.7 Reading analog values.
The Digital and Analog IR sensors.

TCRT5000 sensor that allows you to read analog and digital input.
The TCRT5000 can detect color and distance. The sensor emits IR rays and then detects if it receives a reflection. This sensor is often used in line following robots, edge detection. The sensor allows you to sense white or black surfaces by reading the analog value from it. The measuring distance is between 1 and 8 mm. The onboard potentiometer can be used to adjust the sensitivity.

Infrared Sensor that allows you to read only Digital input.
It emits a pulse of infrared light and then detects the angle at which that light is reflected. The farther away an object is, the lower the output voltage. If the sensor receives no reflection, the output voltage of the sensor will be 0 V. If the object is 10 cm or closer, the output voltage will be equal to 5 V. (In this experiment, we are supplying 5V to the sensor.)
Parts you will need

Arduino Uno

BreadBoard

Jumper Wires

Infrared sensor
Setup of the BreadBoard
The first connection that you will make is to connect the power and ground to the BreadBoard. You will use the 5V output for this project. Most IR sensors are also suitable for receiving 3.3V.

Connecting the IR sensor
After the you connected the 5V and Ground you are ready for connecting the IR sensor. Both of the IR sensors are connected to the 5V rail and the Ground rail. Furthermore, the analog IR sensor is attached to pin A0 while the digital IR sensor is connected to pin 4 of the Arduino.

The Code
// Reading analog Infrared Sensor values // https://www.arduinoplatform.com const int analogPin = A0; // Analog input pin that the receiver is attached to const int digitalPin = 4; // Analog input pin that the receiver is attached to int analogValue = 0; // value read from the Analog receiver int digitalValue = 0; // value read from the Digital receiver void setup() { pinMode(analogPin, INPUT); pinMode(digitalPin, INPUT); // initialize serial communications Serial.begin(9600); } void loop() { // read the analog in value: analogValue = analogRead(analogPin); digitalValue = digitalRead(digitalPin); // print the results to the serial monitor: Serial.print("Analog Sensor = "); Serial.println(analogValue); Serial.print("Digital Sensor = "); Serial.println(analogValue); //set the threshold which works best for you if (analogValue < 100) { //checks if object is there or not Serial.println("Object Detected by Analog Sensor"); } else { Serial.println("No object in Front of Analog Sensor"); } delay(500); if (digitalValue == HIGH) { //checks if object is there or not Serial.println("Object Detected by Digital Sensor"); } else { Serial.println("No object in Front of Digital Sensor"); } delay(500); }
Code Explanation
Declare the variables
First you are going to declare the variables that you will use in this project. The project is not that hard to code however, there are a couple of things to remember. First of all, you need to define the pins which the IR sensors are attached to. Secondly, the received value of the reading needs to be stored. Therefore, for both sensors integeres are made.
// Reading analog Infrared Sensor values // https://www.arduinoplatform.com const int analogPin = A0; // Analog input pin that the receiver is attached to const int digitalPin = 4; // Analog input pin that the receiver is attached to int analogValue = 0; // value read from the Analog receiver int digitalValue = 0; // value read from the Digital receiver
Read out the analog values
To be able to interpret the digital and analog sensor you will need to read the senor. This achieved by using analogRead and digitalRead. Next, the values are printed to the Serial Monitor.
For the analog values you will see values ranging from 0-1023. You can use this sensor for Distance measurement. However, keep in mind that if the distance is the same the value that is read can be different depending on the color of the material that is in front of the sensor. With the same distance, black material will read a much higher value than white material.
// read the analog in value: analogValue = analogRead(analogPin); digitalValue = digitalRead(digitalPin); // print the results to the serial monitor: Serial.print("Analog Sensor = "); Serial.println(analogValue); Serial.print("Digital Sensor = "); Serial.println(analogValue);
Print messages depending on sensor value
//set the threshold which works best for you if (analogValue < 100) { //checks if object is there or not Serial.println("Object Detected by Analog Sensor"); } else { Serial.println("No object in Front of Analog Sensor"); } delay(500); if (digitalValue == HIGH) { //checks if object is there or not Serial.println("Object Detected by Digital Sensor"); } else { Serial.println("No object in Front of Digital Sensor"); } delay(500);