Hardware Reference
In-Depth Information
Interfacing the PIR sensor with Arduino
First off, you are going to leave XBee aside and simply check if the motion sensor is work-
ing 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 topic that you
have purchased from your account at http://www.packtpub.com . If you purchased this topic
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 connec-
ted, in our case 8. In the setup() function of the sketch, we initialize the serial connec-
tion with the computer, so we can print out the results on the serial monitor.
Search WWH ::




Custom Search