Course Content
📘 MODULE 6 – Smart Pet Feeder
0/1
📘 MODULE 7 – Smart Water Management System
0/2
📘 MODULE 8 – Water Quality Monitoring System
0/2
📘 MODULE 10 – Gas Leakage Detection System
0/2
Mastering IoT with 11 Real-World Projects and 1 mega project

Bridge Health Monitoring Using Flex Sensor

  • ✅ Complete ESP32 Code

  • ✅ Step-by-step Code Explanation

  • ✅ Explanation of how result appears in Serial Monitor

  • ✅ Logic explanation (for viva)

💻 ESP32 Code (Bridge Health Monitoring Using Flex Sensor)

// Bridge Health Monitoring System
// Using ESP32 and Flex Sensor

const int flexPin = 34; // Analog pin connected to flex sensor
int flexValue = 0; // Variable to store analog value
int threshold = 2500; // Bending limit (change after calibration)

void setup() {
Serial.begin(115200); // Start serial communication
pinMode(flexPin, INPUT); // Set flex pin as input
}

void loop() {

// Read analog value from flex sensor
flexValue = analogRead(flexPin);

// Print raw sensor value
Serial.print("Flex Sensor Value: ");
Serial.println(flexValue);

// Compare with threshold
if (flexValue > threshold) {
Serial.println("⚠️ BEND DETECTED - Bridge Under Stress!");
} else {
Serial.println("✅ No Bend - Bridge is Safe.");
}

Serial.println("-----------------------------");

delay(1000); // Wait 1 second
}

🧠 Code Explanation (Line by Line)


1️⃣ Pin Definition

 
const int flexPin = 34;
  • Flex sensor is connected to GPIO 34.

  • GPIO 34 is an analog input pin in ESP32.


2️⃣ Variable Declaration

 
int flexValue = 0;
  • Stores the analog value read from the sensor.

 
int threshold = 2500;
  • This is the safety limit.

  • If sensor value goes above this → bridge bending detected.

You can adjust this after testing.


3️⃣ Setup Function

 
void setup() {
Serial.begin(115200);
pinMode(flexPin, INPUT);
}

What happens here?

  • Serial communication starts at 115200 baud rate.

  • This allows ESP32 to send data to Serial Monitor.

  • flexPin is set as input.

This runs only once when ESP32 starts.


4️⃣ Loop Function

The loop runs continuously.


Step 1: Read Sensor

 
flexValue = analogRead(flexPin);
  • Reads voltage from voltage divider circuit.

  • ESP32 ADC range: 0 – 4095 (12-bit resolution).

  • Stores the value in flexValue.


Step 2: Print Sensor Value

 
Serial.print("Flex Sensor Value: ");
Serial.println(flexValue);

Shows the real-time value in Serial Monitor.

Example output:

 
Flex Sensor Value: 1820

Step 3: Compare with Threshold

 
if (flexValue > threshold)

If value is greater than 2500:

 
⚠️ BEND DETECTED - Bridge Under Stress!

Otherwise:

 
No Bend - Bridge is Safe.

Step 4: Delay

 
delay(1000);

Waits 1 second before next reading.


📊 How to Set Correct Threshold (Important!)

  1. Upload code.

  2. Open Serial Monitor.

  3. Note value when:

    • Bridge straight → Example: 1800

    • Bridge bent → Example: 3000

  4. Set threshold between them:

Example:

 
int threshold = 2400;

This makes system more accurate.


🖥️ Example Serial Monitor Output

When bridge is straight:

 
Flex Sensor Value: 1750
✅ No Bend - Bridge is Safe.
-----------------------------

When bridge bends:

 
Flex Sensor Value: 2950
⚠️ BEND DETECTED - Bridge Under Stress!
-----------------------------


🔎 Complete System Logic (For Viva)

  1. Flex sensor changes resistance when bridge bends.

  2. Voltage divider converts resistance change to voltage.

  3. ESP32 ADC converts voltage to digital value (0–4095).

  4. ESP32 compares value with safety threshold.

  5. If value exceeds limit → Warning message.

  6. If not → Safe message.

  7. Data displayed in Serial Monitor.

Scroll to Top