2.2.5 IR Obstacle Avoidance Module

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.

../_images/2.2.5component.png

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Raphael Kit

337

Raphael Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

GPIO Extension Board

BUY

Breadboard

BUY

Jumper Wires

BUY

Obstacle Avoidance Module

BUY

Schematic Diagram

../_images/IR_schematic.png

Experimental Procedures

Step 1: Build the circuit.

../_images/2.2.5fritzing.png

Step 2: Change directory.

cd ~/raphael-kit/c/2.2.5/

Step 3: Compile.

gcc 2.2.5_IrObstacle.c -lwiringPi

Step 4: Run.

sudo ./a.out

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.

Note

If it does not work after running, or there is an error prompt: "wiringPi.h: No such file or directory", please refer to Install and Check the WiringPi.

Code

#include <wiringPi.h>
#include <stdio.h>

#define ObstaclePin      0

void myISR(void)
{
   printf("Detected Barrier !\n");
}

int main(void)
{
   if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen
      printf("setup wiringPi failed !\n");
      return 1;
   }

   if(wiringPiISR(ObstaclePin, INT_EDGE_FALLING, &myISR) < 0){
      printf("Unable to setup ISR !!!\n");
      return 1;
   }

   while(1){
      ;
   }

   return 0;
}

Code Explanation

void myISR(void)
{
   printf("Detected Barrier !\n");
}

Define a function myISR() to print obstacle detected , indicating that an obstacle is detected.

if(wiringPiISR(ObstaclePin, INT_EDGE_FALLING, &myISR) < 0){
   printf("Unable to setup ISR !!!\n");
   return 1;
}

This wiringPiISR() function registers a myISR() function to received interrupts on the specified ObstaclePin.

When ObstaclePin changes from high to low, it means that an obstacle is detected. At this time, call the myISR() function to print “Detected Barrier !”

The prototype of this wiringPiISR() function is shown below.

int wiringPiISR (int pin, int edgeType,  void (*function)(void)) ;

The edgeType parameter is either INT_EDGE_FALLING , INT_EDGE_RISING , INT_EDGE_BOTH or INT_EDGE_SETUP . If it is INT_EDGE_SETUP then no initialisation of the pin will happen – it’s assumed that you have already setup the pin elsewhere (e.g. with the gpio program), but if you specify one of the other types, then the pin will be exported and initialised as specified.

For more information, please refer to: wiringPi-Functions (API).

Phenomenon Picture

../_images/2.2.5IR.JPG