REAL-WORLD IOT + TINYML COLLEGE PROJECT

Edge-AI Cold-Chain Spoilage Predictor

Build an intelligent, low-cost cold-chain monitor that fuses temperature, humidity, and VOC gas sensor data directly on an ESP32-S3 using TinyML—predicting food or vaccine spoilage hours before irreversible damage occurs.

1. Project Overview & Problem Statement

Everything you need to pitch, build, and demonstrate this real-world IoT + Machine Learning project for college evaluations.

Recommended Board ESP32-S3-DevKitC-1 / Seeed XIAO ESP32S3
Difficulty Level Intermediate (College / Final-Year)
Estimated Cost ₹1,200 – ₹1,800 ($15–$22)
Core Technologies TinyML • I2C Sensor Fusion • MQTT

The Real-World Problem

During perishable food, dairy, and vaccine transport, minor refrigeration failures or trapped volatile organic compounds (VOCs) silently accelerate spoilage. Conventional cold-chain loggers only trigger alarms after fixed temperature thresholds are breached—often too late to save the shipment.

How Edge AI Solves It

A BME680 environmental sensor continuously tracks temperature, humidity, and VOC gas resistance inside the storage container. A lightweight neural network trained in Edge Impulse runs directly on the ESP32-S3, detecting subtle early-spoilage patterns offline and triggering local OLED/buzzer alerts plus instant MQTT cloud notifications.

2. Interactive Real Wiring Diagram

Use the circuit filter buttons below to inspect power rails, the shared I2C bus, or alert outputs step-by-step.

3.3V RAIL COMMON GND I2C SDA (G8) I2C SCL (G9) GPIO 4 (Buzzer +) ESP32-S3 WROOM-1 TinyML Ready ESP32-S3-DevKitC-1 Dual-Core AI MCU USB-C 3V3 GND GPIO 8 (SDA) GPIO 9 (SCL) GPIO 4 GND GPIO 5 GPIO 6 MEMS BME680 VOC Gas • Temp • RH I2C Addr: 0x76 / 0x77 VCC GND SDA SCL 0.96" SSD1306 I2C OLED COLD-CHAIN AI STATUS: FRESH 94% CONF 4.2C | 58% | 145kΩ VCC GND SDA SCL + − Active Buzzer (Alarm) 3.3V–5V Piezo Module 220Ω + − Green LED (Fresh) 220Ω + − Red LED (Spoiling) 3.3V Power Common GND I2C SDA (GPIO 8) I2C SCL (GPIO 9) Buzzer (GPIO 4) Green LED (GPIO 5) Red LED (GPIO 6)
Complete Circuit View: All 7 signal and power paths are shown. Notice how the BME680 and SSD1306 OLED share the same 3.3V, GND, SDA (GPIO 8), and SCL (GPIO 9) lines via the I2C bus, leaving plenty of GPIO pins free for alert outputs.

3. Pin-to-Pin Connection Table & Bill of Materials

Follow this exact pin mapping when assembling your breadboard prototype.

Component Component Pin ESP32-S3 Pin Wire Color Circuit Function
BME680 Sensor VCC / GND 3V3 / GND Red / Black 3.3V regulated power supply
BME680 Sensor SDA / SCL GPIO 8 / GPIO 9 Blue / Orange Shared I2C bus (Addr 0x76 or 0x77)
0.96" OLED (SSD1306) VCC / GND 3V3 / GND Red / Black 3.3V regulated power supply
0.96" OLED (SSD1306) SDA / SCL GPIO 8 / GPIO 9 Blue / Orange Shared I2C bus (Addr 0x3C)
Active Buzzer + (Pos) / − (Neg) GPIO 4 / GND Purple / Black Audible alarm on spoilage detection
Green LED + 220Ω Anode (+) / Cathode (−) GPIO 5 / GND Green / Black Visual indicator for FRESH state
Red LED + 220Ω Anode (+) / Cathode (−) GPIO 6 / GND Coral / Black Visual warning for SPOILING alert

Bill of Materials (BOM)

Item Recommended Part / Spec Qty Est. Cost (INR / USD)
Microcontroller ESP32-S3-DevKitC-1 (or Seeed XIAO ESP32S3) 1 ₹550 – ₹800 ($7–$10)
Gas & Climate Sensor BME680 I2C Breakout (Temp + Humidity + VOC) 1 ₹400 – ₹600 ($5–$7)
Display 0.96" I2C OLED Display (SSD1306, 128×64) 1 ₹180 – ₹250 ($2–$3)
Alert Components Active Buzzer + Green/Red 5mm LEDs + 220Ω Resistors 1 set ₹50 – ₹80 ($1)
Prototyping 830-Point Breadboard + Jumper Wires + USB-C Cable 1 set ₹120 – ₹180 ($1.50)
Hardware Safety Note: Always double-check the silkscreen pinout of your exact ESP32-S3 SKU before powering on. The ESP32-S3 operates on 3.3V logic—never connect 5V directly to 3V3 or GPIO pins, and always place a 220Ω current-limiting resistor in series with each LED to prevent burning out the GPIO pin.

4. Step-by-Step Build & Edge Impulse TinyML Workflow

Follow these four stages to go from a bare breadboard to a working Edge-AI device.

1

Hardware Assembly & I2C Scan

Wire the BME680 and OLED onto the shared I2C pins (GPIO 8 & 9). Upload the starter code below and open the Serial Monitor at 115200 baud to confirm both I2C devices respond cleanly.

2

Collect a 3-Class Dataset

Place the sensor inside a sealed box with fruit (e.g., banana or tomato). Log CSV rows of Temp, Humidity, Gas_kOhm over 2–3 days and label them into three files: Fresh, Early_Ripening, and Spoiling.

3

Train in Edge Impulse

Upload your CSV files to a free Edge Impulse project. Add a Raw Data processing block and a Classification (Keras) neural network block. Train until accuracy exceeds 90% on your validation split.

4

Deploy & Add MQTT Alerts

Export the trained model as an Arduino Library (.zip) and include it in your sketch. When inference predicts Spoiling (>80%), trigger GPIO 4/6 locally and publish a JSON alert to HiveMQ or Blynk over Wi-Fi.

5. Interactive Edge-AI Spoilage Simulator

Adjust the virtual BME680 sensor readings below to see how multi-sensor fusion classifies freshness on the OLED, LEDs, and buzzer before you buy hardware.

Temperature (°C) 4.5 °C
Ideal cold-chain storage: 2°C – 8°C
Relative Humidity (%) 58 %
High humidity (>78%) accelerates mold & microbial growth
BME680 VOC Gas Resistance (kΩ) 165 kΩ
Sensor Physics: Lower kΩ = Higher concentration of spoilage VOC gases (ethanol/ethylene)
SSD1306 OLED TINYML LIVE
STATUS: FRESH
CONFIDENCE: 96%
T: 4.5C | RH: 58% | VOC: 165kΩ
Model Confidence 96%
Green LED
ON (GPIO 5)
Red LED
OFF (GPIO 6)
Buzzer
SILENT

6. Copy-Ready Arduino Starter Code (ESP32-S3)

Install Adafruit BME680 and Adafruit SSD1306 from the Arduino Library Manager, select ESP32S3 Dev Module, and upload this sketch to log Edge Impulse CSV data and test your hardware.

cold_chain_spoilage_starter.ino • ESP32-S3 + BME680 + SSD1306 OLED
// =========================================================================
// Edge-AI Cold-Chain Spoilage Predictor — Starter & Data Logger Sketch
// Board: ESP32-S3-DevKitC-1 | I2C: SDA=GPIO 8, SCL=GPIO 9
// Outputs: Buzzer=GPIO 4, Green LED=GPIO 5, Red LED=GPIO 6
// =========================================================================

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME680.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// Pin Definitions matching the IoTInfo Wiring Diagram
#define I2C_SDA_PIN   8
#define I2C_SCL_PIN   9
#define BUZZER_PIN    4
#define GREEN_LED_PIN 5
#define RED_LED_PIN   6

#define SCREEN_WIDTH  128
#define SCREEN_HEIGHT 64

Adafruit_BME680 bme;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
  Serial.begin(115200);
  delay(500);

  // Configure Alert Pins
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(GREEN_LED_PIN, OUTPUT);
  pinMode(RED_LED_PIN, OUTPUT);
  digitalWrite(BUZZER_PIN, LOW);

  // Start I2C on ESP32-S3 custom pins (GPIO 8 = SDA, GPIO 9 = SCL)
  Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN);

  // Initialize 0.96" SSD1306 OLED (Address 0x3C)
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("ERROR: SSD1306 OLED not found at 0x3C");
  }
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);
  display.setCursor(0, 10);
  display.println("Cold-Chain AI Boot...");
  display.display();

  // Initialize BME680 Sensor (tries 0x76 then 0x77)
  if (!bme.begin(0x76) && !bme.begin(0x77)) {
    Serial.println("ERROR: BME680 sensor not found! Check wiring.");
    while (1) delay(1000);
  }

  // Configure BME680 oversampling and VOC gas heater
  bme.setTemperatureOversampling(BME680_OS_8X);
  bme.setHumidityOversampling(BME680_OS_2X);
  bme.setPressureOversampling(BME680_OS_4X);
  bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
  bme.setGasHeater(320, 150); // 320C for 150 ms

  // CSV Header for Edge Impulse Data Forwarder
  Serial.println("temp_c,humidity_pct,pressure_hpa,gas_kohm");
}

void loop() {
  if (!bme.performReading()) {
    Serial.println("Failed to read BME680");
    delay(2000);
    return;
  }

  float tempC     = bme.temperature;
  float humidity  = bme.humidity;
  float pressure  = bme.pressure / 100.0;
  float gasKOhm   = bme.gas_resistance / 1000.0;

  // 1. Print CSV row to Serial Monitor (record this for Edge Impulse training)
  Serial.print(tempC, 2);    Serial.print(",");
  Serial.print(humidity, 2); Serial.print(",");
  Serial.print(pressure, 2); Serial.print(",");
  Serial.println(gasKOhm, 2);

  // 2. Baseline Heuristic (Replace with Edge Impulse run_classifier() after training)
  String statusText = "FRESH";
  if (gasKOhm < 40.0 || (tempC > 18.0 && gasKOhm < 65.0)) {
    statusText = "SPOILING!";
    digitalWrite(GREEN_LED_PIN, LOW);
    digitalWrite(RED_LED_PIN, HIGH);
    digitalWrite(BUZZER_PIN, HIGH);
  } else if (gasKOhm < 95.0 || tempC > 12.0) {
    statusText = "RIPENING";
    digitalWrite(GREEN_LED_PIN, LOW);
    digitalWrite(RED_LED_PIN, HIGH);
    digitalWrite(BUZZER_PIN, LOW);
  } else {
    statusText = "FRESH";
    digitalWrite(GREEN_LED_PIN, HIGH);
    digitalWrite(RED_LED_PIN, LOW);
    digitalWrite(BUZZER_PIN, LOW);
  }

  // 3. Update Live 0.96" OLED Display
  display.clearDisplay();
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.println("COLD-CHAIN EDGE AI");
  display.drawLine(0, 11, 128, 11, SSD1306_WHITE);

  display.setTextSize(2);
  display.setCursor(0, 18);
  display.println(statusText);

  display.setTextSize(1);
  display.setCursor(0, 42);
  display.printf("T:%.1fC  RH:%.0f%%", tempC, humidity);
  display.setCursor(0, 54);
  display.printf("VOC Gas: %.1f kOhm", gasKOhm);
  display.display();

  delay(2000); // Sample every 2 seconds
}

7. College Viva & Presentation Q&A

Click each question to prepare confident, clear answers for project reviews and external examiners.

Spoilage is a non-linear combination of temperature, humidity, and volatile organic compound (VOC) buildup over time. Simple if-else rules trigger false alarms when a door opens briefly or miss early chemical spoilage when temperature is still cool. A TinyML model learns the combined multi-sensor signature and runs locally on the ESP32-S3's AI vector instructions without needing continuous cloud connectivity.
Both devices use the I2C (Inter-Integrated Circuit) communication bus, which supports multiple peripherals on just two shared wires (SDA for data and SCL for clock). The ESP32-S3 tells them apart using unique hexadecimal addresses: the SSD1306 OLED listens at 0x3C, while the BME680 listens at 0x76 (or 0x77).
The BME680 contains a heated metal-oxide (MOX) gas sensing layer. In clean air, oxygen ions keep the layer's electrical resistance high (150–300 kΩ). When spoiling food releases reducing VOC gases like ethanol, ethylene, or ammonia, those gases react with the surface oxygen, freeing electrons and causing the measured resistance to drop sharply (below 40 kΩ).
To deploy in commercial cold-chain trucks, you can add a LiPo battery with ESP32-S3 deep-sleep cycles (waking every 5 minutes to infer), swap Wi-Fi for a 4G/GPS module (like the LilyGO T-SIM7600) or LoRaWAN gateway, enclose the electronics in an IP65 moisture-resistant box with a breathable PTFE membrane over the BME680, and log tamper-proof alerts to a cloud MQTT broker.