Skip to content

Austin-dev-dot/Non-Invasive-Glucometer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Non-Invasive Glucose Monitoring System

A prototype system for non-invasive glucose level estimation using PPG (Photoplethysmography) signals from the MAX30102/MAX30105 sensor and machine learning algorithms.

ESP32 Sensor Display

⚠️ Disclaimer

This is a research prototype only. NOT for medical use.
Accuracy depends on individual calibration and has not been clinically validated.


Table of Contents


Introduction

This project demonstrates a proof-of-concept for non-invasive glucose monitoring using photoplethysmography (PPG) signals. The system captures infrared (IR) and red light absorption data from a MAX30102 sensor, processes the signals to extract meaningful features, and uses a machine learning model (Linear Regression) to estimate glucose levels.

Key Principle

Blood glucose levels affect the optical properties of blood, particularly the absorption characteristics at different wavelengths. By measuring the ratio of IR to red light absorption and analyzing signal variability, we can correlate these features with glucose concentrations.


Features

  • ✅ Real-time PPG signal acquisition from MAX30102/MAX30105 sensor
  • ✅ Dual-wavelength (IR + Red) optical measurement
  • ✅ Feature extraction (IR/Red ratio, signal variability, waveform slope)
  • ✅ Glucose level prediction using Linear Regression model
  • ✅ SSD1306 OLED display for real-time readings
  • ✅ Serial output for data logging
  • ✅ CSV data storage for training data collection
  • ✅ Finger detection with "No Finger" alert

Hardware Requirements

Component Specification Purpose
ESP32 DevKit 30-pin or 38-pin Main microcontroller
MAX30102/MAX30105 PPG Sensor Module Measures IR and Red light
SSD1306 OLED 128x64, I2C interface Display glucose readings
Jumper Wires Male-to-Female Connections
Breadboard Standard Prototyping (optional)
USB Cable Micro-USB Power and programming

Circuit Diagram

Pin Connections

ESP32 DevKit          MAX30102/MAX30105
-----------          -----------------
   3.3V  ─────────────── VIN
   GND   ─────────────── GND
   GPIO18 (SDA) ──────── SDA
   GPIO19 (SCL) ──────── SCL

ESP32 DevKit          SSD1306 OLED
-----------          ------------
   3.3V  ─────────────── VCC
   GND   ─────────────── GND
   GPIO18 (SDA) ──────── SDA
   GPIO19 (SCL) ──────── SCL

Note: Both the MAX30102 and SSD1306 share the same I2C bus. The MAX30102 typically uses I2C address 0x57 and SSD1306 uses 0x3C.

Wiring Diagram

                    ┌─────────────────────┐
                    │      ESP32          │
                    │                     │
    ┌───────────────┤ GPIO18 (SDA)        │
    │   ┌───────────┤ GPIO19 (SCL)        │
    │   │   ┌───────┤ 3.3V                │
    │   │   │   ┌───┤ GND                 │
    │   │   │   │   └─────────────────────┘
    │   │   │   │
    │   │   │   │   ┌─────────────────────┐
    ├───┼───┼───┼───┤ SDA    MAX30102     │
    │   ├───┼───┼───┤ SCL                 │
    │   │   ├───┼───┤ VIN                 │
    │   │   │   ├───┤ GND                 │
    │   │   │   │   └─────────────────────┘
    │   │   │   │
    │   │   │   │   ┌─────────────────────┐
    ├───┼───┼───┼───┤ SDA    SSD1306      │
        ├───┼───┼───┤ SCL                 │
            ├───┼───┤ VCC                 │
                ├───┤ GND                 │
                    └─────────────────────┘

Software Requirements

Arduino IDE Setup

  1. Install Arduino IDE (v1.8.x or newer, or Arduino IDE 2.x)

  2. Add ESP32 Board Support:

    • Go to FilePreferences
    • Add this URL to "Additional Boards Manager URLs":
      https://dl.espressif.com/dl/package_esp32_index.json
      
    • Go to ToolsBoardBoards Manager
    • Search for "ESP32" and install "ESP32 by Espressif Systems"
  3. Install Required Libraries:

    • Go to SketchInclude LibraryManage Libraries
    • Install the following:
      • SparkFun MAX3010x Pulse and Proximity Sensor Library
      • Adafruit SSD1306
      • Adafruit GFX Library

Python Requirements (for ML Training)

pip install pandas numpy scikit-learn matplotlib

Setup Instructions

1. Clone/Download the Project

git clone https://github.com/austin-dev-dot/Non-Invasive-Glucometer.git

2. Open in Arduino IDE

  • Open glucosemonitor.ino in Arduino IDE

3. Configure Board Settings

  • Board: ESP32 Dev Module
  • Upload Speed: 921600
  • Flash Frequency: 80MHz
  • Flash Mode: QIO
  • Partition Scheme: Default 4MB with spiffs
  • Port: Select your COM port

4. Upload Code

  • Click the Upload button or press Ctrl + U

Data Collection Process

To train your own model, you need to collect PPG data alongside reference glucose measurements from a commercial glucometer.

Step 1: Collect Raw Data

  1. Connect the hardware as per the circuit diagram
  2. Upload glucosemonitor.ino to ESP32
  3. Open Serial Monitor (115200 baud)
  4. Place finger on the MAX30102 sensor
  5. Record the following from Serial output:
    • IR Mean value
    • Red Mean value
    • Calculated Ratio
    • Variability (RMSSD)
    • Slope

Step 2: Record Reference Glucose

Simultaneously measure glucose using a commercial glucometer and record the value.

Step 3: Create Training CSV

Create a CSV file glucose_training_data.csv on your Desktop with the following format:

ratio,variability,slope,glucose
1.2534,5104.5,4472.3,110
1.2612,4892.1,3821.5,125
1.2489,5321.8,5102.4,98
...

Note: The script also accepts alternative column names:

  • ir_red_ratio → automatically renamed to ratio
  • reference_glucose → automatically renamed to glucose

Collection Tips

  • Collect data at various times: fasting, post-meal (1hr, 2hr)
  • Minimum 50-100 data points recommended
  • Ensure finger is placed consistently
  • Wait for stable readings (10-20 seconds)
  • Keep finger still during measurement
  • Valid glucose range: 40-400 mg/dL (outliers are filtered)

Machine Learning Training

The training script uses Leave-One-Out Cross-Validation (LOOCV) for robust model evaluation, especially useful with small datasets.

File Paths

File Path
Input CSV C:\Users\AUSTIN PAUL\OneDrive\Desktop\glucose_training_data.csv
Output Directory C:\Users\AUSTIN PAUL\OneDrive\Desktop\glucose_training_data\model_output\

Run Training

python train_model.py

What the Script Does

  1. Loads and cleans data from the CSV file
  2. Filters outliers (glucose values outside 40-400 mg/dL)
  3. Runs Leave-One-Out Cross-Validation for rigorous evaluation
  4. Calculates performance metrics:
    • MAE - Mean Absolute Error (mg/dL)
    • RMSE - Root Mean Square Error (mg/dL)
    • MARD - Mean Absolute Relative Difference (%) - standard metric for glucose monitors
    • Within ±20 mg/dL - Clinical accuracy percentage
  5. Trains final model on all data
  6. Exports coefficients to model_coefficients.h
  7. Generates prediction plot saved as prediction_plot.png

Sample Output

Running Cross-Validation...

===== MODEL PERFORMANCE =====
MAE   : 12.45 mg/dL
RMSE  : 15.32 mg/dL
MARD  : 10.25 %
Within ±20 mg/dL : 78.5%

Training complete. Header and Plot saved to: C:\Users\...\model_output

Output Files

File Description
model_coefficients.h Arduino header with trained coefficients
prediction_plot.png Scatter plot of actual vs predicted glucose

Update Coefficients

Copy the generated model_coefficients.h from the output directory to your Arduino project folder, replacing the existing file.


Deployment

  1. Update model_coefficients.h with your trained model coefficients
  2. Upload the updated code to ESP32
  3. System will automatically use the new model for predictions

Usage

  1. Power On: Connect ESP32 to power via USB
  2. Wait for Initialization: Display shows "Glucose Monitor Initializing..."
  3. Place Finger: Position finger steadily on the MAX30102 sensor
  4. Wait: System needs ~2 seconds to fill the buffer
  5. Read Results: Glucose prediction updates every 5 seconds

Display Messages

Message Meaning
Initializing... System starting up
Place finger Ready for measurement
No Finger Finger not detected or poor contact
XXX.X mg/dL Predicted glucose level

Serial Monitor Output

Open Serial Monitor at 115200 baud to see:

System Ready
Glucose: 115.2 mg/dL
Glucose: 118.5 mg/dL
...

Project Structure

Non Invasive Glucometer/
├── glucosemonitor.ino           # Main Arduino sketch
├── model_coefficients.h         # ML model coefficients
├── train_model.py               # Python training script (LOOCV)
└── README.md                    # This file

Desktop/
├── glucose_training_data.csv    # Your collected training data
└── glucose_training_data/
    └── model_output/
        ├── model_coefficients.h # Generated coefficients
        └── prediction_plot.png  # Training results plot

How It Works

1. PPG Signal Acquisition

The MAX30102 sensor emits both infrared (IR) and red LED light into the fingertip. The reflected light is measured by a photodetector. Blood volume changes during heartbeat cause variations in light absorption.

2. Feature Extraction

Three key features are extracted from the PPG signals:

Feature Calculation Physiological Basis
Ratio IR Mean / Red Mean Related to oxygen saturation and blood composition
Variability RMSSD of IR signal Heart rate variability, blood flow dynamics
Slope (Max - Min) / Time Rate of blood volume change

3. Prediction

Features are standardized using the training data statistics, then passed through a linear regression model:

glucose = intercept + (c1 × z_ratio) + (c2 × z_variability) + (c3 × z_slope)

Where z_* represents standardized (z-score) features.


Future Improvements

  • Add temperature compensation
  • Implement motion artifact detection and rejection
  • Add heart rate variability (HRV) features
  • Develop TinyML model with TensorFlow Lite
  • Create mobile app for calibration and visualization
  • Add SD card logging for long-term monitoring
  • Implement cloud data upload via WiFi
  • Add battery management for portable use
  • Design 3D-printed enclosure
  • Implement per-user calibration

Troubleshooting

Issue Solution
"OLED not found" Check I2C connections, verify 0x3C address
"MAX30102 not found" Check wiring, try I2C scanner sketch
Unstable readings Ensure steady finger placement
"No Finger" message Apply more pressure, check sensor alignment
Prediction accuracy low Collect more calibration data

References

Author

Austin Paul

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors