Using Motion Sensor HC-SR501

  • I try a project using motion sensor.
    if motion detected, turn on a LED.

    Program write in Python.

    # -*- coding: utf-8 -*-
    import time
    import onionGpio
    from datetime import datetime
    # LED GPIO
    led_pin = 0
    pir_pin = 9
    sleeptime = 100000
    led_light_up_time = 1
    led = onionGpio.OnionGpio(led_pin)
    pir = onionGpio.OnionGpio(pir_pin)
    while True:
        value = pir.getValue().rstrip()
        if(value == "1"):
            print "motion_detected"

    It seem work well.
    but a problem is when press Ctrl-C to stop running,
    then try run again, some time cause ”Resource busy“ error.

    root@Omega-6F83:~# python
    Traceback (most recent call last):
      File "", line 17, in <module>
      File "/usr/lib/python2.7/", line 158, in setInputDirection
        ret         = self._setDirection(_GPIO_INPUT_DIRECTION)
      File "/usr/lib/python2.7/", line 134, in _setDirection
        status      = self._initGpio()
      File "/usr/lib/python2.7/", line 46, in _initGpio
    IOError: [Errno 16] Resource busy

    May be onionGpio not release some "Resource" after Ctrl-C.
    How can I fix it?

  • Another amateur here, running in to the same issue.

    For example here, running the Python example, stopping it and trying to run again throws IOError.

    Something needs to be freed, but what?

  • @Sr.-Sibale

    The IOError can happen if you Ctrl-C during a GPIO write/update operation before it unexports the GPIO (this is built-in). As mentioned in the tutorial, you can run a shell script like so:

    echo 1 >/sys/class/gpio/unexport
    echo 2 >/sys/class/gpio/unexport
    echo 3 >/sys/class/gpio/unexport
    echo 11 >/sys/class/gpio/unexport
    echo 18 >/sys/class/gpio/unexport
    echo 19 >/sys/class/gpio/unexport
    echo 0 >/sys/class/gpio/unexport

    Replace the pin #s with the GPIOs you want to free up for reuse.


  • What am I missing? I get
    echo 1 >/sys/class/gpio/unexport
    -bash: echo: write error: Invalid argument

  • @Peter-Carr remove the space before and after ">" so:

    echo 1>/sys/class/gpio/unexport

  • @Venet Removing the space converts that line to something completely different (and not working for the purpose)!

    echo 1 >/sys/class/gpio/unexport

    writes a letter 1 into the file /sys/class/gpio/unexport (which frees gpio1 for re-use, and that's what @Gabriel-Ongpauco wanted to show).

    whereas in

    echo 1>/sys/class/gpio/unexport

    the 1> without a space means redirecting standard output (2> would mean redirecting standard error output), and is eqivalent to just

    echo >/sys/class/gpio/unexport

    (because both 1> and > mean redirecting standard output)
    What this does is writing an empty line to /sys/class/gpio/unexport (with no effect regarding gpios at all)

    @Peter-Carr The "write error: Invalid argument" is because gpio1 was not exported in the first place, thus could not be unexported.

  • @luz you are right of course! I was trying for hours to setup arduino-dock (v.1) on my Omega2+ and getting that same error. Mistyped it without the spaces and that error disappeared. As expected arduino-dock still did not work :smiley:

Log in to reply

Looks like your connection to Onion Community was lost, please wait while we try to reconnect.