ir sensor

IR Sensor Working With Nodemcu | Nodemcu Tutorial

ESP8266 / NodeMCU IoT IoT Tutorial

Introduction

Hello guys, hope you are doing fine. I am back with another new tutorial on nodemcu. In this article, we are going to learn how an IR sensor works with an esp8266 nodemcu board. Before starting I want to give some details about the IR sensor, so basically it is an infrared sensor that uses infrared radiations to detect some object. in this nodemcu tutorial. According to science, every object emits some heat. This sensor is consists of one emitter and one receiver LED. You can’t see the radiations of light emitted by the IR sensor with the naked eyes but you can see it by using a camera. All the important materials like circuit diagrams and code are also given below for your convenience. You can also read our articles on Arduino and IoT. So without wasting more time let’s check out the working of the project.

Working of the Project

There are two ways in which we can use an IR sensor with the nodemcu tutorial that are analog connections and digital connection. Set the baud rate to 9600 for serial communication with the system. Define the pin at which you connect the LED. Initially, the LED will turn on. In the loop section, we are continuously reading the output which is generated by the IR sensor and printing the values on the serial monitor screen.

ir sensor

You can find the serial monitor at the top-most right corner of the Arduino IDE software. If the values are less than 900 then the LED will glow. You can change this value as per your need. Also, check out how an IR sensor works with the Arduino UNO board.

Software simulation

Components Required

  • ESP8266 nodemcu board
  • IR sensor
  • LED
  • 220-ohm resistor
  • Jumper wires and a breadboard
  • USB cable for uploading the code

Components Table/ buy link

S.NoComponent NameQuantityBuy Link
1.Nodemcu esp82661https://amzn.to/3EUGhuu
2.Nodemcu cable1https://amzn.to/3F1ywmu
3.IR Sensor1https://amzn.to/39BGyE9
4.Breadboard1https://amzn.to/39vsRX2
5.Jumper wire40https://amzn.to/39q43jr
6.LED1https://amzn.to/3ofjbsB
7.220 Ohm Resistor1https://amzn.to/39zSbeP

 

Circuit Diagram for the Project

  • for digital connection

ir sensor

Connection Table

Nodemcu esp8266IR sensor
Vin VCC
GGND
D1 PinOUT Pin
NodemcuLED 220 Ohm Resistor
D5 PinAnode  
 Cathode Terminal 1
GND  Terminal 2

Take the IR sensor and place it on a breadboard so that the sensor can standstill. Now connect three jumper wires to it. Connect the VCC pin of the IR sensor with the VIN pin of the nodemcu. Join the GND pin of the IR sensor with the GND pin of the nodemcu. Connect the OUT pin of the sensor with the digital-1 pin of the nodemcu. Take an LED and connect its positive leg with the digital-5 pin. Join the negative leg of the LED with the GND pin of the nodemcu via a 220-ohm resistor. Now your connections are complete and you can proceed to the code section.

  • for analog connection

ir sensor

Connection Table

Nodemcu esp8266IR sensor
Vin VCC
GGND
A0 PinOUT Pin
NodemcuLED 220 Ohm Resistor
D5 PinAnode  
 Cathode Terminal 1
GND  Terminal 2

Just repeat the same connections that you have done above but in this connect the OUT pin of the IR sensor with the analog-0 pin of the nodemcu tutorial.

PCBWay PCB Prototyping Services

I have assembled the whole circuit on a breadboard. As you know breadboard assembly is not effective for this type of projects. So, PCBWay offers Rapid PCB Prototyping for Your Research Work. I personally, recommend PCBWay because you can get your first-try boards right in 24 hours!

PCBWay website

The prototyping stage is the most critical period of time for engineers, students, and hobbyists. PCBWay not only makes your boards quick but also makes your job right as well as cost-effective. This greatly reduces your cost and shortens the time for developing your electronic products.

PCBWay can provide 2 Layer PCBs to highly advanced HDI and flex boards. Even though the PCBs they produce differ a lot regarding functionality and areas of use. I am impressed with the quality of the boards, the delivery time, and the cost-effectiveness.

Code for the Project

NOTE: Please upload the code which is given below to the nodemcu as it is.

  • digital connection code

 // TECHATRONIC.COM  
 int val = 0 ;  
 void setup()  
 {  
   Serial.begin(9600); // sensor buart rate  
   pinMode(14,HIGH);  // Led Pin Connected To D5 Pin    
 }  
 void loop()   
 {  
  val = digitalRead(5); // IR Sensor output pin connected to D1  
  Serial.println(val);  // see the value in serial m0nitor in Arduino IDE  
  delay(100);      // for timer  
  if(val == 1 )  
  {  
   digitalWrite(14,HIGH); // LED ON  
  }  
  else  
  {  
   digitalWrite(14,LOW); // LED OFF  
  }  
  }  
  • analog connection code

 // TECHATRONIC.COM   
 void setup()  
 {  
   Serial.begin(9600); // sensor buart rate  
   pinMode(14,HIGH);  // Led Pin Connected To D5 Pin   
 }   
 void loop()   
 {  
  int s1=analogRead(A0); // IR Sensor output pin connected to A0  
  Serial.println(s1);  // See the Value In Serial Monitor     
  delay(100);  
  if(s1< 900 )  
  {  
   digitalWrite(14,HIGH); // LED ON  
  }  
   else  
  {  
   digitalWrite(14,LOW); // LED OFF  
  }  
 }  

We hope that you like this tutorial and understand all the concepts that we are trying to explain in this article. If you have any doubts regarding this project then please ask them in the comments section below. Also, do check out more such tutorials on Arduino and Raspberry Pi.

video sample

HAPPY LEARNING!

Spread the love, Share this post with friends

Leave a Reply

Your email address will not be published. Required fields are marked *