Reader small image

You're reading from  Mastering Arduino

Product typeBook
Published inSep 2018
Reading LevelBeginner
PublisherPackt
ISBN-139781788830584
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Jon Hoffman
Jon Hoffman
author image
Jon Hoffman

Jon Hoffman has over 25 years of experience in the field of information technology. Over these years, Jon has worked in the areas of system administration, network administration, network security, application development, and architecture. Currently, Jon works as a senior software engineer for Syn-Tech Systems. Jon has developed extensively for the iOS platform since 2008. This includes several apps that he has published in the App Store, apps that he has written for third parties, and numerous enterprise applications. He has also developed mobile applications for the Android and Windows platforms. What really drives Jon the challenges that the field of information technology provides and there is nothing more exhilarating to him than overcoming a challenge. Some of Jon's other interests are spending time with his family, robotic projects, and 3D printing. Jon also really enjoys Tae Kwon Do, where he and his oldest daughter Kailey earned their black belts together early in 2014, Kim (his wife) earned her black belt in December 2014, and his youngest daughter Kara is currently working towards her black belt.
Read more about Jon Hoffman

Right arrow

Motion Sensor

In this chapter, we will look at how to use the HC-SR501 motion sensor. It is a very easy sensor to connected to the Arduino and program, which is why it is usually one of the first sensors that people experiment with when they start working with microcontrollers. It is also very inexpensive and often comes with most starter kits.

In this chapter you will learn:

  • How to connect an HC-SR501 motion sensor to the Arduino
  • How to read the output of the HC-SR501 motion sensor
  • Read the Fritzing diagram of the motion sensor project

Introduction

PIR sensors, also known as Passive Infrared sensors, are used by a microcontroller to sense motion usually by a human being, but they will detect any motion within the range of the sensor. These sensors are small, inexpensive, low-power and easy to use, which makes them perfect for beginners to experiment with, but industrial versions of these sensors can also be found in many consumer and military products as well.

PIR sensors are made of pyroelectric sensors that can detect infrared radiation levels. Every object that has a temperature above absolute zero emits some low-level infrared radiation that the pyroelectric sensor can detect. The passive part of the name means that the sensor does not generate or radiate energy that can be detected by other devices. Instead, it works by detecting the infrared radiation emitted by other objects.

The pyroelectric sensor in...

Components needed

We will need the following components for this chapter's project:

  • One Arduino Uno or compatible board
  • One HC-SR501 motion sensor
  • Jumper wires
  • For the challenge, you will need an LED
  • One breadboard
The breadboard is optional because you may connect the HC-SR501 motion sensor directly to the Arduino. You will need a breadboard to complete the challenge section of this chapter.

Circuit diagrams

The following diagram shows the Fritzing diagram for this project:

With the diagram, we can see that the ground pin on the HC-SR501 motion sensor is connected to the ground rail on the breadboard and the 5V input on the motion sensor is connected to the power rail on the breadboard. The power and ground rails of the breadboard are connected to the 5V power and ground pins on the Arduino.

The output pin on the motion sensor is a digital output (either HIGH or LOW), therefore we can connect the output pin directly to any of the digital pins on the Arduino. In this case, we are connecting the output pin from the sensor to pin three on the Arduino.

Here is the schematic diagram of the same circuit:

Let's look at the Arduino code for this project.

Code

To use the HC-SR501 motion sensor all we need to do is read the digital output from the sensor. If the output is HIGH, then the sensor detected motion and if it is LOW, then it did not. The output from the sensor will stay HIGH for the length of time defined by the output time adjustment screw. I usually keep the output time low, usually a couple oof seconds.

For this project, we will output the status of the sensor to the serial console. The output will get a little fancier in the challenge section.

The following is the code to read the HC-SR501 motion sensor:

#define MOTION_SENSOR 3

void setup() {
 pinMode(MOTION_SENSOR, INPUT);
  Serial.begin(9600);
}
void loop() { int sensorValue = digitalRead(MOTION_SENSOR); if (sensorValue == HIGH) { Serial.println("Motion Detected"); } delay(500); }

This code starts off by using the #define directive to create...

Running the project

We will need to have the serial console open when we run the code to see the output.

Once the code is uploaded and running, wave your hand near the sensor, and you should see the Motion Detected message is printed to the console, as shown in the following screenshot:

Now on to the challenge section of this chapter.

Challenge

The challenge, if you choose to accept it, is to add an LED to this project and have the LED light up when the motion sensor detects motion. In the first few chapters, we will give either the code or circuit diagram for the challenges to make it easier to troubleshoot any issues if the project does not work when first built.

For this challenge, we will give the code and let you figure out how to connect the LED to the project. One hint, do not forget about the resistor for the LED.

The following is the code that will light up an LED connected to pin 5 of the Arduino when the motion sensor detects nearby motion:

#define MOTION_SENSOR 3
#define LED 5

void setup() {
  pinMode(MOTION_SENSOR, INPUT);
  pinMode(LED, OUTPUT);

  digitalWrite(LED, LOW);
  Serial.begin(9600);
}
 
void loop() {
  int sensorValue = digitalRead(MOTION_SENSOR);
  if (sensorValue == HIGH) {
    Serial...

Summary

In this chapter, we learned about the HC-SR501 motion sensor and how it works. We saw how we could wire it to an Arduino where the Arduino provided power for the sensor and also read the output pin of the sensor.

In the next chapter, we will see how we can sense the weather around us.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Arduino
Published in: Sep 2018Publisher: PacktISBN-13: 9781788830584
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.
undefined
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 $15.99/month. Cancel anytime

Author (1)

author image
Jon Hoffman

Jon Hoffman has over 25 years of experience in the field of information technology. Over these years, Jon has worked in the areas of system administration, network administration, network security, application development, and architecture. Currently, Jon works as a senior software engineer for Syn-Tech Systems. Jon has developed extensively for the iOS platform since 2008. This includes several apps that he has published in the App Store, apps that he has written for third parties, and numerous enterprise applications. He has also developed mobile applications for the Android and Windows platforms. What really drives Jon the challenges that the field of information technology provides and there is nothing more exhilarating to him than overcoming a challenge. Some of Jon's other interests are spending time with his family, robotic projects, and 3D printing. Jon also really enjoys Tae Kwon Do, where he and his oldest daughter Kailey earned their black belts together early in 2014, Kim (his wife) earned her black belt in December 2014, and his youngest daughter Kara is currently working towards her black belt.
Read more about Jon Hoffman