Arduino Internet of Things Part 3: External Interrupts Using A Motion Sensor (HC-SR501)
This is the third entry into the Arduino Internet of Things tutorial series. In this installment, I will cover interrupts from passive infrared (PIR) motion sensors using the low-power library discussed in Part 2. If you missed Part 1 or Part 2, please click the links to read through those and ensure you are up to speed with the current tutorial. I will be using the 4 MHz crystal here to lower the consumption of the active breadboard Arduino and optimize battery life. I will also introduce the Raspberry Pi as a Bluetooth receiver and IoT hub to record the data transmitted by the Arduino nodes. This entry will bridge the gap between connected nodes and an Internet of Things.
Required Parts:
PIR sensors are widely used as motion detectors because they passively focus (through optics) infrared radiation given off by human body heat, which allows the sensors to work with very little power supplied. The quiescent current for the PIR used here (HC-SR501) is only about 55 microamps. This, in conjunction with the low cost of these sensors, makes PIR detectors ideal for IoT applications in smart homes and remote security systems.
Wiring:
The important thing to remember when wiring the HC-SR501 is that the sensor is passive and sends only high or low data to the Arduino. Therefore, it is imperative that we put a resistor on the data pin (to lower the current draw) and ensure that the data pin is wired to one of the external interrupt pins (see ATmega328P pinout below).
We will again be wiring the breadboard Arduino to the HM-10 and a LiPo battery. The HC-SR501 is a simple addition to the wiring diagram (see below), and requires only a resistor between the data pin and the interrupt pin. It is also wired directly to the positive and negative terminals of the Arduino - so we will not be controlling the power in this case (like the DHT22 node), since the PIR needs to be powered continuously to enable interrupts.
Now that the motion detector is wired to function as a standalone, Bluetooth-enabled, IoT node, we need to ensure that the following process loop is maintained when programming the Arduino:
In the previous tutorial I covered powering on, sending data, and powering down the Bluetooth module programmatically in Arduino; so I won't cover those processes here again. And since the PIR is binary when detecting motion, we only need to worry about programming the sleep and wake routines. Thankfully, the low-power library handles external interrupts nicely- so we will be using its 'SLEEP_FOREVER' mode to sleep until the PIR trips an interrupt pin. The table below from the Arduino website outlines the interrupt pins on each Arduino board. For our breadboard Arduino, we can use pin D2 or D3.
When programming an interrupt, there are five key lines of code:
- Program interrupt pin - pinMode(interrupt_pin,INPUT)
- Attach Interrupt - attachInterrupt(digitalPinToInterrupt(interrupt_pin), wake_routine, mode)
- Sleep and Wait for Interrupt - LowerPower.powerDown(SLEEP_FOREVER,ADC_OFF,BOD_OFF)
- Detach Interrupt - detachInterrupt(digitalPinToInterrupt(interrupt_pin))
- -- Wake Routine --
These five processes allow the Arduino to run on a single battery for months at a time by sleeping at very low power while also listening for external interrupts. The code below shows the Arduino interrupt routine implemented for the HC-SR501 motion sensor:
The most important thing to remember with the code above is that it is an interrupt code, which means that it will work for any situation where the interrupt pin's state changes. The state change could occur because of a magnetic hall sensor, water level detector, reed switch, photoresistor, capacitive touch sensor, microphone - there are many possibilities. That is the advantage of low-power interrupts with Arduino: any sensor can cause an interrupt as long as the circuit is wired appropriately.
Interrupt Sensor Examples
In this third entry into Arduino Internet of Things tutorial series I covered interrupts and how to program the breadboard Arduino to sleep until an external interrupt is tripped. Interrupts are paramount to smart homes and intelligent security systems because of their reliability, efficiency, and simplicity. Motion sensors, light sensors, entry sensors (hall effect), and water level sensors are all types of interrupt devices that utilize deep sleep cycles to lower power consumption when not active. Here I showed how to wire and code an IoT node that wakes from a deep sleep, sends a notification via Bluetooth, and then sleep indefinitely until another interrupt is triggered. This third installment marks the end of the hardware side of the Arduino IoT series, and I will now focus on the receiving side of the connected devices. Now that we have the capability of several nodes (temperature sensor, motion sensor, hall effect sensor), we can investigate how to connect them all and produce a smart hub system that uses Raspberry Pi to collect the data from the nodes and create a connected Internet of Things.