A Raspberry Pi Movement Sensor written 5 years ago

Raspberry Pi Logo

I’ve had my pi for a couple weeks now, and one of the things I wanted to play with was the GPIO connectivity of the RPi; it seemed quite novel that you can interact with real world electronic things from within a computer.

As I see the RPi as being an always-on device - most likely embedded in something - I wanted a way for it to know when there were people nearby, so it could wake up (turn on a monitor/screen most likely) and start doing something useful. I purchased this PIR sensor from Rapid as it seemed to fit the bill of: having a digital (on/off) type of output; working off 3.3-5v; having some kind of embedded circuitry that meant which would mean that wiring it up would be easy.

I should mention now that I really don’t understand electronics, and that this has mostly been an exercise in making it up.

PIR SensorThe Raspberry Pi with GPIO plugged inThis is not how to do wiring, kids
15 of PIR Sensor; The Raspberry pi all nestled in Wires; And definitely not the way to do electronics.

First thing was to identify which pins to connect the various wires to. I happened upon this eLinux page which gave a suggestion of which pins were the GPIO ones, but curiously introduces a numbering scheme (GPIO 0-7) which doesn’t correspond to the numerical values linux uses to identify the pins. I fashioned up a test LED and after cycling through the pins, I figured :

GPIOPin Name/sys/class/gpio/gpioXWire Colour
GPIO0P1-11/sys/class/gpio/gpio17
GPIO1P1-12 /sys/class/gpio/gpio18Grey Wire
GPIO2P1-13 /sys/class/gpio/gpio21
GPIO3P1-15 /sys/class/gpio/gpio22
GPIO4P1-16 /sys/class/gpio/gpio23White Wire
GPIO5P1-18 /sys/class/gpio/gpio24
GPIO6P1-22 /sys/class/gpio/gpio25
GPIO7P1-07 /sys/class/gpio/gpio4Orange Wire

To simplify things, I wrote some (hilariously short) bash scripts to initialise, read, and turn off/on the pins:

init_gpio.sh (Must be run as root)


    echo $1 > /sys/class/gpio/export
    echo $2 > /sys/class/gpio/gpio$1/direction

gpio_set.sh


    echo $2 > /sys/class/gpio/gpio$1/value

gpio_read.sh


    cat /sys/class/gpio/gpio$1/value

And set the Pi in an infinite loop, reading out the value of pin 23:


    ./init_gpio.sh 23 in
    while true
    do
        ./read_gpio 23
        sleep 0.2
    done

This printed out a never ending stream of zeroes to the screen.

I started off by naively connecting the Sensor straight to the RPi, attaching the GND pin to ground, the Vdd pin to 3v and the Out pin to P1-07 and staring at the screen to see if the stream of zeroes would yield a one. This, mostly failed. I decided that the squiggles on the wiring diagram really did mean something about needing resistors.

Those zigzag line bits - they turn out to be resistors, and are generally needed.

I discover the R-Pi GPIO Tutorials and I learn of pull-up and pull-down resistors. These exist to: 1. Protect the board from short circuits; and 2. Make things work (More science is needed here.). I scrobble out some resistors - a 10KOhm one as a pull-down resistor; and a 150ohm as a pull-up (?!) resistor. 150ohms was chosen mostly because it was one that I had to hand, and I think if we do some maths of I=V/R, we get 3.3 / 150 = which works out at 22 mA. Which is a number that doesn’t seem too massive. It does appear to be more than the 100 micro-Amps written on the circuit board. Hmm, should maybe revisit this! Anyway..

I asked in the R-Pi forums what resistors to use and they advised that a 100kohm and a 39kohm would be the way to go.

How I wired up the PIR Sensor.

As wired up with the above, I was graced by a 1 on screen whenever I waved my hand infront of the sensor. Hurrah!

As a noddy test, I rigged up an LED to one of the other GPIOs and made it turn on when movement was detected:

detect_movement.sh


    MOVEMENT_PIN=23 #white wire
    RED_PIN=4       #orange wire
    YELLOW_PIN=18   #grey wire

    ./init_gpio.sh $MOVEMENT_PIN in
    ./init_gpio.sh $YELLOW_PIN out

    while true do
    do
            movement=`./gpio_read.sh $MOVEMENT_PIN`
            ./gpio_set.sh $YELLOW_PIN $movement
            echo $movement
            sleep 0.2
    done

And to prove it’s all true, a short video:

To-Do List

  1. Find out if the 150ohm resistor is.. bad;
  2. Figure out if there’s a way to do event driven interrupts - rather than having a while..true loop. I tried inotifywait on the virtual file..but it didn’t work..
  3. Do something useful with it!

← previous entry | next entry → Sun 20th May 2012 - 22:54 | 1 comment | tagged with Raspberry Pi, Computery Stuff

 Comments 1 comment made

David’s GravatarDavid 4 months later

Hi,

Thanks for this. Did you ever get an interrupt version to work?

Leave a reply

Name: displayed next to your comment
Email: will not be displayed
URL: to link back to your site
Comments: Markdown syntax allowed

Notes on leaving replies: