Loading...
Hardware & Machine Learning

Predictive Maintenance AI: Real-Time Hardware Anomaly Detection

The Origin: Bridging the Physical and Digital In the world of mechatronics and industrial automation, hardware doesn't just suddenly break; it usually shows subtle warning signs beforehand, like slight temperature spikes or irregular vibrations. I wanted to build a system that could listen to a physical machine and automatically detect when it was behaving strangely. To do this, I engineered a complete data pipeline connecting an Arduino micro-controller to a Python-based machine learning environment.

Technical Execution: The Data Acquisition (DAQ) Pipeline The first challenge was getting reliable data out of the physical world. I wrote a Python script using the pyserial library to establish a direct connection with the Arduino over a USB serial port. Real-world sensor data is notoriously messy—voltage drops or loose wires can send garbled text or incomplete data packets. To handle this, I engineered robust error-handling safeguards into the pipeline. The script decodes the raw byte stream, strips invisible characters, and verifies the integrity of the data array before securely logging the temperature and humidity metrics to a continuously updating CSV file.

Architecting the AI: Unsupervised Learning Once the hardware data was flowing, I needed an algorithm to analyze it. Standard logic (like setting a maximum temperature threshold) is too rigid for complex systems. Instead, I implemented an Isolation Forest, an unsupervised machine learning algorithm from the scikit-learn library.

I specifically chose an Isolation Forest because it doesn't require a pre-labeled dataset of "good" and "bad" states. Instead, it looks at the multidimensional shape of the live data and mathematically isolates outliers. By setting a contamination expectation of 5%, the AI dynamically learns the "normal" operating rhythm of the hardware and automatically flags data points that deviate from that rhythm as system anomalies. Finally, I used pandas and matplotlib to plot the AI's diagnostic results in real-time, clearly marking normal operations in cyan and critical system failures in red.

Technical Growth and Takeaways This project represents the complete mechatronics stack: from a physical sensor, through a serial communication protocol, into a data structure, and finally processed by a machine learning model. It taught me that applied AI isn't just about massive language models; it is a highly practical tool for predictive maintenance, allowing engineers to catch hardware degradation long before a catastrophic mechanical failure occurs.

This is an image of when I had the logger script run for a while longer longer. I got 472 lines of data in the live_sensor_data.csv file. I occasionaly put my hand over the sensor or put it over a hot cup of tea to induce anomalies for the ai to detect. I also jerked the sensor's wires a few times to give it a few errors.