Contents
The Infrared (IR) sensor module is a sensor module that can detect obstacles using reflected infrared light. This module has two main parts, the IR Transmitter and the IR Receiver. The IR transmitter functions to reflect infrared light to obstacles or objects, then processed by the receiver. In the market, the infrared sensor module is present as shown in the picture below.
The Infrared sensor works is by utilizing the infrared reflection from the transmitter. When an object exists in front of the sensor, infrared light from the transmitter will be blocked by the object and will be reflected towards the receiver. When an object is detected by the sensor, it will produce a logic “0” output and when an object is not detected, the sensor outputs a logic “1”. The infrared sensor has 3 pins, namely VCC, GND, and OUT (see picture above). In this tutorial, the sensor module is used to detect an object then mark the LED as an indicator. To better understand this tutorial, let’s do this simple experiment:
Tools and materials for IR sensor-NodeMCU Experiment
- NodeMcu
- Infrared sensor
- Resistor 220
- LED
- Male-female jumper cables as needed
Then make a circuit like in the picture below:
Please be notice with the pin configuration:
• The VCC pin is connected to the 5V pin of the NodeMCU.
• The GND pin is connected to the NodeMcu’s GND pin.
• The OUT pin is connected to the D5 pin of the NodeMCU.
• The LED pins are connected to the D6 pin of NodeMCU
IR Sensor-NodeMCU Source Code
Next, open your Arduino IDE then write this super simple codes:
#define pinIR D5 //Declare sensor pin on D5 #define Led D6 //Declare Led pin on D6 void setup() { Serial.begin(115200); pinMode(pinIR, INPUT); //Declare pin D5 as input pinMode(LED, OUTPUT); //Declare pin D6 as output Serial.println("IR Detection"); delay(1000); } void loop() { int datasensor = digitalRead(pinIR); //instructions for monitoring digital values Serial.print("Sensor Value: "); Serial.println(datasensor); if (datasensor == LOW) { //instruction to activate the LED when an object is detected Serial.println("Object Detected"); digitalWrite(LED, HIGH); delay(2000); } else { Serial.println("No Object"); //instruction to activate the LED when an object is detected digitalWrite(LED, LOW); delay(2000); } }
Upload the code above by pressing the upload button or via CTRL+U hotkey. Please be aware that Tools-Board menu should be set in “NodeMCU 1.0 (ESP-12E Module)”. Then activate the ‘Serial Monitor’ windowin the Arduino IDE via “Tools – Serial Monitor” menu. Make sure the USB cable between the laptop and Arduino remains plugged in. After the program is finished uploading, see the digital value displayed on the Serial Monitor window, related with the LED behaviour.
The code above determines controlling the LED to be ON or OFF based on digital value of the infrared sensor output. If the digital value is logic HIGH then the LED will turn on, but if the digital value is logic LOW then the LED will be active. If the results of your experiment match the results in this tutorial, then you have successfully applied the infrared sensor as an object detector. You can develop this experiment according to your needs. Thank you reading this tutorial. See u next time