In this issue: 1. Arduino Motion Detector Project In issues #1 and #2 I showed you how you can use an Arduino single board microcontroller, an LCD/Keypad shield and an ultrasonic echo module to build a contactless distance measurement device. In this issue we add some code to analyze successive distance measurements and an LED to indicate motion. When a moving object is detected an LED is lit for a few seconds to let you know. Home DIY Electronics HDE Issue #3 Arduino Motion Detector The Arduino is a microcontroller board based on an open source design. The hardware is low cost and available from a number of sources. It’s perfect for your electronics projects. Click here to see the range of inexpensive Arduino hardware. Everyone has to have at least one big data disaster. Catastrophic loss of important information teaches the importance of regular and frequent backups. Yes it’s happened to me and I know a lot of people who have been hit even harder than I have. Welcome to HDE the Magazine from Home DIY Electronics. Hi, I’m Steve. I’m an engineer and I’ve specialised in embedded software for over 40 years. Issue #3 adds motion detection to the Arduino Distance Sensor project from previous issues. Follow along with my projects as I build them. I’m going to release them here before I put them on the web site so be sure that you get your free subscription and you won’t miss an issue. Subscribe To HDE Free YOUR TITLE HERE Home Diy Electronics is a free magazine for people like you who build electronics projects. Make sure that you are on my mailing list so that you never miss an issue. Click here to subscribe to the HDE list. Click here to contact Steve. Thoughts Solderless Breadboards for DIY Electronics Projects February 21st 2014
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
In this issue:
1. Arduino Motion Detector Project
In issues #1 and #2 I showed you how you can use an Arduino single board microcontroller, an LCD/Keypad shield and an ultrasonic echo module to build a contactless distance measurement device. In this issue we add some code to analyze successive distance measurements and an LED to indicate motion. When a moving object is detected an LED is lit for a few seconds to let you know.
THE
Home DIY Electronics HDE Issue #3
Arduino Motion Detector
The Arduino is a microcontroller board
based on an open source design. The
hardware is low cost and available from a
number of sources. It’s perfect for your
electronics projects.
Click here to see the range of inexpensive
Arduino hardware.
Everyone has to have at least one big data
disaster.
Catastrophic loss of important information
teaches the importance of regular and frequent
backups.
Yes it’s happened to me and I know a lot of
people who have been hit even harder than I have.
Welcome to
HDE the Magazine from Home DIY
Electronics. Hi, I’m Steve.
I’m an engineer and I’ve specialised in embedded software for over 40 years. Issue #3 adds motion detection to the Arduino Distance Sensor project from previous issues. Follow along with my projects as I build them. I’m going to
release them here before I put them on the web site so be sure that you get your free subscription and you won’t miss an issue.
Subscribe To HDE Free
YOUR TITLE HERE Home Diy Electronics is a free magazine for people like you who build electronics projects. Make sure that you are on my mailing list so that you never miss an issue. Click here to subscribe to the HDE list. Click here to contact Steve.
Thoughts
Solderless Breadboards for DIY Electronics Projects
I want to use the output to fire a battery operated water gun so that it soaks the cats and drives them away before they have time to leave me any unwanted packages.
Because I’m going to be firing a water gun with a limited supply of water, I can’t afford to have it firing on false triggers very often which would waste ammunition and battery power. This means that I must manage the sensitivity carefully to make sure that it only fires when it should. A task which is easier said than done but I’ve had some success. My cat repeller must work in daylight as well as at night in the dark. Traditional passive infra-red motion detectors are not very good at this. They work by sensing heat from animals and humans so they can become much less sensitive on a bright hot day. An ultrasonic sensor should be consistent day and night because it relies on sound waves rather than electromagnetic infra-red radiation. So let’s get started.
Arduino Motion Detector
Hardware The hardware is the same as it was in issue #1 with the addition of an LED and a 470Ω resistor. Connect the resistor to the Arduino pin 13 with the other end to the Anode of the LED. Connect the cathode of the LED to Gnd. Like this:
In issue #1 I showed you how to hook up an Arduino Uno, an LCD/Keypad shield and an HC-SR04 Ultrasonic Echo module. By loading a little software code onto the Arduino you built a working Ultrasonic distance sensor. Issue #2 developed the software sketch to add more display modes and to introduce the use of the keypad on the shield to switch between modes. In this issue we takes things a little further by adding an LED indicator and a new version of the software to add motion detection to the project.
If you haven’t already built the Arduino Distance sensor from issue #1 or from the website then do it now.
Detection specification There are a number of things that you need to think about before you begin to build any project. The most important question that must be answered is “What will it be used for?” Now your reason for building this may be different to mine but I’m working towards using this system as part of an animal deterrent to stop cats pooping in my yard.
Some processing of the readings is required to provide a reliable motion detection system that will detect when an animal wanders in front of the sensor but doesn’t trigger falsely when all is still. There are many ways to tackle this problem and my way may not be the simplest or best. What I can say however is that it works and it isn’t too difficult to understand so that’s the one I went with. To deal with the problem of distance flipping the code averages the values from multiple readings. But we still need to detect changes due to motion so two averages are computed. One with a long time constant and another with a shorter time constant. The longer average serves as a base line. A normal no-motion value will only change slowly when there is movement in view. The shorter averaged value reacts faster to movement so when the difference in the two averages is computed and it exceeds a threshold value we can determine that there is motion. To further increase the robustness of the system the code triggers only when objects are detected approaching the sensor. I figured that there is little point triggering when the cat is moving away from the sensor.
Definitions First we define how many readings to be used in each of the two averages readings.
Well there isn’t much to say about the additional hardware except to make sure that you get the LED in the correct way around. You can wire up the two components on the HC-SR04 breadboard conveniently.
Software Now for the code. If you want to get on and download the code sketch to your Arduino right now you can find it at the end of this issue or you can get the most up to date sketch from the motion detector software page. The motion detection code enhances the Arduino Distance Sensor project software described in issues #1 and #2. You may want to review those issues before proceeding to the new motion code here.
Overview The hardware detection system consists of a distance finder which delivers a new range value every 200mS. Turning this system into a motion detector is a matter of measuring the difference between readings. It isn’t sufficient however to simply take the difference between successive readings because as we have seen, the range finder can often flip Between multiple echoes from objects at different distances.
#define MOTION_BASE_SIZE 24
#define MOTION_CURRENT_SIZE 6
I’ve set the base line average at 24 readings or 4.8 seconds. The fast average is set to 6 readings or 1.2 seconds. You might want to try different values and tune the characteristics of the motion sensor to your requirements. Here are the variables used by the motion detection logic: volatile int
motion_base_array[MOTION_BASE_SI
ZE];
volatile int
motion_current_array[MOTION_CURR
ENT_SIZE];
volatile int motion_base;
volatile int motion_current;
volatile bool motion_detected =
false;
First the two averaging arrays are defined and sized accordingly. Then two variables to hold the results of the averaging followed by a Boolean flag used to indicate that motion has been detected. All these variables are declared volatile because they are being used in both the interrupt service routines and the background loop.
Declaring variables volatile guarantees that the variable storage is read each time the value is required otherwise the optimising compiler would make mistakes.
Motion detection The main motion detection logic is contained in the function motion_detector() which is called from the echo_interrupt() function. The operation of the motion_detector() is simple. First the distance value made available by echo_interrupt() is added to each of the two arrays. New values overwrite the earliest entries in the arrays. The average of each array is computed by adding all the elements and dividing by the size of the respective array. The two averages are compared and if the difference exceeds a threshold then the motion_detected flag is set. Motion triggering only happens when the current value is less than the base line value. The motion_detected flag is then processed to generate an output in the timer interrupt service routine
Motion output The task of turning on an LED for 2 seconds when motion is detected is handled by the motion_output() function.
The routine is called from the timerIsr(). This timer interrupt service routine is called every 50 micro seconds so the first thing that the motion_output() does is count 2000 calls before executing the processing code. The code is only required to run every 100mS so there is no point running it any more often than this. The LED flash is generated by a Finite State Machine in the motion_output() function. I love using state machines as I’m sure you will notice if you follow my projects. The “state” variable and the two timer counters are declared static in this function so that they hold their value between calls. The state machine is very simple:
Everything that you could ever want to know about Arduino
When motion is detected the LED is turned on and the machine enters state 1. When the “On Timer” times out the LED is turned off and the machine goes to state 2. When the “Off Timer” times out the machine reverts back to state 0 ready to react to more motion detection. State 2 has been included to provide a quiet time after the LED has flashed. This helps to prevent multiple triggering of the LED and anything else that might be controlled by the output.
Display A new display mode has been added to display the variables used in the motion detection.
The display_motion_detector() routine is called from loop() and can be selected by multiple clicks of the “Select” button on the Keypad. A new mode has been added to do_button_input() to allow the selection.
display_motion_detector() calls display_histogram() to show the histogram on the top line of the display as described in earlier issues.
The second line of the display is filled by the new function display_motion_vars(). Both the base line average distance value and the current average value are displayed.
Conclusion It works which is good enough for me. It functions in daytime just as well as it does at night. It detects when animals and people approach and it isn’t overly sensitive. The problem that I set out to solve has a solution. .
That doesn’t mean that it’s the best solution however. There is still room for improvement by tweaking some of the variables or even developing better algorithms that improve upon this simple system. Why don’t you see if you can improve the code yourself. .
In future issues I shall be connecting the motion detector to a relay to enable it to do things like sound an alarm or even fire a battery operated water gun. I have it in mind to construct a device to stop cats pooping on my property. Look out for this project. .
Introduction To Basic Electronics An honest review of this downloadable
course
I go through all of the material in this product to highlight both bad and the good points. Overall it’s a good beginners introduction to the how and why of
electronics. Find out how this information can help you get the most from your electronic project building.