Friday, March 3, 2017

Motion sensor integration with a Raspberry PI


  1. Why ?
    • Background
      • I am a cloud solutions architect, and work from home....
      • I have customized my work space to make it comfortable and productive
      • I have done various projects with the raspberry pi, and have a bunch of spare hardware lying around
    • I just wanted to do some cool automation in my work space using a raspberry pi
    • The end result seems pretty simple, but , as you'll see in the demo... I walk into my work space, and get greeted "Welcome to the study room" !
  2. How ?
    • Hardware needed
      • Raspberry PI model B 
      • Motion sensor detector
          • I used the HC-SR501, but you can use whatever you like... I'd bought five of these for $10 about six months ago on amazon.
      • 1p-1p connector wires (female to female)
        • You can buy 40 of these for a $. We just need 3 wires in this experiment.
      • USB keyboard
      • HDMI cable and monitor
      • Normal computer speakers
      • Ethernet connectivity to the PI
    • Now the fun stuff ..
      • Prepare the software load on the PI
        • I used the NOOBS build (2.2.0 at the time of the blog), and then installed the  full Raspbian (debian jessie) build on the PI
        • Connect your USB keyboard, LAN cable, HDMI cable, Audio out from the Raspberry PI to your peripherals
        • Just get your IP address when the PI boots up, and then you can ssh and scp to it... I won't bore you with the minor details
          • The default  login is pi/raspberry , and you will have to enable ssh login by either using the "sudo raspi-config" or use the UI to enable ssh logins
      • Wire the hardware

        • You can review the pin layouts of the sensor and the PI as provided above, and match the three wires i used (red, yellow, black) to get a better understanding.
        • Red wire
          • Sensor - Power in
          • PI - Pin 14 (DC out)
        • Yellow Wire
          • Sensor - Hi/Lo output (DC out)
          • PI- Pin 26 (GPIO 7)
        •  Black Wire
          • Sensor - Ground
          • PI  - Pin 16 (Ground)
        • ..
      • Program the PI
        • scp the mp3 or m4a audio file you want to play when motion is detected. I called mine study-room.m4a and tested out my audio connection to speakers with the following command
          • omxplayer -o local study-room.m4a
        • Create the following file (e.g. final.py) using vi or scp or whatever tool you like, and run it : "sudo python final.py" 
        • import RPi.GPIO as GPIOimport time
          import os

          GPIO.setmode(GPIO.BCM)
          PIR_PIN = 7
          GPIO.setup(PIR_PIN, GPIO.IN)

          def MOTION(PIR_PIN):
                         print 'Motion Detected!'
                         os.system("omxplayer -local study-room.m4a")

          print 'PIR Module Test (CTRL+C to exit)'
          time.sleep(2)
          print 'Ready'

          try:
             GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=MOTION)
                         while 1:
                             time.sleep(100)
          except KeyboardInterrupt:
                         print ' Quit'
                         GPIO.cleanup()
      • Test it out
        • Trigger the motion sensor and watch the output on the screen and the audio on the speaker
        • Fine tune the motion sensor hardware , if needed
      •  Complete the setup
        • Added a wifi usb adapter and configured it, so that i can remove the wired lan cable
        • Added the command sudo python /home/pi/motion-test/final.py & to /etc/rc.local and modified the python code to provide the full path to the .m4a file - os.system("omxplayer -local \/home\/pi\/motion-test\/study-room.m4a")
    • Demo
    • What's next ?
      • Amazon Echo Dot integration...
      • Add more sensors ......
      • Turn on a lamp, if its dark and motion detected....

    No comments:

    Post a Comment