PRJ-02 · Machine Learning
Engine Health Monitoring with a Neural Network
A small neural network learns how my 2001 Ford Ranger's 3.0 L V6 should breathe and fuel when it is healthy, from OBD-II data that a Raspberry Pi logs automatically on every drive. When the engine's measurements drift away from the model's prediction and stay there, that gap flags a fault.
- Healthy data
- 12.7 h
- Airflow error, unseen drives
- 1.60 g/s
- MAF errors ≥ 5 % detected
- 100 %
- Inference on a Pi 3
- 190 µs
35 drives, logged automatically
vs 2.33 for linear regression
of 2-minute windows, 1.8 % false alarms
per prediction, NumPy only
01Approach
There is no labeled fault data for one specific truck, so a classifier was not an option. Instead, the models are trained only on healthy driving and learn what the engine should do under any operating condition. A fault shows up as a persistent residual between measurement and prediction.
- Airflow model: predicts mass airflow (MAF, g/s) from RPM, vehicle speed, throttle position, intake-air temperature, and coolant temperature.
- Fuel-trim models: one per cylinder bank, predicting long-term fuel trim (%) in warm closed-loop operation from RPM, calculated load, intake-air temperature, and speed.
Engine load isn't used as an input to the airflow model, because the engine computer calculates load from the MAF reading. A faulty sensor would then corrupt the input and the target together and hide itself.
02Data Acquisition
An OBDLink EX adapter reads the truck's SAE J1850 PWM bus. A Raspberry Pi 3 runs the logger as a system service: it waits for the engine to start, writes one CSV per drive, and closes the file at engine-off. The protocol is slow, giving a full row of 16 parameters about every 1.8 s, read one after another rather than all at once.
Because the Pi loses power abruptly when it's unplugged, the root filesystem runs read-only, logs go to a separate partition, and every row is flushed to disk as it's written. Logs upload to cloud storage over home Wi-Fi or a phone hotspot.
| Vehicle | 2001 Ford Ranger, 3.0 L Vulcan V6, MAF-based, ~159k miles |
|---|---|
| Logged channels | RPM, speed, throttle, calculated load, MAF, coolant and intake-air temperature, short- and long-term fuel trim (both banks), spark advance, O₂ sensor voltages, fuel-system status |
| Healthy dataset | 35 drives, 22,199 rows, 12.7 h |
| Real fault data | 5 drives with a stuck-open thermostat (before replacement); 1 drive with a disconnected DPFE sensor hose |
| Public dataset | Vehicle Energy Dataset: 10 other 3.0 L six-cylinder vehicles, 81 h, used for pretraining experiments |
The first drives showed coolant temperature plateauing at 73–78 °C, a stuck-open thermostat. Rather than discard them, we kept those drives as labeled real-fault data and then replaced the thermostat. After the repair, coolant held at 87 °C. Only post-repair drives are used as healthy training data.
03Model & Training
| Architecture | Multilayer perceptron, 5 → 32 → 32 → 1, ReLU, 1,281 parameters |
|---|---|
| Training | PyTorch; Adam, learning rate 10⁻³, batch 128, MSE loss; inputs and target standardized |
| Stopping rule | Early stopping on validation RMSE, patience 30 epochs (max 500), best weights kept |
| Evaluation | Leave-one-drive-out across all 35 drives; seeds fixed |
Data is split by whole drive, never by row. Consecutive rows within a drive are highly correlated, so a row-wise split would leak test information into training and overstate accuracy.
Two baselines were fit to the same data. The first is a physics-based speed-density airflow model with volumetric efficiency fit by least squares, both as a constant and as a function of load. The second is linear regression.
04Prediction Accuracy
| Airflow model | All 35 drives | Town (32) | Highway (3) |
|---|---|---|---|
| Neural network (32×32) | 1.60 | 1.17 | 2.11 |
| Speed-density, ηv(load)* | 1.80 | 1.54 | 2.16 |
| Linear regression | 2.33 | 1.85 | 2.94 |
| Speed-density, constant ηv | 8.63 | 7.49 | 10.19 |
Pooled RMSE in g/s on held-out drives, leave-one-drive-out. *Uses the engine computer's calculated load, which is derived from MAF itself, so it is partly circular and can't be used to detect MAF faults.
The network beats linear regression on 33 of 35 held-out drives.
Error falls quickly up to about an hour of driving and is still improving on highway drives at 10 h, where data is thinnest.
Network size
Eleven architectures from 4 neurons up to 128×128 and three layers were compared. From 16 neurons up, the error is flat at 1.65–1.67 g/s, with differences smaller than the spread between random seeds. The 32×32 network was kept as the main model.
Does pretraining on public data help?
Models were pretrained on the Vehicle Energy Dataset, then fine-tuned on the Ranger. Pretraining helps only with almost no truck data: with one 9-minute drive it cuts error from 8.4 to 5.7 g/s. From about 0.8 h of truck driving on, it is equal to or slightly worse than training from scratch.
Applied with no fine-tuning, the public-data model over-predicts the Ranger's airflow by 2.7–4.9 g/s, because different engines breathe differently. The public dataset also lacks throttle position, an input that matters far more than pretraining.
05Fault Detection
Each detector averages the residual over 2-minute windows (60 rows). Its alarm threshold is the 99th percentile of healthy window residuals from the other drives, so a drive is never judged against its own data. Faults were injected into held-out drives using physical models:
- MAF gain: the sensor reads high or low by a fixed percentage, and fuel trims compensate.
- MAF drift: the gain error ramps up over the drive.
- Vacuum leak: unmetered air enters after the sensor, so the engine runs lean and trims add fuel.
| Injected fault | Fuel-trim detector | Airflow detector |
|---|---|---|
| None (false-alarm rate) | 1.8 % | 1.1 % |
| MAF reads 2.5 % low | 63 % | 2 % |
| MAF reads 5 % low | 100 % | 2 % |
| MAF reads 10 % low | 100 % | 4 % |
| MAF reads 5 % high | 97 % | 2 % |
| MAF drifts to 10 % low | 81 % | 2 % |
| Vacuum leak 0.5 g/s | 76 % | 4 % |
| Vacuum leak 1 g/s | 96 % | 13 % |
| Vacuum leak 2 g/s | 100 % | 58 % |
Share of 2-minute windows flagged, 35-fold leave-one-drive-out.
The fuel-trim detector does almost all the work. It fires once the induced trim shift passes about 2.6 %. The airflow detector is weak because healthy airflow residuals are heavy-tailed at idle, where small absolute errors become large relative ones, so a 10 % sensor error barely clears its threshold. Only large leaks, which are big relative to roughly 5 g/s of idle airflow, stand out.
Real faults on the truck
- MissedStuck-open thermostat. The mean fuel-trim residual of +1.4 % stayed inside the healthy drive-to-drive range (up to 2.4 %). A cold-running engine doesn't shift warm fueling enough to detect this way.
- CorrectDisconnected DPFE hose. This hose senses exhaust pressure for EGR control, not intake air. Both detectors correctly stayed quiet, because air metering and fueling were unaffected.
These are detectors for air-metering and fueling faults, not general-purpose fault detectors.
06Fuel-Trim Calibration Map
An exploratory extension: if the MAF sensor under-reads in some operating region, the engine computer adds fuel there to compensate. Averaging total fuel trim over warm closed-loop data, binned by RPM and load, estimates where a MAF correction would bring trims back to zero. A correction is suggested only where its 95 % confidence interval over drives excludes zero.
Trims are near zero at idle and highway airflow but run +3–4 % in the 7–20 g/s light-cruise band, consistent across early and late drives (r = 0.89) and in steady-state rows only. That shape matches a MAF sensor that under-reads in the mid-range, which is plausible at 159k miles. Fuel-pressure or injector effects at short pulse widths could produce it too, and the data can't tell them apart. These are recommendations only; the engine computer is not reflashed.
07Running on the Raspberry Pi
Training happens on a laptop. On the Pi, the trained network runs as a plain NumPy forward pass with no deep-learning framework installed, and it matches PyTorch's output to within 8 × 10⁻⁶ g/s.
| Hardware | Single prediction (median) | Batched, per row |
|---|---|---|
| Raspberry Pi 3 Model B | 190 µs | 2.4 µs |
| Laptop (Intel i7-8550U) | 30 µs | 0.45 µs |
With a new row arriving only every 1.8 s, inference uses well under 0.1 % of the Pi's time, so live in-vehicle monitoring is practical.
08Interactive 3D Model
This runs the real trained weights of the airflow model in your browser, on 2-minute windows from drives it never trained on: idle, town, and highway. Two buttons inject simulated faults. A 2 g/s vacuum leak at idle is flagged. A MAF reading 10 % low on the highway is missed by the airflow detector, the same weakness found above. The browser's predictions match the Python model to within 10⁻⁴ g/s.
Tap to open the interactive 3D model ↗
09Limitations & Next Steps
- Simulated faults shift trims instantly. The real engine computer learns trims over minutes, so real detection will be slower than these results suggest.
- The fuel-trim detector can't tell a MAF fault from a leak. The airflow residual's pattern (a leak is strongest at idle, a gain error appears everywhere) could, if that detector is improved with absolute residuals, longer windows, or conditioning on operating region.
- A physical vacuum-leak test is planned to calibrate the simulated leak model against the real truck.
- More highway driving is needed; only 3 drives are mostly highway.
- Data quality limits: J1850 reads parameters sequentially over about 1.8 s, and long-term trims drift from day to day.
Results as of December 2, 2026. Final report in progress. The logging Raspberry Pi will later run Ranger Remote.