The need for motion sensing continues to grow in many industrial, commercial, home, and embedded applications. The problem is that motion sensing can require expensive digital sensors that are difficult to interface with. In addition, once the data is received, algorithms still need to be developed to detect the motion, which is a non-trivial endeavor.
Several solutions can sense motion, but infrared (IR) solutions are the most popular. Developers can choose an active solution that is common in many standalone digital sensors but more expensive and complex to implement. The alternative is to take advantage of passive infrared sensors (PIRs), which are less costly and more straightforward to interface with. A PIR provides an analog interface that most microcontrollers can interface to.
This article discusses motion-sensing fundamentals before showing how developers can get started with motion-sensing using a PIR connected to Microchip’s DM080104 ATtiny 1627 Curiosity Nano. It then shows an alternative to complex algorithm development for motion sensing that takes advantage of machine learning (ML) techniques. Tips and tricks to get started are included.
Motion sensing fundamentals
There are many sensing technologies that can detect motion, but IR is most widely used. IR sensors are either active or passive. Active sensors comprise an IR LED transmitter and a photodiode receiver. Active sensors sense the IR reflected off objects and then use the received IR to detect if the subject or object has moved. Depending on the application, the active sensor may contain several photodiodes to see motion direction. For example, by detecting which IR signals lag or lead, four photodiodes can be used to sense directive motion like left, right, forward, backward, up, and down.
Passive infrared sensors can’t transmit IR, only receive it. A PIR sensor uses the IR transmitted by the subject/object of interest to detect its presence and any motion associated with it. For example, a home security system will often have motion sensors that detect IR emitted by a human or animal and determine if it is moving through its field of view. Figure 1 shows what an analog PIR sensor might detect under various conditions such as no IR, IR present, stable, and leaving (cut off).
Figure 1: PIR sensors use the IR emitted by subjects or objects to detect their presence and motion. The various detection stages are shown: no IR, IR present, stable, and leaving (cut off). (Image source: Microchip Technology)
When selecting the right IR sensor type for an application, developers need to consider the trade-offs relative to the following parameters carefully:
- Sensor cost
- Microcontroller interface
- Detection algorithm and computing power
- Sensor range and energy consumption
Let’s examine an example PIR motion detection system that uses the ATtiny1627.
Introduction to the ATtiny1627 Curiosity Nano
An interesting microcontroller (MCU) solution for motion sensing is Microchip Technology’s ATtiny1627. This 8-bit MCU has a built-in 12-bit analog-to-digital converter (ADC) that can be oversampled to 17 bits. It also contains a programmable gain amplifier (PGA) that can adjust the sensitivity. Combining these two features can provide a low-cost motion detection system suited to many applications.
The best low-cost solution to get started is to use the DM080104 ATtiny1627 Curiosity Nano development board (Figure 2). The development board contains an AVR MCU that runs up to 20 megahertz (MHz) with 16 kilobytes (Kbytes) of flash, 2 Kbytes of SRAM and 256 bytes of EEPROM. The board includes a programmer, LED, and user switch. Perhaps most intriguing is that the board is designed to be easily connected through headers for rapid prototyping, or it can be directly soldered onto a prototype or production board.
Figure 2: The ATtiny1627 Curiosity Nano has a built-in 8-bit programmable AVR MCU running at speeds up to 20 MHz with 16 Kbytes of flash, 2 Kbytes of SRAM, and 256 bytes of EEPROM. The development board can be easily soldered or jumpered onto a larger baseboard to ease prototyping and production systems. (Image source: Microchip)
The board also comes with a few additional features that can be helpful to developers. First, it has two logic analyzer channels, DGI and GPIO. These channels can be used to debug and manage the microcontroller. Second, developers can leverage an onboard Virtual COM port (CDC) for debugging or logging messages. Finally, several tools can be used to write and deploy the software. For example, developers can use the Microchip Studio 7.0, a GCC compiler, or MPLAB X, which uses either GCC or the XC8 compiler.
There are also approximately a dozen code repositories that Microchip supports with various examples for the ATtiny1627. These code repositories have examples ranging from PIR motion detection, temperature measurements, analog conversions, and much more.
Building a motion detection test bench
Getting a motion detection test bench up and running is simple and not too expensive. The components necessary to build a test bench include:
- The DM080104 ATtiny1627 Curiosity Nano
- The AC164162T Curiosity Nano Adapter
- MIKROE-3339 PIR sensor from MikroElektronika
We’ve already looked at the ATtiny1627 Curiosity Nano. The Curiosity Nano Adapter provides a carrier board for the ATtiny1627 Curiosity Nano that can be used for rapid prototyping (Figure 3). In addition, it provides three expansion slots for MIKROE click boards along with accessible headers to scope signals or add custom hardware.
Figure 3: The Curiosity Nano Adapter has three expansion slots for MIKROE click boards along with headers to access signals and add custom hardware. (Image source: Microchip)
Finally, the MIKROE-3339 PIR sensor, shown in Figure 4, provides the KEMET PL-N823-01 passive IR sensor in a simple, expandable form that can be directly connected to the Curiosity Nano Adapter. It’s important to note that the MIKROE-3339 requires some modification when used with the Microchip examples for motion detection. These modifications can be found on page 10 of Microchip’s AN3641 Application Note, “Low-Power, Cost-Efficient PIR Motion Detection using the tinyAVR® 2 Family.”
Figure 4: The MIKROE-3339 click board provides a KEMET PL-N823-01 PIR sensor in an easy to prototype form. (Image source: MikroElektronika)
PIR motion detection software
There are several options that developers can use to create their software solutions for motion detection. The first solution is to use the example materials provided by Microchip in AN3641. The code repository for the example motion detection software can be found at Github.
The application occurs in a few phases. First, the application initializes and warms up the PIR sensor. Second, an ADC interrupt service routine is used to periodically sample the PIR sensor. Third, the ADC data is averaged. Finally, a detection algorithm is used to signal whether motion has been detected. If activity is detected, the on-board LED will blink, and a detection signal will be sent over the serial port. The complete program flow can be seen in Figure 5.
Figure 5: The chart represents the software flow for Microchip’s motion detection application. (Image source: Microchip)
The second option for motion detection is to leverage the initialization and the ADC interrupt routine from the Microchip examples, but instead of using their detection algorithm, use ML. PIR data can be collected and then used to train a neural network. The ML model can then be converted to run on the microcontroller with TensorFlow Lite for Microcontrollers, using fixed-point mathematics with 8-bit weights.
What’s interesting about using ML in this manner is that it removes the need for developers to design an algorithm for their specific needs. Instead, they can just sample the sensor under the expected conditions and use cases they need for their application. ML also allows developers to quickly scale and adjust their models as new data becomes available.
Tips and tricks for motion sensing using the ATtiny1627
There are a lot of options available to developers who are interested in getting started with motion detection. “Tips and tricks” developers should keep in mind to simplify and speed up their development, include:
- Build a low-cost prototyping platform using off-the-shelf parts.
- Leverage the motion detection example from Microchip that can be found on GitHub.
- Design prototype hardware with the ATtiny1627 Curiosity Nano footprint and directly solder the board onto the hardware to simplify initial prototypes.
- For smaller, more efficient, optimized code, use Microchip’s XC8 compiler.
- Read Microchip’s AN3641 Low-Power, Cost-Efficient PIR Motion Detection Using the tinyAVR® 2 Family before starting a motion detection application.
- Seriously consider using ML for the motion detection algorithm.
Developers that follow these “tips and tricks” will find that they save quite a bit of time and grief when prototyping their application.
Motion detection is becoming a common feature in many applications, especially where no-touch is beneficial. Developers can minimize their BOM costs and simplify their design by leveraging a PIR sensor and a low-cost MCU. As shown, the ATtiny1627 is an excellent starting point, and Microchip provides a wide range of tools and application notes to help developers get started. In addition, to minimize the complexity of algorithm development to detect motion, ML can be used.