IR proximity with Audio Warning in Node Red

 Here is an example of creating a IoT web application that outputs audio warning when an object is detected with IR proximity sensor. Also in this application visual warning through LED is provided. This can be helpful to setup warning system at home or industrial plants where object detection using IR proximity sensor is useful. 

This example here is extension of the previous tutorial IR Proximity Sensor IoT Application. The IR proximity sensor used here is self build with IR sensor and IR transmitter and LM358 operational amplifier. One can also use the IR proximity sensor module as illustrated in the tutorial IR sensor with Arduino, LCD, Buzzer and LED.

Arduino and IR proximity sensor

The following picture shows the Arduino and IR proximity sensor module build with help of breadboard.

The following shows the schematic diagram of Arduino and IR proximity sensor module.

IR sensor and transmitter with Arduino and LM358

As shown in the diagram above, the LM358 operational amplifier is used as comparator that compares the voltage at the inverting and non-inverting terminals. The IR sensor along with 10KOhm forming voltage divider is connected to the LM358 positive terminal pin 3. A 10KOhm potentiometer is connected to the negative terminal pin 2 of the LM358. When the voltage at the non-inverting terminal exceeds the inverting terminal voltage then output is high otherwise it is low. When an object is detected by the IR sensor LED(which is dark colored) due to reflection of the IR signal sent by the IR LED then the voltage of the non-inverting terminal gets higher than at the inverting terminal and the output is high. In normal state the output from the LM358 is low so the green LED is turned on and when there is object detection, the output is high and the red LED is turned on and the green LED is turned off.  

The output of the LM358 is connected to the Arduino pin 2. The Arduino monitors the state of the output of LM358. The Arduino sends either OFF or ON message via its serial port(USB) to the PC. This is illustrated in the animation video below

Although the above video shows LDR(Light Dependent Resistor), it works also with IR proximity sensor.

Arduino Program

The following is IR proximity Arduino code which detects objects and sends either Off or On message to the serial port.


 const int irPin = 2;  // IR pin

void setup () {
  pinMode(irPin, INPUT);
  Serial.begin(9600);
}

void loop(){
  if (digitalRead(irPin) == HIGH){
    Serial.println("ON");
  }
  else{  
    Serial.println("OFF");
  }
}
  

Node Red 

The following is the node red program that receives incoming data from the serial port and makes Audio alarm when object is detected. When there is no object detection by the IR sensor then audio output is "No Object Detected" and when there object detection then the output is "Object Detected".

In the node program,we have also visual and text indicators. The LED color is green when there is no object detection and the LED color becomes red when object is detected. Similarly text is also displayed which is either "No Object Detected" or "Object Detected".

The serial in node configuration is shown below.


 The text control function node configuration is shown below.


The text control code is below.


var m = msg.payload.trim();

if(m == 'ON'){
    msg.payload = "Object Detected";
}
else{
    msg.payload = "No Object Detected"
}
return msg;

 The led control function node configuration is shown below.


The led control code is below.


if(msg.payload == 'Object Detected'){
    msg.payload = false;

}
else{
    msg.payload = true;
}
    return msg;

 The led node configuration is shown below.

The text input node configuration is shown below.

The audio output node configuration is shown below.

When the dashboard is launched via localhost:1880/ui you should see the following interface.

When you place an object in front of the IR proximity sensor then the LED will turn red and you will hear Object Detected audio alarm.

The following video illustrates this.

 



Post a Comment

Previous Post Next Post