Pi Obstacle Detector

Hi guys, in this instructable we are going to use a PIR sensor for detecting obstacles.

So let us Jump into it.

Supplies:

  1. Raspberry Pi 4B (any variant)
  2. Raspberry Pi 4B compliant power supply
  3. 8GB or bigger micro SD card
  4. HDMI Monitor
  5. micro-HDMI Cable
  6. Mouse
  7. Keyboard
  8. Laptop or another computer to program the memory card
  9. PIR sensor - Buy
  10. Connecting wires (3x Female to Female Dupont wires)

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Preparing the Raspberry Pi 4

Install Shunya OS on Raspberry Pi 4

Shunya OS is an operating system like Android or Raspbian which runs on devices like Raspberry Pi, Orange Pi, Tinker board, etc.

1) Download Shunya OS from the official release site

2) Load Shunya OS on the SD-card using the steps given below:

  1. Right-click on the zip file downloaded and select Extract here
  2. Once the file is extracted double click on the extracted folder in which you will find the image file (OS file) and release info.
  3. Right-click on the image file(.img file)
  4. Select Open with -> Disk image writer
  5. Choose Destination as SD Card Reader
  6. Enter your password this will start loading the SD-card with Shunya OS. Be patient and wait for the process to complete (100%)

3) Insert micro SD card into Raspberry Pi 4.

4) Connect mouse & keyboard to Raspberry Pi 4.

5) Connect Monitor to Raspberry Pi 4 via micro-HDMI

6) Connect the power cable and Power ON the Raspberry Pi 4.

The Raspberry Pi 4 should boot up with Shunya OS. After the OS boots up you should see a login screen.
Here are the login details:

Username: shunya

Password: shunya

Install Shunya Interfaces on Raspberry Pi 4

Shunya Interfaces is a GPIO library for all boards supported by Shunya OS.

To install Shunya Interfaces we need to connect it to the wifi with internet access.

1. Connect to the wifi using the command

$ nmtui

2. Installing the Shunya Interfaces is easy, just run the command

$ sudo apt update && sudo apt install shunya-interfaces

Step 2: Understanding the GPIO Pins Raspberry Pi

GPIO (General Purpose Input and Output) pins are hardware pins which can be controlled by software on the Raspberry Pi 4.

GPIO pins are digital by nature and setting them HIGH or 1 by software will produce 5V or 3.3V at the hardware pin and setting then LOW or 0 by software will produce 0V at the hardware pin.

Raspberry Pi 4 contains 40 pin headers which are GPIO pins except for 14 special pins that provide Power (3.3V or 5V) or Ground (GND).

Step 3: Connect Stuff Together

Before we start,

Warning - Wrong connections can destroy your Raspberry Pi. Make sure you never connect Vcc and GND to each other (short circuit). Always recheck your connections before powering on the Raspberry Pi 4.

The PIR sensor module has 3 connections Vcc, GND and Out

  1. Connect Vcc to Pin 1 on the Raspberry Pi 4 Pin 1.
  2. Connect GND to Pin 6 on the Raspberry Pi 4 Pin 6.
  3. Connect Out to any Pin 40 on the Raspberry Pi.

Check your connections against the attached Diagram and the photos.

Step 4: Mini-intro to Shunya Interfaces

Shunya Interfaces is a C library, with syntax similar to Arduino. It runs on Shunya OS and is made for devices like Raspberry Pi, Orange Pi, Tinkerboard etc.

For writing code for the PIR sensor, we will be using 5 functions

  1. shunyaInterfacesSetup() - Initialize Shunya Interfaces library
  2. pinMode (physicalPin, INPUT) - Set GPIO pin as INPUT or OUTPUT
  3. digitalWrite(physicalPin, HIGH) - Write High or low to a GPIO pin
  4. digitalRead(physicalPin) - Reads GPIO pin
  5. delay(miliseconds) - Add delay in miliseconds

For more details on Shunya Interfaces API, checkout Shunya Interfaces API docs.

Step 5: Writing & Compiling Code

  • Download the code given below.
  • Compile it using the command
$ gcc -o pir pir.c -lshunyaInterfaces

Step 6: Let's Start Detecting Obstacles

To start detecting obstacles

  • Open Terminal Ctrl + Alt + T
  • And Run command
$ sudo ./pir

Whenever the obstacle gets detected, you will see the "Obstacle detected" printed.

Yatta! You did it!

Have you tried this yet? What will you do next? Take this and improve on it.

Be the First to Share

    Recommendations

    • CNC Contest

      CNC Contest
    • Make it Move

      Make it Move
    • Teacher Contest

      Teacher Contest

    Discussions

    0
    None
    PospaPunk

    5 weeks ago

    Since this is PIR module, that detects changes in emitted heat in quite wide angle (110° horizontally) for 3 to approx. 7 meters, it is quite inappropriate for detecting obstacles, especially when used on moving robot. And you are missing 2 minutes begin interval for proper initialization. Delay 100 is not enough, because when it detects change in heat, there is minimal time that output is set high. This way it is usable as motion sensor for static device, otherwise useless.
    I guess you wanted to use HC-SR04 ultrasonic range module. Using this you can measure distance of nearest obstacle (and compare with previous values).