Tuesday, April 26, 2016

Beaglebone Black motion Detector with Swift and the HC-SR501 Sensor

In an earlier post I showed how we could use the Swift on the Beaglebone Black to read the state of a button to turn a LED on or off.  While this is a good start for using Swift with the Beaglebone Black, it really is not that exciting therefore in the next couple of posts I would like to show how we can interact with various sensors.  In this post I will show how we could use the HC-SR501 sensor to create a motion detector.

Before we start here are a couple of links that you may want to look at:
    -   You can look at this post to see how to install Swift on your Beaglebone Balck and also how to use the SwiftyGPIO library
    -  The datasheet for the HC-SR501 sensor can be found here.
    -   You can get the SwiftyGPIO library here.

 The first thing we need to do is to wire the HC-SR501 sensor to our Beaglebone Black.  The following diagram shows how we would connect the sensor to our Beaglebone Black.





One thing with the HC-SR501 sensor, is there really isn't a good way to have it stand up by itself therefore I made a stand that I printed using my Monoprice Maker Select 3D printer.  You can find the STL file for the sensor on thingiverse here.  The following image is a picture of my setup:




Now that we have everything wired up we can write our application that will interact with the HC-SR501 sensor through a GPIO port on the Beaglebone Black.  We need to start by creating a directory for our code.  Lets name this directory motion_detector.  We would create this directory with the following command  mkdir motion_detector

In the motion_detector directory copy the SwiftyGPIO.swift file from SwiftyGPIO library.  Now create a file named main.swift and put the following code in it:

     import Glibc

     let gpios = SwiftyGPIO.getGPIOsForBoard(.BeagleBoneBlack)
     var sensor = GPIO(name:"GPIO_60", id: 60)
    
     sensor.direction = .IN
    
     while(true){
          if sensor.value == 0 {
     print("No Motion")
          } else {
             print("INTRUDER")
          }
          usleep(100000)
     }

In this file we start off by importing the GLibc module.  In the next line we retrieve the list of GPIOs available for the BeagleBone Black.  We then get a reference to GPIO_60 (pin 12 of the P9 expansion header).  

The next line configures the port direction for the GPIO port.  We can use GPIODirection.IN or GPIODirection.OUT here.  In this example we used GPIODirection.IN because we want to read the state of the port.  Next we create a while loop.  Within the while loop we check the state of the port and if it is low (value of 0) we print the message “No Motion” to the console letting us know there was no motion detected otherwise we print the message “INTRUDER” to the console letting us know that the sensor detected motion.  We then use the usleep function to pause before we loop back.

To compile this application we use the following command:

    swiftc –o sensor SwiftyGPIO.swift main.swift 

This command uses the swift compiler to compile SwiftyGPIO.swift and main.swift and writes the output to the file named sensor.  We are now able to run our application.  The following command will run our application.

    sudo ./sensor

If everything is connected correctly the application will print either “No Motion” or “INTRUDER” to the screen depending on if it detects motion or not.


1 comment: