How to Use IR Infrared Sensor With Arduino
by STEAM-DIY in Circuits > Arduino
811 Views, 10 Favorites, 0 Comments
How to Use IR Infrared Sensor With Arduino
Welcome back to my blog. We described the PIR(Motion) sensor in the previous post. Today we are going to talk about the IR sensor modules in this post. This sensor is the most important for making robots. I promise to present those projects in the next articles.
1st Sensor type
This sensor has four pins. The DO pin of the sensor returns a digital value and the AO pin returns an analog value. If you want to change the sensing range, do so with the preset control.
The PIN structure of this sensor
Supplies
- Arduino Uno board
- IR sensor
- LED bulb x 2
- Buzzer x 1
- Breadboard
- Jumper Wires
Firstly, identify these components.
Diagram
Diagram
Code
copy and paste the following program to the Arduino IDE.
This code sets the 2nd pin as the input pin and the 3,4,5 pin as the output pin.
This code takes the digital value and puts it into the Boolean variable. The Boolean variable is named ‘led’. This value is checked using the IF condition. When the digital value is 0, the green LED bulb turns on and when the digital value is 1, the yellow LED bulb turns on.
This code takes the analog value and it put into the integer variable. The integer variable is named “buzzer”. This value is checked using the IF condition. If the analog value is less than or equal to 80, the buzzer turns on.
Now, select the board and port. After, click the upload button.
2nd Type of IR Sensor
2nd sensor type
Ok, let’s see how to use another type of IR sensor with Arduino.
The PIN structure of this sensor
So, step by step we will learn how to connect this sensor with Arduino. For that, the required components are given below.
- Arduino Nano board
- IR sensor
- LED bulb
- Breadboard
- Jumper Wires
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Step 1
Firstly, identify these components.
Step 2
Attach the components to your breadboard.
Step 3
Connect these components to your Arduino board using the jumper wires. Do it according to the circuit diagram below.
Connect the sensor OUT pin to the Arduino board D2 pin and connect the LED anode pin to the D3 pin.
Step 4
Now, copy and paste the following program to the Arduino IDE.
- The complete program of this project – Download
This code sets 2 pins as input pins and 3 pins as output pins.
Takes the digital value and puts it into the Boolean variable. The Boolean variable is named ‘value’.
This value is checked using the IF condition. When the digital value is 1, the LED bulb turns on and when the digital value is 0, the LED bulb turns off.
Step 5
Now, select the board and port. After, click the upload button.
So, now you can test this sensor. The full video guide is given below.
IR infrared sensor with Arduino – How does work IR infrared sensor
3rd sensor type
This sensor includes three pins. Also, we can get a digital value through this sensor. This sensor can also be used to detect black and white as well as obstacles. Also, we can control the sensing range using the potentiometer on this sensor.
The PIN diagram of this sensor
So, step by step we will learn how to connect this sensor with Arduino. For that, the required components are given below.
- Arduino Nano board —
- Line tracking sensor —
- LED bulb x 2 —
- Buzzer x 1 —
- 180-ohm resistor x 2 —
- Breadboard —
- Jumper Wires —
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Step 1
Firstly, identify these components.
Step 2
Secondly, connect these components. For that, use the circuit diagram below.
Step 3
Thirdly, we will create the required program for this. It is as follows.
- The complete program of this project – Download
Code explanation
Firstly, this code defines the sensor, LED, and buzzer-connected pins.
Secondly, this code sets the sensor pin as the input pin and the buzzer, LED pin as the output pin.
In the loop function, the sensor values are taken and put into the Boolean variable. After, these values are then checked using the IF condition. Then if the value is 0, the buzzer and green LED bulb will be activated. If the value is 1, the red LED bulb will be activated.
Step 4
Lastly, select the board and port. Afterward, upload this code.
OK, now you can test this sensor. For that, use a black and white surface. The full video guide is given below. So, we will meet in the next tutorial. Have a good day.
Please subscribe my channel thanks