Book Image

Arduino Development Cookbook

By : Cornel M Amariei
Book Image

Arduino Development Cookbook

By: Cornel M Amariei

Overview of this book

Table of Contents (16 chapters)
Arduino Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Detecting motion – PIR sensor


Ever wondered how those motion sensors work? Usually, we find them in lights that turn up when we move. Almost all of them use a simple and common sensor called a Passive Infrared (PIR) sensor.

Here, we will build one of those annoying movement-sensitive lights, using an Arduino, its built-in LED, and a PIR sensor.

Getting ready

The following are the ingredients needed to execute this recipe:

  • An Arduino board connected to a computer via USB

  • Jumper wires

  • A PIR sensor

How to do it…

Hooking up a PIR sensor is easy:

  1. It has three terminals. One is the voltage input, one is the ground, and the last one is the data pin.

  2. Connect the ground on the PIR to one of the GND pins on the Arduino. Connect the voltage input to 5V, and finally, connect the data pin to one digital pin. Here we used pin 2.

Schematic

This is one possible implementation using pin D2 as the input:

Here is an example of how to wire it:

Code

The following code will read the value of the digital pin on which the PIR...