Photoelectric Infrared Proximity Arduino NodeMCU

Photoelectric/Infrared Proximity Sensor Tutorial

In this tutorial, I will examine about photoelectric switch or usually called an infrared proximity sensor.
This sensor has more distance range and more stable compared with the small infrared/obstacle avoidance sensor I ever examine here. Since this sensor is not disturbed by sunlight. So you can use this sensor outdoors without any problem.

Just like another infrared sensor, this sensor also use it’s own light reflection to an object or obstacle to tell if there’s something in front of this sensor.

We can also adjust the distance of the object we want to detect or the sensor sensitivity with the potentiometer on the sensor. Since this is very simple and easy to use. It just gives us a digital signal, this sensor has 3 pins. The VCC and GND are for the power source and one more for the signal.

Photoelectric/Infrared Proximity Wiring Diagram with Arduino

Photoelectric Infrared Proximity Sensor with arduino
Photoelectric Switch/Infrared Proximity Sensor with Arduino

Let’s hook up this sensor to an Arduino. VCC and Ground as usual just connect them to 5V and GND. Then signal pin to pin 2 of Arduino. You can use any pins for this.

The Code with Arduino

Since this sensor just outputs a digital signal, then we can easily read the digital signal on the pin we are already connected to. So I modify the digitalRead Arduino Example sketch, we use pin 2. So I will just leave it, I will just rename the variable to photoelectric.

/*
  Read digital pulse form photoelectric sensor/Infrared
  by miliohm.com
*/

// digital pin 2 has a pushbutton attached to it. Give it a name:
int photoElectric = 2;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(photoElectric, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int sensorState = digitalRead(photoElectric);
  // print out the state of the button:
  Serial.println(sensorState);
  delay(1);        // delay in between reads for stability
}

Upload the code to Arduino and open the Serial Monitor.

Then I test it by putting my hand in front of the sensor, and it works. When my hand is in range or detected by the sensor. The red LED turned on. and in Serial Monitor will print “0”. Otherwise, when there is no object detected it will print “1”.

Adjust The Sensor Distance/Sensitivity

You can turn the potentiometer on the back of the sensor to the left to decrease the distance, and turn it to the right to increase the sensor distance to the object.

Photoelectric/Infrared Proximity Wiring Diagram with Arduino

If you are using NodeMCU, you can do it just the same way as Arduino, since the sensor just outputting a digital voltage. But this sensor needs 5V to power up. So you still need 5V from an external power source (since NodeMCU doesn’t have one).

Photoelectric Infrared Proximity Sensor with NodeMCU
NodeMCU with Photoelectric switch wiring diagram

In the video tutorial, I use an Arduino to get 5V.

The Code with NodeMCU

The code is just the same, except the pin name. Since on NodeMCU I use the pin D2.

/*
  Read digital pulse form photoelectric sensor/Infrared
  by miliohm.com
*/

// digital pin 2 has a pushbutton attached to it. Give it a name:
int photoElectric = D2;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(photoElectric, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int sensorState = digitalRead(photoElectric);
  // print out the state of the button:
  Serial.println(sensorState);
  delay(1);        // delay in between reads for stability
}

And that’s it. This is the end of the tutorial. Very easy right?

Here’s the complete video tutorial.