Here a simple basic IR proximity sensor based Internet of Things(IoT) application is illustrated. The infrared proximity sensor(IR) is build using IR sensor LED, IR transmitting LED, LM358 comparator and some passive components. Then the IR proximity sensor is connected to Arduino. When object near the IR sensor is detected, the Arduino sends this information to the PC via serial USB. On PC web application is written in Node-Red to monitor and show the IR proximity sensor state on the web. Anyone within local LAN or on the internet can see whether an object near the IR proximity sensor. So this is a simple IoT application example build using Node Red, Arduino and self build IR proximity sensor.
Hardware Connection
The hardware required are Arduino Uno(or Nano, Mega), IR transmitter LED and IR sensor LED. LM358 comparator IC, 10KOhm potentiometer, a LED, 220Ohm resistor, 10KOhm resistor, breadboard and connecting wires.
The following shows interfacing of LM358 IC, potentiometer,IR transmiiter, IR sensor Arduino schematic diagram.
In the above circuit diagram, the black LED is IR sensor LED and the light blueish gray LED is the IR receiver LED. The IR LED works in the invisible light wavelength which is higher than 700nm. For example, CCTV commonly use 850nm IR LED and television remote controls uses 950nm IR LEDs . These two should be close to each other.
The IR transmitter LED transmits invisible IR light and when it hits an object, light is reflected which is detected by the IR sensor LED. The IR transmitter LED is connected across +5V to ground with a 220Ohm resistor. The black IR sensor LED along with a 10KOhm forms a voltage divider the junction of which is connected to the positive terminal pin 3 of the LM358 comparator chip. One should note that the negative terminal of the IR sensor should be connected to the +5V and the positive terminal is connected to one end of the 10KOhm resistor. The 10KOhm middle pin is connected to the negative terminal pin 2 of the LM358 IC. The pin 4 and pin 8 of the LM358 chip are connected to ground and +5V rails on the breadboard. The +5V and ground are connected to the Arduino +5V and ground respectively. So the power for the The output of the LM358 which is pin 1 is connected to pin 2 of Arduino Uno .
At the output of LM358 comparator, two LEDs, green LED and red LED are connected . The green LED and red LED are connected to the LM358 output in opposite direction. That is the green LED positive terminal is connected to 5V and negative terminal is connected to the LM358 output pin. So in normal state when there is no object detection, the green LED is turned on because the output of the LM358 is low. Similarly the red LED positive terminal is connected to the LM358 and its ground is connected to the ground rails so that when object is detected, LM358 output is high and the red LED turns on. These LEDs emits visible light and are not IR leds. The green LED has wavelength in the range 520nm to 555nm and the red LED has wavelength in the range 640nm to 700nm. Also although not used and not shown these LEDs should have a series 220Ohm connected.
The LM358 is connected as a comparator and so when the input at its positive terminal is higher than the input at its negative terminal the output gets high. The potentiometer can be adjusted such that the voltage input at the negative terminal can be varied. When the IR sensor LED detects objects, the voltage at the junction between it and the 10KOhm resistor changes and when this junction voltage is higher than the voltage set by the potentiometer voltage at the negative terminal then the output becomes high. When the output gets high the green LED connected to the LM358 gets high and turns on, indicating that the IR sensor has detected an object.
The Arduino is programmed to detect high level. When Arduino detects high value at its digital pin 2 then it sends string message ON serially to PC over the USB. Otherwise Arduino will send OFF message to the PC.
Arduino Program
The following is the Arduino program code which monitors the state of the pin 2 and sends ON or OFF message serially depending upon whether the IR sensor detects object or not.
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");
}
}
The above code should be loaded into Arduino. The COM port should also be noted which is COM37 in this example. Also note the baud rate which is 9600bps. After the code is uploaded, the serial monitor should show OFF when there is not object infront of the IR proximity detector. When an object is placed infront of the IR detector, the message ON should be displayed on the serial monitor.
Red Node Programming
The next step is to write red node program that receives message from the serial USB and display the object detected event on the browser.
The IR proximity sensor flow diagram is shown below.
It consist of serial in node, split node, function node, text node and a led node. The serial node, the text node and led is not available in new node red installation. So you have to install it which is very easy to do. It was explained how to install serial library and dashboard library in the previous tutorial Node Red with Arduino Simple Example. To install any new node library, go to menu then click on manage palette.
To install the any library select the install tab and there search for
the library. For example to install serial library and dashboard library.
To install the led library for using led node search for led and install the node-red-contrib-ui-led. This is illustrated below.
Following the above IR proximity sensor node red flow diagram above, drag and create the flow.
In the serial in node, search and configure the com port to which your Arduino is connected to which is COM37 in this example.
In the split node choose split using /\n/ as shown below.
In the function node, write the following function.
var m = msg.payload.trim();
if(m == 'ON'){
msg.payload = "Object Detected";
return msg;
}
else{
msg.payload = "No Object Detected";
return msg;
}
This function node receives payload from the split node and removes any white spaces using the split() function. Then it compares the incoming string message with ON string. If the incoming message is ON then we send Object Detected otherwise we send No Object Detected.
For text node and led node we will first create tab and groups because this is required in order to use the dashboard library.
Create the tab and group from the dashboard>layout as shown.
Click the Deploy button and then Start.
Next go to the localhost:1880/ui or click on the link icon on the dashboard tab. You should see the following dashboard.
So this is accessible inside your LAN which is useful for home based IR proximity detection system.
Or you can also view it on the public web on custom domain like ee-diary.ga URL which anybody can see as shown shown below.
To make the node red application publicly available see the tutorial Deploy Arduino Red Node App on Web with Cloudflare Tunnel.
Now when an object is detected, the message displayed changes to Object Detected and the LED color changes to Red as shown below.
Watch the following video which shows how this IR proximity IoT application build with Node Red IoT framework works.
Below is the IR proximity Red Node flow. If you don't know how to import and export flow in red node then see the tutorial How to save node red flows in local directory.
[
{
"id": "8abd7d340e4a2cc1",
"type": "tab",
"label": "Flow 5",
"disabled": false,
"info": "",
"env": []
},
{
"id": "dd66cdc8f15fff74",
"type": "serial in",
"z": "8abd7d340e4a2cc1",
"name": "",
"serial": "36594b5169de2c99",
"x": 130,
"y": 220,
"wires": [
[
"a5d70558f49f0f37"
]
]
},
{
"id": "27d13c71a4d57b8c",
"type": "function",
"z": "8abd7d340e4a2cc1",
"name": "function 1",
"func": "var m = msg.payload.trim();\n\nif(m == 'ON'){\n msg.payload = \"Object Detected\";\n return msg;\n}\nelse{\n msg.payload = \"No Object Detected\";\n return msg;\n}\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 420,
"y": 220,
"wires": [
[
"e5dfedc072f35da8",
"697fe0e4c115dbf9"
]
]
},
{
"id": "e5dfedc072f35da8",
"type": "ui_text",
"z": "8abd7d340e4a2cc1",
"group": "216a6ac98d1ff24d",
"order": 1,
"width": 0,
"height": 0,
"name": "",
"label": "",
"format": "{{msg.payload}}",
"layout": "row-left",
"className": "",
"x": 590,
"y": 220,
"wires": []
},
{
"id": "a5d70558f49f0f37",
"type": "split",
"z": "8abd7d340e4a2cc1",
"name": "",
"splt": "/\\n/",
"spltType": "str",
"arraySplt": 1,
"arraySpltType": "len",
"stream": false,
"addname": "",
"x": 270,
"y": 220,
"wires": [
[
"27d13c71a4d57b8c"
]
]
},
{
"id": "697fe0e4c115dbf9",
"type": "ui_led",
"z": "8abd7d340e4a2cc1",
"order": 1,
"group": "93bab989f17505b6",
"width": "1",
"height": "1",
"label": "",
"labelPlacement": "right",
"labelAlignment": "right",
"colorForValue": [
{
"color": "#ff0000",
"value": "Object Detected",
"valueType": "str"
},
{
"color": "#008000",
"value": "No Object Detected",
"valueType": "str"
}
],
"allowColorForValueInMessage": false,
"shape": "circle",
"showGlow": true,
"name": "",
"x": 590,
"y": 280,
"wires": []
},
{
"id": "36594b5169de2c99",
"type": "serial-port",
"serialport": "COM37",
"serialbaud": "9600",
"databits": "8",
"parity": "none",
"stopbits": "1",
"waitfor": "",
"dtr": "none",
"rts": "none",
"cts": "none",
"dsr": "none",
"newline": "\\n",
"bin": "false",
"out": "char",
"addchar": "",
"responsetimeout": "10000"
},
{
"id": "216a6ac98d1ff24d",
"type": "ui_group",
"name": "Message",
"tab": "835a1b82dad0688d",
"order": 1,
"disp": false,
"width": "4",
"collapse": false,
"className": ""
},
{
"id": "93bab989f17505b6",
"type": "ui_group",
"name": "LED",
"tab": "835a1b82dad0688d",
"order": 2,
"disp": false,
"width": "1",
"collapse": false,
"className": ""
},
{
"id": "835a1b82dad0688d",
"type": "ui_tab",
"name": "IR Proximity Sensor",
"icon": "dashboard",
"order": 5,
"disabled": false,
"hidden": false
}
]
So this red node tutorial showed how one can build simple IR proximity sensor web application with node red as Internet of Things framework. We can also build other types of IoT application such as Node Red IoT with Arduino DHT11 humidity and temperature display.