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
-
Flex sensor is connected to GPIO 34.
-
GPIO 34 is an analog input pin in ESP32.
2️⃣ Variable Declaration
-
Stores the analog value read from the sensor.
-
This is the safety limit.
-
If sensor value goes above this → bridge bending detected.
You can adjust this after testing.
3️⃣ Setup Function
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
-
Reads voltage from voltage divider circuit.
-
ESP32 ADC range: 0 – 4095 (12-bit resolution).
-
Stores the value in flexValue.
Step 2: Print Sensor Value
Shows the real-time value in Serial Monitor.
Example output:
Step 3: Compare with Threshold
If value is greater than 2500:
Otherwise:
Step 4: Delay
Waits 1 second before next reading.
📊 How to Set Correct Threshold (Important!)
-
Upload code.
-
Open Serial Monitor.
-
Note value when:
-
Bridge straight → Example: 1800
-
Bridge bent → Example: 3000
-
-
Set threshold between them:
Example:
This makes system more accurate.
🖥️ Example Serial Monitor Output
When bridge is straight:
When bridge bends:
🔎 Complete System Logic (For Viva)
-
Flex sensor changes resistance when bridge bends.
-
Voltage divider converts resistance change to voltage.
-
ESP32 ADC converts voltage to digital value (0–4095).
-
ESP32 compares value with safety threshold.
-
If value exceeds limit → Warning message.
-
If not → Safe message.
-
Data displayed in Serial Monitor.