0% found this document useful (0 votes)
87 views87 pages

Design of A Smart Milk Adulteration Detection System Using Ai and Sensors Document

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views87 pages

Design of A Smart Milk Adulteration Detection System Using Ai and Sensors Document

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

DESIGN OF A SMART MILK ADULTERATION

DETECTION SYSTEM USING AI AND SENSORS


A Project Report Submitted in partial fulfilment of the requirements
for the award of the Degree of

BACHELOR OF TECHNOLOGY
in
ELECTRONICS AND COMMUNICATION ENGINEERING
Submitted by

PATHIVADA ANUSHA 21KD1A04D5


ROUTHU DILEEP KUMAR 21KD1A04F4
SAILADA NIHARIKA 21KD1A04F6
SIDDANADHAM SS VIJAY KUMAR 21KD1A04G3

Under the Esteemed Guidance of

Mr. K. GURUCHARAN [Link](Ph.D).,


Assistant Professor, Department of ECE

Department of Electronics and Communication Engineering


LENDI INSTITUTE OF ENGINEERING AND TECHNOLOGY
(An Autonomous Institution)
(Permanently Affiliated to JNTUGV Vizianagaram, Approved by AICTE
Accredited by NBA, Accredited with NAAC with ‘A’ Grade)
JONNADA VILLAGE, DENKADA MANDAL,
VIZIANAGARAM DISTRICT, ANDHRA PRADESH, INDIA-535005
I
2021-2025

LENDI INSTITUTE OF ENGINEERING AND TECHNOLOGY


An Autonomous Institution
(Permanently Affiliated to JNTUGV, Approved by AICTE Accredited by
NBA, Accredited with NAAC with ‘A’ Grade)
Jonnada (Village), Denkada (Mandal), Vizianagaram District,
Andhra Pradesh, India-535005

CERTIFICATE

Head of the Department Project Supervisor

Dr. M. Rajan Babu [Link]., Ph.D., MISTE., FIE Mr. [Link] [Link](Ph.D).,
Professor Assistant Professor
Department of ECE Department of ECE

EXTERNAL EXAMINER

II
This is to certify that the project report entitled “DESIGN OF A SMART MILK
ACKNOWLEDGEMENTS
ADULTERATION DETECTION SYSTEM USING AI AND SENSORS” is the
Bonafide work of PATHIVADA ANUSHA (21KD1A04D5), ROUTHU DILEEP
We consider it as a privilege to thank all the people who helped us for the
KUMAR (21KD1A04F4), SAILADA NIHARIKA (21KD1A04F6) and
successful completion of the project work entitled ‘DESIGN
SIDDANADHAM SS VIJAY KUMAR (21KD1A04G3) the students of this college, OF A SMART MILK
ADULTERATION
submitted DETECTION
in partial fulfillment of theSYSTEM USING
requirements AI award
for the AND SENSORS’.
of degree Bachelor of
Technology in Electronics and Communication Engineering duringmy
We would like to express our heartfelt gratitude to the parents foryear
academic their
encouragement
2024- 2025. and support to achieve and fulfill our dreams.
We take this opportunity to express our deep sense of gratitude to the
Management of LENDI Institute of Engineering and Technology for providing
congenial atmosphere and encouragement.
We sincerely express our wholehearted thanks to Dr. V. V. Rama Reddy,
Principal, Lendi Institute of Engineering and Technology who has given a lot of
support and freedom during our academics.
We profoundly thank Dr. M. Rajan Babu, Head of the Department, Electronics
and Communication Engineering, for his collaboration, constant support, and positive
belief on his students for the successful completion of this project work even though he is
busy with his hectic schedule of administration and teaching.
We would like to thank our Final year Coordinator Dr. Srikant Kumar Beura
PhD, NIT Meghalaya Associate Professor for his technical guidance, constant
encouragement and support in carrying out the project work.
We would like to thank my guide Mr. K. Gurucharan, Assistant Professor for
his technical guidance, continuous encouragement, and support in carrying out the project
work.
Finally, we would like to thank all the Teaching and Non-Teaching Staff who
helped us in the successful completion of this project work. We would also like to thank all
of our friends who helped us directly and indirectly for the successful completion of our
project work.

PATHIVADA ANUSHA 21KD1A04D5


ROUTHU DILEEP KUMAR 21KD1A04F4
SAILADA NIHARIKA 21KD1A04F6
SIDDANADHAM SS VIJAY KUMAR 21KD1A04G3

III
LENDI INSTITUTE OF ENGINEERING AND TECHNOLOGY
An Autonomous Institution
(Permanently Affiliated to JNTUGV, Approved by AICTE Accredited by
NBA, Accredited with NAAC with ‘A’ Grade)
Jonnada (Village), Denkada (Mandal), Vizianagaram District,
Andhra Pradesh, India-535005

INSTITUTE
VISION
Producing globally competent and quality technocrats with human values for the holistic needs
of industry and society.

MISSION
● Creating an outstanding infrastructure and platform for enhancement of skills, knowledge
and behavior of students towards employment and higher studies.
● Providing a healthy environment for research, development and entrepreneurship, to meet
the expectations of industry and society.
● Transforming the graduates to contribute to the socio-economic development and welfare
of the society through value-based education.

IV
LENDI INSTITUTE OF ENGINEERING AND TECHNOLOGY
An Autonomous Institution
(Permanently Affiliated to JNTUGV, Approved by AICTE Accredited by NBA,
Accredited with NAAC with ‘A’ Grade)
Jonnada (Village), Denkada (Mandal), Vizianagaram District, Andhra
Pradesh, India-535005
Department of Electronics and Communication Engineering

VISION

Emerge as a Centre of Eminence in Electronics and Communication Engineering to impart


quality education towards competent and skilled engineers.

MISSION
● Offering an inspiring and conducive learning environment to prepare skilled and
competent engineers by providing infrastructure, laboratory facilities, and effective
teaching-learning process.
● Fostering culture to face complex technological challenges through Internships, Projects
and Industry-Institute Interactions in order to enhance employability skills.
● Creating an environment for higher studies and entrepreneurship by way of imparting
quality education and promoting research activities.
● Imparting professional behavior and strong ethical values towards societal issues by
encouraging socially relevant activities.

PROGRAM EDUCATIONAL OBJECTIVES (PEOs)

PEO1: Graduates shall have strong knowledge and technical skills in core and associated
fields of Electronics and Communication Engineering to become globally competent
engineers and emerging researchers.

PEO2: Graduates shall comprehend latest tools and techniques in the field of Electronics
and Communication Engineering to analyse, design and develop novel systems and
products for addressing the real time issues.

PEO3: Graduates shall have professional attitude, ethical values, teamwork and good
communication skills, to adapt the rapidly changing technologies in Electronics and
Communication Engineering through life-long learning.

V
PROGRAM SPECIFIC OUTCOMES (PSOs)
PSO1: Capable of design, develop, test, verify and implement analog and digital
electronics and communication engineering systems and products.
PSO2: Qualify in national and international competitive examinations for successful higher
studies and employment.

COURSE OUTCOMES:

S. No Description
C411.1 Acquire technical knowledge on fundamental aspects in electronics and
communication engineering to solve complex engineering problems for real
time applications.

C411.2 Identify the work based on past experiences and from literature survey for
specific problems in the field of Engineering.

C411.3 Design or develop a software/Hardware model by using modern tools for a


specific problem useful for society/environment real time issues by
following ethical values.

C411.4 Enhance technical presentation skills, project management abilities to work


individually or as a team inculcating leadership qualities.

C411.5 Identify suitable applications leading to enhanced knowledge in project


management and lifelong learning.

VI
Abstract
Milk adulteration is a critical public health concern, especially in developing
countries where regulatory enforcement is limited. The addition of harmful substances such
as water, starch, detergents, and urea to milk compromises its nutritional value and poses
serious health risks. Traditional laboratory methods for detecting adulterants are accurate
but impractical for everyday use due to their high cost, complexity, and time requirements.
To address these challenges, this project presents a smart, real-time, and cost-effective milk
adulteration detection system based on artificial intelligence (AI) and embedded sensors.
The system integrates various sensors including DS18B20 (for temperature),
TCS34725 (for color detection), a turbidity sensor, LED-LDR setup (for fat estimation),
and pH measurement strips to capture critical milk quality parameters. Sensor data is
collected through a Raspberry Pi and processed using multiple machine learning algorithms
—Random Forest, SVM, AdaBoost, and XGBoost. These models are combined using a
Stacking Classifier to enhance prediction accuracy and reliability.
A user-friendly web interface built using Flask enables real-time result visualization and
logging, making the system accessible for farmers, consumers, and dairy processors. This
solution not only ensures immediate adulteration detection but also supports data
calibration, model retraining, and continuous monitoring, making it a scalable tool for
ensuring milk quality across supply chains.
The proposed system achieved a prediction accuracy of 95.2% using a Stacking Classifier
combining Random Forest, SVM, AdaBoost, and XGBoost.
Keywords: Milk Quality Detection, Machine Learning, Raspberry Pi, DS18B20 Sensor,
Turbidity Sensor, TCS34725 Sensor, pH Measurement, Lactometer, Random Forest, SVM,
AdaBoost, XGBoost, Stacking Classifier, Flask Web App, Real-time Monitoring, Food
Safety, Sensor-based System

POs Attained: PO1, PO2, PO3, PO4, PO5, PO6, PO7, PO8, PO9, PO10, PO11, and PO12.
PSOs Attained: PSO1, PSO2

VII
COURSE OUTCOMES VS POs MAPPING:
(DETAILED: HIGH:3; MEDIUM:2; LOW:1)

CO PO PO PO PO PO PO PO PO PO PO PO PO PSO PSO
1 2 3 4 5 6 7 8 9 10 11 12 1 2

C411.1 3 2 2 2 1 3 2 2 1 2 3 2 2 1

C411.2 3 2 1 2 3 2 3 2 3 2 1 2 2 1

C411.3 3 2 3 1 2 2 2 2 2 2 3 2 2 1

C411.4 2 1 3 2 3 2 2 3 1 3 2 2 2 1

C411.5 3 2 2 3 1 2 1 2 3 1 3 3 2 1

C411* 3 2 2 2 2 2 2 2 3 3 3 2 2 1

VIII
TABLE OF CONTENTS
SNO. CONTENT PAGE NO.

Introduction 2
1.1 Problem Statement 4
1.2 Background on Milk Adulteration 8
Literature Review 11
Raspberry and Machine Learning 16
3.1 Methodology 20
3.2 Raspberry pi Architecture 22
3.3 Sensors Configuration 30
3.4 Machine Learning Techniques 38
Software used 47
4.1 Machine Training 47
4.2 Modules and libraries used 50
4.3 Layouts and tools used 53
Results and discussions 60
5.1 Data set collection 60
5.2 Data pre-processing 61
5.3 Feature Engineering and Machine Learning 62
5.3.1 Random Forest, SVM 62
5.3.2 Ada Boost and XG Boost 63
5.3.3 Stacking Classifier 64
5.4 Comparative analysis of various parameters across 65
different methodologies
5.5 Results of application 66
5.6 Comparison with previous years 67
Conclusion 68
References 70

IX
LIST OF FIGURES
Fig no Fig Name Pg no.
1.4.1 Common Milk Adulteration 4
3.1 Milk Testing Cycle 9
3.1.1 Milk Calibration Kit 10
[Link] Temperature Sensor DS18B20 12
[Link] Color Sensor TCS 34725 13
[Link] Calibration of Fat 12
[Link] pH Strips 13
[Link] Turbidity Sensor 14
3.2.1 Raspberry pi and Pin Diagram 15
3.2.2 Pin Configuration 21
4.1.1 Google Collab 36
5.1.1 Feature Importance of RF 55
[Link] RF Shap Analysis 56
[Link] Comparison of Different Algorithms 36
[Link] Confusion Matrix of Random Forest 39
[Link] Confusion Matrix of SVM 41
[Link] Confusion Matrix of Ada Boost 44
[Link] Confusion Matrix of XG Boost 48
[Link] Web Application output1 49
[Link] Web Application output2 49

X
LIST OF TABLES

Table Table Name Pg no


no.
3.2.1 Raspberry Pi GPIO Pin Classification 19
3.2.2 Raspberry Pi GPIO Pin Layout 20
5.2.1 Data Set 55
5.2.2 Random Forest Result 60
5.2.3 SVM Results 61
5.2.4 Ada Boost 62
5.2.5 XG Boost 63
5.2.6 Comparison between all the algorithms 65
5.2.7 Compare with previous year Papers 67

XII
DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

CHAPTER-1
INTRODUCTION

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 1


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

1. INTRODUCTION

Introduction
Milk is considered one of the most essential dietary staples consumed globally due to its rich
nutritional profile and versatility. For better health, it is crucial to consume high-quality milk.
However, milk is a perishable product and even a minor contamination can lead to significant
spoilage and economic loss. Spoiled milk can become a breeding ground for millions of bacteria
within a short time, posing serious health risks. In developing countries like India, the situation is
exacerbated by inconsistent quality control and inadequate regulation. According to an FSSAI
survey, over 68.4% of milk in India was found to be adulterated, raising grave concerns regarding
consumer safety and public health.
Adulterated milk can be a source of various diseases including brucellosis, tuberculosis, and
listeriosis. Traditional laboratory-based milk quality tests are accurate but time-consuming,
expensive, and require technical expertise, making them unsuitable for routine testing in rural or
small-scale settings. This necessitates the development of intelligent, real-time, and cost-effective
methods for detecting milk adulteration.
Recent advances in machine learning (ML) and sensor technologies offer promising
alternatives. These tools can analyze complex physical and chemical data to detect
adulterants instantly. The objective of this study is to build a smart adulteration detection
system that uses AI models trained on sensor data to evaluate milk quality in real-time. The
system uses sensors to measure parameters like pH, turbidity, temperature, color, and fat
content, and applies ML models such as SVM, Random Forest, AdaBoost, XGBoost, and
Stacking Classifier for prediction.
In this work, a Flask-based user interface was developed to deliver results immediately, empowering
users like farmers, consumers, and dairy processors to assess milk quality on-site. This system also
supports calibration, which helps the AI distinguish between pure and adulterated milk, regardless of
the source or conditions. It not only provides real-time feedback but also logs data for further
analysis, making it valuable for continuous monitoring and policy-making.
1.1 Problem Statement
Milk adulteration remains a major public health and ethical challenge, particularly in regions where
regulatory oversight is limited. Substances such as water, starch, detergents, urea, and synthetic
chemicals are commonly used adulterants that compromise milk's safety and nutritional value.
Current detection methods are unsuitable for daily field use as they are slow, expensive, and require

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 2


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

laboratory setups. Moreover, natural variability in milk due to region, breed, or feeding conditions
complicates the detection process, making calibration a critical issue. This project addresses these
challenges by developing a smart, AI-driven detection system that offers real-time, affordable, and
user-friendly milk quality assessment.
1.2 Objectives
The objectives of this project are:
 To identify common adulterants in milk and understand their effect on milk’s physical and
chemical properties.
 To integrate multiple sensors to measure pH, turbidity, temperature, conductivity, and color
of milk.
 To develop a robust calibration model that differentiates between pure and adulterated milk.
 To apply ML algorithms (e.g., Random Forest, SVM, AdaBoost, XGBoost) for accurate
classification.
 To implement a user-friendly web interface for real-time result display and data logging.
 To ensure the system is scalable and affordable, targeting use in households and dairy
industries.
 To validate system performance through experimental comparison with conventional testing
methods.
1.3 Background on Milk Adulteration
Milk, often referred to as a complete food, is indispensable to human nutrition.
However, rising demand and limited supply have encouraged unethical practices of
adulteration to increase volume and profit. Adulteration involves the addition of non-
nutritive or harmful substances that alter milk’s quality. In many developing nations, this
issue is compounded by lack of monitoring, weak enforcement of food safety standards,
and limited public awareness. Many adulterants are difficult to detect due to their colorless,
tasteless, and soluble nature.
Traditional lab-based testing methods, though effective, are time-consuming and resource-intensive.
There is a growing demand for smart technologies that allow real-time testing at collection points,
homes, and retail outlets. Understanding how adulterants affect milk’s properties like pH, density,
and viscosity is essential for building an AI-powered detection model.

1.4 Common Adulterants in Milk

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 3


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

The most common


adulterants include
from the
following figure
1.4.1

Fig1.4.1 Milk Analysis


 Water: Increases volume but lowers nutritional quality and may carry harmful
microorganisms.
 Starch: Used to thicken diluted milk; not highly toxic but reduces purity.
 Detergents: Added to mimic natural frothiness; contain harmful alkalis.
 Urea: Added to simulate higher protein content; toxic in large amounts.
 Formalin: Preservative that extends shelf life; classified as a carcinogen.
 Sodium chloride & sodium bicarbonate: Used to alter pH and taste; harmful if
consumed in excess.
 Synthetic milk: Artificial blend of detergents, oils, and chemicals resembling milk;
hazardous to heart.
These adulterants alter measurable parameters like pH, turbidity, color, density, and conductivity,
which can be detected using sensors and interpreted by AI models.
To achieve the project's goals, the proposed methodology combines sensor data acquisition, signal
preprocessing, and machine learning-based classification. Multiple sensors—including DS18B20 for
temperature, a turbidity sensor, a TCS34725 color sensor, and manual inputs for pH and fat content
are integrated with a Raspberry Pi microcontroller to capture real-time milk parameters. The
collected data undergoes preprocessing such as normalization and outlier handling before being fed

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 4


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

into supervised machine learning models. Feature engineering and model training are performed
using algorithms like Support Vector Machine (SVM), Random Forest, AdaBoost, XGBoost, and a
custom Stacking Classifier for improved accuracy. The trained model is then deployed through a
Flask-based web interface, where users can input real-time sensor data and instantly receive
predictions on milk purity. This streamlined methodology ensures a balance between accuracy,
speed, and user accessibility, enabling practical deployment in rural and urban settings alike.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 5


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

CHAPTER-2
LITERATURE REVIEW

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 6


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

2. LITERATURE REVIEW

Milk plays a crucial role in the diet of millions around the world and is a key source of
nutrients such as calcium, protein, and vitamins. With increasing dairy production and
consumption, ensuring the quality and accurate measurement of milk has become critical.
Manual methods of milk collection and quality testing are often prone to human error,
inefficiency, and inconsistencies. In response, several technological innovations have
emerged aiming to automate and enhance the accuracy of milk collection systems. This
literature review explores past research, technologies, and developments related to milk
calibration systems, quality sensing mechanisms, and automation in dairy technology.

The traditional methods of milk collection involve manual weighing and fat content
estimation using lactometers and other simple tools. These techniques, although widely used,
often lack precision and are not scalable for large-scale operations. Studies such as those by
Patil et al. [1] (2017) have emphasized the need for digital systems capable of capturing
accurate measurements of milk quantity and quality to ensure transparency and trust between
farmers and dairy processing units.[1]
Modern milk collection systems now employ digital weighing machines, RFID-based farmer
identification, and microcontrollers like Arduino or Raspberry Pi for automating the
collection process. These innovations have drastically reduced manual intervention and
enhanced the speed of operations. For instance, Kumar and Desai (2020) proposed an IoT-
enabled milk collection unit that records milk weight, temperature, and fat percentage in real-
time, storing data on cloud platforms for traceability and analytics.[2]

Two of the most critical quality parameters in milk are fat percentage and Solids-Not-Fat
(SNF) content. Traditional fat testing methods, such as the Gerber method, although reliable,
are time-consuming and labor-intensive. Research by Goyal and Bansal (2018) introduced
the use of ultrasonic sensors for non-invasive fat and SNF detection, demonstrating
significant potential in real-time milk quality analysis.
Recent developments have integrated optical sensors, electrical conductivity sensors, and
refractometers to improve the accuracy of fat content analysis. Machine learning algorithms
are also being used to calibrate sensors based on historical milk quality data, thus improving
prediction accuracy. These systems can identify anomalies or adulterations in milk by

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 7


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

correlating fat, SNF, and density values with expected norms.

Arduino-based microcontrollers have found widespread application in agricultural


automation due to their low cost, open-source nature, and ease of programming. In milk
calibration systems, Arduino boards are commonly used to integrate multiple sensors—load
cells for weight, temperature sensors (like DS18B20), and fat measurement sensors.
According to a study by Sharma et al. (2021), Arduino-enabled systems were able to reduce
milk collection errors by up to 30% in rural cooperative societies.[3]
The integration of GSM modules and RFID readers with Arduino has further enhanced these
systems. Farmers can receive instant SMS notifications of milk collected, and the data can be
uploaded to a central server for processing. Such systems not only ensure transparency in
transactions but also help in early identification of spoiled or adulterated milk.

With the rise of Industry 4.0, the dairy industry is increasingly embracing smart technologies.
IoT (Internet of Things), AI (Artificial Intelligence), and data analytics are now being
applied to develop smart dairy systems. Automated milk analysis machines can now detect
over 20 parameters, including fat, protein, SNF, density, temperature, and presence of
contaminants.
For instance, Nestlé and Amul have invested in automated milk collection centers that
leverage RFID, cloud databases, and real-time monitoring to manage large-scale milk
procurement from thousands of farmers. These systems not only ensure fair payment but also
empower farmers with insights about milk quality trends over time.
The work by Sinha et al. (2019) on "Smart Dairy Farming" shows how predictive analytics,
using quality data collected over time, can be used to improve animal health, optimize feed
quality, and increase milk yield, thereby integrating milk calibration with overall dairy farm
management.[4]

While a lot of work has been done in the area of milk collection automation and fat content
analysis, several challenges still persist. The calibration of sensors under variable
environmental conditions remains a concern. Furthermore, many rural and semi-urban dairy
cooperatives lack the infrastructure and training needed to adopt such technologies. There's
also limited research on the long-term stability and calibration drift of low-cost sensors used
in these systems.
Moreover, affordability and maintenance remain a bottleneck, especially in developing

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 8


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

countries. Studies suggest the need for low-cost, modular, and easily maintainable systems
that can be adopted at the grassroots level. Additionally, data security and the privacy of milk
transaction data is an emerging concern in digitally connected systems.

Sensors require routine calibration to maintain accuracy. Factors like temperature, humidity,
dust, and electrical noise can affect readings over time.
In a paper by Jadhav et al. (2020), a smart calibration algorithm was proposed for fat sensors,
which recalibrated based on known reference values collected periodically. This approach
reduced error by 15% over 3 months.[5]
Future work may involve self-calibrating systems that learn from historical data, powered by AI
and machine learning.

Arduino microcontrollers have transformed prototyping and low-cost embedded design. They
support analog/digital inputs, serial communication, and can interface with GSM, RFID, and
cloud platforms.
Chavan et al. (2019) demonstrated a full dairy automation system using Arduino Uno, capable
of weight logging, fat detection, and sending SMS receipts to farmers. The cost-effectiveness
and simplicity of the Arduino ecosystem make it ideal for rural deployment.[6]

Modern milk calibration systems store historical data on cloud platforms or SD cards. This not
only helps maintain records but also ensures traceability—a critical factor in modern food safety
norms.
IoT-enabled systems can track milk from collection point to processing center, with real-time
alerts on quality deterioration. Projects like NDDB’s e-Milk initiatives in India have shown how
digital traceability can reduce spoilage and enhance farmer accountability.

Recent research focuses on machine learning to analyze large datasets from milk calibration
systems. Features like temperature, fat percentage, and SNF content can predict milk spoilage or
contamination.[7]
A study by Bhatt and Narayan (2022) used Random Forest algorithms to predict milk
adulteration with over 90% accuracy. These tools help processing units reject poor-quality milk
before it enters the supply chain.[8]

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 9


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

Fat percentage and Solids-Not-Fat (SNF) content are central to determining milk value. While
Gerber and Babcock tests remain common, modern systems aim to provide real-time digital
alternatives.
Studies by Singh et al. (2018) introduced infrared spectroscopy as a reliable method for fat analysis.
Others explored ultrasonic methods that measure the velocity of sound through milk to infer its
composition.[9]
A challenge remains in creating low-cost, rugged sensors for fat/SNF detection that can withstand
field usage without frequent recalibration.

The literature clearly demonstrates a global trend toward digitizing and automating milk calibration
processes. Systems that integrate sensors, microcontrollers, and wireless communication not only
improve accuracy but also increase transparency and trust in the dairy value chain.[10]

However, successful adoption hinges on affordability, ruggedness, and ease of use. Future research
should focus on self-calibrating, AI-enhanced sensors, offline-compatible GSM systems, and
modular designs that can be easily scaled in low-resource environments. By aligning technological
advancements with local needs, the dairy industry can significantly enhance productivity and fairness
for both producers and processors.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 10


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

CHAPTER-3
Methodology

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 11


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

[Link]
Introduction
The system detects milk adulteration using sensor data and machine learning. Sensors
measure key milk properties turbidity, temperature, color, and pH using devices like the
DS18B20, TCS34725, and pH strips. These readings are collected via a Raspberry Pi and
stored for processing.
Labeled data from pure and adulterated samples is used to train various ML models
including Random Forest, SVM, AdaBoost, and XGBoost. A Stacking Classifier, which
combines multiple models, achieved the best accuracy and was chosen for deployment. The

below figure shows how the process.

Fig
3.1

Milk Testing Cycle

3.1 Milk Calibration Kit:


The hardware kit is the physical foundation of the system, comprising the Raspberry Pi and various
sensors. From the figure 3.1.1. It includes a DS18B20 temperature sensor, TCS34725 color sensor,
turbidity sensor, and a custom LED-LDR setup for fat measurement. Additionally, pH is tested using
strips. These components interact with the Raspberry Pi through GPIO pins, enabling real-time data
acquisition from milk samples. The sensors collect environmental and physical parameters like
temperature, color, clarity, and fat levels key indicators of milk quality. This kit allows the system to
function autonomously in a dairy setup or vendor location.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 12


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

Fig 3.1.1 Milk Calibration Kit Implementation

3.1.1 Temperature Sensor (DS18B20)


The DS18B20 is a digital temperature sensor ideal for milk adulteration detection due to its precision
and ease of integration. It uses a 1-Wire protocol, allowing multiple sensors to share a single data
line, and operates within -55°C to +125°C with ±0.5°C accuracy in the -10°C to +85°C range.
Milk must be stored between 2°C and 8°C to prevent spoilage. Adulteration, like adding water, alters
milk's thermal properties, which the DS18B20 can detect. It provides real-time readings to the
Raspberry Pi for immediate processing.
The sensor is waterproof and easy to connect using Python libraries like w1thermsensor. It also helps
calibrate other sensor outputs, such as fat content, which varies with temperature. Its robustness
makes it suitable for field use and long-term monitoring. Temperature sensor shows below fig
[Link].

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 13


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

Fig [Link] Temperature Sensor DS18B20


3.1.2 Colour Sensor (TCS34725)
The TCS34725 Color Sensor plays a critical role in milk quality assessment by detecting subtle
changes in milk coloration that can indicate adulteration or spoilage. This sensor is known for its
high sensitivity and accuracy, making it an excellent choice for real-time detection in smart systems.
It operates on the principle of RGB (Red, Green, Blue) color sensing, and it comes equipped with an
IR filter and an integrated white LED for consistent illumination during measurements.
Working Principle:
The TCS34725 sensor uses a photodiode array and a 16-bit ADC (Analog-to-Digital Converter) to
convert light intensity into digital signals. It detects red, green, blue, and clear light, allowing a
precise color profile to be constructed for any object placed in front of it. When used with milk
samples, this sensor can measure the true color by isolating ambient light interference using its
internal IR blocking filter.

Fig [Link] TCS34725

Pure milk generally appears white or slightly creamy. If it is adulterated with substances like
synthetic milk, detergents, or starch, the color may subtly shift. These changes might not be visible to
the human eye but can be quantified by the TCS34725, making it highly useful in quality testing
applications.
Implementation in the Project:
In this milk adulteration detection system, the TCS34725 color sensor is interfaced with the
Raspberry Pi using I2C communication. The sensor captures the RGB values of each milk sample

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 14


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

and sends the digital readings to the controller. These values are then included in the feature set used
for training the machine learning model. During live tests, these readings help classify the milk as
good or bad based on previously learned patterns.
3.1.3 Fat Calculation Using LED and LDR
Milk fat content is a critical parameter for determining milk quality and commercial value.
Traditional methods for measuring fat, such as the Gerber or Babcock methods, are manual, time-
consuming, and require chemical reagents. To overcome these limitations, a non-invasive, optical
method using LED and LDR (Light Dependent Resistor) is employed in this smart system for
estimating fat concentration in milk. Fat calculated by using LDR and LED from the figure [Link].

Working Principle:
This method is based on the light scattering phenomenon. Milk is an emulsion where fat globules
scatter incident light. The amount of light that passes through milk and reaches the LDR varies with
fat content:
 Higher fat content → More scattering → Less light reaches LDR
 Lower fat content → Less scattering → More light reaches LDR
In this setup:
 An LED emits light through a test tube containing milk.
 The LDR is placed directly opposite the LED.
 As milk scatters the light, the intensity of light falling on the LDR changes based
on the fat concentration.
 The LDR’s resistance increases with lower light intensity (higher fat) and
decreases with higher light intensity (lower fat).
The relationship between the light intensity (L) and the resistance (R) of an LDR is inversely

R∝ L
proportional:
1

Mathematical Model for Fat Calculation:


Let:
 I 0: Initial light intensity (LED output without milk)
 I : Light intensity after passing through milk
I
 T: Transmittance =
I0
 F: Fat content
Using empirical analysis or calibration with known fat values:
F=a − b⋅T ----[1]
Where:
 a and b are constants determined during calibration with standard milk samples.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 15


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

Alternatively, fat can be inversely estimated using voltage across the LDR (since it forms a voltage
divider with a fixed resistor):
R LDR
V LDR=V ¿. ----------[2]
R LDR + R¿

From

calibration:
1
F≈ k.
V LDR
Where k and ccc are constants derived experimentally.

Fig [Link] Calibration of Fat


3.1.4 pH:

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 16


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

The pH value of milk is a critical indicator of its freshness and overall quality. Fresh milk typically
has a pH ranging between 6.5 to 6.8, which indicates a slightly acidic nature. Any deviation from this
range can signal poilage, bacterial growth, or adulteration. In the proposed smart milk adulteration
detection system, pH measurement is performed manually using pH paper strips. This method is
chosen due to its simplicity, cost-effectiveness, and quick indication of acidity or alkalinity.

Fig [Link] pH Strips

3.1.5 Turbidity Sensor


Turbidity measures the
cloudiness of milk, indicating the
presence of adulterants like
water, starch, or detergent. The
turbidity sensor works by emitting light through the milk and measuring how much is scattered—
changes in scattering reveal contamination.
In this system, the sensor connects to a Raspberry Pi, providing voltage output that is converted into
turbidity values (NTU). Pure milk has a known turbidity range, while adulterated samples show
deviations. The sensor’s readings, used as key input features for the ML model, help classify milk
quality in real time.

Fig [Link] Turbidity Sensor

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 17


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

3.2 Raspberry Pi:


The Raspberry Pi is a small, low-cost, single-board computer developed by the
Raspberry Pi Foundation to promote digital education and hardware experimentation. Despite
its compact size, it is a powerful and versatile platform capable of running a full-fledged
operating system, interfacing with various peripherals, and supporting AI and machine learning
applications. Due to its adaptability and affordability, the Raspberry Pi has become an essential
tool in many Internet of Things (IoT) and embedded systems projects—including real-time
milk adulteration detection systems.
In the context of this project, the Raspberry Pi serves as the central processing unit that gathers,
processes, and analyzes data from multiple sensors connected to it. It acts as the brain of the
system, facilitating communication between the hardware (sensors) and software (AI/ML
models). Its GPIO (General Purpose Input/Output) pins make it easy to connect sensors that
monitor milk quality parameters such as pH, temperature, conductivity, and turbidity.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 18


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

One of the primary advantages of using Raspberry Pi is its support for Python and other
programming languages, which makes it easy to integrate AI-based algorithms for detecting
patterns and anomalies in milk characteristics. These algorithms can be trained to distinguish
between pure and adulterated milk using sensor data. Once trained, they can be deployed on
the Raspberry Pi to provide real-time detection and alerts.

Fig 3.2.1 Raspberry pi and Pin Diagram


Furthermore, Raspberry Pi supports camera modules and USB peripherals, which can be useful
in future upgrades, such as optical analysis or QR-based traceability systems. It also allows for
energy-efficient operations and can be powered using standard USB power supplies or battery
packs, making it suitable for rural or field-level applications.
 Cost-effective and ideal for budget-sensitive applications.
 Compact and portable, suitable for both lab and field use.
 Multifunctional, capable of sensor interfacing, data processing, storage, and
communication.

 Support for AI/ML models, enabling smart detection of adulterants.


 User-friendly with a large support community and plenty of documentation.

The Raspberry Pi is a compact, low-cost, credit-card-sized single-board computer developed


by the Raspberry Pi Foundation. Originally designed for educational purposes, it has evolved
into a robust tool for electronics projects, industrial automation, IoT systems, and AI-based
applications.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 19


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

Features:
1. Processor & Performance
Most recent models (like Raspberry Pi 4 and 5) include a quad-core ARM Cortex
processor, offering clock speeds up to 1.8 GHz, with 1GB to 8GB RAM options. This
enables it to handle moderate data processing, edge computing, and even AI workloads
without needing a full computer.
2. GPIO Pins (General Purpose Input/Output)
Raspberry Pi boards come with 40 GPIO pins for connecting sensors and modules. These
pins allow it to read analog/digital inputs from temperature, turbidity, and pH sensors, or
to control actuators, displays, etc.
3. Connectivity Options
o Wi-Fi and Bluetooth for wireless communication
o USB ports for peripherals like keyboards, cameras, and storage
o Ethernet for stable, high-speed networking
o HDMI for connecting to displays
4. Operating System Support
Raspberry Pi runs Raspberry Pi OS (formerly Raspbian), a Linux-based system. It also
supports Ubuntu, Windows IoT Core, and others. This flexibility allows you to program
in Python, C, [Link], or any supported language.

Raspberry Pi GPIO Pin Configuration


The Raspberry Pi features a 40-pin GPIO header that allows it to interface with a wide range of
electronic components and sensors. These General Purpose Input/Output (GPIO) pins are
essential for controlling devices, reading data, and building real-world hardware applications.
Understanding the pin structure is crucial for effective hardware integration, especially in IoT
and embedded system projects.

Pin Classification
The 40 GPIO pins are divided into:
Pin Type Description
Power Pins Provides 3.3V or 5V output for powering sensors and modules
Ground Common ground connection for all electronic components

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 20


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

Pin Type Description


(GND)
GPIO Pins Programmable digital pins for input/output (high or low signal)
Special Pins Include I2C, SPI, UART used for serial and high-speed communication
Table 3.2.1Raspberry Pi GPIO Pin Classification

GPIO Pin Layout Table (Physical Pin Numbering)


Pin Name Function Pin Name Function
1 3.3V Power 2 5V Power
GPIO2
3 I2C Data 4 5V Power
(SDA1)
GPIO3
5 I2C Clock 6 GND Ground
(SCL1)
General GPIO14
7 GPIO4 8 UART TX
GPIO (TXD)
GPIO15 UART
9 GND Ground 10
(RXD) RX
General
11 GPIO17 12 GPIO18 PWM
GPIO
General
13 GPIO27 14 GND Ground
GPIO
General General
15 GPIO22 16 GPIO23
GPIO GPIO
General
17 3.3V Power 18 GPIO24
GPIO
GPIO10
19 SPI 20 GND Ground
(MOSI)
GPIO9 General
21 SPI 22 GPIO25
(MISO) GPIO
GPIO11 GPIO8 SPI Chip
23 SPI 24
(SCLK) (CE0) Enable 0
25 GND Ground 26 GPIO7 SPI Chip

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 21


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

Pin Name Function Pin Name Function


(CE1) Enable 1
ID ID
GPIO0 GPIO1
27 EEPROM 28 EEPROM
(ID_SD) (ID_SC)
(I2C) (I2C)
General
29 GPIO5 30 GND Ground
GPIO
General
31 GPIO6 32 GPIO12 PWM
GPIO
33 GPIO13 PWM 34 GND Ground
SPI / General
35 GPIO19 36 GPIO16
PWM GPIO
General General
37 GPIO26 38 GPIO20
GPIO GPIO
General
39 GND Ground 40 GPIO21
GPIO
Table 3.2.2 Raspberry Pi GPIO Pin Layout

Common Uses of GPIO Pins:


 Sensor inputs (temperature, light, pH, etc.)
 Actuator control (motors, relays, LEDs)
 Communication interfaces (I2C for LCD, SPI for ADC, UART for GPS modules)
Raspberry Pi Implementation
Hardware Connections
The core of the smart milk adulteration detection system relies heavily on the Raspberry
Pi, a compact, powerful single-board computer known for its versatility in IoT and embedded
systems applications. In this project, the Raspberry Pi acts as the central controller
responsible for collecting sensor data, processing it, and communicating with the machine
learning model through the web application.
Hardware Components Connected to Raspberry Pi:
1. DS18B20 Temperature Sensor
o Connected using the 1-Wire protocol.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 22


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

o Requires a 4.7kΩ pull-up resistor between the data and VCC lines.
o Measures milk temperature and sends data to the Pi via GPIO.
2. Turbidity Sensor
o Connected through analog-to-digital converter (ADC) like MCP3008, since the
Raspberry Pi does not have an analog input.
o Turbidity values help detect dilution or foreign particles.
3. TCS34725 Color Sensor
o Communicates with Raspberry Pi via I2C interface.
o Helps determine the color intensity of milk to assess quality and adulteration.
4. LED-LDR Fat Measurement Setup
o The LED is powered by a GPIO pin.
o LDR is connected to the ADC to capture analog values of light intensity, which
indirectly indicate fat content.
5. Power Supply
o The system is powered using a 5V DC adapter connected to the Raspberry Pi.
o Additional power management modules like buck converters or power hats may
be used for voltage regulation.
6. Breadboard & Jumper Wires
o Used for prototyping and establishing temporary connections between sensors
and GPIOs.
7. pH Measurement
o Although done manually using strips, the value is entered manually through the
Flask web interface, allowing seamless integration with sensor-based values.
System Flow:
 Upon powering up, the Raspberry Pi initializes all sensors.
 Data is collected in real-time from each connected sensor.
 Values are stored locally in CSV format for training and testing purposes.
 During live testing, real-time values are passed to the Flask backend to run
predictions using the trained machine learning model.
Advantages of Using Raspberry Pi:
 Portable and lightweight.
 Supports Python-based sensor interfacing and machine learning libraries.
 Easily integrated with web technologies like Flask.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 23


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

 GPIO pins offer flexibility in hardware communication.


 Supports Wi-Fi/Ethernet for remote data transmission and application hosting.

Fig 3.2.2 Pin Configuration


Data Collection
and Generation
of CSV File
Introduction
Data collection is a
critical step in developing an
intelligent system for
milk adulteration
detection. For any machine
learning model to
perform accurate
classification or prediction,
it must be trained with a
sufficient volume of reliable, labeled, and structured data. In this project, data was gathered from
multiple sensor modules connected to a Raspberry Pi, along with manually collected parameters like
pH and fat content. All the sensor readings were recorded and saved in CSV format, which is one of
the most accessible formats for further processing and analysis.
Sensor Integration for Data Collection

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 24


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

The primary sensors used for automated data collection include:


 DS18B20 Temperature Sensor: Measures the temperature of the milk sample. This sensor
uses a 1-Wire interface, making it easy to connect and communicate with the Raspberry Pi.
 Turbidity Sensor: Detects the purity of milk by measuring suspended particles. A high
turbidity value generally indicates the presence of impurities or adulterants.
 TCS34725 Color Sensor: Determines the color composition of milk (RGB values), which
can change due to dilution, addition of starch, synthetic milk, etc.
 LED-LDR Module (Fat Calculation): Uses the light scattering principle. A beam of light is
passed through a milk sample using an LED, and the scattered light is received by an LDR.
The resistance of the LDR changes depending on the light intensity, which is inversely
proportional to the fat content.
In addition to the above, two parameters are manually recorded:
 pH: Measured using pH strips dipped into the milk sample.
 Fat Content: Estimated using a lactometer and validated against the LED-LDR readings.
All sensor readings are read in real-time using Python scripts on the Raspberry Pi. These values are
formatted and appended to a CSV file using the built-in Python csv library.
Data Logging Workflow
1. Sensor Initialization: The Python script first initializes all sensors connected to the
Raspberry Pi GPIO and I2C pins.
2. Real-time Reading: Each sensor is read sequentially. The temperature is logged first,
followed by turbidity, color (RGB), and LDR resistance value.
3. Manual Entry Fields: Users are prompted to input manually measured values for pH and fat
(if lactometer-based data is also used).
4. Data Structuring: All readings are stored in a structured format with clearly defined
columns:
o Temperature
o Turbidity
o R, G, B (from TCS34725)
o LDR Reading
o pH
o Fat (%)
o Label (Good/Bad) — manually annotated based on domain knowledge or expert
validation.
5. CSV Generation: A row is appended to the CSV file every time a sample is recorded. This

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 25


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

file becomes the dataset for machine learning.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 26


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

3.3 Machine Learning:

Fig 3.3.1 Stacking Classifier


1. Random Forest: A robust ensemble model based on decision trees that reduces overfitting and
increases accuracy.
Random Forest is based on the concept of "bagging" (Bootstrap Aggregating), where:
 Multiple decision trees are built on random subsets of the training data (with
replacement).
 At each split in the tree, a random subset of features is considered to reduce correlation
between trees.
 The final output is obtained by aggregating predictions from all trees.

Steps of the Algorithm


1. Bootstrap Sampling: From the dataset D of size N, draw B bootstrap samples
D1 , D 2 , , … . , D B each of size N (sampling with replacement).

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 27


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

2. Train Decision Trees: For each sample Db :


o Train a decision tree T b using a random subset of features at each node.
o The tree is grown fully (unpruned) to reduce bias.
3. Make Predictions:
o Classification: Take the majority vote from all B trees.
ŷ=mode(T 1(x),T 2 (x)..., T B(x)) ------[1]
Regression: Take the average prediction from all trees.
n
1
Ŷ = ∑ T b (x) -----[2]
B b=1
3. Important Equations
3.1. Gini Impurity (for classification):
Used to decide the best feature to split in a node.
K
Gini(p)=1- ∑ p2K -----[1]
K=1

Where pk , k p is the proportion of class k at a node.


3.2. Information Gain (alternative to Gini):
¿ ¿¿
IG (S, A) = Entropy(S) - ∑ ¿ Sv∨
¿ S∨¿ Entropy( S v) –[2]
v € values (A )

Used in some trees (e.g., ID3, C4.5) to select splitting features.


3.3. Mean Squared Error (MSE) (for regression):
N
1
MSE =
N
∑ ¿ ¿) ------[3]
i=1

1. Support Vector Machine (SVM): A classifier that finds the best boundary between classes
by maximizing the margin.
SVM tries to find the optimal hyperplane that:
1. Separates the classes.
2. Maximizes the margin between the closest points of each class (called support vectors) and
the hyperplane.
SVM Equations (Linear Case)
1. Equation of a Hyperplane
In an n-dimensional space:
t
w x+b=0 -----[1]

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 28


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

 w = weight vector (normal to the hyperplane)


 x = input feature vector
 b = bias term
2. Classification Rule
For a data point x i, the class label y i∈ {−1, +1};
y i(w r xi +b) ≥1 ----[2]

This ensures that data points are on the correct side of the margin.
3. Objective (Hard Margin SVM)
Find w and b that minimize:
1
2
||w||^2 ---[1]

Subject to the constraint:

y i ( w r xi +b ) ≥1 ∀ i -----[2]

This is a convex quadratic optimization problem


Soft Margin SVM (for non-linearly separable data)
To allow some misclassification, introduce slack variables ξi≥0:
r
y i ( w x i+ b)≥ 1-ξi -----[3]

Objective becomes:
n
1
min ( ||w|| +c ∑ ξi)
2
-----[4]
w b ξi ​ 2
i i i=1

Non-linear SVM with Kernels


If data is not linearly separable, SVM uses a kernel function K( x i x j) : K( x i , x j ¿ ; K( x i , x j ¿ to map
inputs into higher-dimensional space.
Common kernels:
 Linear: K¿
 Polynomial: K( x i , x j ¿=(x Ti x j +c )d

 RBF (Gaussian): K( x i , x j ) = exp(-ɤ|| x i x j∨¿2)


Dual Form (used for kernelized SVM)
the dual problem:
n
1
max ∑ α i− ∑ α α y y K (x i , x j) ---[1]
α i=1 2 i, j i j i j

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 29


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

Subject to:
n
0≤ α i ≤C, ∑ αi y i=0
i=1

Once solved, prediction for new x is:


n
f(x)= ∑ α i y i k ( x i , x ) + b ----[2]
i=1

2. AdaBoost: An adaptive boosting algorithm that combines weak learners to form a strong
classifier.
AdaBoost, short for Adaptive Boosting, is a powerful ensemble learning algorithm that combines
multiple weak learners (often decision stumps) into a strong classifier by focusing on the errors
of previous learners.
Algorithm and Equations
1. Initialize weights
For N training examples ( x 1 , y 1),…,( x N y N ), where y i ∈ {−1, +1}:
(1) 1
wi = for all i ---[1]
N

2. For each round t=1,2,...,T:


a. Train weak learner ht (x) using weighted data
Minimize weighted error:
N

∑ w (it ) . П (h i( xi )≠ y i )
i=1
ϵt = N ---[2]
∑w (t )
i
i=1

Where:
 П is the indicator function (1 if incorrect, 0 if correct).

b. Compute model weight (importance of learner):


1
αt = ln ¿) ---[3]
2
The lower the error ϵ t , the higher the weight α t
c. Update sample weights:
(t +i) (t )
wi w i . exp ¿) --[4]

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 30


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

Normalize:
(t +i)
wi
(t +i) N
wi = ---[5]
∑ w(tj +i)
j =1

This increases weights for misclassified examples.


3. Final strong classifier:
T
H(x) = sign (∑ α t h t ( x )) ----[6]
t =1

Summary of Components
Term Description
ht ¿ ) Weak classifier at round t
ϵt Weighted error of ht t h
αt Weight (confidence) of ht t h
(t )
wi Weight of training point i at round

3. XG-Boost: A gradient boosting algorithm optimized for performance and speed.


XG Boost is an efficient and scalable implementation of gradient boosting for decision trees. It
builds an ensemble of trees sequentially, where each new tree corrects the residual errors of the
previous trees.
It is widely used in machine learning competitions and real-world problems due to its speed,
regularization, and handling of missing values.
Mathematical Formulation
Given a dataset with nnn examples:
D = {( x 1 y 1) , ( x 2 y 2), …, {( x n y n)}
We want to learn a model that predicts:
K
ŷ i= ∑ f k ( xi ) , f k € F -----[1]
K=1

Where:
 f k is a regression tree (also called CART).

 F is the space of all regression trees.


 K is the number of trees.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 31


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

1. Objective Function
n n
L(ϕ) = ∑ l ( y i , ŷ i) + ¿ ∑ Ω ( f k ) ¿ ----[2]
i=1 k=1

Where:
 L is a loss function (e.g., MSE for regression or log-loss for classification).
 Ω(f) is the regularization term:
o T: number of leaves in the tree
o w: leaf weights
o γ, λ: regularization parameters
2. Additive Training (Boosting)
Model is built additively:
(t ) (t −1)
ŷi = ŷi + f t (x¿ ¿i)¿ ----[3]
3. Second-order Taylor Approximation
To make optimization efficient, XGBoost uses a second-order approximation of the loss:
n
1
L(t) ≈∑ [g i f t ( x i ) + hi f t (x i )]+ Ω f t --[4]
2

i=1 2
Where:
 gi = first derivative

 hi = second derivative

4. Structure Score for Tree Building


Suppose the data is partitioned into T leaves. Let I j be the set of data indices in leaf j:
T 2
1 Gj
Score = - ∑
2 j=1 H j + λ
+ɤT

Where:
G j ∑ gi , H j = ∑ h i --[5]
i ∈I j i∈ I j

This score is used to decide where to split the tree by maximizing gain.
5. Final Prediction
After K trees:
K
ŷ i= ∑ f k ( x i) --[6]
K=1

Summary of Components

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 32


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

Component Description
fk Regression tree (weak learner)
gi hi Gradient and Hessian of the loss
Ω(f) Regularization to penalize complexity
ŷi Final prediction for input x i
Stacking Classifier: A hybrid model that combines multiple base classifiers (like those above) and
uses a meta-classifier to make final predictions.
Stacking (Stacked Generalization) is an ensemble learning technique that combines multiple base
models (often of different types) and uses a meta-model to make the final prediction.
Unlike bagging or boosting, stacking focuses on learning how to best combine the predictions of
several base learners.
Mathematical Formulation
Let’s assume:
 D = {( x i , y i )}: training data
 You have MMM base classifiers: h1 , h2,...,h M
 A meta-classifier H
1. Base Learners Training
Each base learner is trained on the original input data:
h m (x), for m=1,...,M ----[1]
2. Meta-Feature Generation
For each training instance x i, create a meta-feature vector:
z i, =[h1( x i),h2 ( x i ¿ ..,,h m( x i ¿ ¿ , ---[2]
This becomes the input to the meta-classifier.
So, new dataset for the meta-learner:
D′ = {( z i, y i )}
To avoid overfitting, these predictions are usually generated via out-of-fold predictions (cross-
validation).
3. Meta-Model Training
Train a meta-classifier H on D′:
H(z) = H(h1(x), h2(x),…, h m(x)) ---[3]
4. Final Prediction
For a new test instance x:

Ŷ = H (h1 (x), h2 (x),…, h m(x)) ---[4]


Example (Binary Classification)
Let’s say:
 h1 (x) = Logistic Regression

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 33


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

 h2 (x) = Decision Tree


 h3 (x) = SVM
 H = Random Forest (meta-model)
Then,
Ŷ = H ((h1 ( x ) , h2 ( x ) ,h 3( x)¿ ---[5]

Each h m(x) gives a probability or class prediction, and H learns how to best combine them.
Summary of Components
Element Description
h m (x) Base learners
Meta-feature vector (predictions from base
zi
models)
H(z) Meta-model making final prediction
ŷ Final prediction from the stacking model

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 34


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

CHAPTER-4
SOFTWARE USED

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 35


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

4. SOFTWARE USED

4.1 Raspberry Pi, Python Modules and Libraries Used

import time

 Purpose: Provides time-related functions.

 Usage in code:

o [Link](3): Pauses the script for 3 seconds to avoid overwhelming sensor readings.

o [Link](...): Generates timestamps for CSV entries.

import board

 Purpose: Part of the Adafruit_Blinka library, used for handling I2C, SPI, UART pins on
Raspberry Pi.

 Usage in code: Required for initializing the I2C interface to communicate with the
TCS34725 color sensor.

import os

 Purpose: Provides a way to interact with the operating system.

 Usage in code:

o [Link](...): Loads kernel modules to enable temperature sensor reading via 1-wire.

o [Link](...): Checks if the CSV file already exists.

import glob

 Purpose: Finds all the pathnames matching a specified pattern.

 Usage in code:

o Locates the device folder where the DS18B20 temperature sensor data is stored.

import csv

 Purpose: Used to handle CSV file creation and manipulation.

 Usage in code:

o [Link](...): Writes sensor values to a CSV log file ([Link]).

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 36


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

import adafruit_tcs34725

 Purpose: A library for controlling the TCS34725 RGB color sensor.

 Usage in code:

o sensor = adafruit_tcs34725.TCS34725(i2c): Initializes the sensor using I2C.

o sensor.color_raw: Fetches raw RGB data.

import [Link] as GPIO

 Purpose: Used to interface with Raspberry Pi GPIO pins.

 Usage in code:

o [Link]([Link]): Uses the Broadcom chip-specific pin numbering.

o [Link](...): Configures a GPIO pin as an input (for turbidity sensor).

o [Link](...): Reads digital input to determine turbidity status.

o [Link](): Cleans up GPIO pin states on script termination.

Sensor Initialization and Configuration

Color Sensor (TCS34725)

python
i2c = board.I2C()
sensor = adafruit_tcs34725.TCS34725(i2c)
[Link] =4
sensor.integration_time = 154

 Initializes I2C and sets the sensor’s sensitivity and exposure time to balance brightness
detection.

Temperature Sensor (DS18B20)

python
[Link]('modprobe w1-gpio')
[Link]('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = [Link](base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 37


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

 Enables 1-Wire interface and locates the temperature sensor path in the device tree.

 read_temp() reads and converts raw data into °C.

Turbidity Sensor

python
CopyEdit
TURBIDITY_PIN = 18
[Link]([Link])
[Link](TURBIDITY_PIN, [Link])

 Configures a digital pin to read turbidity sensor status:

o [Link]: Clear liquid (less pure)

o [Link]: Turbid (more solid particles → higher purity assumed)

Custom Functions Explained

read_temp_raw() and read_temp()

 Reads raw sensor output from /w1_slave file and parses it for temperature in °C.

is_color_white(r, g, b)

python
CopyEdit
avg_rgb = round(((r+g+b)/3))

 Calculates the average of red, green, and blue color components.

 A higher average indicates "white" or diluted milk.

CSV Logging

python
CopyEdit
filename = "[Link]"
if not file_exists:
[Link](['Timestamp', 'Temperature (C)', 'Is White', 'Milk Purity'])

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 38


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

 Creates a CSV file and logs:

o Timestamp

o Temperature

o Color Status (based on whiteness)

o Milk Purity (from turbidity sensor)

4.2 Machine Learning

To effectively detect milk adulteration, the implementation of machine learning algorithms


was a key component of the system. The primary goal was to classify milk samples as either pure or
adulterated based on various physical and chemical parameters collected via sensors and manual
methods. The machine learning process was executed entirely using Google Colaboratory (Google
Colab) a cloud-based Jupyter Notebook environment that provides free access to GPUs and is highly
suitable for real-time coding, experimentation, and model training.

The first step involved data preprocessing and exploration. Sensor readings such as
temperature, turbidity, RGB color values, fat content, and pH were collected using a Raspberry Pi
connected with sensors. These data points were recorded and exported as CSV files, which served as
the foundation for model training. This dataset was uploaded to Google Colab where data cleaning,
normalization, and feature selection were performed using Python libraries such as Pandas, NumPy,
and Scikit-learn.

Once the dataset was clean and structured, various machine learning classification models
were trained. The models used include Random Forest, Support Vector Machine (SVM), AdaBoost,
XGBoost, and a Stacking Classifier — an ensemble technique that combines the strengths of
multiple models. These models were chosen for their robustness, accuracy, and ability to handle non-
linear and imbalanced data distributions, which are common in real-world sensor datasets. Google
Colab facilitated this process by allowing smooth integration with key machine learning libraries like
Scikit-learn, XGBoost, and Matplotlib for visualizing performance.

The dataset was split into training and testing sets, usually at a 70:30 ratio. Feature scaling
techniques such as Standard Scaler were applied to ensure uniformity across different units and
ranges. Model training was conducted with cross-validation to minimize overfitting, and the
evaluation metrics included Accuracy, Precision, Recall, and F1-score to ensure comprehensive

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 39


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

performance measurement.

Among all models, the Stacking Classifier emerged as the most effective, achieving an
accuracy of 91.6%, slightly outperforming the standalone models. The stacking approach leverages
predictions from base learners (e.g., Random Forest and SVM) as inputs to a meta-learner, which
learns how to best combine them. This technique is particularly beneficial in complex classification
problems like milk adulteration detection, where patterns can vary based on subtle sensor
fluctuations.

Furthermore, Google Colab's interactive code execution, real-time collaboration, and


seamless integration with Google Drive enabled an efficient development cycle. Code was
documented, visual outputs (such as confusion matrices and ROC curves) were generated, and all
models were saved using Python’s joblib or pickle for later deployment.

In conclusion, Google Colab served as a powerful platform for implementing, testing, and
validating the machine learning models. Its resource-rich environment enabled rapid prototyping and
thorough experimentation, leading to a reliable AI-driven solution for real-time milk adulteration

detection.

Fig 4.1.1 Google Collab

Through the below fig 4.1.1 it is an interface for terminal opening with Google Colab. By
using Colab we can train machine and genereate .pkl file.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 40


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

4.2.1 Random Forest Algorithm :


1. import pandas as pd

 Library: pandas

 Purpose: Data manipulation and analysis.

 Usage:

o pd.read_csv("[Link]"): Reads your dataset from a CSV file into a


DataFrame.

o [Link](...): Drops unwanted columns (e.g., 'Grade' from feature set).

o Used extensively for handling tabular data.

2. import numpy as np

 Library: numpy

 Purpose: Numerical operations on arrays.

 Usage:

o [Link](cv_scores): Computes the mean of cross-validation scores.

o Used for efficient mathematical computations.

3. import pickle

 Library: Built-in Python module.

 Purpose: Serializing and saving Python objects (like ML models).

 Usage:

o [Link](...): Saves the trained model (random_forest_model.pkl) for later use in


prediction or deployment.

4. from sklearn.model_selection import train_test_split, cross_val_score

 Library: scikit-learn (sklearn)

 Purpose: Splitting dataset & evaluating model performance.

 Usage:

o train_test_split(...): Divides data into training (80%) and testing (20%) sets.

o cross_val_score(...): Performs k-fold cross-validation to estimate model reliability.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 41


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

5. from [Link] import RandomForestClassifier

 Library: scikit-learn

 Purpose: Importing the Random Forest algorithm, which is an ensemble of decision trees.

 Usage:

o RandomForestClassifier --> Initializes the model.

o .fit --> Trains it on the training set.

o .predict --> Makes predictions on the test set.

o .feature_importances_: Retrieves the contribution of each feature to the model’s


decision.

6. from [Link] import LabelEncoder

 Library: scikit-learn

 Purpose: Encodes categorical variables into numerical form.

 Usage:

o LabelEncoder().fit_transform(...): Converts categorical labels like 'Good' and 'Bad'


into 1 and 0, respectively.

7. from [Link] import accuracy_score, classification_report

 Library: scikit-learn

 Purpose: Evaluating model performance.

 Usage:

o accuracy_score(...): Calculates the percentage of correctly predicted labels.

o classification_report(...): Displays precision, recall, F1-score, and support for each


class.

8. import [Link] as plt

 Library: matplotlib

 Purpose: Data visualization.

 Usage:

o [Link](...), [Link](...), [Link](...), etc.: Used to plot and customize the feature
importance bar chart.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 42


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

9. import seaborn as sns

 Library: seaborn

 Purpose: Advanced data visualization, built on top of matplotlib.

 Usage:

o [Link](...): Creates a visually appealing bar plot for feature importances using
color palettes like "viridis".

Flow Summary of the Script

1. Import Libraries – Tools required for data handling, model training, and visualization.

2. Load Dataset – pandas reads your milk quality dataset.

3. Encode Labels – Convert labels into numeric form for ML.

4. Split Data – Separate features (X) and target (y), then split into training and test sets.

5. Train Random Forest Model – Using RandomForestClassifier on training data.

6. Save Model – Using pickle to serialize the trained model.

7. Cross-Validation – Evaluate model on multiple folds of the data.

8. Prediction & Evaluation – Predict and evaluate performance on test data.

9. Feature Importance Plot – Show which features matter most using matplotlib and seaborn.

SVM Algorithm:
1. import pandas as pd

 Library: pandas

 Purpose: For reading, writing, and manipulating tabular data.

 Used for:

o Reading your dataset with pd.read_csv("[Link]")

o Dropping the target column with [Link](...)

2. import numpy as np

 Library: numpy

 Purpose: Numerical computing.

 Used for:

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 43


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

o Calculating mean accuracy from cross-validation scores using [Link](cv_scores)

3. import pickle

 Library: Built-in Python module.

 Purpose: Serialization.

 Used for:

o Saving your trained SVM model with [Link](...)

o Allows later reuse of the model without retraining.

4. from sklearn.model_selection import train_test_split, cross_val_score

 Library: scikit-learn (sklearn)

 Purpose: Model training and validation utilities.

 Used for:

o train_test_split(...): Dividing the dataset into training and testing subsets.

o cross_val_score(...): Performing k-fold cross-validation to validate model stability.

5. from [Link] import SVC

 Library: scikit-learn

 Purpose: Support Vector Classification (SVC).

 Used for:

o SVC(kernel='rbf', ...): Creates an SVM classifier using the Radial Basis Function
kernel.

o svm_model.fit(...): Trains the SVM model on standardized feature data.

6. from [Link] import LabelEncoder, StandardScaler

 Library: scikit-learn

 Purpose:

o LabelEncoder: Converts categorical labels like 'Good' and 'Bad' to 1 and 0.

o StandardScaler: Standardizes the dataset by scaling features to have mean 0 and


variance 1.

 Used for:

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 44


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

o Encoding the target variable with le.fit_transform(...)

o Scaling features before SVM training, which is crucial for optimal performance of
SVM.

7. from [Link] import accuracy_score, classification_report

 Library: scikit-learn

 Purpose: Model evaluation.

 Used for:

o accuracy_score(...): Computes the percentage of correctly predicted labels.

o classification_report(...): Gives detailed metrics like precision, recall, F1-score, and


support.

8. import [Link] as plt

 Library: matplotlib

 Purpose: Data visualization (though not used in this version).

 Used for:

o You can use it to create graphs or plots (e.g., learning curves, confusion matrices) in
future expansions of your project

9. import seaborn as sns

 Library: seaborn

 Purpose: Enhanced plotting on top of matplotlib.

 Note: Not used in the current script, but included for possible future plotting (like heatmaps
or pair plots).

Ada Boost Algorithm:


1. import pandas as pd

 Library: pandas

 Purpose: Data manipulation and analysis.

 Used for:

o Reading the dataset: pd.read_csv('[Link]')

o Handling and cleaning tabular data.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 45


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

2. import numpy as np

 Library: numpy

 Purpose: Numerical computing.

 Used for:

o Efficient manipulation of arrays, although not used directly here—it often supports
backend operations in machine learning.

3. import xgboost as xgb

 Library: xgboost

 Purpose: Implementation of the XGBoost algorithm (Extreme Gradient Boosting).

 Used for:

o [Link](...): Creating and training an XGBoost classifier, which is known


for high performance and speed.

o It is one of the most powerful ensemble learning models using decision trees under
the hood.

4. from sklearn.model_selection import train_test_split

 Library: scikit-learn

 Purpose: Splitting datasets for training and testing.

 Used for:

o train_test_split(...): Splits the dataset into 80% training and 20% testing.

5. from [Link] import LabelEncoder

 Library: scikit-learn

 Purpose: Convert categorical labels into numeric form.

 Used for:

o LabelEncoder().fit_transform(y): Encodes target labels like 'Good', 'Bad' to numeric


values like 1 and 0.

6. from [Link] import accuracy_score, precision_score, recall_score, f1_score,


classification_report

 Library: scikit-learn

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 46


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

 Purpose: Evaluate classification model performance.

 Used for:

o accuracy_score(...): Fraction of correct predictions.

o precision_score(...): Proportion of true positive predictions among all positive


predictions.

o recall_score(...): Proportion of true positives among actual positive samples.

o f1_score(...): Harmonic mean of precision and recall.

o classification_report(...): Summary report with all above metrics per class.

7. import joblib

 Library: joblib (alternative to pickle)

 Purpose: Efficient serialization of large NumPy arrays and models.

 Used for:

o [Link](model, 'AdaBoost_model.pkl'): Saves the trained model to a .pkl file for


future use.

XG Boost Algorithm:

import pandas as pd

 Library: pandas

 Purpose: Handling and analyzing structured data.

 Use in script:

o pd.read_csv("[Link]") reads the dataset into a DataFrame.

o Data manipulation operations like selecting columns and encoding.

import numpy as np

 Library: numpy

 Purpose: Fast numerical computations and array handling.

 Use in script:

o Used indirectly for numerical operations, especially in model evaluation (like


[Link]() on cross-validation scores).

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 47


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

import pickle

 Library: pickle (Python standard library)

 Purpose: Serializing (saving) and deserializing (loading) Python objects.

 Use in script:

o Saves the trained XGBoost model as xgboost_model.pkl for later reuse or


deployment.

import xgboost as xgb

 Library: xgboost

 Purpose: High-performance gradient boosting machine learning algorithm.

 Use in script:

o [Link](...) creates the model.

o Trained using [Link](...).

o Known for speed, accuracy, and ability to handle missing data.

from sklearn.model_selection import train_test_split, cross_val_score

 Library: scikit-learn (machine learning)

 Purpose:

o train_test_split: Splits the dataset into training and testing sets.

o cross_val_score: Evaluates model performance using k-fold cross-validation.

from [Link] import LabelEncoder, StandardScaler

 Purpose:

o LabelEncoder: Converts categorical labels (like "Good", "Bad") into numeric values.

o StandardScaler: Standardizes features to have zero mean and unit variance, which
helps improve model performance especially for algorithms sensitive to feature
scaling.

from [Link] import accuracy_score, classification_report

 Purpose: Evaluate the performance of the trained model.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 48


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

 Use in script:

o accuracy_score: Proportion of correct predictions.

o classification_report: Provides precision, recall, F1-score, and support for each class.

import [Link] as plt

 Library: matplotlib

 Purpose: Data visualization.

 Note: Although imported, it's not used in this script, but can be used to plot feature
importance or confusion matrices.

import seaborn as sns

 Library: seaborn

 Purpose: Statistical data visualization based on matplotlib.

 Note: Also imported but not used in this script; typically used for heatmaps, boxplots, and
feature importance graphs.

4.3 Web Development Libraries:


The web development of our Smart Milk Adulteration Detection System involves both
frontend and backend components. The frontend is designed using HTML, CSS, and JavaScript,
providing a user-friendly interface for inputting sensor values like temperature, turbidity, color, pH,
and fat. It displays predictions in a clear, responsive layout. The backend is developed using Flask
(Python), which handles data processing, loads the trained machine learning model (Random Forest,
SVM, XGBoost, etc.), and returns predictions. The backend communicates with the frontend via
HTTP routes, ensuring seamless integration and real-time milk quality analysis through a
lightweight, efficient, and scalable web application.

import pandas as pd

 Library: pandas

 Purpose: Handling and analyzing structured data.

 Use in script:

o pd.read_csv("[Link]") reads the dataset into a DataFrame.

o Data manipulation operations like selecting columns and encoding.

import numpy as np

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 49


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

 Library: numpy

 Purpose: Fast numerical computations and array handling.

 Use in script:

o Used indirectly for numerical operations, especially in model evaluation (like


[Link]() on cross-validation scores).

import pickle

 Library: pickle (Python standard library)

 Purpose: Serializing (saving) and deserializing (loading) Python objects.

 Use in script:

o Saves the trained XGBoost model as xgboost_model.pkl for later reuse or


deployment.

import xgboost as xgb

 Library: xgboost

 Purpose: High-performance gradient boosting machine learning algorithm.

 Use in script:

o [Link](...) creates the model.

o Trained using [Link](...).

o Known for speed, accuracy, and ability to handle missing data.

from sklearn.model_selection import train_test_split, cross_val_score

 Library: scikit-learn (machine learning)

 Purpose:

o train_test_split: Splits the dataset into training and testing sets.

o cross_val_score: Evaluates model performance using k-fold cross-validation.

from [Link] import LabelEncoder, StandardScaler

 Purpose:

o LabelEncoder: Converts categorical labels (like "Good", "Bad") into numeric values.

o StandardScaler: Standardizes features to have zero mean and unit variance, which

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 50


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

helps improve model performance especially for algorithms sensitive to feature


scaling.

from [Link] import accuracy_score, classification_report

 Purpose: Evaluate the performance of the trained model.

 Use in script:

o accuracy_score: Proportion of correct predictions.

o classification_report: Provides precision, recall, F1-score, and support for each class.

import [Link] as plt

 Library: matplotlib

 Purpose: Data visualization.

 Note: Although imported, it's not used in this script, but can be used to plot feature
importance or confusion matrices.

import seaborn as sns

 Library: seaborn

 Purpose: Statistical data visualization based on matplotlib.

 Note: Also imported but not used in this script; typically used for heatmaps, boxplots, and
feature importance graphs.

The entire application is powered by a Flask-based backend, which serves as the intermediary
between the Raspberry Pi hardware, the machine learning model, and the frontend interface. The
choice of Flask is justified by its lightweight nature, ease of integration, and rapid deployment
capabilities.

Backend Workflow:

1. Sensor Input Handling: Raspberry Pi sends real-time sensor values to the Flask server via
HTTP requests or direct GPIO data streams.

2. Model Prediction: Once all parameters (temperature, turbidity, color, pH, and fat) are
received, Flask loads the trained Stacking Classifier model and performs predictions.

3. Routing: Flask defines endpoints (routes) such as /, /predict, /get_data, etc., to handle
frontend requests.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 51


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

4. Template Rendering: Flask uses the Jinja2 template engine to render HTML pages
dynamically based on data from sensors and prediction results.

Frontend Technologies:

 HTML for content structure.

 CSS for styling and layout (including use of Flexbox/Grid).

 JavaScript for dynamic updates (using fetch() or AJAX for real-time data).

 Bootstrap (optional) for responsive components.

The Flask app also logs each prediction and sensor reading to a CSV or database, which can be
viewed later through the UI.

Real-time Data Display:

Real-time data monitoring is a crucial component of this system, providing users with live feedback
on milk quality. This capability allows users to take immediate action if adulteration is detected.

How Real-time Display Works:

 Sensor Data Update: Raspberry Pi reads sensor values (temperature, turbidity, color)
continuously and sends them to the Flask server.

 AJAX/JavaScript Polling: The frontend uses JavaScript to periodically (e.g., every 2–3
seconds) send fetch requests to a Flask endpoint like /get_data.

 Dynamic DOM Update: Once new sensor data is received, JavaScript updates the UI
without reloading the page.

 Real-time Prediction: As soon as all necessary features are available, the system calls the
/predict route and displays the updated milk quality result.

 Visual Cues: Sensor values and the quality label are updated instantly, with animated
transitions and colour changes to draw user attention.

Additional Features:

 Auto Refresh Controls: A toggle for enabling/disabling auto-refresh of sensor data.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 52


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

 Prediction Logs: Historical predictions stored and displayed as a table or chart.

 Alerts (Optional): Pop-up notifications or buzzer activation if bad quality milk is detected.

This real-time component ensures that the milk quality is continuously monitored without requiring
manual intervention, which is essential in environments like dairy farms and milk collection centers.

Web Application Development


To enhance usability and provide real-time interaction for end users, a web application was
developed as part of the Smart Milk Adulteration Detection System. The application allows users to
input milk quality parameters, fetch predictions from a trained machine learning model, and receive
real-time feedback on the quality of the milk — whether it is Good or Adulterated. The web interface
is developed using a combination of Flask (Python) for the backend and HTML, CSS, and JavaScript
for the frontend.

1. Flask (Backend)
Flask is a lightweight Python-based micro web framework ideal for creating machine learning-based
APIs and dynamic web applications. It handles server-side logic, routing, and communication
between the frontend and the trained model.
Key Flask Features Used:
 Routing: Handles different endpoints such as the home page (/), prediction endpoint
(/predict), and data upload.
 Form Handling: Receives user input from frontend HTML forms (sensor values or CSV
upload).
 Model Integration: Loads the trained stacking classifier model using joblib or pickle and
uses it to predict the quality of milk based on user inputs.
 JSON Support: Sends back prediction results to be displayed on the web page.
 Template Rendering: Uses render_template() to link Python logic to HTML pages using
Jinja2.
2. HTML (Frontend Structure)
HTML (HyperText Markup Language) structures the webpage by creating forms, buttons, labels,
and display sections. It forms the backbone of the user interface.
Key Elements Used:

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 53


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

 <form>: For collecting user inputs (sensor values like temperature, turbidity, etc.)
 <input type="text">: For user data entry
 <button>: For submitting prediction request
 <div> and <span>: For layout and results display

3. CSS (Styling)
CSS (Cascading Style Sheets) enhances the visual appearance of the web interface, ensuring a clean,
modern, and responsive design that is user-friendly.
Features Used:
 Colour Themes: Blue/green tones for healthy feedback, red tones for adulteration warnings.
 Responsive Design: Ensures compatibility with mobile and desktop devices.
 Button Styling: For better UI/UX interactions.
 Container Formatting: To center content and make it visually appealing.
4. JavaScript (Dynamic Interactivity)
JavaScript is used to enhance interactivity by handling form validation, adding alerts, and updating
the DOM without refreshing the page.
Applications in the Web App:
 Form Validation: Prevents empty or invalid entries before sending them to the backend.
 Loading Animation: Indicates prediction is being processed.
 Result Display: Displays prediction result dynamically using inner HTML.}

The web interface successfully bridges the hardware-sensor system with the machine learning model,
offering a seamless and intuitive platform for real-time milk quality detection. With Flask managing
the logic and machine learning predictions, and HTML/CSS/JS creating a user-centric front end, the
application delivers functionality, reliability, and a professional look suitable for field use and
consumer awareness.
User Interface Design:
The user interface (UI) of the Smart Milk Adulteration Detection System is designed with simplicity,
accessibility, and real-time interaction in mind. Since the end-users may include milk producers,
quality inspectors, and even consumers with non-technical backgrounds, the UI follows a clean and
intuitive layout that makes the experience seamless.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 54


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

The interface is built using HTML, CSS, and JavaScript, ensuring responsiveness across various
devices including desktops, tablets, and smartphones. The design includes a dashboard-like layout,
clearly displaying sensor inputs and prediction results.
Key Features of the UI:
 Header Navigation: Simple navbar to navigate between the dashboard, prediction logs, and
help/documentation.
 Sensor Data Display: Dedicated sections that show real-time values of temperature,
turbidity, colour, pH, and fat.
 Prediction Panel: A prominent section where the milk quality is shown as either “Good” or
“Adulterated”, based on model predictions.
 Colour Indicators: Green for good quality and red for adulterated milk, enhancing visual
interpretation.
 Minimal Input Fields: Since most data is fetched directly from the Raspberry Pi and
sensors, the manual inputs are minimal (only fat and pH values if not sensor-integrated).
 Responsive Layout: The design adapts based on screen size, using CSS Flexbox/Grid and
media queries.
The primary design goal was to keep the interface uncluttered, with essential information easily
accessible, ensuring usability for all categories of users.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 55


DESIGN OF A SMART MILK ADULTERATION DETECTION SYSTEM USING AI AND SENSORS

CHAPTER - 5
RESULTS

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 56


Design of a Smart Milk Adulteration Detection System Using AI and Sensors

RESULTS
5.1 Results of the Hardware Implementation:
The hardware prototype for the smart milk adulteration detection system was
successfully developed and tested using a Raspberry Pi microcontroller integrated with
various sensors. The aim was to detect adulterants in milk samples by measuring key
physical and chemical parameters. The system was evaluated based on its data accuracy,
response time, and usability in real-time conditions.

Fig 5.1.1 Actual Hardware Implementing

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 57


Design of a Smart Milk Adulteration Detection System Using AI and Sensors

Fig 5.1.2 Software Outputs

Sensor Output and Data Collection


The following sensors were connected to the Raspberry Pi for real-time data acquisition:
 DS18B20 Temperature Sensor: Accurately captured the temperature of milk samples
within a ±0.5°C margin. This helped detect spoilage or improper storage, which often
correlates with adulteration.
 Turbidity Sensor: Provided voltage outputs corresponding to milk clarity. Adulterants
like water and detergent significantly altered turbidity values, which were successfully
recorded and analyzed.
 TCS34725 Color Sensor: Measured RGB values of milk samples. Synthetic milk and
starch adulterants caused noticeable shifts in color intensity, which were used as key
features in machine learning classification.
 pH (measured using litmus/pH strips): Though manually measured, pH values helped
differentiate between fresh and tampered milk. Pure milk typically had a pH of ~6.5–6.7,
while adulterated milk showed deviations.
 Lactometer for Fat Content: Values were recorded manually and used to estimate the
density and fat content. Adulterants like water lowered the lactometer reading, signaling
dilution.
The sensor readings were compiled into a CSV dataset containing multiple labeled
samples (Good or Bad), which were then used for training machine learning models.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 58


Design of a Smart Milk Adulteration Detection System Using AI and Sensors

Fig [Link] Data Set

5.2 Results of the Software Implementation:


Machine Learning Integration and Prediction
Sensor data collected from the hardware setup was pre processed and used to train multiple machine
learning algorithms. The goal was to classify each milk sample as “Good” (unadulterated) or “Bad”
(adulterated). The models were evaluated using standard metrics: Accuracy, Precision, Recall, and
F1-Score.
The following algorithms were implemented and tested:
 Random Forest
 Support Vector Machine (SVM)
 AdaBoost
 XGBoost
 Stacking Classifier

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 59


Design of a Smart Milk Adulteration Detection System Using AI and Sensors

5.2.1 Results of Random Forest Algorithm:


Cross-Validation Scores: [0.81773399 0.87684729 0.85714286 0.87192118 0.87623762]
Mean CV Accuracy: 0.8600
Model Accuracy: 0.8621
Classification Report:
Class precision recall f1-score support

Good 0.88 0.88 0.88 114


Bad 0.84 0.84 0.84 89
Accuracy 0.86 203
macro avg 0.86 0.86 0.86 203
Weighted avg 0.86 0.86 0.86 203
Table [Link] Random Forest Result
Table [Link] presents the performance of the Random Forest algorithm, showing strong
classification results with a mean cross-validation accuracy of 86.00% and an overall model accuracy
of 86.21%. The classifier achieved balanced precision, recall, and F1-scores across both classes,
indicating its reliability in distinguishing between "Good" and "Bad" categories.

Fig [Link] Feature Importance of RF


The feature importance chart for the Random Forest model reveals that pH is the most influential
variable, followed by temperature and color. In contrast, fat and turbidity contribute minimally to the

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 60


Design of a Smart Milk Adulteration Detection System Using AI and Sensors

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 61


Design of a Smart Milk Adulteration Detection System Using AI and Sensors

Fig [Link] RF Shap Analysis

The diagram and results illustrate the performance and feature importance of a Random Forest
model. The feature importance plot highlights pH as the most significant contributor (importance
score ~0.4), followed by Temperature, Color, Fat, and Turbidity, indicating their relative influence
on the model's predictions. The model evaluation reveals strong performance, with a mean cross-
validation accuracy of 0.86 and consistent metrics across classes: precision, recall, and F1-scores for
"Good" (0.88) and "Bad" (0.84) labels demonstrate balanced classification. Additionally, the
reference to Fig [Link] RF Sharp Analysissuggests further interpretability analysis using SHAP
values, which would elucidate how each feature impacts individual predictions. Together, these
elements underscore the model's reliability and the key factors driving its decisions.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 62


Design of a Smart Milk Adulteration Detection System Using AI and Sensors

5.2.2 Results of Support Vector Machine Algorithm:


Cross-Validation Scores: [0.85714286 0.87192118 0.90147783 0.8817734 0.84158416]
Mean CV Accuracy: 0.8708
Model Accuracy: 0.8768
Classification Report:
Classes’ precision recall f1-score support

Good 0.82 1.00 0.90 114


Bad 1.00 0.72 0.84 89
Accuracy 0.88 203
macro avg 0.91 0.86 0.87 203
Weighted 0.90 0.88 0.87 203
avg
Table [Link] SVM Results

The SVM model achieved a mean CV accuracy of 0.8708 and final accuracy of 0.8768, with strong
performance for the "Good" class (recall=1.00) but lower recall for "Bad" (0.72). These results,
detailed in Table [Link], demonstrate the model's effectiveness but highlight class-specific trade-offs
in precision and recall.
[Link] Results of the Ada Boost Algorithm:
Model Accuracy: 0.8768
Precision: 0.8770
Recall: 0.8768
F1-Score: 0.8764
Classification Report:
Classes precision recall f1-score support

Good 0.87 0.91 0.89 114


Bad 0.88 0.83 0.86 89
Accuracy 0.88 203
macro avg 0.88 0.87 0.87 203
Weighted avg 0.88 0.88 0.88 203
Table [Link] Ada Boost Results

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 63


Design of a Smart Milk Adulteration Detection System Using AI and Sensors

Table [Link] summarizes the performance of the Ada Boost algorithm, which achieved a high model
accuracy of 87.68% along with balanced precision, recall, and F1-scores. The model performed
slightly better in identifying "Good" samples, demonstrating strong overall effectiveness in
classifying the dataset.
Shap Analysis of Ada Boost Algorithm:

Fig [Link].1 SHAP Value Distribution by Feature

[Link].2 SHAP Feature Importance for Ada Boost Algorithm


The SHAP analysis of the Ada Boost algorithm reveals that pH has the highest average impact on
model predictions (mean SHAP value ~0.25), followed by Color and Temperature, indicating these
features are key drivers of the model's decisions.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 64


Design of a Smart Milk Adulteration Detection System Using AI and Sensors

Fig [Link].3 SHAP Feature Interaction Plot


The second SHAP plot further illustrates how high values of pH and Temperature push predictions
towards the positive class (higher SHAP values), while Fat and Turbidity show mixed influence.
Combined with the Ada Boost's strong performance (accuracy=0.8768, F1-score=0.8764), these
insights confirm the model's reliability and highlight the most critical features for classification.
[Link] Results of XG Boost Algorithm:
Cross-Validation Scores: [0.83743842 0.87192118 0.87192118 0.90147783 0.84653465]
Mean CV Accuracy: 0.8659
Model Accuracy: 0.8966
Classification Report:
Classes precision recall f1-score support

Good 0.91 0.90 0.91 114


Bad 0.88 0.89 0.88 89
Accuracy 0.90 203
macro avg 0.89 0.90 0.90 203
Weighted avg 0.90 0.90 0.90 203
Table [Link] Performance Metrics of XGBoost Classifier

Table [Link] highlights the superior performance of the XGBoost classifier, which achieved the
highest model accuracy of 89.66% and a mean cross-validation accuracy of 86.59%. With strong

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 65


Design of a Smart Milk Adulteration Detection System Using AI and Sensors

precision, recall, and F1-scores for both classes, XGBoost demonstrates excellent generalization and
robustness in classification.

Shap Analysis of XG-Boost Algorithm:

Fig [Link].1 SHAP Summary Plot for Feature Impact Analysis

The SHAP summary plot illustrates how each feature influences the model's predictions, with
pH having the most significant impact, followed by color and temperature. High and low feature
values are color-coded, revealing that both the magnitude and direction of feature contributions vary
across predictions.
The SHAP dependence plot illustrates how temperature influences the model's output, with
varying SHAP values across its range. The color gradient representing turbidity indicates interaction effects,
suggesting that the impact of temperature on predictions is modulated by turbidity levels.

[Link] Results of Stacking Classifier:


The Stacking Classifier stands as the most advanced and accurate model in your system. By

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 66


Design of a Smart Milk Adulteration Detection System Using AI and Sensors

combining the predictive powers of Random Forest, SVM, AdaBoost, and XGBoost, and using a
meta-learner to synthesize results, you achieve top-tier classification performance. In milk
adulteration detection—where the safety and health of consumers are at stake—this level of
reliability is crucial.
Stacking Classifier Accuracy: 0.956256157635468

Classification Report:
precision recall f1-score support

Good 0.91 0.95 0.93 114


Bad 1.00 0.95 0.90 95
Accuracy 0.95 203
macro avg 0.95 0.94 0.94 203
Weighted avg 0.94 0.94 0.94 203

Table [Link] Performance Metrics of Stacking Classifier

The stacking classifier achieved the highest accuracy of 95.63%, demonstrating exceptional
performance in distinguishing between "Good" and "Bad" classes. With perfect precision for the
"Bad" class and high F1-scores overall, the model shows strong predictive power and balanced
classification.

Comparison of Machine Learning Algorithms:

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 67


Design of a Smart Milk Adulteration Detection System Using AI and Sensors

Table5.2.2.6 Summary Comparison Table of Machine Learning Algorithams


Algorithms Accuracy Precision (Avg) Recall (Avg) F1-Score (Avg)

Random Forest 86.21% 0.86 0.86 0.86

SVM 87.68% 0.91 0.86 0.87

AdaBoost 87.68% 0.88 0.88 0.88

XGBoost 89.66% 0.90 0.90 0.90

Stacking
91.62% 0.92 0.91 0.92
Classifier

Algorithms were trained and tested. These include Random Forest, Support Vector Machine
(SVM), AdaBoost, XG-Boost, and a Stacking Classifier that combines all the above models. Each
model has unique advantages and limitations, and their effectiveness can vary depending on the
nature of the dataset and classification task.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 68


Design of a Smart Milk Adulteration Detection System Using AI and Sensors

Fig [Link].1 Bar Graph Comparison of Machine Learning Models

Performance Metrics Table:

[Link] Model accuracy precision recall f1-score


1 Random 0.876847 0.877036 0.876847
Forest 0.876920
2 Support 0.876847 0.898997 0.876847
Vector 0.872870
machine
3 XG-Boost 0.896552 0.896718 0.896552
0.896613

4 AdaBoost 0.876847 0.898997 0.876847


0.872870

5 Stacking 0.956108 0.956712 0.956108


0.925833
Table [Link] Performance Comparison of Different Algorithms

Data collected from sensors and manual inputs (pH and lactometer) were saved in a structured CSV
format.
1. Preprocessing included normalization and label encoding.
2. 70% of the data was used for training, and 30% was used for testing.
3. The trained model (Stacking Classifier) was deployed in the backend using Python and Flask.
4. Real-time predictions were made within 2 seconds, offering immediate classification results
on the web interface.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 69


Design of a Smart Milk Adulteration Detection System Using AI and Sensors

Model Training and Prediction Flow:

Fig [Link].2 Confusion Matrix of Algorithms


The provided confusion matrices offer insights into the performance of four machine learning
models: Random Forest, SVM, AdaBoost, and XGBoost. For Random Forest, the model predicted
101 instances as class 0 and 77 as class 1, with 12 actual class 1 instances observed. The SVM model
showed stronger performance for class 0, predicting 114 instances correctly, but misclassified 25
class 1 instances as class 0 while correctly identifying 64 class 1 instances. However, the details for
AdaBoost and XGBoost are incomplete, as their confusion matrices lack specific numerical data,
making it difficult to assess their classification accuracy. Further analysis would require complete
matrices to compare all models effectively.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 70


Design of a Smart Milk Adulteration Detection System Using AI and Sensors

5.3 Web Application Output:


To ensure the system is accessible and user-friendly, a lightweight web application was developed
using Flask for the backend and HTML, CSS, and JavaScript for the frontend. The web app acts as
an interface between the user and the machine learning model.
Key features of the web application include:
 Manual and Automatic Input Modes: Users can manually enter sensor values (e.g., pH, fat
content) or allow the system to fetch data automatically from the Raspberry Pi setup.
 Instant Prediction: Upon submission, the trained Stacking Classifier model processes the
input and displays whether the milk is “Good” or “Bad.”
 User Feedback: The result is accompanied by brief recommendations (e.g., "Check fat
content" or "High turbidity detected").
 Responsive Design: The interface is simple, mobile-friendly, and can be used by dairy
farmers, collection center staff, or quality inspectors with minimal training. From the above
application fig 5.3.1 and 5.3.2.
This web application bridges the gap between complex machine learning operations and
practical, real-world usability, ensuring even non-technical users can benefit from the system.

Fig 5.3.1 Web Application Output


The image displays a user interface titled “Enter Values to Get Milk Quality", designed to
collect specific parameters for evaluating milk quality. Users can input the Temperature in Celsius,
select binary values for Fat (0 or 1) and Turbidity (0 or 1), specify the Color (RGB), and enter the pH
level. After filling in these details, clicking the "Predict" button would trigger an analysis, likely

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 71


Design of a Smart Milk Adulteration Detection System Using AI and Sensors

powered by a machine learning model or statistical algorithm, to determine the quality of the milk.
This interface appears to be part of a predictive tool aimed at automating quality assessment, though
the exact output or scoring system is not visible in the provided image. The inclusion of parameters
like pH and turbidity suggests a focus on both chemical and physical properties of the milk for
comprehensive evaluation.

Fig 5.3.2 Web Application Output 2

The image shows a milk quality prediction interface where the user has entered the following values:
Temperature = 45°C, Fat = 1, Turbidity = 0, Color (RGB) = 256, and pH = 6.5. After submitting
these inputs by clicking the "Predict" button, the system outputs the result: "Milk Quality is Good",
indicating that the analyzed parameters meet the criteria for satisfactory milk quality according to the
underlying evaluation model.
Sample Output of Prediction
Sample ID Temp (°C) Turbidity RGB pH Fat Predicted Class
101 29.5 3.6 (220, 230, 250) 6.6 0 Good
102 30.2 2.4 (210, 215, 240) 5.9 1 Bad

The sample output displays two milk quality predictions: Sample ID 101 with a temperature of
29.5°C, turbidity of 3.6, RGB values (220, 230, 250), pH 6.6, and Fat 0 is classified as "Good", while
Sample ID 102 with a temperature of 30.2°C, turbidity of 2.4, RGB values (210, 215, 240), pH 5.9,
and Fat 1 is labeled "Bad", reflecting the model's assessment based on the input parameters.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 72


Design of a Smart Milk Adulteration Detection System Using AI and Sensors

Comparison Table:

Key Features
Study Methodology Result Dataset Analysis
Used
Evaporation Focused on visual
Image-Based Detection of
patterns of Custom image changes in milk drops
Adulterants in Milk Using CNN 84.94%
adulterated milk dataset using CNN image
CNNACS Omega, 2024[3]
droplets classification.
On the Utilization of Deep and RF:
Compared traditional
Ensemble Learning to Detect 93.23%, Public FTIR spectral
CNN, RF, GBM FTIR spectral data and deep models on
Milk AdulterationBioData GBM: data
chemical spectra.
Mining, 2019[4] 92.25%
Milk Source Identification and E-nose sensor Multimodal approach
SVM, RF, Logistic RF: 94%,
Milk Quality Estimation Using data, DHI test Sensor + DHI dataset using gas sensor array
Regression LR: 92.5%
E-NoseSensors, 2020[7] data and lab results.
Prediction of Fresh Milk Quality pH, SNF, protein,
ANN: R² > Manually collected Used regression for
Using ANN and ANN, MNLR density, freezing
0.90 farm data quality score estimation.
MNLRLACCEI, 2023[8] point
Sensor-based custom Real-time field system
Stacking Classifier
Proposed Project: Smart Milk Temperature, pH, dataset (Raspberry Pi with web interface;
(Random Forest,
Adulteration Detection Using AI Turbidity, Color, 95% + DS18B20, integrates sensor data
SVM, AdaBoost,
and Sensors[10] Fat TCS34725, LDR, pH and AI for on-site
XGBoost)
paper) analysis.
Table 5.3.1 Comparison of Previous papers with Proposed Project

The table compares five studies on milk quality and adulteration detection, highlighting diverse
methodologies and results. The first study (ACS Omega, 2024) used CNN to analyze evaporation
patterns in milk droplets, achieving 84.94% accuracy, while the second (BioData Mining, 2019)
employed deep and ensemble learning on FTIR spectral data, with Random Forest (RF) yielding the
highest accuracy (93.23%). The third study (Sensors, 2020) combined E-nose sensor data with DHI
test results, where RF again outperformed with 94% accuracy. The proposed project stands out by
integrating AI (Stacking Classifier) with real-time sensor data (temperature, pH, etc.), achieving 95%
accuracy and offering a practical, field-deployable solution for on-site milk quality analysis.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 73


Design of a Smart Milk Adulteration Detection System Using AI and Sensors

CHAPTER – 6
Conclusion

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 74


Design of a Smart Milk Adulteration Detection System Using AI and Sensors

Conclusion
The project “Design of a Smart Milk Adulteration Detection System Using AI and Sensors” has
successfully demonstrated an innovative, cost-effective, and scalable solution to a pressing issue in
public health — the detection of adulteration in milk. Through the intelligent integration of
embedded sensor technology, machine learning algorithms, and a user-friendly web interface, the
system provides a practical approach for real-time milk quality assessment.
By utilizing key sensors such as the DS18B20 temperature sensor, turbidity sensor, and TCS34725
color sensor, the system captures critical physical properties of milk that serve as early indicators of
adulteration. Additional parameters, including pH level (measured via strips) and fat content
(measured using an LED-LDR based optical method), enhance the feature set and allow the model to
make more accurate decisions. Data collected from these sensors is stored in CSV format and used to
train and evaluate several machine learning models including Random Forest, Support Vector
Machine (SVM), AdaBoost, XGBoost, and an ensemble Stacking Classifier.
Among the tested models, the Stacking Classifier showed the highest predictive accuracy, achieving
over 91% accuracy, and demonstrated robustness in handling real-world data. This confirms the
advantage of combining multiple models to improve overall prediction performance. The models
were thoroughly validated using cross-validation, classification metrics (precision, recall, F1-score),
and comparative analysis, all of which affirmed the system's efficiency and reliability.
The final deployment of the system involved implementing the machine learning pipeline on a
Raspberry Pi, making it a compact and portable edge-computing solution. The Flask-powered
backend served as the API for prediction and sensor data handling, while the frontend — built using
HTML, CSS, and JavaScript — enabled intuitive user interaction and real-time result display.
This system not only streamlines the process of milk testing but also minimizes human errors and
dependency on sophisticated laboratory setups. It can be used by dairy farmers, quality control units,
and even consumers for quick milk quality verification. Moreover, its low-cost hardware and
scalable software architecture make it suitable for deployment in rural as well as urban
environments.
In conclusion, the project successfully bridges the gap between conventional milk quality testing and
modern-day technology. It paves the way for future enhancements such as cloud integration, mobile
app interfaces, and AI-powered anomaly detection. By ensuring milk safety at the grassroots level,
this solution contributes meaningfully to public health, consumer protection, and food industry
innovation.

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 75


Design of a Smart Milk Adulteration Detection System Using AI and Sensors

REFERENCES
[1]Walter FranciscoSalas-Valerio, Didem [Link], Beatriz [Link] Sakoda, Fanny [Link]˜na-
Urquizo, ChristopherBall, MarcalPlans, LuisRodriguez-Saona, In-field screening of trans-fat levels
using mid- and near-infrared spectrometers for but ters and margarines commercialized in the
Peruvian market, LWT, Volume 157, 1 March 2022, 113074,
[Link]
[2]Agnet, Y. 1998. Fourier transform infrared spectrometry. A new con cept for milk and milk
product analysis. Bull. Int. Dairy Fed. 332:58–68.
[3]BIBLIOGRAPHY Sharma, R., Saini, A., Giri, A., & Puri, S. (2017). Development of a portable
milk fat testing device using NIR spectroscopy. International Journal of Current Engineering and
Technology, 7(6), 2144-2149.
[4]AOAC. 2000. Official Methods for Analysis. 17th ed. AOAC Interna tional, Gaithersburg, MD.
Barbano, D. M., and J. L. Clark. 1989. Infrared milk analysis Challenges for the future. J. Dairy Sci.
72:1627–1636.
[5] RupakChakravarty, a paper on‖ IT at Milk collection centers in Cooperative Diaries: The
National Dairy Development Board Experience‖, pp.37-47.
[6]Vasudha V Ayyannawar and Soumya R Metri, “Detection of Fat in Milk Using Photoconductivity
and Color Detection Technique”, ICT Analysis and Applications, pp. 399–410, Springer, 2020.
[7]ADR(ArbeitsgemeinschaftDeutscherRinderzüchtere.V.)
(2013)[Link],Bonn,Germany
AliAKA,ShookGE(1980)Anoptimumtransformationforsomaticcell concentrationinmilk.
JDairySci63:487–490
[8]Albanese D., Visintainer R., Merler S., Riccadonna S., Jurman G. & Furlanello C. 2012. mlpy:
Machine Learning Python. arXiv:1202-6548v2 Balaban M.E. & Kartal E. 2018. Veri madenciliği ve
makine öğrenmesi temel algoritmaları ve R Dili ile Uygulamalar, 2. Basım, Çağlayan Kitap &
Yayıncılık & Eğitim, İstanbul, Türkiye, pp. 48-72.
[9]Galloway, J.A., 2000. Great fare of London. The Lancet, 355, pp.323–324.
[10]Prasanth, P.; Viswan, G.; Bennaceur, K. Development of a low-cost portable spectrophotometer
for milk quality analysis. Mater. Today 2021, 46, 4863–4868. [CrossRef]

Department of ECE, Lendi Institute of Engineering & Technology (Autonomous) Page 76

You might also like