Code: Select all
int ledPin = 8; // Define pin 8 as the output pin for 5V
int measurePin = A0; // Define the analog pin A0 for measuring Vout
float Vout = 0.0; // Variable to store the measured bridge output voltage
float Vin = 5.0; // Input voltage (assumed to be 5V from pin 8)
float R1 = 1000.0; // Resistance of R1 in ohms
float R2 = 1000.0; // Resistance of R2 in ohms
float R3 = 1000.0; // Resistance of R3 in ohms
float R4 = 0.0; // Calculated resistance of R4
void setup() {
pinMode(ledPin, OUTPUT); // Set pin 8 as output
pinMode(measurePin, INPUT); // Set pin A0 as input
// Initialize serial communication for debugging
}
void loop() {
// Apply 5V to the Wheatstone bridge
digitalWrite(ledPin, HIGH);
// Measure Vout at the midpoint of the bridge
int rawValue = analogRead(measurePin); // Read raw ADC value (0-1023)
Vout = (rawValue / 1023.0) * Vin; // Convert raw ADC value to voltage
// Calculate R4 using the Wheatstone bridge formula
if (Vout > 0.0 && Vout < Vin) { // Ensure Vout is within a valid range
R4 = (R2 * R3) / (R1 * ((Vin / Vout) - 1));
} else {
R4 = -1; // Indicate an error if Vout is invalid
}
// Print results to Serial Monitor
Serial.print("Measured Voltage (Vout): ");
Serial.print(Vout);
Serial.println(" V");
if (R4 > 0) {
Serial.print("Calculated Resistance (R4): ");
Serial.print(R4);
Serial.println(" ohms");
} else {
Serial.println("Error: Invalid voltage, cannot calculate R4.");
}
delay(1000); // Delay 1 second before the next measurement
}