2.2.5 IR Obstacle Avoidance Sensor

Introduction

In this project, we will learn IR obstacle avoidance module, which is a sensor module that can be used to detect obstacles at short distances, with small interference, easy to assemble, easy to use, etc. It can be widely used in robot obstacle avoidance, obstacle avoidance trolley, assembly line counting, etc.

../_images/2.2.5IR_Obstacle1.png

Schematic Diagram

../_images/IR_schematic.png

Experimental Procedures

Step 1: Build the circuit

../_images/2.2.5fritzing.png

Step 2: Go to the folder of the code.

cd /home/pi/raphael-kit/nodejs/

Step 3: Run the code.

sudo node ir_obstacle.js

After the code runs, when you put your hand in front of the module’s probe, the output indicator on the module lights up and the “Detected Barrier!” will be repeatedly printed on the screen until the your hand is removed.

Code

const Gpio = require('pigpio').Gpio;

const ir_ob = new Gpio(17, {
   mode: Gpio.INPUT,
   pullUpDown: Gpio.PUD_DOWN,
   edge: Gpio.FALLING_EDGE
});

ir_ob.on('interrupt', () => {
   console.log('Detected Barrier!');
});

Code Explanation

const Gpio = require('pigpio').Gpio;

const ir_ob = new Gpio(17, {
   mode: Gpio.INPUT,
   pullUpDown: Gpio.PUD_DOWN,
   edge: Gpio.FALLING_EDGE
});

Import the pigpio module, create an object to control the IO port Gpio17, Set it to input mode and interrupt on falling edge.

ir_ob.on('interrupt', () => {
   console.log('Detected Barrier!');
});

When an interrupt is triggered, meaning an obstacle is detected, print “Detected Barrier!”.

Phenomenon Picture

../_images/2.2.5IR.JPG