Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
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.
Required Components¶
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Raphael Kit |
337 |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
Schematic Diagram¶
Experimental Procedures¶
Step 1: Build the circuit
Step 2: Go to the folder of the code.
cd ~/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!”.