Categories
DIY Projects Internet of Things (IoT)

Interfacing a Motion Detector Sensor with a Raspberry Pi 3

In the previous posts we discussed about why Internet of Things is the buzzword of the decade and how to get started working on a Raspberry Pi to fire up an LED light. But now we’re stepping up the game from lighting up LEDs to detecting motion.

Sitting in front of the laptop staring at my pi3 and the sensors I had just bought, I was wondering what to work on. Something that is pretty cool, not mundane, something that can be used as a building block for the upcoming projects, yet not very complicated for beginners.

Some brainstorming and my natural inclination towards image processing led me to interfacing the raspberry pi with the PIR motion detector sensor. One might wonder what a motion detector sensor has to do with images. Fret not, I have a camera module that is going to come in handy for the next building block.

The bigger picture I’m looking at, is to activate the camera module and detect a face in the frame when the motion detector sensor is triggered. Watch this space for more such building blocks, like installing opencv and learning more image processing, to lead up to the bigger picture.

Introduction to the sensor:

PIR sensor is a motion detection sensor that is used to detect whether a human has moved in or out of the sensor’s range. They are inexpensive, low power, small and easy to use. PIR is made of a pyroelectric sensor, (refer image) the rectangular crystal (which detects the level of IR radiation) covered with the round can. Ideal input voltage is 5V and the output signal when motion triggers the sensor is 3V.

The IR sensor in a motion detector is split in halves, because we’re only looking to detect motion, and not the IR levels. The sensor we are using use the BISS0001 chip. This chip takes the output of the sensor and does some minor processing on it to emit a digital output pulse from the analog sensor. Remember that the sensor can also be set off by your cat, just saying!

Motion Detector Sensor Raspberry Pi 3

Moving on to the project, let’s wire up the sensor to the Raspberry pi. You could connect the sensor directly to the GPIO pins of the pi, however we’re going to be connecting via a breadboard because it is easier to expand this project in the future. Remember that the sensor would work just as good, no matter which way you pick to connect it to the pi.

  1. Plug three M to F jumpers into the three pins on the PIR sensor.
  2. Plug the input pin (PIR-VCC) into the positive row of your breadboard, ground pin (PIR-GND) into the negative row, and output pin (PIR-OUT) into the other blank row.
  3. Connect the pin 6 (GPIO-GND) on the pi, to the negative row of the breadboard and PIR-OUT into any other blank row.
  4. Use another jumper wire to connect the pin 2 (GPIO 5V) on the pi to the positive rail of your breadboard (The same rail as PIR-GND wire)
  5. Connect the pin 26 (GPIO 7) to the same rail as the PIR-OUT. This is because we will be using pin 26 as an input to sense when the PIR detects motion.

Now that the hardware setup is done, let’s push some code into the pi so that it prints out a statement when the sensor senses movement.

https://gist.github.com/yayyme/88269e70674749580ca6b6c45f12b50c

The PIRs have a potentiometer inbuilt, that can be tuned to adjust the sensitivity of the sensor. Clockwise tuning increases the sensitivity, so if the print statements are running on an infinite loop, then tune the potentiometer in the anticlockwise direction.

Unfortunately, this method uses up a lot of computation power and makes sure the sensor keeps sensing continuously. We definitely don’t want that when we’re working on a more “real world” application. Since this is a pet project and something that we’re going to be using as a building block to a bigger project in the future, I’m inserting time.sleep() statement to not let the sensor work continuously.

Watch this space for more posts in the coming days. We will discuss on the architecture of Raspberry Pi, why I chose the Pi3 over Pi2, and more such topics on the foundation of IoT and Raspberry Pi. Also, do let me know if you have any questions or ideas!

Categories
DIY Projects Internet of Things (IoT)

How to Glow an LED using Raspberry Pi and Python

Today, most of the IoT based applications and products are powered by Raspberry Pi. If you haven’t yet read the previous article, about why IoT is the buzzword of the decade, do check it out! Raspberry Pi is a pocket sized affordable computer which is both fun to play around and build projects as per your ideas. You can plug any computer monitor or TV into the Pi for the display. To get started, you can further connect a standard keyboard and mouse to use it like a mini computer. In true sense, It’s as good as a full sized desktop computer. You can browse the internet, create documents and spreadsheets, watch high definition (HD) videos and play games as well. In this article, we look at a simple DIY Project on how to build your first IoT application using Python to glow an LED.

Raspberry Pi is pretty simple to get started. It provides you a practical approach to learn programming. If you’re familiar with Python, and know how to connect LEDs and resistors to a breadboard, you are good to go! However, if you’re not familiar with Python then I recommend going through this website.

Make sure you have a Raspberry Pi with Raspbian Jessie installed. The circuit is connected as shown below.

LEDs Glow IoT Demo

Connect the LEDs and Raspberry Pi to the breadboard as shown in the diagram using Jumper Wires. Hook up the Raspberry Pi to a monitor via the HDMI port on the board.

Launch the terminal and open ‘nano’ text editor, or you could use any text editor that is already familiar to you. This demo is as simple as a Hello World program. Running the following code will make the LEDs blink.

https://gist.github.com/techdotink/92216a99cf8d6596c3b1dee2b18b7261

Now that we have blinking LEDs, lets go ahead and build a silly game. Modify the current code that picks a random number from a given list, glows green LED when you guess the number correctly and glows the red LED if you’re wrong. Run the following code, happy playing!

https://gist.github.com/techdotink/1302f7b4ecdd2493322b296b9581bda2

The sheer form factor and hardware coupled with its ease of use, gives Raspberry Pi the capabilities to interact with everyday things/devices and makes it one of the favourite boards for IoT based products. People around the world are already making phenomenal tools using Raspberry Pi and also share them with the community.

I hope building your first ever IoT project was fun. Let us know what you think in the comments below. Keep checking this space for more projects in the coming days!