Developing window security nodes with level shifting I/O (Part 2)

Article By : Robert Perkel

Here is a demonstration of how an MVIO inside the MCU can be used in an application as an alternative to the external level shifters.

The Part 1 of this two-article series discussed a new peripheral called multi-voltage I/O (MVIO), which is available on the AVR DB family of microcontrollers. In short, the MVIO is an internal level shifter on the die of the microcontroller that allows one I/O port to operate in a different voltage domain than the rest of the device. This reduces the bill of materials (BOM) and the design area while providing more flexibility than external solutions.

The Part 2 of this series will look at the MVIO being used in an application. It’s based on a demo that uses 3D magnetometer as a window security sensor to detect whether a window is open or closed.

One of the simplest ways to detect whether a window is open is to use a magnetic reed switch. A reed switch is a simple device with electrical contacts that are opened or closed by the presence of a magnetic field. By mounting a magnet to a window and the switch to a fixed position, the state of the window can be determined by continuity through the contacts. However, there are two limitations with a reed switch.

Firstly, it cannot differentiate between a cracked window and an open window. If the alarm is used, then the window cannot be cracked without setting the alarm off. Secondly, the reed switch can be easily tampered with by placing a second magnet nearby. The secondary magnet holds the contacts of the switch in place while the window is opened.

To improve tamper resistance and usability, an alternative solution was developed with a magnetometer. Unlike a reed switch, which is either open or closed, the magnetometer measures and digitalizes the magnetic field components. By monitoring the field components, the sensor can differentiate a cracked window from an open window and is much more tamper-resistant to magnets placed near the sensor.

To demonstrate the advantage of a magnetometer-based solution, we co-developed a smart window-security sensor with Melexis, a supplier of semiconductor-based sensing solutions. It’s based on the microcontroller from the AVR DB family, which is the first microcontroller to have an MVIO peripheral. Here, an MLX90392 3D magnetometer has been used for monitoring the magnetic field strength. The magnetometer is powered from 1.8 V and communicates over I2C.

This demonstration also uses an MLX90632 far infra-red (FIR) sensor to monitor the temperature of the room. The FIR sensor selected is a better choice than a local temperature sensor, as the node will experience air drafts and direct sunlight, which can cause measurement errors. While the MLX90632 is powered from 3.3 V, there is a device variant designed for 1.8 V I2C communication.

For wireless connectivity, an RN4870 Bluetooth module has been used for simplicity. This allows a user to interact with the demo from the smartphone, rather than a custom-developed communication bridge. In a production application, however, we’d recommend switching to an alternative simpler and lower power radio solution, like Sub-GHz radio, for each sensor node. The source code and documentation are linked at the end of the article.

Prototyping and development setup

For development, we used a Curiosity Nano Base for Click Boards (AC164162) with an AVR DB Curiosity Nano Development Board (EV35L43A). The RN4870 Bluetooth module and the MLX90632 temperature sensor are available on Click boards from MikroElektronika (RN4870 Click and IrThermo 3 Click). The MLX90392 magnetometer was provided by Melexis on a pre-assembled evaluation board for MLX90392.

After initial sensor communication testing, we quickly realized that a test fixture to control the position of the magnet and sensor would be needed. Using our latent arts-and-crafts abilities, we assembled a simple test fixture with cardboard and hot glue, as shown below.

 

Figure 1 In this prototype test fixture and setup, the small white box attached to the fixture come from an off-the-shelf door switch. The included magnet was used as an example of a standard security magnet for testing purposes. Source: Microchip

The magnetometer

Conceptually, the window alarm aspect of this system would appear to be the simplest to implement, but it was a lot more complicated than we originally thought. There are a couple of reasons for the added complexity.

Firstly, the magnetometer is extremely sensitive. Small differences in the position of the magnet or magnetometer would throw the numbers off dramatically. The graphs below were derived from the raw values of the magnetometer without calibration or normalization (Figure 2). During data collection, the sliding mechanism was slid back and forth.

Figure 2 The graph shows raw magnetometer field component intensities. Source: Microchip

Rather than directly monitoring each component’s maximum and minimum, which will vary depending on the mounting position of the sensor, we used the total magnetic field strength instead (R = √(X2 + Y2 + Z2). For software optimization reasons, the application uses the square of the magnetic field strength. The graph below shows the magnitude of the magnetic field during the same test. In application, the magnitude is calculated from normalized 16-bit values, as shown in Figure 3.

Figure 3 The graph shows the magnitude of the magnetic field strength during the test. Source: Microchip

To make the system more sensitive overall, the magnetic field values in each axis were normalized. From the sample data shown, the magnitude of the Y-axis is the largest, followed by Z and then X. Each axis increases at a different rate from the others. This intuitively makes sense, as the magnetic flux will be the highest in the perpendicular direction and the weakest in parallel. To normalize each axis, the microcontroller scales each by right shifting the results to fit within a signed 8-bit value. The scaling factors are calculated during the user calibration sequence to make the system more sensitive overall.

Originally, the application was going to also calculate the angle ratios of X/Y, X/Z and Y/Z for additional tamper resistance. But in testing, this calculation was found to be very unreliable due to the large variances in field intensity. These angles added to the memory usage and the active time of the demo. We found the application worked well without them and disabled them via a macro. The computed angle ratios from the earlier data are shown below (Figure 4).

Figure 4 The X/Y, X/Z and Y/Z angle ratios of magnetometer have been computed from the earlier data. Source: Microchip

To reliably utilize the magnetometer, we developed a four-step calibration process. This process must be performed during the initial setup, but the user can retrigger it later, if needed. The four steps to calibrate the application are:

  1. Zeroing
  2. Normalization
  3. Setting threshold
  4. Verification

In zeroing, the window is fully opened, which places the magnet as far from the sensor as possible. The average value recorded for each axis is defined as the offset value. Next, the window is closed, which places the magnet very close to the sensor. The field intensities in each axis are averaged in this position. After the set number of samples, the microcontroller computes the normalization factor for each axis.

Next, the user opens the window to the desired open/closed threshold. While the user is opening the window, the application is monitoring the maximum field strength (R2) recorded. Depending on positioning, the maximum field strength should occur at or close to the closed-window position.

After this step, the user will close the window. At this point, the system is checking to see if the alarm will be triggered while the window is being closed. If the alarm does trip, then the calibration failed and should be repeated. On the other hand, when the calibration is successful, the values are written into internal EEPROM for later use.

Implementing temperature sensor

To interface with the MLX90632 sensor, we developed a new lightweight API that was designed to leverage microcontroller-specific features to improve performance. In the case of the MLX90632 temperature sensor, we implemented a calibration-constant caching system to store the sensor’s factory constants into the microcontroller’s EEPROM. This is automatically performed by the microcontroller. On startup, the microcontroller checks its EEPROM to determine whether the constants from the sensor have been programmed into memory. To verify the constants, the microcontroller:

  • Matches the stored serial number to the sensor’s serial number
  • Verifies the XOR of the serial number at the end of the memory segment

If either check fails, the microcontroller will reacquire the sensor’s constants, compute the floating-point equivalents and then rewrite them into the EEPROM. If the memory is considered valid, the microcontroller simply reads the stored constants rather than reacquiring and recomputing them.

This feature can be disabled in software by undefining a macro. Alternatively, if the calibration button is held when the application powers up, the application will start in safe mode, which considers all settings and values in the EEPROM invalid and will reacquire/reprogram (if enabled).

Working with 1.8 V

At 1.8 V, serial communication must be handled more carefully than the more common 3.3-V or 5-V types. For instance, the internal pull-up resistors in the microcontroller can be too weak for the bus to meet the timing requirements. To resolve this issue, we added external pull-up resistors to the bus.

During early testing, we tried to directly measure the current consumption of an external 1.8-V power supply using a precision digital multimeter. However, the burden voltage of the multi-meter was just enough that the bus would brown-out during serial communication. We resolved this issue in the prototype by adding a large tank capacitor to the power supply to ensure the bus would remain above the brown-out threshold while the meter measured the current consumption. This is not an issue in the full application, since current can be measured from the power input and the power supplies are regulated on the board, rather than externally.

Final results

Once the system was functioning correctly, we started development of a dedicated PCB. The image below is of the board that was developed (photographed without the plastic case). The prototype system is to be shown at Sensors Converge 2022 on June 27-29 at the Microchip booth # 218.

Figure 5 The prototype board has been photographed without the plastic case. Source: Microchip

Interfacing mixed-voltage domains is a crucial element of many designs. In most cases, an external level shifter must be used to convert between voltage domains. The MVIO eliminates this component and integrates it into the microcontroller, which makes it almost invisible to the developer. There are many possible designs and applications that will benefit from the addition of the MVIO peripheral in the system.

Source code and documentation of the above application is available at:

https://github.com/microchip-pic-avr-examples/avr128db28-smart-security-sensor-mplab

Editor’s Note: The author extends special thanks to Melexis for its assistance in developing this application.

This article was originally published on EDN.

Robert Perkel is application engineer for 8-bit MCU business unit at Microchip Technology.

 

Leave a comment