Sunday, February 28, 2016

PIR Motion Detector With Arduino: Operated at Lowest Power Consumption Mode

This Project demonstrates quick and easy approach of reducing power consumption by around 60-70% for ATMEGA 328P based Arduino boards  interfaced with PIR or PID sensors for motion detection. [Part B]

As a comparison, standard digital input pin StateChangeDetection sketch (Examples- Digital- StateChangeDetection) is used to investigate differences in power consumption of PIR sensor module and arduino nano. [Part A]


We will learn following things-
1) Interfacing Pyroelectric/Passive Infra red (PIR) motion sensor with arduino (or any microcontroller)
2) Using Interrupts of arduino boards (here Arduino NanoV 3.0 clone ) to detect output of PIR sensor
3) Extending battery life or reducing power consumption by putting arduino nano into deep sleep or hibernation when motion is not detected by PIR sensor.



Pyroelectric/Passive infrared (PIR) sensors are widely used in Automatic doors or lights, surveillance cameras, toys, night lamps, automatic cameras etc. We can easily recognize such PIR sensors around us, because these sensors mostly have typical hemispherical dome with honeycomb like small facets (Fresnel lens) all around its body.
Rattlesnakes have natural "invisible heat" or "infrared radiation" sensor on its head, It can sense extremely tiny variations in surrounding temperatures. This helps the snake to detect its prey without actually "seeing" it, just by "sensing" the warmth of the body of its prey. Electronic PIR sensors work in similar manner.

PIR sensors are used to generate electronic signal output upon detection of any human/animal movement within the vicinity of sensor's detection range. These sensors detect minute changes in surrounding temperature emitted in the form of infrared radiation (IR radiations) by our bodies. Every object above above absolute zero degree temperature ( −273.15°) emits IR radiations. Special kind of pyroelectric materials are used in these sensors which generate temporary electrical voltage depending on variations in its temperature. Therefore, PIR sensors are called passive sensors, i.e. they do not consume energy for detection purpose; but they catch energy (Infrared radiations) emitted by other bodies . This makes PIR sensors useful for applications where mechanical, magnetic or light sensors cannot be used for one or the other reason.

The PIR sensor used in this project is purchased from banggood.com with following features:-

*Detection range of 3 to 7m, 
*Possibility to adjust its output ON state duration from 5 to 200 sec 
*Operating input voltage between 4.5 to 20 V. 
*Predicted idle state current consumption is less than 60 micro-Amps. 
*Output of PIR sensor is 3.3V TTL logic level making it compatible also with ESP8266, arduino Due (apart from arduino uno, nano etc) and other boards operating at 3.3V logic levels. No need of logic level converters.


Typical operation is as follows-
1) Output remains LOW in idle state
2) Output goes HIGH (3.3V) when motion is detected by sensor i.e. if anybody moves within its detection range
(Detection range or Sensitivity can be adjusted by Sx or sensitivity potentiometer)
3) Output remains HIGH for fixed time set by Tx or time setting potentiometer.
The output of sensor can be given as intput to microcontrollers, processors, logic level ICs, data acquisition systems, MOSFETs etc.

Connection diagram and components:


Connections are shown in fritzing circuit diagram on the left. Since, we will be using hardware interrupt of arduino nano board, I have connected output of PIR sensor to pin D2 (or interrupt 0). Arduino nano has two interrupt pins, pin D2 and D3 for interrupt 0 and 1 respectively.
We will read PIR sensor output using "hardware interrupt" functionality of arduino. Interrupts help in letting our arduino do its regular job or sleep into deep hibernation mode until some signal is received at its interrupt pins. This lets us use arduino perform other tasks or remain in "nearly OFF state" while waiting for signal at interrupt pins. This helps in reducing power consumption of arduino and sensor system.
Bill of materials (BoM)-
1) PIR sensor
2) 1) Arduino Nano V 3.0 board ( or other boards can be used, provided interrupt pins are selected accordingly- read more )
-For Uno, Nano, Mini, other 328-based boards- pins 2 (int 0) ,3 (int 1)
-For Mega, Mega2560, MegaADK- pins 2, 3, 18, 19, 20, 21
-For Micro, Leonardo, other 32u4-based- pins 0, 1, 2, 3, 7
-For Zero, MKR1000- all digital pins, except pin 4
-For Due- all digital pins
2) Breadboard or PCB
3) Power supply or battery with at least 5.5V since we will be powering arduino from VIN pin
4) Jumper wires at least 5
5) LED or relay to connect to output pin (here pin D13) of arduino

 

[A] Traditional method: pin state change detection mode (Higher power consumption)

This sketch is given on my instructable page here.

Before going to main concept of using interrupt and power saving deep sleep mode, let us briefly understand power consumption using Pin state change polling sketch.
I have slightly, modified the standard sketch available in Examples section of Arduino IDE.
In this sketch, we are continuously reading/polling the input pin D2 to which output of PIR sensor is connected.
The arduino never goes to sleep mode, ADCs are enabled, hence continuously consumes maximum power.





The current and power consumption is as follows-
* No motion detected by PIR sensor - external LED at pin D13 is OFF:- 12.87milliAmp / 62.45 mWatt
* Motion detected by PIR sensor - external LED at pin D13 is turned ON:- 36.55 milliAmp / 175 mWatt
Considering it is continuously in OFF state and no motion detected:-
* Typical 9V battery ( approx 500mAh) will last for 500/12.87 = 38 hours
* Typical AA size batteries(approx 2500mAh) will last for 2500/12.87 = 195 hours or 8 days


[B] Power saving mode: Interrupt driven Sleep and Wakeup (Lowest power consumption mode)

  

This sketch is available here. and at the end of this post.

To save power/battery consumption, it would be beneficial to put arduino into lowest power consumption mode i.e. Deep Sleep or Hibernation until PIR sensor detects motion. We will achieve this using hardware interrupt and Power Down Mode of arduino board's ATMEGA328P microcontroller. Moreover, we will disable analog to digital converters (ADC) since we do not need any ADC.
"The Power-down mode saves the register contents but freezes the Oscillator, disabling all othe r chip functions until the nex t interrupt or hardware reset." text from ATMEGA328 datasheet.
In this sketch, we are using input pin D2 or Interrupt 0 to which output of PIR sensor is connected. (you can also use pin D3 i.e. interrupt 1,  and for other boards different pins as mentioned here).

Since PIR detector itself has ON state timer, we do not need any timer from arduino to keep output pin D13 ON for fixed time. The ON time can be physically adjusted using Tx potentiometer, as shown in previous step.




-When PIR sensor detects motion, PIR sensor output state changes from LOW to HIGH. This rising edge signal is picked up by interrupt 0, arduino Awakes from sleep mode, interrupts are disabled temporarily and consequently pin D13 goes HIGH.
Once, set time is passed, PIR output again reverts back to LOW from HIGH, this falling edge signal raises another interrupt, putting arduino into hibernation once again.


- Arduino on first power ON, blinks LED at pin 13 for a minute, this is just for visual indication of warm-up period required by PIR sensor to stabilize to ambient temperature.

for( int i = 1; i <= 120; i++) {
digitalWrite(LedPin, HIGH);
delay(100);
digitalWrite(LedPin, LOW); delay(100); }

-The Arduino is kept into Hibernation until D2 pin is LOW.
In function 
Hibernate();

-we will configure all pins as INPUT, except pin 13 to which we have connected our LED or relay.
for (int i = 0; i < 20; i++) {
if(i != 13)
pinMode(i, INPUT);}

-set arduino sleep mode to lowest power consumption mode
set_sleep_mode(SLEEP_MODE_PWR_DOWN);

-disable ADCs
ADCSRA &= ~(1 << 7);

-disable Brown out detector used to check input voltage level
sleep_bod_disable();

-we attach interrupt 0 of pin 2 into detect CHANGE in state of signal at pin 2
attachInterrupt(PIRsensorInterrupt,wakeUpNow, CHANGE);

-finally Sleep function is called using
sleep_mode();

Let us analyze power consumption with hibernation and interrupt based sketch:

The current and power consumption reduces drastically using: power down mode, putting all pins to INPUT (or OUTPUT) and disabling ADCs.

* No motion detected by PIR - external LED at pin D13 is OFF (Arduino Hibernated) :- 5 milliAmp / 25 mWatt
* Motion detected by PIR - external LED at pin D13 is turned ON (Awake):- 32 milliAmp / 167mWatt

Considering it is continuously in OFF state and no motion detected:-
A typical 9V battery considering giving 500mAh, will last for 500/5 = 100 hours or 4 days
a typical AA size batteries giving 2500mAh, will last for 2500/5 = 500 hours or 21 days

We have reduced OFF state power consumption from 12.87 to 5mA. Bravo....

Note- Although, predicted current consumption in power down mode for ATMEGA 328P is in microAmp range, we are still in miliAmp range. This is because, arduino nano board has power regulators and other components that consume current. To achieve ultimate lowest current consumption, we will need bare ATMEGA chip with least possible external components. Our goal is to reduce power consumption to lowest possible level for arduino nano development board.

Please suggest, if someone has more optimized code for arduino nano to further reduce power consumption. Looking forward for your suggestions... in comments.



Useful links - acknowledgements:-
http://playground.arduino.cc/Learning/ArduinoSleepCode  for information about sleep.h library and sleep modes
Motion detector from-