Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Arduino Home Automation Projects

You're reading from   Arduino Home Automation Projects Automate your home using the powerful Arduino platform.

Arrow left icon
Product type Paperback
Published in Jul 2014
Publisher
ISBN-13 9781783986064
Length 132 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Marco Schwartz Marco Schwartz
Author Profile Icon Marco Schwartz
Marco Schwartz
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Building Wireless XBee Motion Detectors 2. Control Lights from Your Phone or Tablet FREE CHAPTER 3. Measuring the Temperature Using Bluetooth 4. Weather Station in the Cloud with Xively 5. Monitor Your Energy Consumption in the Cloud 6. Hack a Commercial Home Automation Device 7. Build Your Own Home Automation System Index

Interfacing the PIR sensor with Arduino

First off, you are going to leave XBee aside and simply check if the motion sensor is working correctly. What you will do in the first sketch is print out the readings from the motion sensor on the serial port. This is the complete code for this part that you can just copy and paste in the Arduino IDE:

// Simple motion sensor
int sensor_pin = 8;

void setup() {
  Serial.begin(9600);
}

void loop() {
  
  // Read sensor data
  int sensor_state = digitalRead(sensor_pin);
  
  // Print data
  Serial.print("Motion sensor state: ");
  Serial.println(sensor_state);
  delay(100);
}

Tip

Downloading the example code and colored images

You can download the example code files and colored images for this Packt book that you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Let's see what this code does. It starts by declaring the pin on which the sensor is connected, in our case 8. In the setup() function of the sketch, we initialize the serial connection with the computer, so we can print out the results on the serial monitor.

Then, in the loop() part of the sketch, we read out the state of the motion sensor using a simple digitalRead() command, and store that result into a variable. This state is then simply printed out on the serial port every 100 ms.

You can now upload the sketch to your Arduino board and open the serial monitor. This is what you should see:

Motion sensor state:0
Motion sensor state:1
Motion sensor state:1
Motion sensor state:1
Motion sensor state:0
Motion sensor state:0

If you can see the state of the sensor changing when you wave your hand in front of it, it means that the sensor is working correctly and that you can proceed to the rest of the project.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime